/*****************************************************************************
**	Filename  : keyboard_drive.c
**	Project   :
**	Module	  :
**	Processor : Freescale S12Z
**	Date/Time : 2019-09-04, 9:00
**	Abstract  :
**	Contents  :
**	Author	  : Lyj
**	http	  :
**	mail	  :
**	--------------------------------------------------------------------------
**
**	--------------------------------------------------------------------------
******************************************************************************/
#include "common_types.h"
#include "common_memory.h"
#include "key_app.h"
//#include "power_drive.h"
#include "system_voltage_manage.h"
#include "Com_Cfg.h"
#include "app_encoder.h"
#include "can_app.h"
#include "Com.h"
/*****************************************************************************
* Include Files
*****************************************************************************/

/*****************************************************************************
* Defines
*****************************************************************************/
#define SHOW_MOD_TIME_10S	(10000u / 10u)
#define MILEAGE_CLEAR_TIME_15S	(15000u / 10u)
#define APP_KEY_MEDIA_RESET_3S		(3000U/10u)
#define APP_KEY_MIDSCREEN_RESET_10S		(10000U/10u)
/*****************************************************************************
* Macros
*****************************************************************************/

/*****************************************************************************
* Type Defines
*****************************************************************************/
typedef struct
{
	uint16_t AppKey_MediaReset_Timer;
	uint16_t AppKey_MidScreenReset_Timer;
	uint16_t AppKey_CoDriverScreenReset_Timer;
	uint16_t AppKey_HandrailScreenReset_Timer;
	uint16_t AppKey_BackRowReset_Timer;
}AppKeyboard_Type;
/*****************************************************************************
*Static Variable
*****************************************************************************/
static AppKeyboard_Type AppKeyboard;
/*****************************************************************************
*Global Variable
*****************************************************************************/
static key_app_s g_key_app;

/*****************************************************************************
*Constant
*****************************************************************************/

/*****************************************************************************
*Function Prototype
*****************************************************************************/

void app_key_event_clear(void);
#ifdef KEY_APP_ENABLE_SLEEP_FUNCTION
static void key_app_sleep_state_task(void);
#endif

/*****************************************************************************
|Prototype         :
|Called by         :
|Preconditions     : -
|Input parameters  : None
|Output parameters : None
|Return value      : None
|Description       :
*****************************************************************************/
void key_app_init(void)
{
	#ifdef KEY_DIGITAL_EN
	key_digital_driver_init();
	#endif
	#ifdef KEY_ADC_EN
	key_adc_mid_init();
	//key_app_cfg_init();
	#endif

	common_memory_clear((u8*)&g_key_app,sizeof(g_key_app));
}







/*****************************************************************************
|Prototype         :
|Called by         :
|Preconditions     : -
|Input parameters  : None
|Output parameters : None
|Return value      : None
|Description       :
*****************************************************************************/
void key_app_goto_sleep(void)
{
	#ifdef KEY_DIGITAL_EN
	key_digital_driver_goto_sleep();
	#endif
	#ifdef KEY_ADC_EN
	key_adc_mid_goto_sleep();
	key_adc_mid_init();
	#endif

	//还有别的按键模块，如ADC，
	g_key_app.timecount=0u;
	g_key_app.sub_state=KEY_APP_SUBSTATE_SLEEP;
	g_key_app.state=KEY_APP_STATE_SLEEP;
}


/**
 * @brief  唤醒
 * @param  input:
 * @param  output:
 * @return   none:
 * @calls
 * @calls by
 * @adc_asils
 */
void key_app_goto_wake(void)
{
	u8 i;
	#ifdef KEY_DIGITAL_EN
	key_digital_driver_goto_wakeup();
	#endif
	#ifdef KEY_ADC_EN
	key_adc_mid_goto_wakeup();
	#endif
	for(i=0;i<KEY_APP_MEDIUM_TYPE_TOTAL;i++)
	{
		#ifdef KEY_APP_ENABLE_SLEEP_FUNCTION
		g_key_app.medium[i].timecount=0u;
		g_key_app.medium[i].state = KEY_APP_MEDIUM_STATE_NORMAL;
		#endif
	}
	app_key_event_clear();
	g_key_app.timecount=0u;
	g_key_app.sub_state=KEY_APP_SUBSTATE_NORMAL;
	g_key_app.state=KEY_APP_STATE_NORMAL;
}


