📄 key.c
字号:
//----------------------------------------------------------------------------------------------------
// ID Code : Key.c No.0000
// Update Note :
//
//----------------------------------------------------------------------------------------------------
#define __KEY__
#include "Common\Header\Include.h"
//--------------------------------------------------
// Description : Key scan process
// Input Value : None
// Output Value : None
//--------------------------------------------------
void CKeyHandler(void)
{
// Clear the key message
ucKeyMessage = _NONE_KEY_MESSAGE;
if(CKeyScanReady())
{
// Store previous key state
ucKeyStatePrev = ucKeyStateCurr;
// Get current key state
ucKeyStateCurr = CKeyScan();
// Power key process, return if power key is pressed
if(CKeyPowerKeyProc())
return;
// Convert key state to key message, store in (ucKeyNotify)
CKeyMessageProc();
}
}
//--------------------------------------------------
// Description : Check power key process
// Input Value : None
// Output Value : None
//--------------------------------------------------
void CKeyCheckPowerKey(void)
{
// Store previous key state
ucKeyStatePrev = ucKeyStateCurr;
// Get current key state
ucKeyStateCurr = CKeyScan();
// Power key process
CKeyPowerKeyProc();
}
//--------------------------------------------------
// Description : Initial key status
// Input Value : None
// Output Value : None
//--------------------------------------------------
void CKeyInitial(void)
{
CLR_KEYSCANREADY();
CLR_KEYSCANSTART();
}
//--------------------------------------------------
// Description : Key scan ready process. We wait 0.02 sec in order to keep the keypad debounce
// Input Value : None
// Output Value : None
//--------------------------------------------------
bit CKeyScanReady(void)
{
if(GET_KEYSCANSTART() && GET_KEYSCANREADY())
{
return _TRUE;
}
else if(!GET_KEYSCANSTART())
{
// Wait 0.02 sec in order to keep the keypad debounce
SET_KEYSCANSTART();
CTimerReactiveTimerEvent(SEC(0.02), CKeyScanReadyTimerEvent);
return _FALSE;
}
else
{
return _FALSE;
}
}
//--------------------------------------------------
// Description : Key scan ready timer event
// Input Value : None
// Output Value : None
//--------------------------------------------------
void CKeyScanReadyTimerEvent(void)
{
SET_KEYSCANREADY();
}
//--------------------------------------------------
// Description : Key repeat enable timer event
// Input Value : None
// Output Value : None
//--------------------------------------------------
void CKeyRepeatEnableTimerEvent(void)
{
SET_KEYREPEATSTART();
}
//--------------------------------------------------
// Description : Key message translation
// Input Value : ucKeyMask --> Key mask
// ucKeyMsg --> Key message
// Output Value : None
//--------------------------------------------------
void CKeyMessageConvert(BYTE ucKeyMask, BYTE ucKeyMsg)
{
if((ucKeyStatePrev ^ ucKeyStateCurr) & ucKeyMask)
{
ucKeyMessage = ucKeyMsg;
}
else
{
if(GET_KEYREPEATENABLE())
{
if(GET_KEYREPEATSTART())
{
ucKeyMessage = ucKeyMsg;
}
else
{
CTimerActiveTimerEvent(SEC(_KEY_REPEAT_START_TIME),CKeyRepeatEnableTimerEvent);
}
}
}
}
//--------------------------------------------------
// Description : Power key process
// Input Value : None
// Output Value : Return _TRUE if power key is pressed
//--------------------------------------------------
bit CKeyPowerKeyProc(void)
{
if(ucKeyStateCurr & _POWER_KEY_MASK)
{
if((ucKeyStatePrev ^ ucKeyStateCurr) & _POWER_KEY_MASK)
{
CTimerDelayXms(25);
ucKeyStateCurr = CKeyScan();
if((ucKeyStatePrev ^ ucKeyStateCurr) & _POWER_KEY_MASK)
{
CKeyPowerKeyMix();
SET_POWERSWITCH();
return _TRUE;
}
}
}
return _FALSE;
}
//--------------------------------------------------
// Description : Get key status
// Input Value : None
// Output Value : Return Key status
//--------------------------------------------------
BYTE CKeyScan(void)
{
#if(_PCB_TYPE == _HKC023L_V783)
#if 1 //782Key
BYTE ucKeyState = 0;
BYTE ucKeyValue = 0;
ucKeyState = 0;
ucKeyValue = MCU_ADC_F10;
ucKeyValue &= 0x3F;
if(ucKeyValue<10)
{ucKeyState |= _MENU_KEY_MASK;
}
else if(ucKeyValue<35)
ucKeyState |= _RIGHT_KEY_MASK; //MENU_KEY_MASK;
else if(ucKeyValue<50)
ucKeyState |= _EXIT_KEY_MASK; //ENTER_KEY_MASK;
/*
if(ucKeyValue<10)
{ucKeyState |= _MENU_KEY_MASK;
}
else if(ucKeyValue<25)
ucKeyState |= _RIGHT_KEY_MASK; //MENU_KEY_MASK;
else if(ucKeyValue<50)
ucKeyState |= _EXIT_KEY_MASK; //ENTER_KEY_MASK;
*/
if(ucKeyState)
return ucKeyState;
MCU_ADC_F10 = ADCHANNEL1;
CTimerDelayXms(2);
ucKeyValue = MCU_ADC_F10;
ucKeyValue &= 0x3F;
if(ucKeyValue<25)
ucKeyState |= _LEFT_KEY_MASK;
else if(ucKeyValue<50)
ucKeyState |= _POWER_KEY_MASK;
MCU_ADC_F10 = ADCHANNEL0;
return ucKeyState;
#else //key-04
BYTE ucKeyState = 0;
BYTE ucKeyValue = 0;
ucKeyState = 0;
ucKeyValue = MCU_ADC_F10;
ucKeyValue &= 0x3F;
if(ucKeyValue<10)
{ucKeyState |= _MENU_KEY_MASK;
}
else if(ucKeyValue<25)
ucKeyState |= _LEFT_KEY_MASK; //MENU_KEY_MASK;
else if(ucKeyValue<50)
ucKeyState |= _EXIT_KEY_MASK; //ENTER_KEY_MASK;
if(ucKeyState)
return ucKeyState;
MCU_ADC_F10 = ADCHANNEL1;
CTimerDelayXms(2);
ucKeyValue = MCU_ADC_F10;
ucKeyValue &= 0x3F;
if(ucKeyValue<25)
ucKeyState |= _RIGHT_KEY_MASK;
else if(ucKeyValue<50)
ucKeyState |= _POWER_KEY_MASK;
MCU_ADC_F10 = ADCHANNEL0;
return ucKeyState;
#endif
#endif
#if(_PCB_TYPE == _PM023LD1)
BYTE keystate = 0;
if(!bRIGHT_KEY)
keystate = keystate | _RIGHT_KEY_MASK;
if(!bLEFT_KEY)
keystate = keystate | _LEFT_KEY_MASK;
if(!bEXIT_KEY)
keystate = keystate | _EXIT_KEY_MASK;
if(!bMENU_KEY)
keystate = keystate | _MENU_KEY_MASK;
if(!bPOWER_KEY)
keystate = keystate | _POWER_KEY_MASK;
// if(!bAUTO_KEY)
// keystate = keystate | _AUTO_KEY_MASK;
if(keystate != 0)
CKeyInitial();
return keystate;
#endif
#if(_PCB_TYPE == _PM023LB1)
BYTE keystate = 0;
if(!bRIGHT_KEY)
keystate = keystate | _RIGHT_KEY_MASK;
if(!bLEFT_KEY)
keystate = keystate | _LEFT_KEY_MASK;
if(!bEXIT_KEY)
keystate = keystate | _EXIT_KEY_MASK;
if(!bMENU_KEY)
keystate = keystate | _MENU_KEY_MASK;
if(!bPOWER_KEY)
keystate = keystate | _POWER_KEY_MASK;
if(keystate != 0)
CKeyInitial();
return keystate;
#endif
#if 0
BYTE keystate = 0;
if(!bRIGHT_KEY)
keystate = keystate | _RIGHT_KEY_MASK;
if(!bLEFT_KEY)
keystate = keystate | _LEFT_KEY_MASK;
if(!bEXIT_KEY)
keystate = keystate | _EXIT_KEY_MASK;
if(!bMENU_KEY)
keystate = keystate | _MENU_KEY_MASK;
if(!bPOWER_KEY)
keystate = keystate | _POWER_KEY_MASK;
if(keystate != 0)
CKeyInitial();
return keystate;
#endif
}
//--------------------------------------------------
// Description : We can add some settings here while combo key with power key
// Input Value : None
// Output Value : None
//--------------------------------------------------
void CKeyPowerKeyMix(void)
{
switch(ucKeyStateCurr)
{
case _POWER_RIGHT_KEY_MASK:
ucOsdEventMsg = _ENTER_FACTORY_MODE_MSG;
break;
case _POWER_MENU_KEY_MASK:
break;
case _POWER_LEFT_RIGHT_KEY_MASK:
break;
}
}
//--------------------------------------------------
// Description : Convert keypad status into key message, stores in ucKeyNotify
// Input Value : None
// Output Value : None
//--------------------------------------------------
void CKeyMessageProc(void)
{
switch(ucKeyStateCurr)
{
case _MENU_KEY_MASK:
CKeyMessageConvert(_MENU_KEY_MASK, _MENU_KEY_MESSAGE);
break;
case _RIGHT_KEY_MASK:
CKeyMessageConvert(_RIGHT_KEY_MASK, _RIGHT_KEY_MESSAGE);
break;
case _LEFT_KEY_MASK:
CKeyMessageConvert(_LEFT_KEY_MASK, _LEFT_KEY_MESSAGE);
break;
case _EXIT_KEY_MASK:
CKeyMessageConvert(_EXIT_KEY_MASK, _EXIT_KEY_MESSAGE);
break;
default:
CLR_KEYREPEATSTART();
CTimerCancelTimerEvent(CKeyRepeatEnableTimerEvent);
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -