#include "hard_led.h"
#include <stdint.h>
#include "gpio.h"
#include "soft_p_2_c.h"
#include "soft_flash.h"


LED_Red L_red;
LED_Blue L_blue;
LED_Green L_green;
LED_Purple L_purple;
LED_Cyan L_cyan;
LED_Yellow L_yellow;
LED_White L_white;
bool led_is_ok = false;

void Update_LED_Time(){
	L_red.time = HAL_GetTick();
	L_blue.time = HAL_GetTick();
	L_green.time = HAL_GetTick();
	L_purple.time = HAL_GetTick();
	L_cyan.time = HAL_GetTick();
	L_yellow.time = HAL_GetTick();
	L_white.time = HAL_GetTick();
}

/**
  * @file    led_set_blink
  * @brief   LED闪烁
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void led_set_blink(uint8_t LED, uint8_t on_time, uint8_t off_time, uint8_t repeat)
{
	if (LED == Red)
	{
		L_red.on_time_ms = on_time * 100;
		L_red.off_time_ms = off_time * 100;
		LEDRed_OFF();
		L_red.repeat = repeat;
		L_red.time = HAL_GetTick();
		L_red.status = OFF;
	}
	else if (LED == Green)
	{
		L_green.on_time_ms = on_time * 100;
		L_green.off_time_ms = off_time * 100;
		LEDGreen_OFF();
		L_green.repeat = repeat;
		L_green.time = HAL_GetTick();
		L_green.status = OFF;
	}
	else if (LED == Blue)
	{
		L_blue.on_time_ms = on_time * 100;
		L_blue.off_time_ms = off_time * 100;
		LEDBlue_OFF();
		L_blue.repeat = repeat;
		L_blue.time = HAL_GetTick();
		L_blue.status = OFF;
	}
	else if (LED == Purple)
	{
		L_purple.on_time_ms = on_time * 100;
		L_purple.off_time_ms = off_time * 100;
		LEDPurple_OFF();
		L_purple.repeat = repeat;
		L_purple.time = HAL_GetTick();
		L_purple.status = OFF;
	}
	else if (LED == Yellow)
	{
		L_yellow.on_time_ms = on_time * 100;
		L_yellow.off_time_ms = off_time * 100;
		LEDYellow_OFF();
		L_yellow.repeat = repeat;
		L_yellow.time = HAL_GetTick();
		L_yellow.status = OFF;
	}
	else if (LED == Cyan)
	{
		L_cyan.on_time_ms = on_time * 100;
		L_cyan.off_time_ms = off_time * 100;
		LEDCyan_OFF();
		L_cyan.repeat = repeat;
		L_cyan.time = HAL_GetTick();
		L_cyan.status = OFF;
	}
	else if (LED == White)
	{
		L_white.on_time_ms = on_time * 100;
		L_white.off_time_ms = off_time * 100;
		LEDWhite_OFF();
		L_white.repeat = repeat;
		L_white.time = HAL_GetTick();
		L_white.status = OFF;
	}
}

/**
  * @file    led_blink_repeat
  * @brief   LED循环
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
uint32_t L_red_time = 0;
void led_blink_repeat()
{	
	//青岛中飞机型不需要LED
	if(uavinf.uavtype == VK_ALL_IN_ONE)
		return;
	
	//没收到fmu数据 15秒内常亮红灯
	if(recv_fmu_data == false)
	{
		if (HAL_GetTick() < 15000)
		{
			LEDRed_ON();
		}
		else
		{
			if(HAL_GetTick() - L_red_time > 2000)
			{
				HAL_GPIO_TogglePin(GPIOB,LED_R_Pin);
				L_red_time = HAL_GetTick();
			}
		}
	}


	if ((L_red.repeat == 0) && (L_blue.repeat == 0) && (L_green.repeat == 0) && (L_cyan.repeat == 0) &&
		(L_purple.repeat == 0) && (L_yellow.repeat == 0) && (L_white.repeat == 0))
	{
		return;
	}
	if(recv_fmu_data == true)
	{
		if (L_red.repeat != 0)
		{

			if ((L_red.off_time_ms == 25500) || (L_red.on_time_ms == 25500))
			{
				L_red.repeat = 0;
				if (L_red.off_time_ms == 25500)
				{
					LEDRed_OFF();
				}
				else if (L_red.on_time_ms == 25500)
				{
					LEDRed_ON();
				}

				return;
			}
			if (L_red.status == ON)
			{
				if (HAL_GetTick() - L_red.time > L_red.on_time_ms)
				{
					L_red.repeat--;
					LEDRed_OFF();
					Update_LED_Time();
					L_red.status =OFF;
					
				}
			}
			else
			{
				if (HAL_GetTick() - L_red.time > L_red.off_time_ms)
				{
					if (L_red.repeat != 0)
					{
						LEDRed_ON();
						L_red.time = HAL_GetTick();
						L_red.status = ON;
					}
				}
			}
		}
		else if (L_green.repeat != 0)
		{

			if ((L_green.off_time_ms == 25500) || (L_green.on_time_ms == 25500))
			{
				L_green.repeat = 0;
				if (L_green.off_time_ms == 25500)
				{
					LEDGreen_OFF();
				}
				else if (L_green.on_time_ms == 25500)
				{
					LEDGreen_ON();
				}

				return;
			}
			if (L_green.status == ON)
			{
				if (HAL_GetTick() - L_green.time > L_green.on_time_ms)
				{
					L_green.repeat--;
					LEDGreen_OFF();
					Update_LED_Time();
					L_green.status = OFF;
				}
			}
			else
			{
				if (HAL_GetTick() - L_green.time > L_green.off_time_ms)
				{
					if (L_green.repeat != 0)
					{
						LEDGreen_ON();
						L_green.time = HAL_GetTick();
						L_green.status = ON;
					}
				}
			}
		}

		else if (L_blue.repeat != 0)
		{
			if ((L_blue.off_time_ms == 25500) || (L_blue.on_time_ms == 25500))
			{
				L_blue.repeat = 0;
				if (L_blue.off_time_ms == 25500)
				{
					LEDBlue_OFF();
				}
				else if (L_blue.on_time_ms == 25500)
				{
					LEDBlue_ON();
				}

				return;
			}
			if (L_blue.status == ON)
			{
				if (HAL_GetTick() - L_blue.time > L_blue.on_time_ms)
				{
					L_blue.repeat--;
					LEDBlue_OFF();
					Update_LED_Time();
					L_blue.status = OFF;
				}
			}
			else
			{
				if (HAL_GetTick() - L_blue.time > L_blue.off_time_ms)
				{
					if (L_blue.repeat != 0)
					{
						LEDBlue_ON();
						L_blue.time = HAL_GetTick();
						L_blue.status = ON;
					}
				}
			}
		}

		else if (L_purple.repeat != 0)
		{

			if ((L_purple.off_time_ms == 25500) || (L_purple.on_time_ms == 25500))
			{
				L_purple.repeat = 0;
				if (L_purple.off_time_ms == 25500)
				{
					LEDPurple_OFF();
				}
				else if (L_purple.on_time_ms == 25500)
				{
					LEDPurple_ON();
				}

				return;
			}
			if (L_purple.status == ON)
			{
				if (HAL_GetTick() - L_purple.time > L_purple.on_time_ms)
				{
					L_purple.repeat--;
					LEDPurple_OFF();
					Update_LED_Time();
					L_purple.status = OFF;
				}
			}
			else
			{
				if (HAL_GetTick() - L_purple.time > L_purple.off_time_ms)
				{
					if (L_purple.repeat != 0)
					{
						LEDPurple_ON();
						L_purple.time = HAL_GetTick();
						L_purple.status = ON;
					}
				}
			}
		}

		else if (L_yellow.repeat != 0)
		{

			if ((L_yellow.off_time_ms == 25500) || (L_yellow.on_time_ms == 25500))
			{
				L_yellow.repeat = 0;
				if (L_yellow.off_time_ms == 25500)
				{
					LEDYellow_OFF();
				}
				else if (L_yellow.on_time_ms == 25500)
				{
					LEDYellow_ON();
				}

				return;
			}
			if (L_yellow.status == ON)
			{
				if (HAL_GetTick() - L_yellow.time > L_yellow.on_time_ms)
				{
					L_yellow.repeat--;
					LEDYellow_OFF();
					Update_LED_Time();
					L_yellow.status = OFF;
				}
			}
			else
			{
				if (HAL_GetTick() - L_yellow.time > L_yellow.off_time_ms)
				{
					if (L_yellow.repeat != 0)
					{
						LEDYellow_ON();
						L_yellow.time = HAL_GetTick();
						L_yellow.status = ON;
					}
				}
			}
		}
		
		else if (L_cyan.repeat != 0)
		{

			if ((L_cyan.off_time_ms == 25500) || (L_cyan.on_time_ms == 25500))
			{
				L_cyan.repeat = 0;
				if (L_cyan.off_time_ms == 25500)
				{
					LEDCyan_OFF();
				}
				else if (L_cyan.on_time_ms == 25500)
				{
					LEDCyan_ON();
				}

				return;
			}
			if (L_cyan.status == ON)
			{
				if (HAL_GetTick() - L_cyan.time > L_cyan.on_time_ms)
				{
					L_cyan.repeat--;
					LEDCyan_OFF();
					Update_LED_Time();
					L_cyan.status = OFF;
				}
			}
			else
			{
				if (HAL_GetTick() - L_cyan.time > L_cyan.off_time_ms)
				{
					if (L_cyan.repeat != 0)
					{
						LEDCyan_ON();
						L_cyan.time = HAL_GetTick();
						L_cyan.status = ON;
					}
				}
			}
		}
		else if (L_white.repeat != 0)
		{

			if ((L_white.off_time_ms == 25500) || (L_white.on_time_ms == 25500))
			{
				L_white.repeat = 0;
				if (L_white.off_time_ms == 25500)
				{
					LEDWhite_OFF();
				}
				else if (L_white.on_time_ms == 25500)
				{
					LEDWhite_ON();
				}

				return;
			}
			if (L_white.status == ON)
			{
				if (HAL_GetTick() - L_white.time > L_white.on_time_ms)
				{
					L_white.repeat--;
					LEDWhite_OFF();
					Update_LED_Time();
					L_white.status = OFF;
				}
			}
			else
			{
				if (HAL_GetTick() - L_white.time > L_white.off_time_ms)
				{
					if (L_white.repeat != 0)
					{
						LEDWhite_ON();
						L_white.time = HAL_GetTick();
						L_white.status = ON;
					}
				}
			}
		}
	}
}
