#include "main.h"
#include "led.h"

// 定义两个灯的控制器
//LEDControl_t led_green = {GPIOB, LED_GREEN_Pin, 1000, 0, 1}; // 默认1秒闪一次
//LEDControl_t led_red   = {GPIOB, LED_RED_Pin,   0,    0, 0}; // 默认不闪
LEDControl_t led_red = {
  .GPIOx = LED2_R_GPIO_PORT,
  .GPIO_Pin = LED2_R_PIN,
  .enabled = 0,
  .flash_interval_ms = 0,
  .counter = 0,
  .flash_times = 0,
  .current_flash_count = 0,
  .flash_pause_ms = 0,
  .pause_counter = 0,
  .in_pause = 0,
	.pin_state = 0
};

LEDControl_t led_green = {
  .GPIOx = LED1_R_GPIO_PORT,
  .GPIO_Pin = LED1_G_PIN,
  .enabled = 0,
  .flash_interval_ms = 0,
  .counter = 0,
  .flash_times = 0,
  .current_flash_count = 0,
  .flash_pause_ms = 0,
  .pause_counter = 0,
  .in_pause = 0,
	.pin_state = 0
};

// 每个灯的闪烁处理函数
void LED_FlashHandler(LEDControl_t* led)
{
  if (!led->enabled || led->flash_interval_ms == 0)
    return;

  led->counter += 10;

  if (led->in_pause)
  {
    if (led->counter >= led->flash_pause_ms)
    {
      led->counter = 0;
      led->current_flash_count = 0;
      led->in_pause = 0;
      led->pin_state = 0; // 确保暂停阶段LED灭
      HAL_GPIO_WritePin(led->GPIOx, led->GPIO_Pin, GPIO_PIN_RESET);
    }
    return;
  }

  if (led->counter >= led->flash_interval_ms)
  {
    led->counter = 0;
    led->pin_state = !led->pin_state;

    if (led->pin_state)
    {
      HAL_GPIO_WritePin(led->GPIOx, led->GPIO_Pin, GPIO_PIN_SET);  // 先亮
    }
    else
    {
      HAL_GPIO_WritePin(led->GPIOx, led->GPIO_Pin, GPIO_PIN_RESET); // 再灭
      led->current_flash_count++;  // 只在灭时计一次闪
      if (led->current_flash_count >= led->flash_times)
      {
        led->in_pause = 1;
      }
    }
  }
}



void UpdateLEDStatus(LEDStatus_t status)
{
  switch (status)
  {
    case LED_NORMAL:
      led_green.enabled = 0;
//      led_green.flash_interval_ms = 1000;
      led_red.enabled = 0;
		HAL_GPIO_WritePin(LED1_G_GPIO_PORT, LED1_G_PIN, GPIO_PIN_SET);  // 绿灯常亮
		HAL_GPIO_WritePin(LED2_R_GPIO_PORT, LED2_R_PIN, GPIO_PIN_SET);  // 红灯常亮
      break;

    case LED_COMM:
      led_green.enabled = 1;
      led_green.flash_interval_ms = 500;
      led_red.enabled = 0;
      break;

    case LED_WARNING_1:
      led_green.enabled = 1;
			led_green.flash_interval_ms = 500;
			led_red.enabled = 1;
			led_red.flash_interval_ms = 100;    // 每次亮或灭为100ms（一次闪为200ms）
			led_red.flash_times = 1;           // 连闪1次
			led_red.flash_pause_ms = 2000;      // 每轮连闪之间暂停2s
			led_red.current_flash_count = 0;
			led_red.in_pause = 0;
      break;
		
		case LED_WARNING_2:
      led_green.enabled = 1;
			led_green.flash_interval_ms = 500;
			led_red.enabled = 1;
			led_red.flash_interval_ms = 100;    // 每次亮或灭为100ms（一次闪为200ms）
			led_red.flash_times = 2;           // 连闪2次
			led_red.flash_pause_ms = 2000;      // 每轮连闪之间暂停2s
			led_red.current_flash_count = 0;
			led_red.in_pause = 0;
      break;
		
		case LED_WARNING_3:
      led_green.enabled = 1;
			led_green.flash_interval_ms = 500;
			led_red.enabled = 1;
			led_red.flash_interval_ms = 100;    // 每次亮或灭为100ms（一次闪为200ms）
			led_red.flash_times = 3;           // 连闪3次
			led_red.flash_pause_ms = 2000;      // 每轮连闪之间暂停2s
			led_red.current_flash_count = 0;
			led_red.in_pause = 0;
      break;
		
		case LED_WARNING_4:
      led_green.enabled = 1;
			led_green.flash_interval_ms = 500;
			led_red.enabled = 1;
			led_red.flash_interval_ms = 100;    // 每次亮或灭为100ms（一次闪为200ms）
			led_red.flash_times = 4;           // 连闪4次
			led_red.flash_pause_ms = 2000;      // 每轮连闪之间暂停2s
			led_red.current_flash_count = 0;
			led_red.in_pause = 0;
      break;
		
		case LED_WARNING_5:
      led_green.enabled = 1;
			led_green.flash_interval_ms = 500;
			led_red.enabled = 0;
			HAL_GPIO_WritePin(LED2_R_GPIO_PORT, LED2_R_PIN, GPIO_PIN_SET);  // 红灯常亮
      break;
				
		case LED_IAP:
      led_green.enabled = 1;
			led_green.flash_interval_ms = 100;    // 每次亮或灭为100ms（一次闪为200ms）
			led_green.flash_times = 5;           // 连闪5次
			led_green.flash_pause_ms = 200;      // 每轮连闪之间暂停200ms
			led_green.current_flash_count = 0;
			led_green.in_pause = 0;
			led_red.enabled = 1;
			led_red.flash_interval_ms = 100;    // 每次亮或灭为100ms（一次闪为200ms）
			led_red.flash_times = 5;           // 连闪5次
			led_red.flash_pause_ms = 200;      // 每轮连闪之间暂停200ms
			led_red.current_flash_count = 0;
			led_red.in_pause = 0;
      break;
		
    default:
      led_green.enabled = 0;
      led_red.enabled = 0;
      break;
  }
}





