void app_key_event_clear(void)
{
	uint8_t i;
	uint8_t event;
	for(i = 0; i < KEY_ADC_ID_MAX; i ++)
	{
		event = key_adc_mid_driver_get_event(i);
	}
	for(i = 0; i < KEY_DIGITAL_DRIVER_KEY_TOTAL; i ++)
	{
		event = key_digital_driver_get_event(i);
	}
}

void multi_media_vedio_key_process(uint8_t *key_state)
{

	uint8_t event;
	uint8_t output;

	if(key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 0u\
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 0u)
	{
		event = key_adc_mid_driver_get_event(KEY_ADC_ID_VEDIO);
		if(event & KEY_ADC_EVENT_PRESSED)
		{
			event &= ~KEY_ADC_EVENT_RELEASE;
			output = 0x15;
            app_sig_sws_r_multimedia_tx_process(output, 1, 1);

		}
		if ((event & KEY_ADC_EVENT_RELEASE) || (event & KEY_ADC_EVENT_SHORT_DOWN_RELEASE))
		{
			output = 0x00;
            app_sig_sws_r_multimedia_tx_process(output, 3, 0);
		}

	}
	else
	{
		if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u)
		{
			event = key_adc_mid_driver_get_event(KEY_ADC_ID_VEDIO);
		}
	}

}


void multi_media_lmenu_key_process(uint8_t *key_state)
{

	uint8_t event;
	uint8_t output;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u \
	 && key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 0u)
	{
		event = key_adc_mid_driver_get_event(KEY_ADC_ID_LMENU);
		if(event & KEY_ADC_EVENT_SHORT_DOWN_RELEASE)
		{
			event &= ~KEY_ADC_EVENT_RELEASE;
			output = 0x1;
            app_sig_sws_r_left_menu_tx_process(output, 4, 1);
		}
		if(event & KEY_ADC_EVENT_LONG_DOWN)
		{
			event &= ~KEY_ADC_EVENT_RELEASE;
			output = 0x2;
            app_sig_sws_r_left_menu_tx_process(output, 1, 1);
		}
		if (event & KEY_ADC_EVENT_RELEASE)
		{
			output = 0x00;
			COM_IpduRepetitionTxNumTable[COM_TXIPDUSTEERING_RIGHT_SWITCH_4A8] = 3;
			app_sig_sws_r_left_menu_tx_process(output, 3, 0);
		}

	}
	else
	{
		if(key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u)
		{
			event = key_adc_mid_driver_get_event(KEY_ADC_ID_LMENU);
		}
	}

}


void multi_media_custom_key_process(uint8_t *key_state)
{

	uint8_t event;
	uint8_t output;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u \
	 && key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 0u)
	{
		event = key_adc_mid_driver_get_event(KEY_ADC_ID_CUSTOM);
		if(event & KEY_ADC_EVENT_SHORT_DOWN_RELEASE)
		{
			event &= ~KEY_ADC_EVENT_RELEASE;
			output = 0x25;
            app_sig_sws_r_multimedia_tx_process(output, 4, 1);
		}
		if(event & KEY_ADC_EVENT_LONG_DOWN)
		{
			event &= ~KEY_ADC_EVENT_RELEASE;
			output = 0x26;
			app_sig_sws_r_multimedia_tx_process(output, 1, 1);
		}
		if (event & KEY_ADC_EVENT_RELEASE)
		{
			output = 0x00;
			app_sig_sws_r_multimedia_tx_process(output, 3, 0u);
		}

	}
	else
	{
		if(key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u)
		{
			event = key_adc_mid_driver_get_event(KEY_ADC_ID_CUSTOM);
		}
	}

}


void multi_media_prev_key_process(uint8_t *key_state)
{
	uint8_t event;
	uint8_t output;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u \
	 && key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 0u)
	{
		event = key_digital_driver_get_event(KEY_DIGI_ID_PREV);
		if(event & KEY_DIGITAL_EVENT_SHORT_DOWN_RELEASE)
		{
			event &= ~KEY_DIGITAL_EVENT_RELEASE;
			output = 0x07;
			app_sig_sws_r_multimedia_tx_process(output, 4, 1);

		}
		if(event & KEY_DIGITAL_EVENT_LONG_DOWN)
		{
			output = 0x08;
			app_sig_sws_r_multimedia_tx_process(output, 1, 1);

		}
		if (event & KEY_DIGITAL_EVENT_RELEASE)
		{
			output = 0x00;
			app_sig_sws_r_multimedia_tx_process(output, 3, 0);
		}

	}
	else
	{
		if(key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 0u)
		{
			event = key_digital_driver_get_event(KEY_DIGI_ID_PREV);
		}
	}
}






