#include "HW_config.h"
#include "uart_key_drv.h"
#include "relay_task.h"
#include "uart_task.h"
#include "CnCpp.h"

#define KEY_UART 7

typedef enum{
	SEND_CMD_40H_SET_STATE 				= 0x001,
	SEND_CMD_41H_SELF_CHECK 			= 0x002,
	SEND_CMD_42H_RESET 						= 0x004,
	SEND_CMD_42H_RESET_DUMP					= 0x008,
	SEND_CMD_43H_SET_POWER_COUNT 	= 0x010,
	SEND_CMD_44H_GET_POWER0_VER 	= 0x020,
	SEND_CMD_44H_GET_POWER1_VER 	= 0x040,
	SEND_CMD_44H_GET_POWER2_VER 	= 0x080,
	SEND_CMD_45H_GET_BRD_VER 			= 0x100,
	SEND_CMD_46H_GET_BRD_DESCP 		= 0x200,
	SEND_CMD_47H_SCAN_POWER 	= 0x400,
	SEND_CMD_60H_ASK_POWER_CNT 	= 0x800,
};

unsigned int send_cmd_flag = 0;
Sys_State_Struct p_d2u_data;

unsigned char set_power_count = 0;

//key brd
void uart7_rx_task(void)
{
	uart_info_8bit_struct * p_uart_info;
	p_uart_info = &uart_info_8bit[7];
	unsigned int aLen;
	unsigned int cmd;
	
	if(p_uart_info->rx_complete){
		p_uart_info->rx_complete = 0;
		if((p_uart_info->rx_len <= UART_RX_BUF_MAX)&&(p_uart_info->rx_len > 3)){
			if(((p_uart_info->rx_len - 4) == p_uart_info->rx_buf[1]) &&
				(KEY_UART_HEAD == p_uart_info->rx_buf[0])	&&
				(KEY_UART_END == p_uart_info->rx_buf[p_uart_info->rx_len-1]) &&
				(!check_sum8(p_uart_info->rx_buf,(p_uart_info->rx_len - 2))))
			{
				aLen = p_uart_info->rx_buf[1];
				cmd = p_uart_info->rx_buf[2];
				if(aLen == 4){
					if(1 == p_uart_info->rx_buf[3]){
						KeyRdy = 1;
						KeyValue =	p_uart_info->rx_buf[4];
					}
					LoadBatStateViaKeyPad(p_uart_info->rx_buf[5]);
					KeyPadSilenceTickClear();
				}else
				if(aLen == 1){
					if(0x43 == cmd){
						KeyPadAskPowerCount();
						KeyPadSilenceTickClear();
					}
				}else
				if(aLen == 14){
					if(p_uart_info->rx_buf[2] == 0){
						LoadPsBatStateViaKeyPad(&p_uart_info->rx_buf[4]);
						KeyPadSilenceTickClear();
					}else if(p_uart_info->rx_buf[2] == 1){
						LoadPsBatStateViaKeyPadSub1(&p_uart_info->rx_buf[3]);
						KeyPadSilenceTickClear();
					}else if(p_uart_info->rx_buf[2] == 2){
						LoadPsBatStateViaKeyPadSub2(&p_uart_info->rx_buf[3]);
						KeyPadSilenceTickClear();
					}
				}else
				if(aLen == 9){
					if(0x44 == cmd){
						GetHwInfo(p_uart_info->rx_buf[3],1,&p_uart_info->rx_buf[3]);
					}else
					if(0x45 == cmd){
						GetHwInfo(p_uart_info->rx_buf[3],1,&p_uart_info->rx_buf[3]);
					}else
					if(0x46 == cmd){
						GetHwInfo(p_uart_info->rx_buf[3],0,&p_uart_info->rx_buf[3]);
					}
				}else
				if(aLen == 5){
					if(0x60 == cmd){
						GetPowerCount(p_uart_info->rx_buf[3], p_uart_info->rx_buf[4], p_uart_info->rx_buf[5], 0);
					}
				}
			}
		}
	}
}

void uart_key_task(void)
{
	uart7_rx_task();
}

void sys_display_led_state(unsigned int aState)
{
	p_d2u_data.D32 = aState;
	send_cmd_flag |= SEND_CMD_40H_SET_STATE;
}