/***************************************************************/
// PWM 控制
// LED控制结构体
LED_Control leds[] = {
    {LED1_R_GPIO_PORT, LED1_R_PIN, LED1_G_GPIO_PORT, LED1_G_PIN, LED1_R_PWM_CHANNEL, LED1_G_PWM_CHANNEL, LED_OFF, 0, 0, 0, 0},
    {LED2_R_GPIO_PORT, LED2_R_PIN, LED2_G_GPIO_PORT, LED2_G_PIN, LED2_R_PWM_CHANNEL, LED2_G_PWM_CHANNEL, LED_OFF, 0, 0, 0, 0}
};

// 设置LED亮度
void LED_SetBrightness(LED_Control* led, uint8_t r_brightness, uint8_t g_brightness) 
{
    if (r_brightness > 100) r_brightness = 100; // 限制亮度范围
    if (g_brightness > 100) g_brightness = 100; // 限制亮度范围
    led->r_brightness = r_brightness;
    led->g_brightness = g_brightness;
    uint16_t r_pwm_value = (r_brightness * htim15.Init.Period) / 100; // 计算占空比
    uint16_t g_pwm_value = (g_brightness * htim15.Init.Period) / 100; // 计算占空比
    __HAL_TIM_SET_COMPARE(&htim15, led->R_PWM_Channel, r_pwm_value);  // 设置PWM占空比
    __HAL_TIM_SET_COMPARE(&htim15, led->G_PWM_Channel, g_pwm_value);  // 设置PWM占空比
}

// 设置LED状态
void LED_SetState(LED_Control* led, LED_State state) 
{
    led->state = state;
    switch (state) {
        case LED_OFF:
            LED_SetBrightness(led, 0, 0);  // 关闭LED
            break;
        case LED_ON:
            LED_SetBrightness(led, 100, 100);  // 打开LED（最大亮度）
            break;
        case LED_BREATHING:
            // 初始化呼吸灯参数
            led->r_brightness = 0;  // 初始亮度为0
            led->g_brightness = 0;
            led->blink_count = 0;   // 呼吸灯不需要闪烁次数
            led->blink_interval = 10;  // 呼吸灯亮度变化间隔（ms）
            break;
        case LED_BLINKING:
            // 初始化闪烁灯参数
            led->r_brightness = 100;  // 初始亮度为100
            led->g_brightness = 100;
            led->blink_count = 10;    // 闪烁次数（例如闪烁10次）
            led->blink_interval = 500;  // 闪烁间隔（ms）
            break;
    }
}

// 呼吸灯效果
void LED_BreathingEffect(LED_Control* led) 
{
    static uint8_t direction = 1; // 1: 增加亮度, 0: 减少亮度
    static uint8_t brightness = 0;

    if (direction) {
        brightness++;
        if (brightness >= 100) direction = 0;
    } else {
        brightness--;
        if (brightness <= 0) direction = 1;
    }

    // 设置红色和绿色亮度
    LED_SetBrightness(led, brightness, brightness);

    // 延时，控制呼吸灯速度
    HAL_Delay(led->blink_interval);
}

// 闪烁灯效果
void LED_BlinkingEffect(LED_Control* led) 
{
    static uint32_t last_tick = 0;
    static uint8_t led_state = 0;

    if (HAL_GetTick() - last_tick >= led->blink_interval) {
        last_tick = HAL_GetTick();
        led_state = !led_state;  // 切换亮灭状态
        LED_SetBrightness(led, led_state ? 100 : 0, led_state ? 100 : 0);  // 设置亮度
        if (led->blink_count > 0) led->blink_count--;  // 减少剩余闪烁次数
    }
}

// LED状态机更新
void LED_UpdateStateMachine(void) 
{
    for (int i = 0; i < 2; i++) {
        switch (leds[i].state) {
            case LED_BREATHING:
                LED_BreathingEffect(&leds[i]);  // 处理呼吸灯效果
                break;
            case LED_BLINKING:
                LED_BlinkingEffect(&leds[i]);   // 处理闪烁灯效果
                if (leds[i].blink_count == 0) {
                    LED_SetState(&leds[i], LED_OFF);  // 闪烁结束后关闭LED
                }
                break;
            default:
                break;
        }
    }
}

