按键扫描实现的另一种方式:
/*获取按键状态*/
buttonEvent_e buttonGetState()
{
buttonEvent_e currentState = state;
state = buttonIdle;
return currentState;
}
/*按键扫描处理*/
void buttonProcess()
{
static unsigned int lastTick;
static unsigned int pressedTick;
static bool pressed = false;
if (lastTick != systickGetTick())
{
lastTick = systickGetTick();
if(pressed==false && BUTTON_READ()==BUTTON_PRESSED)
{
pressed = true;
pressedTick = systickGetTick();
}
else if(pressed==true)
{
if(BUTTON_READ()==BUTTON_RELEASED)
pressed = false;
if ((systickGetTick()-pressedTick) > BUTTON_LONGPRESS_TICK)
state = buttonLongPress;
else if(BUTTON_READ()==BUTTON_RELEASED)
state = buttonShortPress;
}
}
}
buttonProcess()函数中需要在while中,不断地扫描按键状态。