#include "KeyPad.h"
#include "runtime.h"
extern "C"{
	#include "uart_key_drv.h"
}

/*typedef union
{
	volatile unsigned char state_val[4];
	struct
	{
		volatile unsigned int take_charge      : 2; 
		volatile unsigned int feedback         : 2; 
		volatile unsigned int start_delay      : 2; 
		volatile unsigned int start            : 2; 
		volatile unsigned int mute             : 2; 
		volatile unsigned int fault            : 2; 
		volatile unsigned int alarm            : 2; 
		volatile unsigned int rsv1           	: 2; 
		volatile unsigned int linkage        	: 2; 
		volatile unsigned int rx               : 2; 
		volatile unsigned int tx               : 2;
		volatile unsigned int self_check       : 2; 
		volatile unsigned int alarm_fault      : 2; 
		volatile unsigned int alarm_start      : 2; 
		volatile unsigned int shield           : 2; 
		volatile unsigned int rsv2           	 : 2;
	}state;
}Sys_State_Struct;*/

void TKeyPad::Init()
{
	LedState.D32 = 0;
	IsSendPowerSuplyCount = 0;
}

void TKeyPad::SendDumpCmd()
{
	sys_set_key_reset_dump_state();
}

void TKeyPad::SendResetCmd()
{
	sys_set_key_reset_state();
}

void TKeyPad::SendSelfCheckCmd()
{
	sys_set_key_self_detect_state(0);
}

void TKeyPad::SendPowerSuplyCount()
{
	//sys_set_power_suply_count(PowerPrmValue.SubCount);
}

void TKeyPad::InitKeyPad()
{
	sys_display_led_state(LedState.D32);
}
void TKeyPad::KeyPadLedSetSuperSv(TKeyPadLedState	aSta)
{
	LedState.state.take_charge = aSta;
	sys_display_led_state(LedState.D32);
}
void TKeyPad::KeyPadLedSetFeedBack(TKeyPadLedState	aSta)
{
	LedState.state.feedback = aSta;
	sys_display_led_state(LedState.D32);
}
void TKeyPad::KeyPadLedSetStartDelay(TKeyPadLedState	aSta)
{
	LedState.state.start_delay = aSta;
	sys_display_led_state(LedState.D32);
}
void TKeyPad::KeyPadLedSetStart(TKeyPadLedState	aSta)
{
	LedState.state.start = aSta;	
	sys_display_led_state(LedState.D32);
}
void TKeyPad::KeyPadLedSetMute(TKeyPadLedState	aSta)
{
	LedState.state.mute = aSta;
	sys_display_led_state(LedState.D32);	
}
void TKeyPad::KeyPadLedSetFault(TKeyPadLedState	aSta)
{
	LedState.state.fault = aSta;
	sys_display_led_state(LedState.D32);	
}
void TKeyPad::KeyPadLedSetAlarm(TKeyPadLedState	aSta)
{
	LedState.state.alarm = aSta;
	sys_display_led_state(LedState.D32);	
}
void TKeyPad::KeyPadLedSetLinkage(TKeyPadLedState	aSta)
{
	
}
void TKeyPad::KeyPadLedSetSelfCheck(TKeyPadLedState	aSta)
{
	LedState.state.self_check = aSta;
	sys_display_led_state(LedState.D32);	
}

void TKeyPad::KeyPadLedSetNormalCheck(TKeyPadLedState	aSta)
{
	LedState.state.normal_check = aSta;
	sys_display_led_state(LedState.D32);	
}
void TKeyPad::KeyPadLedSetAlarmFault(TKeyPadLedState	aSta)
{
	LedState.state.alarm_fault = aSta;
	sys_display_led_state(LedState.D32);	
}
void TKeyPad::KeyPadLedSetAlarmStart(TKeyPadLedState	aSta)
{
	LedState.state.alarm_start = aSta;
	sys_display_led_state(LedState.D32);	
}
void TKeyPad::KeyPadLedSetMask(TKeyPadLedState	aSta)
{
	LedState.state.shield = aSta;	
	sys_display_led_state(LedState.D32);
}