void multi_media_next_key_process(uint8_t *key_state)
{
	uint8_t event;
	uint8_t output;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u \
	 && key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 0u)
	{
		event = key_digital_driver_get_event(KEY_DIGI_ID_NEXT);
		if(event & KEY_DIGITAL_EVENT_SHORT_DOWN_RELEASE)
		{
			event &= ~KEY_DIGITAL_EVENT_RELEASE;
			output = 0x0A;
			app_sig_sws_r_multimedia_tx_process(output, 4, 1);

		}
		if(event & KEY_DIGITAL_EVENT_LONG_DOWN)
		{
			output = 0x0B;
			app_sig_sws_r_multimedia_tx_process(output, 1, 1);

		}
		if (event & KEY_DIGITAL_EVENT_RELEASE)
		{
			output = 0x00;
			app_sig_sws_r_multimedia_tx_process(output, 3, 0);
		}

	}
	else
	{
		if(key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 0u)
		{
			event = key_digital_driver_get_event(KEY_DIGI_ID_NEXT);
		}
	}
}



void multi_media_voice_key_process(uint8_t *key_state)
{
	uint8_t event;
	uint8_t output;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u \
	 && key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 0u)
	{
		event = key_digital_driver_get_event(KEY_DIGI_ID_VOICE);
		if(event & KEY_DIGITAL_EVENT_PRESSED)
		{
			output = 0x1;
			app_sig_sws_r_voicd_expand_2_tx_process(output, 1, 1);
		}

		if(event & KEY_DIGITAL_EVENT_SHORT_DOWN_RELEASE)
		{
			event &= ~KEY_DIGITAL_EVENT_RELEASE;
			output = 0x13;
			app_sig_sws_r_multimedia_tx_process(output, 4, 1);

			output = 0x1;
			app_sig_sws_r_voicd_expand_2_tx_process(output, 4, 1);
		}
		if(event & KEY_DIGITAL_EVENT_LONG_DOWN)
		{
			output = 0x14;
			app_sig_sws_r_multimedia_tx_process(output, 1, 1);

			output = 0x1;
			app_sig_sws_r_voicd_expand_2_tx_process(output, 1, 1);
		}
		if (event & KEY_DIGITAL_EVENT_RELEASE)
		{
			output = 0x00;
			app_sig_sws_r_multimedia_tx_process(output, 3, 0);
			output = 0x2u;
			app_sig_sws_r_voicd_expand_2_tx_process(output, 3, 0);
		}

	}
	else
	{
		if(key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 0u)
		{
			event = key_digital_driver_get_event(KEY_DIGI_ID_VOICE);
		}
	}
}


void multi_media_call_key_process(uint8_t *key_state)
{
	uint8_t event;
	uint8_t output;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 0u \
	 && key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 0u)
	{
		event = key_digital_driver_get_event(KEY_DIGI_ID_PHONE);
		if(event & KEY_DIGITAL_EVENT_PRESSED)
		{
			event &= ~KEY_DIGITAL_EVENT_RELEASE;
			output = 0x0c;
			app_sig_sws_r_multimedia_tx_process(output, 1, 1);
		}
		if ((event & KEY_DIGITAL_EVENT_RELEASE) || (event & KEY_DIGITAL_EVENT_SHORT_DOWN_RELEASE))
		{
			output = 0x00;
			app_sig_sws_r_multimedia_tx_process(output, 3, 1);
		}

	}
	else
	{
		if(key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u)
		{
			event = key_digital_driver_get_event(KEY_DIGI_ID_PHONE);
		}
	}
}



