/*
 * @Author: mbw
 * @Date: 2024-11-30 15:46:21
 * @LastEditors: mbw && 1600520629@qq.com
 * @LastEditTime: 2025-02-21 09:19:50
 * @FilePath: \ble_bjq_ch303rct6_ml307\bsp\src\bsp_led.c
 * @Description:
 *
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
 */
#include "bsp_led.h"
#include "rtthread.h"
#include "pin.h"
#include "user_sys.h"
#include "bsp_ml307.h"

#define LOG_TAG "bsp.led"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>

ALIGN(4)
static char user_led_thread_stack[512];
static struct rt_thread user_led_thread;

// 用到了atoi
#include <stdlib.h>

agile_led_t *led_r = RT_NULL;
agile_led_t *led_g = RT_NULL;
agile_led_t *led_y = RT_NULL;

static void BSP_Led_thread_entry(void *param)
{
    while (1)
    {
        if (SysControl.status == kNormalDetectionEvents)
        {
            if (ml307_connect_sever_flag)
            {
                LED_G_NORMAL;
            }
            else
            {
                LED_G_INTERNET;
            }
        }
        rt_thread_mdelay(1000);
    }
}

int BSP_LED_Init(void)
{
    led_r = agile_led_create(LED_R_PIN, PIN_LOW, "100,100", 0);
    led_g = agile_led_create(LED_G_PIN, PIN_LOW, "100,100", 0);
    led_y = agile_led_create(LED_Y_PIN, PIN_LOW, "100,100", 0);

    rt_err_t ret = rt_thread_init(&user_led_thread,
                                  "user_led_thread",
                                  BSP_Led_thread_entry,
                                  RT_NULL,
                                  &user_led_thread_stack[0],
                                  sizeof(user_led_thread_stack),
                                  5, 5);

    if (ret == RT_EOK)
    {
        rt_thread_startup(&user_led_thread);
    }

    LOG_D("BSP_LED_Init");
    return RT_EOK;
}
// INIT_DEVICE_EXPORT(BSP_LED_Init);

#ifdef TEST_ENABLE
static void TEST_LED(int argc, char **argv)
{
    if (argc == 4)
    {
        char led_name = argv[1][0];
        int loop_cnt  = atoi(argv[3]);
        switch (led_name)
        {
            case 'r':
                agile_led_dynamic_change_light_mode(led_r, argv[2], loop_cnt);
                agile_led_start(led_r);
                break;
            case 'g':
                agile_led_dynamic_change_light_mode(led_g, argv[2], loop_cnt);
                agile_led_start(led_g);
                break;
            case 'y':
                agile_led_dynamic_change_light_mode(led_y, argv[2], loop_cnt);
                agile_led_start(led_y);
                break;
        }
    }
    else
    {
        LOG_E("TEST_LED      --use _cmd_ [r/g/y][mode][loop_cnt]");
    }
}
MSH_CMD_EXPORT(TEST_LED, "TEST_LED");
#endif