/***********************************************************************************************************
 * Copyright (c)  2021 - 2022, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : app_led_sw.c
 * Description : app led sw source file
 * Author(s)   : yanhaihua
 * version     : V1.0
 * Modify date : 2021-09-24
 ***********************************************************************************************************/
#include "app_led_sw.h"
#include "gpio.h"
#include "delay.h"

#define APP_LED_LFP_X_PIN											PA3
#define APP_LED_LFP_Y_PIN											PA5
#define APP_LED_LFP_Z_PIN											PA4
#define APP_LED_RSSI_0_PIN											PD0
#define APP_LED_RSSI_1_PIN											PD1
#define APP_LED_RSSI_2_PIN											PD2
#define APP_LED_RSSI_3_PIN											PD3
#define APP_LED_RSSI_4_PIN											PD4
#define APP_LED_S_P_PIN												PB4
				
#define APP_SW_KEY_PIN												PA0

#define APP_UM2020_LED_RSSI_LEVEL_0									0x01
#define APP_UM2020_LED_RSSI_LEVEL_1									0x04
#define APP_UM2020_LED_RSSI_LEVEL_2									0x07
#define APP_UM2020_LED_RSSI_LEVEL_3									0x0A
#define APP_UM2020_LED_RSSI_LEVEL_4									0x0D


static void app_sw_key_irq(void);
/***********************************************************************************************************
 * Function		: app_led_init
 * Description	: app_led_init
 * Input		: none
 * Output		: none
 * Return		: none
 ***********************************************************************************************************/
void app_led_init(void)
{
	gpio_init(APP_LED_LFP_X_PIN);
	gpio_set_dir(APP_LED_LFP_X_PIN,GPIO_DIR_OUTPUT);
	gpio_set_pu(APP_LED_LFP_X_PIN,ENABLE);
	gpio_set_bit(APP_LED_LFP_X_PIN);
	
	gpio_init(APP_LED_LFP_Y_PIN);
	gpio_set_dir(APP_LED_LFP_Y_PIN,GPIO_DIR_OUTPUT);
	gpio_set_pu(APP_LED_LFP_Y_PIN,ENABLE);
	gpio_set_bit(APP_LED_LFP_Y_PIN);
	
	gpio_init(APP_LED_LFP_Z_PIN);
	gpio_set_dir(APP_LED_LFP_Z_PIN,GPIO_DIR_OUTPUT);
	gpio_set_pu(APP_LED_LFP_Z_PIN,ENABLE);
	gpio_set_bit(APP_LED_LFP_Z_PIN);
	
	gpio_init(APP_LED_RSSI_0_PIN);
	gpio_set_dir(APP_LED_RSSI_0_PIN,GPIO_DIR_OUTPUT);
	gpio_set_pu(APP_LED_RSSI_0_PIN,ENABLE);
	gpio_set_bit(APP_LED_RSSI_0_PIN);
	
	gpio_init(APP_LED_RSSI_1_PIN);
	gpio_set_dir(APP_LED_RSSI_1_PIN,GPIO_DIR_OUTPUT);
	gpio_set_pu(APP_LED_RSSI_1_PIN,ENABLE);
	gpio_set_bit(APP_LED_RSSI_1_PIN);
	
	gpio_init(APP_LED_RSSI_2_PIN);
	gpio_set_dir(APP_LED_RSSI_2_PIN,GPIO_DIR_OUTPUT);
	gpio_set_pu(APP_LED_RSSI_2_PIN,ENABLE);
	gpio_set_bit(APP_LED_RSSI_2_PIN);
	
	gpio_init(APP_LED_RSSI_3_PIN);
	gpio_set_dir(APP_LED_RSSI_3_PIN,GPIO_DIR_OUTPUT);
	gpio_set_pu(APP_LED_RSSI_3_PIN,ENABLE);
	gpio_set_bit(APP_LED_RSSI_3_PIN);
	
	gpio_init(APP_LED_RSSI_4_PIN);
	gpio_set_dir(APP_LED_RSSI_4_PIN,GPIO_DIR_OUTPUT);
	gpio_set_pu(APP_LED_RSSI_4_PIN,ENABLE);
	gpio_set_bit(APP_LED_RSSI_4_PIN);

	gpio_init(APP_LED_S_P_PIN);
	gpio_set_dir(APP_LED_S_P_PIN,GPIO_DIR_OUTPUT);
	gpio_set_pu(APP_LED_S_P_PIN,ENABLE);
	gpio_set_bit(APP_LED_S_P_PIN);
}