void sys_set_key_self_detect_state(unsigned char self_det_state)
{
	send_cmd_flag |= SEND_CMD_41H_SELF_CHECK;
}

void sys_set_key_reset_dump_state(void)
{
	send_cmd_flag |= SEND_CMD_42H_RESET_DUMP;
}

void sys_set_key_reset_state(void)
{
	send_cmd_flag |= SEND_CMD_42H_RESET;
}

void sys_set_power_suply_count(unsigned char aCnt)
{
	if(aCnt > 3)return;
	
	set_power_count = aCnt;
	send_cmd_flag |= SEND_CMD_43H_SET_POWER_COUNT;
}

void sys_get_power_ver(unsigned char power_ord)
{
	if(power_ord > 2)return;
	
	if(0 == power_ord){
		send_cmd_flag |= SEND_CMD_44H_GET_POWER0_VER;
	}else
	if(1 == power_ord){
		send_cmd_flag |= SEND_CMD_44H_GET_POWER1_VER;
	}else
	if(2 == power_ord){
		send_cmd_flag |= SEND_CMD_44H_GET_POWER2_VER;
	}
}

void sys_get_sw_brd_ver(void)
{
	send_cmd_flag |= SEND_CMD_45H_GET_BRD_VER;
}

void sys_get_sw_brd_descp(void)
{
	send_cmd_flag |= SEND_CMD_46H_GET_BRD_DESCP;
}

void sys_set_scan_power(void)
{
    send_cmd_flag |= SEND_CMD_47H_SCAN_POWER;
}

void sys_get_power_Count(void)
{
	send_cmd_flag |= SEND_CMD_60H_ASK_POWER_CNT;
}

