#ifndef _LED_H_
#define _LED_H_

#include "main.h"
#include "tim.h"
#include "gpio.h"

#define LED1_R_GPIO_PORT GPIOB
#define LED1_R_PIN GPIO_PIN_7
#define LED1_G_GPIO_PORT GPIOB
#define LED1_G_PIN GPIO_PIN_6
#define LED2_R_GPIO_PORT GPIOA
#define LED2_R_PIN GPIO_PIN_12
#define LED2_G_GPIO_PORT GPIOA
#define LED2_G_PIN GPIO_PIN_11

// ======== LED 状态类型定义 ========
typedef enum {
  LED_NORMAL,
  LED_COMM,
  LED_WARNING_1,
	LED_WARNING_2,
	LED_WARNING_3,
	LED_WARNING_4,
	LED_WARNING_5,
	LED_IAP
} LEDStatus_t;

// ======== LED 控制结构体 ========
typedef struct {
  GPIO_TypeDef* GPIOx;
  uint16_t GPIO_Pin;
  uint16_t flash_interval_ms;   // 单次闪烁周期
  uint16_t counter;
  uint8_t enabled;              // 是否启用闪烁
  uint8_t flash_times;          // 连续闪烁次数（例如：2 表示每轮闪2下）
  uint8_t current_flash_count;  // 当前这轮闪了几次
  uint16_t flash_pause_ms;      // 连闪之间的间隔时间
  uint16_t pause_counter;       // 连闪之间的等待计数器
  uint8_t in_pause;             // 是否处于暂停阶段
	uint8_t pin_state;  					// 当前LED引脚状态（0灭，1亮）
} LEDControl_t;



// 定义两个灯的控制器（可扩展）
extern LEDControl_t led_green; 	// 绿灯
extern LEDControl_t led_red; 		// 红灯


void LED_FlashHandler(LEDControl_t* led);
void UpdateLEDStatus(LEDStatus_t status);




/*************************************************************/
// 定义LED的GPIO和PWM通道


#define LED1_R_PWM_CHANNEL TIM_CHANNEL_1
#define LED1_G_PWM_CHANNEL TIM_CHANNEL_2


#define LED2_R_PWM_CHANNEL TIM_CHANNEL_1
#define LED2_G_PWM_CHANNEL TIM_CHANNEL_2



// 定义LED状态
typedef enum {
    LED_OFF = 0,
    LED_ON,
    LED_BREATHING,
    LED_BLINKING
} LED_State;

// 定义LED控制结构体
typedef struct {
    GPIO_TypeDef* R_GPIO_Port;
    uint16_t R_GPIO_Pin;
    GPIO_TypeDef* G_GPIO_Port;
    uint16_t G_GPIO_Pin;
    uint32_t R_PWM_Channel;
    uint32_t G_PWM_Channel;
    LED_State state;
    uint8_t r_brightness;  // 红色亮度值（0-100）
    uint8_t g_brightness;  // 绿色亮度值（0-100）
    uint8_t blink_count;   // 闪烁次数
    uint16_t blink_interval; // 闪烁间隔（ms）
} LED_Control;

extern LED_Control leds[];



void LED_SetBrightness(LED_Control* led, uint8_t r_brightness, uint8_t g_brightness) ;
void LED_SetState(LED_Control* led, LED_State state) ;	
void LED_BreathingEffect(LED_Control* led) ;
void LED_BlinkingEffect(LED_Control* led) ;
void LED_UpdateStateMachine(void) ;
#endif