/***********************************************************************************************************
 * Function		: app_led_flicker
 * Description	: LED闪烁
 * Input		: none
 * Output		: none
 * Return		: none
 ***********************************************************************************************************/
void app_led_flicker(uint16_t ms,uint8_t cnt)
{
	for(uint8_t i=0;i<cnt;i++)
	{
		gpio_clr_bit(APP_LED_LFP_X_PIN);
		gpio_clr_bit(APP_LED_LFP_Y_PIN);
		gpio_clr_bit(APP_LED_LFP_Z_PIN);
		gpio_clr_bit(APP_LED_RSSI_0_PIN);
		gpio_clr_bit(APP_LED_RSSI_1_PIN);
		gpio_clr_bit(APP_LED_RSSI_2_PIN);
		gpio_clr_bit(APP_LED_RSSI_3_PIN);
		gpio_clr_bit(APP_LED_RSSI_4_PIN);
		delay_ms(ms);
		gpio_set_bit(APP_LED_LFP_X_PIN);
		gpio_set_bit(APP_LED_LFP_Y_PIN);
		gpio_set_bit(APP_LED_LFP_Z_PIN);
		gpio_set_bit(APP_LED_RSSI_0_PIN);
		gpio_set_bit(APP_LED_RSSI_1_PIN);
		gpio_set_bit(APP_LED_RSSI_2_PIN);
		gpio_set_bit(APP_LED_RSSI_3_PIN);
		gpio_set_bit(APP_LED_RSSI_4_PIN);
		delay_ms(ms);
	}
}

/***********************************************************************************************************
 * Function		: app_led_off
 * Description	: app_led_off
 * Input		: none
 * Output		: none
 * Return		: none
 ***********************************************************************************************************/
void app_led_off(void)
{
	gpio_set_bit(APP_LED_LFP_X_PIN);
	gpio_set_bit(APP_LED_LFP_Y_PIN);
	gpio_set_bit(APP_LED_LFP_Z_PIN);
	gpio_set_bit(APP_LED_RSSI_0_PIN);
	gpio_set_bit(APP_LED_RSSI_1_PIN);
	gpio_set_bit(APP_LED_RSSI_2_PIN);
	gpio_set_bit(APP_LED_RSSI_3_PIN);
	gpio_set_bit(APP_LED_RSSI_4_PIN);
}

/***********************************************************************************************************
 * Function		: app_led_xyz_rssi
 * Description	: app_led_xyz_rssi
 * Input		: none
 * Output		: none
 * Return		: none
 ***********************************************************************************************************/
