u8 KeyStateMachine(void)
{
if(KeyDone) //按键对应处理执行了没有
{
KeyValue=(GetKeyValue()&NoKeyPressMask);/*检测按键端口获得按键值*/
switch(CurKeyState)
{
case NoKeyPress:
if(KeyValue!=NoKeyPressMask)
{
CurKeyState=KeyDrop;
}
else
{
#ifdef NoKeyTimeCntEn //支持无击使能标志
NoKeyTimeCnt++; //支持无击计数器增长
#endif
KeyValid= 0xff;
}
break;
case KeyDrop:
if(KeyTempBuf[0]!=KeyTempBuf[1]&&KeyValue==KeyTempBuf[1]&&KeyValue!=NoKeyPressMask)
{
CurKeyState=KeyPress;
KeyAge=0; //年龄计数器清零
}
else
{
CurKeyState=NoKeyPress;
}
break;
case KeyPress: //此阶段处理长击和连击
if(KeyTempBuf[0]==KeyTempBuf[1]&&KeyValue==KeyTempBuf[1]&&KeyValue!=NoKeyPressMask)
{
KeyAge++; //年龄计数器累加
KeyValid= KeyValue;//或者压KEYPADBUF
}
else
{
CurKeyState=NoKeyPress;
}
case KeyRelease: //此阶段处理键盘弹起
if(KeyTempBuf[0]==NoKeyPressMask&&KeyTempBuf[1]==NoKeyPressMask&&KeyValue==NoKeyPressMask) //确认弹起
{
CurKeyState=NoKeyPress;
if(KeyAge>=KeyLongPreesLimit)
SingleKeyLongShortFlag=1;
else
SingleKeyLongShortFlag=0;
KeyAge=0; //年龄计数器清零
KeyDone=0; //按键置未处理标志
KeyTempBuf[0]=KeyTempBuf[1]=KeyValue=0xff;
}
break;
default:
CurKeyState=NoKeyPress;
break;
}
KeyTempBuf[0]=KeyTempBuf[1];
KeyTempBuf[1]=KeyValue;
return KeyValid;
}
else{
//键盘扫描无效
return 0xff;
}
} |