#ifndef USER_LED_H__
#define USER_LED_H__

#include <stdint.h>
#include <stdbool.h>
#include "define.h"

#ifdef __cplusplus
extern "C" {
#endif

#define LED_INTERVAL						                    10

#define LED_ON_DUTY                                             95
#define LED_OFF_DUTY                                            0

#define TIM_LED_PERIOD					                        100
#define TIM_LED_PRESCALER					                    360
#define TIM3_LED_TYPE						                    TIM3
#define TIM4_LED_TYPE						                    TIM4
#define TIM5_LED_TYPE						                    TIM5

#define LED_PWM_RCC_ENABLE					                    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4 | RCC_APB1Periph_TIM5, ENABLE);
#define LED_GPIO_RCC_ENABLE					                    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);

#define LED_REMAPCONFIG_ENABLE                                  GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);

#define DEV_LED_GPIO                                      	    GPIOB
#define DEV_LED_PIN                                       	    GPIO_Pin_12
#define DEV_LED_ON						                        GPIO_WriteBit(DEV_LED_GPIO, DEV_LED_PIN, Bit_RESET)
#define DEV_LED_OFF						                        GPIO_WriteBit(DEV_LED_GPIO, DEV_LED_PIN, Bit_SET)

#define LED_01_R_GPIO                                     	    GPIOB
#define LED_01_R_PIN                                       	    GPIO_Pin_4

#define LED_01_G_GPIO                                      	    GPIOB
#define LED_01_G_PIN                                       	    GPIO_Pin_5

#define LED_02_R_GPIO                                     	    GPIOB
#define LED_02_R_PIN                                       	    GPIO_Pin_6

#define LED_02_G_GPIO                                      	    GPIOB
#define LED_02_G_PIN                                       	    GPIO_Pin_7

#define LED_03_R_GPIO                                     	    GPIOB
#define LED_03_R_PIN                                       	    GPIO_Pin_8

#define LED_03_G_GPIO                                      	    GPIOB
#define LED_03_G_PIN                                       	    GPIO_Pin_9

#define LED_04_R_GPIO                                     	    GPIOA
#define LED_04_R_PIN                                       	    GPIO_Pin_0

#define LED_04_G_GPIO                                      	    GPIOA
#define LED_04_G_PIN                                       	    GPIO_Pin_1

#define LED_05_R_GPIO                                     	    GPIOA
#define LED_05_R_PIN                                       	    GPIO_Pin_2

#define LED_05_G_GPIO                                      	    GPIOA
#define LED_05_G_PIN                                       	    GPIO_Pin_3

#define LED_01_R_INIT(InitStructure)					        TIM_OC1Init(TIM3_LED_TYPE, InitStructure);
#define LED_01_G_INIT(InitStructure)				            TIM_OC2Init(TIM3_LED_TYPE, InitStructure);

#define LED_02_R_INIT(InitStructure)				            TIM_OC1Init(TIM4_LED_TYPE, InitStructure);
#define LED_02_G_INIT(InitStructure)				            TIM_OC2Init(TIM4_LED_TYPE, InitStructure);

#define LED_03_R_INIT(InitStructure)				            TIM_OC3Init(TIM4_LED_TYPE, InitStructure);
#define LED_03_G_INIT(InitStructure)				            TIM_OC4Init(TIM4_LED_TYPE, InitStructure);

#define LED_04_R_INIT(InitStructure)				            TIM5_OC1Init(InitStructure);
#define LED_04_G_INIT(InitStructure)				            TIM5_OC2Init(InitStructure);

#define LED_05_R_INIT(InitStructure)				            TIM5_OC3Init(InitStructure);
#define LED_05_G_INIT(InitStructure)				            TIM5_OC4Init(InitStructure);

#define LED_01_R_RUN						                    TIM_OC1PreloadConfig(TIM3_LED_TYPE, TIM_OCPreload_Enable);
#define LED_01_R_STOP						                    TIM_OC1PreloadConfig(TIM3_LED_TYPE, TIM_OCPreload_Disable);
#define LED_01_R_COMPARE(value)					                TIM_SetCompare1(TIM3_LED_TYPE, value)

#define LED_01_G_RUN						                    TIM_OC2PreloadConfig(TIM3_LED_TYPE, TIM_OCPreload_Enable);
#define LED_01_G_STOP						                    TIM_OC2PreloadConfig(TIM3_LED_TYPE, TIM_OCPreload_Disable);
#define LED_01_G_COMPARE(value)					                TIM_SetCompare2(TIM3_LED_TYPE, value)

#define LED_02_R_RUN						                    TIM_OC1PreloadConfig(TIM4_LED_TYPE, TIM_OCPreload_Enable);
#define LED_02_R_STOP						                    TIM_OC1PreloadConfig(TIM4_LED_TYPE, TIM_OCPreload_Disable);
#define LED_02_R_COMPARE(value)					                TIM_SetCompare1(TIM4_LED_TYPE, value)

#define LED_02_G_RUN						                    TIM_OC2PreloadConfig(TIM4_LED_TYPE, TIM_OCPreload_Enable);
#define LED_02_G_STOP						                    TIM_OC2PreloadConfig(TIM4_LED_TYPE, TIM_OCPreload_Disable);
#define LED_02_G_COMPARE(value)					                TIM_SetCompare2(TIM4_LED_TYPE, value)

#define LED_03_R_RUN						                    TIM_OC3PreloadConfig(TIM4_LED_TYPE, TIM_OCPreload_Enable);
#define LED_03_R_STOP						                    TIM_OC3PreloadConfig(TIM4_LED_TYPE, TIM_OCPreload_Disable);
#define LED_03_R_COMPARE(value)					                TIM_SetCompare3(TIM4_LED_TYPE, value)

#define LED_03_G_RUN						                    TIM_OC4PreloadConfig(TIM4_LED_TYPE, TIM_OCPreload_Enable);
#define LED_03_G_STOP						                    TIM_OC4PreloadConfig(TIM4_LED_TYPE, TIM_OCPreload_Disable);
#define LED_03_G_COMPARE(value)					                TIM_SetCompare4(TIM4_LED_TYPE, value)

#define LED_04_R_RUN						                    TIM5_OC1PreloadConfig(TIM_OCPreload_Enable);
#define LED_04_R_STOP						                    TIM5_OC1PreloadConfig(TIM_OCPreload_Disable);
#define LED_04_R_COMPARE(value)					                TIM5_SetCompare1(value)

#define LED_04_G_RUN						                    TIM5_OC2PreloadConfig(TIM_OCPreload_Enable);
#define LED_04_G_STOP						                    TIM5_OC2PreloadConfig(TIM_OCPreload_Disable);
#define LED_04_G_COMPARE(value)					                TIM5_SetCompare2(value)

#define LED_05_R_RUN						                    TIM5_OC3PreloadConfig(TIM_OCPreload_Enable);
#define LED_05_R_STOP						                    TIM5_OC3PreloadConfig(TIM_OCPreload_Disable);
#define LED_05_R_COMPARE(value)					                TIM5_SetCompare3(value)

#define LED_05_G_RUN						                    TIM5_OC4PreloadConfig(TIM_OCPreload_Enable);
#define LED_05_G_STOP						                    TIM5_OC4PreloadConfig(TIM_OCPreload_Disable);
#define LED_05_G_COMPARE(value)					                TIM5_SetCompare4(value)

typedef enum
{
    LED_QUEUE_TIME	= 0,
}LED_QUEUE_TYPE;

typedef enum{
    DEV_LED 		= 0,
    LED_01 		    = 1,
    LED_02 		    = 2,
    LED_03 		    = 3,
    LED_04 		    = 4,
    LED_05 		    = 5,
    LED_TYPE_MAX	= 6,
}LED_TYPE;

typedef enum{
    RED_LED 		= 0,
    GREEN_LED 		= 1,
    YELLOW_LED 		= 2,
}LED_COLOR;

typedef enum{
    LED_STAT_OFF 	= 0,
    LED_STAT_ON		= 1
}LED_STAT;

typedef enum{
    LED_WORK_OFF 	    = 0,            //暗
    LED_WORK_ON 	    = 1,            //亮
    LED_WORK_INVERT 	= 2,            //闪烁
    LED_WORK_BREATHE    = 4,            //呼吸模式
    LED_WORK_MAX 	    = 5,
}LED_WORK_STAT;

typedef void (*led_set_duty_func)(uint16_t duty);

typedef enum{
    LED_VIEW_NULL = 0,
    LED_VIEW_ALARM,                         //报警
    LED_VIEW_WARN,                          //警告
    LED_VIEW_LOCK_ELECT,                    //有挂锁并上锁
    LED_VIEW_NOLOCK_ELECT,                  //无挂锁并上锁
    LED_VIEW_LOCK_NOELECT,                  //有挂锁没上锁
    LED_VIEW_NOLOCK_NOELECT,                //无挂锁没上锁
}led_view_stat_e;

typedef enum{
    LED_ALL_ALARM_RST = 0,
    LED_ALL_ALARM_BLE = 1,
    LED_ALL_ALARM_CAN_IDX_ERROR = 2,
    LED_ALL_ALARM_OFFLINE = 3,
}led_all_alarm_stat_e;

typedef struct{
    const uint8_t idx;
    LED_STAT stat;                        
    LED_COLOR color;
    LED_WORK_STAT workStat;
    led_view_stat_e viewStat;
    uint32_t single_flicker_period;
    uint32_t flicker_period ;
    uint32_t flicker_onperiod;
    uint32_t flicker_count;
    uint8_t duty_on;
    uint32_t duty_value;
    uint32_t single_on_durationTime;		//PWM变化结束后持续时间，单位：ms
    uint32_t on_durationTime;			    //PWM变化结束后持续时间，单位：ms
    uint32_t single_off_durationTime;		//PWM变化结束后持续时间，单位：ms
    uint32_t off_durationTime;			    //PWM变化结束后持续时间，单位：ms
    led_set_duty_func set_duty;
}led_work_s;

void led_task_init(void);
void led_task_start(void);
bool set_led_indication(LED_TYPE type, LED_COLOR color, LED_WORK_STAT work_stat, ...);
void vUser_led_set_view_stat(LED_TYPE idx, led_view_stat_e stat);
void vUser_led_set_alarm_view(led_all_alarm_stat_e alarmStat, bool stat);

extern tmosTaskID led_task_id;

#ifdef __cplusplus
}
#endif

#endif // LED_H__

/** @} */