void sys_key_uart_tx_task(void)//1ms
{
	static unsigned int send_tick = 0;
	unsigned char check_sum = 0;
	
	send_tick++;
	if(send_tick > 8){
		send_tick = 0;
	
		if(send_cmd_flag & SEND_CMD_40H_SET_STATE){
			send_cmd_flag &=~SEND_CMD_40H_SET_STATE;
			
			UART7->TDR = KEY_UART_HEAD;
			UART7->TDR = 0x40;
			UART7->TDR = 0x04;
			UART7->TDR = p_d2u_data.state_val[0];
			UART7->TDR = p_d2u_data.state_val[1];
			UART7->TDR = p_d2u_data.state_val[2];
			UART7->TDR = p_d2u_data.state_val[3];
			
			check_sum = KEY_UART_HEAD+0x40+0x04+p_d2u_data.state_val[0]+p_d2u_data.state_val[1]+p_d2u_data.state_val[2]+p_d2u_data.state_val[3];
			UART7->TDR = check_sum;
			
			UART7->TDR = KEY_UART_END;
		}else
		
		if(send_cmd_flag & SEND_CMD_41H_SELF_CHECK){
			send_cmd_flag &=~SEND_CMD_41H_SELF_CHECK;
			
			UART7->TDR = KEY_UART_HEAD;
			UART7->TDR = 0x41;
			UART7->TDR = 0x04;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			
			check_sum = KEY_UART_HEAD+0x41+0x04;
			UART7->TDR = check_sum;
			
			UART7->TDR = KEY_UART_END;
		}else
		
		if(send_cmd_flag & SEND_CMD_42H_RESET){
			send_cmd_flag &=~SEND_CMD_42H_RESET;
			
			UART7->TDR = KEY_UART_HEAD;
			UART7->TDR = 0x42;
			UART7->TDR = 0x04;
			UART7->TDR = 0;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			
			check_sum = KEY_UART_HEAD+0x42+0x04;
			UART7->TDR = check_sum;
			
			UART7->TDR = KEY_UART_END;
		}else
		
		if(send_cmd_flag & SEND_CMD_42H_RESET_DUMP){
			send_cmd_flag &=~SEND_CMD_42H_RESET_DUMP;
			
			UART7->TDR = KEY_UART_HEAD;
			UART7->TDR = 0x42;
			UART7->TDR = 0x04;
			UART7->TDR = 0xAA;
			UART7->TDR = 0x55;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			
			check_sum = KEY_UART_HEAD+0x42+0x04+0xAA+0x55;
			UART7->TDR = check_sum;
			
			UART7->TDR = KEY_UART_END;
		}else
		
		if(send_cmd_flag & SEND_CMD_43H_SET_POWER_COUNT){
			send_cmd_flag &=~SEND_CMD_43H_SET_POWER_COUNT;
			
			UART7->TDR = KEY_UART_HEAD;
			UART7->TDR = 0x43;
			UART7->TDR = 0x04;
			UART7->TDR = set_power_count;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			
			check_sum = KEY_UART_HEAD+0x43+0x04+set_power_count;
			UART7->TDR = check_sum;
			
			UART7->TDR = KEY_UART_END;
		}else
		
		if(send_cmd_flag & SEND_CMD_44H_GET_POWER0_VER){
			send_cmd_flag &=~SEND_CMD_44H_GET_POWER0_VER;
			
			UART7->TDR = KEY_UART_HEAD;
			UART7->TDR = 0x44;
			UART7->TDR = 0x04;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			
			check_sum = KEY_UART_HEAD+0x44+0x04+0x00;
			UART7->TDR = check_sum;
			
			UART7->TDR = KEY_UART_END;
		}else
		
		if(send_cmd_flag & SEND_CMD_44H_GET_POWER1_VER){
			send_cmd_flag &=~SEND_CMD_44H_GET_POWER1_VER;
			
			UART7->TDR = KEY_UART_HEAD;
			UART7->TDR = 0x44;
			UART7->TDR = 0x04;
			UART7->TDR = 0x01;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			
			check_sum = KEY_UART_HEAD+0x44+0x04+0x01;
			UART7->TDR = check_sum;
			
			UART7->TDR = KEY_UART_END;
		}else
		
		if(send_cmd_flag & SEND_CMD_44H_GET_POWER2_VER){
			send_cmd_flag &=~SEND_CMD_44H_GET_POWER2_VER;
			
			UART7->TDR = KEY_UART_HEAD;
			UART7->TDR = 0x44;
			UART7->TDR = 0x04;
			UART7->TDR = 0x02;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			
			check_sum = KEY_UART_HEAD+0x44+0x04+0x02;
			UART7->TDR = check_sum;
			
			UART7->TDR = KEY_UART_END;
		}else
		
		if(send_cmd_flag & SEND_CMD_45H_GET_BRD_VER){
			send_cmd_flag &=~SEND_CMD_45H_GET_BRD_VER;
			
			UART7->TDR = KEY_UART_HEAD;
			UART7->TDR = 0x45;
			UART7->TDR = 0x04;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			
			check_sum = KEY_UART_HEAD+0x45+0x04+0;
			UART7->TDR = check_sum;
			
			UART7->TDR = KEY_UART_END;
		}else
		
		if(send_cmd_flag & SEND_CMD_46H_GET_BRD_DESCP){
			send_cmd_flag &=~SEND_CMD_46H_GET_BRD_DESCP;
			
			UART7->TDR = KEY_UART_HEAD;
			UART7->TDR = 0x46;
			UART7->TDR = 0x04;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			
			check_sum = KEY_UART_HEAD+0x46+0x04+0;
			UART7->TDR = check_sum;
			
			UART7->TDR = KEY_UART_END;
		}else
        
		if(send_cmd_flag & SEND_CMD_47H_SCAN_POWER){
			send_cmd_flag &=~SEND_CMD_47H_SCAN_POWER;

			UART7->TDR = KEY_UART_HEAD;
			UART7->TDR = 0x47;
			UART7->TDR = 0x04;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;

			check_sum = KEY_UART_HEAD+0x47+0x04+0;
			UART7->TDR = check_sum;

			UART7->TDR = KEY_UART_END;
		}else
		
		if(send_cmd_flag & SEND_CMD_60H_ASK_POWER_CNT){
			send_cmd_flag &=~SEND_CMD_60H_ASK_POWER_CNT;
			
			UART7->TDR = KEY_UART_HEAD;
			UART7->TDR = 0x60;
			UART7->TDR = 0x04;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;
			UART7->TDR = 0x00;

			check_sum = KEY_UART_HEAD+0x60+0x04+0;
			UART7->TDR = check_sum;

			UART7->TDR = KEY_UART_END;
		}
	}
}

void lcd_bl_on(void)
{
	LL_TIM_OC_SetCompareCH2(TIM3,255);//bl;
}

void lcd_bl_off(void)
{
	LL_TIM_OC_SetCompareCH2(TIM3,0);//bl;
}


