/*************************************************************************************
 * @File Name: mw_led.c
 * @brief    led驱动文件
 * @Version : 1.0
 * @Create Date : 2025-04-29
 * @Author : 小鱼干儿看世界 email : 3026007337@qq.com
 * 
 * @copyright Copyright (c) 2025 Tianyun Mountain
 * 
 * modification history :
 * Date:       Version:      Author:     Description:       
  *************************************************************************************/
#include "mw_led.h"
#include <stdio.h>
#include "app_global.h"
#include "bsp_gpio.h"
#include "bsp_timer.h"

#define LED1_SOFT_TIMER (led1_timer)
#define RGB_SOFT_TIMER (rgb_timer)
/* led middleware instantiation */
mw_led_t led_drv_buf[led_num];
/* led1软件定时器 */
mw_led_timer_t str_led1_timer;
/* rgb三色灯 软件定时器 */
mw_led_timer_t str_rgb_timer;

// /* led1 基础函数包*/
// static void mw_led1_on(void)
// {
//     bsp_led_on(LED1);
// }
// static void mw_led1_off(void)
// {
//     bsp_led_off(LED1);
// }
// static uint8_t mw_get_led1_state(void)
// {
//     uint8_t retVal = 0;
    
//     retVal = bsp_get_led_ttlState(LED1);
    
//     return retVal;
// }
// /* led2 基础函数包 */
// static void mw_led2_on(void)
// {
//     // bsp_led_on(LED2);
// }
// static void mw_led2_off(void)
// {
//     // bsp_led_off(LED2);
// }
// static uint8_t mw_get_led2_state(void)
// {
//     uint8_t retVal = 0;
    
//     // retVal = bsp_get_led_ttlState(LED2);
    
//     return retVal;
// }

/*************************************************************************************
 * @brief    Get the obj of Led.
 * 
 * @param[in/out] e_led_type
 * @return mw_led_t    
 * @warning  
 * @note     
 *************************************************************************************/
mw_led_t mw_get_led_obj(led_type_enum e_led_type)
{
    if(e_led_type >= led_num)
    {
        // invalid
        while(1);
    }
    return led_drv_buf[e_led_type];
}
// 获取对应led的定时器对象
mw_led_timer_t mw_get_led1_timer_obj(void)
{
    // if(e_led_type >= led_num)
    // {
    //     // invalid
    //     while(1);
    // }
    return str_led1_timer;
}

// 获取对应led的定时器对象
mw_led_timer_t mw_get_rgb_timer_obj(void)
{
    // if(e_led_type >= led_num)
    // {
    //     // invalid
    //     while(1);
    // }
    return str_rgb_timer;
}

// 开始led1定时器
void mw_led1_start_timer(uint32_t _period)
{
    // bsp_StartTimer(led1_timer, _period);
    // led_timer_buf[e_led_type].start_timer();
    bsp_StartTimer(LED1_SOFT_TIMER, _period);
}
// 获取led1定时器是否超时
uint8_t mw_get_led1_timer_flag(void)
{
    return bsp_CheckTimer(LED1_SOFT_TIMER);
}
// 开始rgb定时器
void mw_rgb_start_timer(uint32_t _period)
{
    // bsp_StartTimer(led1_timer, _period);
    // led_timer_buf[e_led_type].start_timer();
    bsp_StartTimer(RGB_SOFT_TIMER, _period);
}
// 获取rgb定时器是否超时
uint8_t mw_get_rgb_timer_flag(void)
{
    return bsp_CheckTimer(RGB_SOFT_TIMER);
}

/*************************************************************************************
 * @brief    Led driver installation.
 * 
 * @warning  
 * @note     
 *************************************************************************************/
void mw_led_init(void)
{
    led_type_enum e_val;
    /* led1 installation */
    led_drv_buf[LED1].led_drv = LED1;
    led_drv_buf[LED1].init    = bsp_gpio_led1_init;
    led_drv_buf[LED1].on      = bsp_gpio_led1_on;
    led_drv_buf[LED1].off     = bsp_gpio_led1_off;
    led_drv_buf[LED1].state   = bsp_gpio_led1_status;
    led_drv_buf[LED1].changeBrightness = 0;
    
//    /* led2 installation */
//    led_drv_buf[LED2].led_drv = LED2;
//    led_drv_buf[LED2].init = bsp_led2_init;
//    led_drv_buf[LED2].on = mw_led2_on;
//    led_drv_buf[LED2].off = mw_led2_off;
//    led_drv_buf[LED2].state = mw_get_led2_state;
//    led_drv_buf[LED2].changeBrightness = 0;

#if LED_RGB_ENABLE > 0
/* RGB_RED installation */
    led_drv_buf[RGB_RED].led_drv = RGB_RED;
    led_drv_buf[RGB_RED].init = bsp_RGB_RedInit;
    led_drv_buf[RGB_RED].on = 0;
    led_drv_buf[RGB_RED].off = 0;
    led_drv_buf[RGB_RED].state = 0;
    led_drv_buf[RGB_RED].changeBrightness = bsp_changeBrightness_RGB_Red;
    /* RGB_GREEN installation */
    led_drv_buf[RGB_GREEN].led_drv = RGB_GREEN;
    led_drv_buf[RGB_GREEN].init = bsp_RGB_GreenInit;
    led_drv_buf[RGB_GREEN].on = 0;
    led_drv_buf[RGB_GREEN].off = 0;
    led_drv_buf[RGB_GREEN].state = 0;
    led_drv_buf[RGB_GREEN].changeBrightness = bsp_changeBrightness_RGB_Green;
    /* RGB_BLUE installation */
    led_drv_buf[RGB_BLUE].led_drv = RGB_BLUE;
    led_drv_buf[RGB_BLUE].init = bsp_RGB_BlueInit;
    led_drv_buf[RGB_BLUE].on = 0;
    led_drv_buf[RGB_BLUE].off = 0;
    led_drv_buf[RGB_BLUE].state = 0;
    led_drv_buf[RGB_BLUE].changeBrightness = bsp_changeBrightness_RGB_Blue;

#endif

    /* 软件定时器初始化 */
    str_led1_timer.start_timer = mw_led1_start_timer;
    str_led1_timer.get_over_time_flag = mw_get_led1_timer_flag;
    str_rgb_timer.start_timer = mw_rgb_start_timer;
    str_rgb_timer.get_over_time_flag = mw_get_rgb_timer_flag;

    /* init all led peripherals */
    for(e_val = LED1; e_val < led_num; e_val++)
    {
        if(led_drv_buf[e_val].init != NULL)
        {
            led_drv_buf[e_val].init();
        }
        else
        {
            while(1);
        }
    }
}
// /*************************************************************************************
//  * @brief    RGB彩灯初始化
//  * 
//  * @warning  【不可重入,阻塞等警告】
//  * @note     【重大修改】
//   *************************************************************************************/
// void mw_RGB_LED_Init(void)
// {
//     bsp_RGB_LedInit();
// }