void multi_media_rmenu_key_process(uint8_t *key_state)
{
	uint8_t event;
	uint8_t output;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 0u\
	 && key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 0u \
	 && key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 0u)
	{
		event = key_digital_driver_get_event(KEY_DIGI_ID_RMENU);
		if(event & KEY_DIGITAL_EVENT_PRESSED)
		{
			event &= ~KEY_DIGITAL_EVENT_RELEASE;
			output = 0x23;
			app_sig_sws_r_multimedia_tx_process(output, 1, 1);
		}
		if (event & KEY_DIGITAL_EVENT_RELEASE || event & KEY_DIGITAL_EVENT_SHORT_DOWN_RELEASE)
		{
			output = 0x00;
			app_sig_sws_r_multimedia_tx_process(output, 3, 0);
		}

	}
	else
	{
		if(key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 0u)
		{
			event = key_digital_driver_get_event(KEY_DIGI_ID_RMENU);
		}
	}
}


void multi_media_mute_key_process(uint8_t *key_state)
{
	uint8_t event;
	uint8_t output;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 0u)
	{
		event = key_digital_driver_get_event(KEY_DIGI_ID_MUTE);
		if(event & KEY_DIGITAL_EVENT_SHORT_DOWN_RELEASE)
		{
			event &= ~KEY_DIGITAL_EVENT_RELEASE;
			output = 0x24;
			app_sig_sws_r_multimedia_tx_process(output, 4, 1);
			app_encoder_delay_set();
		}
		if(event & KEY_DIGITAL_EVENT_LONG_DOWN)
		{
			output = 0xF7;
			app_sig_sws_r_multimedia_tx_process(output, 1, 1);
			app_encoder_delay_set();
		}
		if (event & KEY_DIGITAL_EVENT_RELEASE)
		{
			output = 0x00;
			app_sig_sws_r_multimedia_tx_process(output, 3, 0);
			Com_SendSignal(COM_TX_SIGSWS_R_MULTIMEDIA, &output);
		}

	}
	else
	{
		if(key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 0u)
		{
			event = key_digital_driver_get_event(KEY_DIGI_ID_MUTE);
		}
	}
}




void auto_show_in_mode_process(uint8_t *key_state)
{
	static uint16_t timer = 0;
	uint8_t output;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 1u && key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 1u\
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 0u)
	{
		if(timer < SHOW_MOD_TIME_10S)
		{
			timer ++;
			if(timer >= SHOW_MOD_TIME_10S)
			{
				output = 1;
				app_sig_sws_r_auto_show_in_tx_process(output, 3, 3);
			}
		}
	}
	else
	{
		if(timer >= SHOW_MOD_TIME_10S)
		{
			output = 0;
			Com_SendSignal(COM_TX_SIGSWS_R_MULTIMEDIA, &output);
			app_sig_sws_r_auto_show_in_tx_process(output, 3, 0);

		}
		timer = 0;
	}
}


void auto_show_out_mode_process(uint8_t *key_state)
{
	static uint16_t timer = 0;
	uint8_t output;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 1u \
	&& key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 1u\
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 0u)
	{
		if(timer < SHOW_MOD_TIME_10S)
		{
			timer ++;
			if(timer >= SHOW_MOD_TIME_10S)
			{
				output = 1;
				app_sig_sws_r_auto_show_out_tx_process(output, 3, 3);
			}
		}
	}
	else
	{
		if(timer >= SHOW_MOD_TIME_10S)
		{
			output = 0;
			Com_SendSignal(COM_TX_SIGSWS_R_MULTIMEDIA, &output);
			app_sig_sws_r_auto_show_out_tx_process(output, 3, 0);
		}
		timer = 0;
	}
}

void multi_media_mileage_clear_process(uint8_t *key_state)
{
	static uint16_t timer_mileage = 0;
	uint8_t output = 0;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 1u\
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 1u)
	{
		if(timer_mileage < MILEAGE_CLEAR_TIME_15S)
		{
			timer_mileage ++;
			if(timer_mileage >= MILEAGE_CLEAR_TIME_15S)
			{
				output = 1;
				app_sig_sws_r_mileage_clear_tx_process(output, 3, 3);
			}
		}
	}
	else
	{
		if(timer_mileage >= MILEAGE_CLEAR_TIME_15S)
		{
			output = 0;
			Com_SendSignal(COM_TX_SIGSWS_R_MULTIMEDIA, &output);
			app_sig_sws_r_mileage_clear_tx_process(output, 3, 0);

		}
		timer_mileage = 0;
	}
}

