#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 = LED1_R_GPIO_PORT,
  .GPIO_Pin = LED1_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_self, LEDControl_t* led_other)
{
  if (!led_self->enabled || led_self->flash_interval_ms == 0)
    return;

  led_self->counter += 10;

  if (led_self->in_pause)
  {
    if (led_self->counter >= led_self->flash_pause_ms)
    {
      led_self->counter = 0;
      led_self->current_flash_count = 0;
      led_self->in_pause = 0;
      led_self->pin_state = 0;
      HAL_GPIO_WritePin(led_self->GPIOx, led_self->GPIO_Pin, GPIO_PIN_RESET);
    }
    return;
  }

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

    if (led_self->pin_state)
    {
      // 点亮自己前，先熄灭对方，保持双色灯互斥
      HAL_GPIO_WritePin(led_other->GPIOx, led_other->GPIO_Pin, GPIO_PIN_RESET);
      HAL_GPIO_WritePin(led_self->GPIOx, led_self->GPIO_Pin, GPIO_PIN_SET);
    }
    else
    {
      HAL_GPIO_WritePin(led_self->GPIOx, led_self->GPIO_Pin, GPIO_PIN_RESET);
      led_self->current_flash_count++;
      if (led_self->current_flash_count >= led_self->flash_times)
      {
        led_self->in_pause = 1;
      }
    }
  }
}




void UpdateLEDStatus(LEDStatus_t status)
{
  switch (status)
  {
    case LED_NORMAL:
      led_green.enabled = 0;
      led_red.enabled = 0;
			HAL_GPIO_WritePin(LED1_G_GPIO_PORT, LED1_G_PIN, GPIO_PIN_SET);  // 绿灯常亮
			HAL_GPIO_WritePin(LED1_R_GPIO_PORT, LED1_R_PIN, GPIO_PIN_RESET);  // 红灯灭
      break;
		
		case LED_G:
      led_green.enabled = 0;
      led_red.enabled = 0;
			HAL_GPIO_WritePin(LED1_G_GPIO_PORT, LED1_G_PIN, GPIO_PIN_SET);  // 绿灯常亮
			HAL_GPIO_WritePin(LED1_R_GPIO_PORT, LED1_R_PIN, GPIO_PIN_RESET);  // 红灯灭
      break;
		
		case LED_R:
      led_green.enabled = 0;
      led_red.enabled = 0;
			HAL_GPIO_WritePin(LED1_G_GPIO_PORT, LED1_G_PIN, GPIO_PIN_RESET);  // 绿灯灭
			HAL_GPIO_WritePin(LED1_R_GPIO_PORT, LED1_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(LED1_R_GPIO_PORT, LED1_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 = 1;           // 连闪5次
			led_green.flash_pause_ms = 0;      // 每轮连闪之间暂停0ms
			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 = 1;           // 连闪5次
			led_red.flash_pause_ms = 0;      // 每轮连闪之间暂停0ms
			led_red.current_flash_count = 0;
			led_red.in_pause = 0;
      break;
		
    default:
      led_green.enabled = 0;
      led_red.enabled = 0;
      break;
  }
}