void app_led_xyz_rssi(uint8_t ch1_rssi,uint8_t ch2_rssi,uint8_t ch3_rssi)
{
	uint8_t max_rssi = 0;
	if(ch1_rssi == 0 && ch2_rssi == 0 && ch3_rssi == 0)
	{
		gpio_set_bit(APP_LED_LFP_X_PIN);
		gpio_set_bit(APP_LED_LFP_Y_PIN);
		gpio_set_bit(APP_LED_LFP_Z_PIN);
	}
	else
	{
	
		if(ch1_rssi == ch2_rssi)
		{
			if(ch1_rssi == ch3_rssi)
			{
				max_rssi = ch1_rssi;
				gpio_clr_bit(APP_LED_LFP_X_PIN);
				gpio_clr_bit(APP_LED_LFP_Y_PIN);
				gpio_clr_bit(APP_LED_LFP_Z_PIN);
			}
			else if(ch1_rssi > ch3_rssi)
			{
				max_rssi = ch1_rssi;
				gpio_clr_bit(APP_LED_LFP_X_PIN);
				gpio_clr_bit(APP_LED_LFP_Y_PIN);
				gpio_set_bit(APP_LED_LFP_Z_PIN);
			}
			else
			{
				max_rssi = ch3_rssi;
				gpio_set_bit(APP_LED_LFP_X_PIN);
				gpio_set_bit(APP_LED_LFP_Y_PIN);
				gpio_clr_bit(APP_LED_LFP_Z_PIN);
			}
		}
		else if(ch1_rssi > ch2_rssi)
		{
			if(ch1_rssi == ch3_rssi)
			{
				max_rssi = ch1_rssi;
				gpio_clr_bit(APP_LED_LFP_X_PIN);
				gpio_set_bit(APP_LED_LFP_Y_PIN);
				gpio_clr_bit(APP_LED_LFP_Z_PIN);
			}
			else if(ch1_rssi > ch3_rssi)
			{
				max_rssi = ch1_rssi;
				gpio_clr_bit(APP_LED_LFP_X_PIN);
				gpio_set_bit(APP_LED_LFP_Y_PIN);
				gpio_set_bit(APP_LED_LFP_Z_PIN);
			}
			else
			{
				max_rssi = ch3_rssi;
				gpio_set_bit(APP_LED_LFP_X_PIN);
				gpio_set_bit(APP_LED_LFP_Y_PIN);
				gpio_clr_bit(APP_LED_LFP_Z_PIN);
			}
		}
		else
		{
			if(ch2_rssi == ch3_rssi)
			{
				max_rssi = ch2_rssi;
				gpio_set_bit(APP_LED_LFP_X_PIN);
				gpio_clr_bit(APP_LED_LFP_Y_PIN);
				gpio_clr_bit(APP_LED_LFP_Z_PIN);
			}
			else if(ch2_rssi > ch3_rssi)
			{
				max_rssi = ch2_rssi;
				gpio_set_bit(APP_LED_LFP_X_PIN);
				gpio_clr_bit(APP_LED_LFP_Y_PIN);
				gpio_set_bit(APP_LED_LFP_Z_PIN);
			}
			else
			{
				max_rssi = ch3_rssi;
				gpio_set_bit(APP_LED_LFP_X_PIN);
				gpio_set_bit(APP_LED_LFP_Y_PIN);
				gpio_clr_bit(APP_LED_LFP_Z_PIN);
			}
		}
	}
	
	if(max_rssi > APP_UM2020_LED_RSSI_LEVEL_0)
	{
		gpio_clr_bit(APP_LED_RSSI_0_PIN);
	}
	else
	{
		gpio_set_bit(APP_LED_RSSI_0_PIN);
	}
	
	if(max_rssi > APP_UM2020_LED_RSSI_LEVEL_1)
	{
		gpio_clr_bit(APP_LED_RSSI_1_PIN);
	}
	else
	{
		gpio_set_bit(APP_LED_RSSI_1_PIN);
	}
	
	if(max_rssi > APP_UM2020_LED_RSSI_LEVEL_2)
	{
		gpio_clr_bit(APP_LED_RSSI_2_PIN);
	}
	else
	{
		gpio_set_bit(APP_LED_RSSI_2_PIN);
	}
	
	if(max_rssi > APP_UM2020_LED_RSSI_LEVEL_3)
	{
		gpio_clr_bit(APP_LED_RSSI_3_PIN);
	}
	else
	{
		gpio_set_bit(APP_LED_RSSI_3_PIN);
	}
	
	if(max_rssi > APP_UM2020_LED_RSSI_LEVEL_4)
	{
		gpio_clr_bit(APP_LED_RSSI_4_PIN);
	}
	else
	{
		gpio_set_bit(APP_LED_RSSI_4_PIN);
	}
}

/***********************************************************************************************************
 * Function		: app_sw_init
 * Description	: app_sw_init
 * Input		: none
 * Output		: none
 * Return		: none
 ***********************************************************************************************************/
void app_sw_init(void)
{
	gpio_init(APP_SW_KEY_PIN);																				/* 使能时钟 */
	gpio_set_dir(APP_SW_KEY_PIN,GPIO_DIR_INPUT);															/* 设置方向为输入 */
	gpio_set_pu(APP_SW_KEY_PIN,ENABLE);																		/* 使能上拉 */
	gpio_set_ie(APP_SW_KEY_PIN,ENABLE);																		/* 使能输入 */
	gpio_set_ibe(APP_SW_KEY_PIN,GPIO_IBE_SINGLE);															/* 单边沿触发 */
	gpio_set_iev(APP_SW_KEY_PIN,GPIO_IEV_FALLING_EDGE_LOW_LEVEL);											/* 下降沿 */
	gpio_set_is(APP_SW_KEY_PIN,GPIO_IS_EDGE);																/* 边沿检测 */
	gpio_irq_init(APP_SW_KEY_PIN,ENABLE,app_sw_key_irq);													/* 使能中断 */
}

/***********************************************************************************************************
 * Function		: app_sw_key_irq
 * Description	: app_sw_key_irq
 * Input		: none
 * Output		: none
 * Return		: none
 ***********************************************************************************************************/
static void app_sw_key_irq(void)
{
	app_led_off();
}



