Optimize key event processing and add debounce logic.

This commit is contained in:
whywilson 2025-06-16 23:18:45 +08:00 committed by Jonathan Bennett
parent 195b7cc30a
commit 7a38368494
2 changed files with 22 additions and 8 deletions

View File

@ -33,17 +33,26 @@ int32_t UpDownInterruptBase::runOnce()
{ {
InputEvent e; InputEvent e;
e.inputEvent = INPUT_BROKER_NONE; e.inputEvent = INPUT_BROKER_NONE;
unsigned long now = millis();
if (this->action == UPDOWN_ACTION_PRESSED) { if (this->action == UPDOWN_ACTION_PRESSED) {
if (now - lastPressKeyTime >= PRESS_DEBOUNCE_MS) {
lastPressKeyTime = now;
LOG_DEBUG("GPIO event Press"); LOG_DEBUG("GPIO event Press");
e.inputEvent = this->_eventPressed; e.inputEvent = this->_eventPressed;
}
} else if (this->action == UPDOWN_ACTION_UP) { } else if (this->action == UPDOWN_ACTION_UP) {
if (now - lastUpKeyTime >= UPDOWN_DEBOUNCE_MS) {
lastUpKeyTime = now;
LOG_DEBUG("GPIO event Up"); LOG_DEBUG("GPIO event Up");
e.inputEvent = this->_eventUp; e.inputEvent = this->_eventUp;
}
} else if (this->action == UPDOWN_ACTION_DOWN) { } else if (this->action == UPDOWN_ACTION_DOWN) {
if (now - lastDownKeyTime >= UPDOWN_DEBOUNCE_MS) {
lastDownKeyTime = now;
LOG_DEBUG("GPIO event Down"); LOG_DEBUG("GPIO event Down");
e.inputEvent = this->_eventDown; e.inputEvent = this->_eventDown;
} }
}
if (e.inputEvent != INPUT_BROKER_NONE) { if (e.inputEvent != INPUT_BROKER_NONE) {
e.source = this->_originName; e.source = this->_originName;
@ -52,7 +61,6 @@ int32_t UpDownInterruptBase::runOnce()
} }
this->action = UPDOWN_ACTION_NONE; this->action = UPDOWN_ACTION_NONE;
return 100; return 100;
} }

View File

@ -27,4 +27,10 @@ class UpDownInterruptBase : public Observable<const InputEvent *>, public concur
input_broker_event _eventUp = INPUT_BROKER_NONE; input_broker_event _eventUp = INPUT_BROKER_NONE;
input_broker_event _eventPressed = INPUT_BROKER_NONE; input_broker_event _eventPressed = INPUT_BROKER_NONE;
const char *_originName; const char *_originName;
unsigned long lastUpKeyTime = 0;
unsigned long lastDownKeyTime = 0;
unsigned long lastPressKeyTime = 0;
const unsigned long UPDOWN_DEBOUNCE_MS = 300;
const unsigned long PRESS_DEBOUNCE_MS = 500;
}; };