void TKeyPad::SetLedStateViaInteger(unsigned int aState)
{
	LedState.D32 = aState;
	sys_display_led_state(LedState.D32);
}

unsigned int TKeyPad::GetLedStateViaInteger()
{
	return LedState.D32;
}

//_______________________________

void TKeyPad::sInit(TSys_State_Struct *fState)
{
	fState->D32 =0;
}

void TKeyPad::sKeyPadLedSetSuperSv(TSys_State_Struct *fState, TKeyPadLedState	aSta)
{
	fState->state.take_charge = aSta;
}
void TKeyPad::sKeyPadLedSetFeedBack(TSys_State_Struct *fState, TKeyPadLedState	aSta)
{
	fState->state.feedback = aSta;
}
void TKeyPad::sKeyPadLedSetStartDelay(TSys_State_Struct *fState, TKeyPadLedState	aSta)
{
	fState->state.start_delay = aSta;
}
void TKeyPad::sKeyPadLedSetStart(TSys_State_Struct *fState, TKeyPadLedState	aSta)
{
	fState->state.start = aSta;	
}
void TKeyPad::sKeyPadLedSetMute(TSys_State_Struct *fState, TKeyPadLedState	aSta)
{
	fState->state.mute = aSta;	
}
void TKeyPad::sKeyPadLedSetFault(TSys_State_Struct *fState, TKeyPadLedState	aSta)
{
	fState->state.fault = aSta;	
}
void TKeyPad::sKeyPadLedSetAlarm(TSys_State_Struct *fState, TKeyPadLedState	aSta)
{
	fState->state.alarm = aSta;	
}
void TKeyPad::sKeyPadLedSetLinkage(TSys_State_Struct *fState, TKeyPadLedState	aSta)
{
	
}
void TKeyPad::sKeyPadLedSetSelfCheck(TSys_State_Struct *fState, TKeyPadLedState	aSta)
{
	fState->state.self_check = aSta;	
}

void TKeyPad::sKeyPadLedSetNormalCheck(TSys_State_Struct *fState, TKeyPadLedState	aSta)
{
	fState->state.normal_check = aSta;	
}
void TKeyPad::sKeyPadLedSetAlarmFault(TSys_State_Struct *fState, TKeyPadLedState	aSta)
{
	fState->state.alarm_fault = aSta;	
}
void TKeyPad::sKeyPadLedSetAlarmStart(TSys_State_Struct *fState, TKeyPadLedState	aSta)
{
	fState->state.alarm_start = aSta;	
}
void TKeyPad::sKeyPadLedSetMask(TSys_State_Struct *fState, TKeyPadLedState	aSta)
{
	fState->state.shield = aSta;	
}
void TKeyPad::sKeyPadLedSetBuz(TSys_State_Struct *fState, unsigned char aSta)
{
	fState->state.buzzer = aSta;
}
void TKeyPad::sKeyPadLedSetAutoAllowState(TSys_State_Struct *fState, unsigned char aSta)
{
	fState->state.auto_state = aSta;
}
void TKeyPad::sKeyPadLedSetManualAllowState(TSys_State_Struct *fState, unsigned char aSta)
{
	fState->state.manual_state = aSta;
}
void TKeyPad::sKeyPadLedSetScreenSaveState(TSys_State_Struct *fState, unsigned char aSta)
{
	fState->state.ScreenSave = aSta;
}

void TKeyPad::CheckKeyPadOffline()
{
	SilenceTick++;
	if(SilenceTick > 22){
		SilenceTick = 26;
		GPIOE->ODR |=0x04;;
	}else{
		GPIOE->ODR &= ~0x04;
	}
}

void TKeyPad::ForceClearSysErrLed()
{
	GPIOE->ODR &= ~0x04;
	SilenceTick = 0;
}