void AppKeyboard_MediaResetProcess(uint8_t *key_state)
{
	uint8_t output = 0u;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 1u && key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 0u\
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 1u)
	{
		if(AppKeyboard.AppKey_MediaReset_Timer < APP_KEY_MEDIA_RESET_3S)
		{
			AppKeyboard.AppKey_MediaReset_Timer ++;
			if(AppKeyboard.AppKey_MediaReset_Timer >= APP_KEY_MEDIA_RESET_3S)
			{
				output = 1u;
				AppCan_EventSig_SWS_R_MediaReset_TxProcess(output, 3, 3);
			}
		}
	}
	else
	{
		if(AppKeyboard.AppKey_MediaReset_Timer >= APP_KEY_MEDIA_RESET_3S)
		{
			output = 0u;
			Com_SendSignal(COM_TX_SIGSWS_R_MULTIMEDIA, &output);
			AppCan_EventSig_SWS_R_MediaReset_TxProcess(output, 3, 0);

		}
		AppKeyboard.AppKey_MediaReset_Timer = 0u;
	}
}

void AppKeyboard_MidScreenRecoverProcess(uint8_t *key_state)
{
	uint8_t output = 0u;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 1u \
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 0u\
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 1u)
	{
		if(AppKeyboard.AppKey_MidScreenReset_Timer < APP_KEY_MIDSCREEN_RESET_10S)
		{
			AppKeyboard.AppKey_MidScreenReset_Timer ++;
			if(AppKeyboard.AppKey_MidScreenReset_Timer >= APP_KEY_MIDSCREEN_RESET_10S)
			{
				output = 1u;
				AppCan_EventSig_SWS_R_OtherRecover_TxProcess(output, 3, 3);
			}
		}
	}
	else
	{
		if(AppKeyboard.AppKey_MidScreenReset_Timer >= APP_KEY_MIDSCREEN_RESET_10S)
		{
			output = 0u;
			Com_SendSignal(COM_TX_SIGSWS_R_MULTIMEDIA, &output);
			AppCan_EventSig_SWS_R_OtherRecover_TxProcess(output, 3, 0);

		}
		AppKeyboard.AppKey_MidScreenReset_Timer = 0u;
	}
}

void AppKeyboard_CoDriverRecoverProcess(uint8_t *key_state)
{
	uint8_t output = 0u;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 1u \
	&& key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 0u\
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 1u)
	{
		if(AppKeyboard.AppKey_CoDriverScreenReset_Timer < APP_KEY_MIDSCREEN_RESET_10S)
		{
			AppKeyboard.AppKey_CoDriverScreenReset_Timer ++;
			if(AppKeyboard.AppKey_CoDriverScreenReset_Timer >= APP_KEY_MIDSCREEN_RESET_10S)
			{
				output = 2u;
				AppCan_EventSig_SWS_R_OtherRecover_TxProcess(output, 3, 3);
			}
		}
	}
	else
	{
		if(AppKeyboard.AppKey_CoDriverScreenReset_Timer >= APP_KEY_MIDSCREEN_RESET_10S)
		{
			output = 0u;
			Com_SendSignal(COM_TX_SIGSWS_R_MULTIMEDIA, &output);
			AppCan_EventSig_SWS_R_OtherRecover_TxProcess(output, 3, 0);

		}
		AppKeyboard.AppKey_CoDriverScreenReset_Timer = 0u;
	}
}

void AppKeyboard_HandrailRecoverProcess(uint8_t *key_state)
{
	uint8_t output = 0u;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 1u \
	&& key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 1u \
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 0u\
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 0u)
	{
		if(AppKeyboard.AppKey_HandrailScreenReset_Timer < APP_KEY_MIDSCREEN_RESET_10S)
		{
			AppKeyboard.AppKey_HandrailScreenReset_Timer ++;
			if(AppKeyboard.AppKey_HandrailScreenReset_Timer >= APP_KEY_MIDSCREEN_RESET_10S)
			{
				output = 3u;
				AppCan_EventSig_SWS_R_OtherRecover_TxProcess(output, 3, 3);
			}
		}
	}
	else
	{
		if(AppKeyboard.AppKey_HandrailScreenReset_Timer >= APP_KEY_MIDSCREEN_RESET_10S)
		{
			output = 0u;
			Com_SendSignal(COM_TX_SIGSWS_R_MULTIMEDIA, &output);
			AppCan_EventSig_SWS_R_OtherRecover_TxProcess(output, 3, 0);

		}
		AppKeyboard.AppKey_HandrailScreenReset_Timer = 0u;
	}
}

