#include "bsp_led.h"
// #include "platform.h"
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

static struct led *led_table[LED_MAX_NUM];
static uint32_t p_led_table = 0;
static uint32_t delay_time_ms = 1;

osThreadId_t ledHandle;
const osThreadAttr_t led_attributes = {
    .name = "led",
    .stack_size = 256 * 4,
    .priority = (osPriority_t)osPriorityBelowNormal,
};

My_LED_TypeDef my_led = {
    Led_Init,
    Led_Port,
    0,
    0,
    // led_table,

};

/*  修改 MY_LED_COLOR_SEQ ，以及在 Led_Init 添加LED*/

int Led_Init()
{

    led_setup(LED_SEQ_0, LED_RED_GPIO_Port, LED_RED_Pin, LED_STATE_ON, RESET);
    led_set_state(LED_SEQ_0, LED_STATE_FLICKER_MEDIUM);

    led_setup(LED_COLOR_GREEN, LED_GRN_GPIO_Port, LED_GRN_Pin, LED_STATE_ON, RESET);

 
    return 0;
}

void Led_Task(void *argument)
{
    uint16_t *delay_ms = argument;
    for (;;)
    {
        led_callback();
        osDelay(*delay_ms);
        // osDelay(200);
    }
}

void Led_Port(uint16_t delay_ms)
{
    delay_time_ms = delay_ms;
    ledHandle = osThreadNew(Led_Task, (void *)(&delay_time_ms), &led_attributes);
    return 0;
}

int led_setup(uint8_t seq, GPIO_TypeDef *gpio, uint16_t pin, enum led_state state, uint16_t level_on)
{
    if (seq >= LED_MAX_NUM - 1 || seq < 0)
    {
        return -1;
    }
    struct led *Handle = (struct led *)malloc(sizeof(struct led));
    if (Handle == NULL)
    {
        return -1;
    }
    led_table[seq] = Handle;
    led_table[seq]->gpio = gpio;
    led_table[seq]->pin = pin;
    led_table[seq]->state = state;
    led_table[seq]->level_on = level_on;
    p_led_table++;
    return 0;
}

// int led_register(struct led *led, GPIO_TypeDef *gpio,uint16_t pin,enum led_state state,uint16_t level_on)
// {
//     if(p_led_table>=LED_MAX_NUM-1){
//         return -1;
//     }
//     led->gpio=gpio;
//     led->pin=pin;
//     led->state=state;
//     led->level_on=level_on;
//     led_table[p_led_table++]=led;
//     return 0;
// }

int led_set_state(uint8_t seq, enum led_state state)
{

    if (led_table[seq] == NULL)
    {
        return -1;
    }
    led_table[seq]->state = state;
    return 0;
}

/**
 * @brief led回调函数 每100ms调用一次
 *
 */
void led_callback(void)
{
    int i;
    static uint32_t tick_con = 0;
    tick_con++;
    for (i = 0; i < p_led_table; i++)
    {
        switch (led_table[i]->state)
        {
        case LED_STATE_OFF:
            HAL_GPIO_WritePin(led_table[i]->gpio, led_table[i]->pin, (~led_table[i]->level_on) & 1);
            // GPIO_WriteBit(led_table[i]->gpio,led_table[i]->pin,(~led_table[i]->level_on)&1);
            break;
        case LED_STATE_ON:
            HAL_GPIO_WritePin(led_table[i]->gpio, led_table[i]->pin, led_table[i]->level_on & 1);
            // GPIO_WriteBit(led_table[i]->gpio,led_table[i]->pin,led_table[i]->level_on&1);
            break;
        case LED_STATE_FLICKER_QUICK:
            HAL_GPIO_TogglePin(led_table[i]->gpio, led_table[i]->pin);
            // GPIO_WriteBit(led_table[i]->gpio,led_table[i]->pin,!GPIO_ReadOutputDataBit(led_table[i]->gpio,led_table[i]->pin));
            break;
        case LED_STATE_FLICKER_MEDIUM:

            if ((tick_con & 0x01) == 0)
            {
                HAL_GPIO_TogglePin(led_table[i]->gpio, led_table[i]->pin);
                // GPIO_WriteBit(led_table[i]->gpio,led_table[i]->pin,!GPIO_ReadOutputDataBit(led_table[i]->gpio,led_table[i]->pin));
            }
            break;
        case LED_STATE_FLICKER_SLOW:
            if ((tick_con & 0x03) == 0)
            {
                HAL_GPIO_TogglePin(led_table[i]->gpio, led_table[i]->pin);
                // GPIO_WriteBit(led_table[i]->gpio,led_table[i]->pin,!GPIO_ReadOutputDataBit(led_table[i]->gpio,led_table[i]->pin));
            }
            break;
        default:
            break;
        }
    }
}