void AppKeyboard_BackRowRecoverProcess(uint8_t *key_state)
{
	uint8_t output = 0u;

	if(key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_PREV_ID] == 0u \
	&& key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] == 1u && key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] == 1u \
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] == 0u && key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] == 0u\
	&& key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] == 0u)
	{
		if(AppKeyboard.AppKey_BackRowReset_Timer < APP_KEY_MIDSCREEN_RESET_10S)
		{
			AppKeyboard.AppKey_BackRowReset_Timer ++;
			if(AppKeyboard.AppKey_BackRowReset_Timer >= APP_KEY_MIDSCREEN_RESET_10S)
			{
				output = 4u;
				AppCan_EventSig_SWS_R_OtherRecover_TxProcess(output, 3, 3);
			}
		}
	}
	else
	{
		if(AppKeyboard.AppKey_BackRowReset_Timer >= APP_KEY_MIDSCREEN_RESET_10S)
		{
			output = 0u;
			Com_SendSignal(COM_TX_SIGSWS_R_MULTIMEDIA, &output);
			AppCan_EventSig_SWS_R_OtherRecover_TxProcess(output, 3, 0);

		}
		AppKeyboard.AppKey_BackRowReset_Timer = 0u;
	}
}

void multi_media_switch_process(void)
{
	uint8_t i;
    uint8_t key_state[NUM_OF_MULTI_MEDIA_KEY];
	uint8_t PowerGear;
    can_app_receive_signal(COM_RXSIGBCM1_POWER_GEAR,&PowerGear);
    key_state[KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID] = key_adc_mid_get_result(KEY_ADC_ID_VEDIO);
    key_state[KEY_APP_MULTI_MEDIA_VOL_LMENU_ID] = key_adc_mid_get_result(KEY_ADC_ID_LMENU);
    key_state[KEY_APP_MULTI_MEDIA_CUSTOM_ID] = key_adc_mid_get_result(KEY_ADC_ID_CUSTOM);
    for(i = KEY_MAPP_MULTI_MEDIA_VOL_VEDIO_ID; i <= KEY_APP_MULTI_MEDIA_CUSTOM_ID; i ++)
    {
        if((key_state[i] == KEY_ADC_RESULT_UNKNOW) || (key_state[i] == KEY_ADC_RESULT_IDLE) || (key_state[i] == KEY_ADC_RESULT_FAULT))
        {
            key_state[i] = 0u;
        }
        else
        {
            key_state[i] = 1u;
        }
    }

    key_state[KEY_APP_MULTI_MEDIA_PREV_ID] = key_digital_driver_get_state(KEY_DIGI_ID_PREV);
    key_state[KEY_APP_MULTI_MEDIA_NEXT_ID] = key_digital_driver_get_state(KEY_DIGI_ID_NEXT);
    key_state[KEY_APP_MULTI_MEDIA_VOL_VOICE_ID] = key_digital_driver_get_state(KEY_DIGI_ID_VOICE);
    key_state[KEY_APP_MULTI_MEDIA_VOL_CALL_ID] = key_digital_driver_get_state(KEY_DIGI_ID_PHONE);
    key_state[KEY_APP_MULTI_MEDIA_VOL_RMENU_ID] = key_digital_driver_get_state(KEY_DIGI_ID_RMENU);
    key_state[KEY_APP_MULTI_MEDIA_VOL_MUTE_ID] = key_digital_driver_get_state(KEY_DIGI_ID_MUTE);

    for(i = KEY_APP_MULTI_MEDIA_PREV_ID; i <=KEY_APP_MULTI_MEDIA_VOL_MUTE_ID; i ++)
    {
        if(key_state[i] == KEY_DIGITAL_DRIVER_STATE_UP)
        {
            key_state[i] = 0u;
        }
        else
        {
            key_state[i] = 1u;
        }
    }
	if(	PowerGear == POWER_GEAR_ON)
	{
		multi_media_vedio_key_process(&key_state[0]);
		multi_media_lmenu_key_process(&key_state[0]);
		multi_media_custom_key_process(&key_state[0]);
		multi_media_prev_key_process(&key_state[0]);
		multi_media_next_key_process(&key_state[0]);
		multi_media_voice_key_process(&key_state[0]);
		multi_media_mute_key_process(&key_state[0]);
		multi_media_call_key_process(&key_state[0]);
		multi_media_rmenu_key_process(&key_state[0]);
		AppKeyboard_MediaResetProcess(&key_state[0]);
		AppKeyboard_MidScreenRecoverProcess(&key_state[0]);
		AppKeyboard_CoDriverRecoverProcess(&key_state[0]);
		AppKeyboard_HandrailRecoverProcess(&key_state[0]);
		AppKeyboard_BackRowRecoverProcess(&key_state[0]);
	}
	multi_media_mileage_clear_process(&key_state[0]);
	auto_show_in_mode_process(&key_state[0]);
	auto_show_out_mode_process(&key_state[0]);
}



void cruise_set_spd_sub_res_spd_add_key_process(void)
{
	uint8_t set_spd_sub_key;
	uint8_t res_spd_add_key;
	uint8_t output;

	set_spd_sub_key = key_adc_mid_get_result(KEY_ADC_ID_SET_SPD_SUB);
	res_spd_add_key = key_adc_mid_get_result(KEY_ADC_ID_RES_SPD_ADD);


	if(set_spd_sub_key == KEY_ADC_RESULT_PRESSS || set_spd_sub_key == KEY_ADC_RESULT_LONG)
	{
		output = 0x01u;
		Com_SendSignal(COM_TX_SIGSWS_L_SPD_SET, &output);
	}
	else if(res_spd_add_key == KEY_ADC_RESULT_PRESSS ||  res_spd_add_key == KEY_ADC_RESULT_LONG )
	{
		output = 0x03u;
		Com_SendSignal(COM_TX_SIGSWS_L_SPD_SET, &output);
	}
	else
	{
		output = 0x00u;
		Com_SendSignal(COM_TX_SIGSWS_L_SPD_SET, &output);
	}



}




void cruise_dsp_plus_key_process(void)
{
	uint8_t state;
	uint8_t output;

	state = key_adc_mid_get_result(KEY_ADC_ID_DSP_PLUS);

	if(state == KEY_ADC_RESULT_PRESSS || state == KEY_ADC_RESULT_LONG)
	{
		output = 0x01u;
		Com_SendSignal(COM_TX_SIGSWS_L_DSP_PLUS, &output);
	}
	else
	{
		output = 0x0u;
		Com_SendSignal(COM_TX_SIGSWS_L_DSP_PLUS, &output);
	}
}
void cruise_dsp_sub_key_process(void)
{
	uint8_t state;
	uint8_t output;

	state = key_adc_mid_get_result(KEY_ADC_ID_DSP_SUB);

	if(state == KEY_ADC_RESULT_PRESSS || state == KEY_ADC_RESULT_LONG)
	{
		output = 0x01u;
		Com_SendSignal(COM_TX_SIGSWS_L_DSP_SUB, &output);
	}
	else
	{
		output = 0x0u;
		Com_SendSignal(COM_TX_SIGSWS_L_DSP_SUB, &output);
	}
}


void cruise_fault_process(void)
{
	uint8_t state[4];
	uint8_t cruise_fault;
	uint8_t i;
	uint8_t output;

	state[0] = key_adc_mid_get_result(KEY_ADC_ID_SET_SPD_SUB);
	state[1] = key_adc_mid_get_result(KEY_ADC_ID_RES_SPD_ADD);
	state[2] = key_adc_mid_get_result(KEY_ADC_ID_DSP_PLUS);
	state[3] = key_adc_mid_get_result(KEY_ADC_ID_DSP_SUB);

	cruise_fault = 0;
	for(i = 0; i < 4; i ++)
	{
		if(state[i] >= KEY_ADC_RESULT_STICK)
		{
			cruise_fault = 1u;
			break;
		}
	}
	if(cruise_fault)
	{
		output = 0u;
		Com_SendSignal(COM_TX_SIGSWS_L_CRUISE_FAULT, &output);
	}
	else
	{
		output = 1u;
		Com_SendSignal(COM_TX_SIGSWS_L_CRUISE_FAULT, &output);
	}

}


void cruise_pick_process(void)
{
	uint8_t l_pick;
	uint8_t r_pick;
	uint8_t output;

	r_pick = key_digital_driver_get_state(KEY_DIGI_ID_RPICK);
	l_pick = key_adc_mid_get_result(KEY_ADC_ID_LPICK);

	if(r_pick == KEY_DIGITAL_DRIVER_STATE_SHORT_DOWN)
	{
		output = 0x1u;
		Com_SendSignal(COM_TX_SIGSWS_L_RIGHT_PICK, &output);
	}
	else if (r_pick == KEY_DIGITAL_DRIVER_STATE_LONG_DOWN)
	{
		output = 0x2u;
		Com_SendSignal(COM_TX_SIGSWS_L_RIGHT_PICK, &output);
	}
	else
	{
		output = 0x0u;
		Com_SendSignal(COM_TX_SIGSWS_L_RIGHT_PICK, &output);
	}


	if(l_pick == KEY_ADC_RESULT_PRESSS)
	{
		output = 0x1u;
		Com_SendSignal(COM_TX_SIGSWS_L_LEFT_PICK, &output);
	}
	else if (l_pick == KEY_ADC_RESULT_LONG)
	{
		output = 0x2u;
		Com_SendSignal(COM_TX_SIGSWS_L_LEFT_PICK, &output);
	}
	else
	{
		output = 0x0u;
		Com_SendSignal(COM_TX_SIGSWS_L_LEFT_PICK, &output);
	}

}

void cruise_switch_process(void)
{
	uint8_t PowerGear;
    can_app_receive_signal(COM_RXSIGBCM1_POWER_GEAR,&PowerGear);
	if(	PowerGear == POWER_GEAR_ON)
	{
		cruise_set_spd_sub_res_spd_add_key_process();
		cruise_dsp_plus_key_process();
		cruise_dsp_sub_key_process();
		cruise_fault_process();
		cruise_pick_process();
	}
}

/*****************************************************************************
|Prototype         :
|Called by         :
|Preconditions     : -
|Input parameters  : None
|Output parameters : None
|Return value      : None
|Description       :
*****************************************************************************/
void key_app_task(void)
{
	multi_media_switch_process();
	cruise_switch_process();
}






key_app_sw_state_e key_app_get_digi_state(u8 id)
{
	key_digital_driver_state_e l_key_state;
	key_app_sw_state_e l_state_check = KEY_APP_SW_STATE_INACTIVE;
	if(id<KEY_DIGITAL_DRIVER_KEY_TOTAL)
	{
		l_key_state = key_digital_driver_get_state(id);
		switch(l_key_state)
		{
			case KEY_DIGITAL_DRIVER_STATE_UP:
				l_state_check = KEY_APP_SW_STATE_INACTIVE;
			break;
			case KEY_DIGITAL_DRIVER_STATE_SHORT_DOWN:
			case KEY_DIGITAL_DRIVER_STATE_LONG_DOWN:
			case KEY_DIGITAL_DRIVER_STATE_STICK:
				l_state_check = KEY_APP_SW_STATE_ACTIVE;
			break;

			default:
				l_state_check = KEY_APP_SW_STATE_INACTIVE;
			break;
		}
	}
	return l_state_check;
}


/****增加每个AD按键的应用状态*主要是给诊断*******/
key_app_sw_state_e key_app_get_adc_state(u8 id)
{
	key_adc_result_e l_key_state;
	key_app_sw_state_e l_state_check = KEY_APP_SW_STATE_INACTIVE;
	if(id<KEY_ADC_ID_MAX)
	{
		l_key_state = key_adc_mid_get_result(id);
		switch(l_key_state)
		{
			case KEY_ADC_RESULT_IDLE:
				l_state_check = KEY_APP_SW_STATE_INACTIVE;
			break;
			case KEY_ADC_RESULT_PRESSS:
			case KEY_ADC_RESULT_LONG:
			case KEY_ADC_RESULT_STICK:
				l_state_check = KEY_APP_SW_STATE_ACTIVE;
			break;

			default:
				l_state_check = KEY_APP_SW_STATE_INACTIVE;
			break;
		}
	}
	return l_state_check;
}
