Add a debounce time parameter and use it in the runOnce method to debounce the key.

This commit is contained in:
whywilson 2025-06-17 06:05:45 +08:00 committed by Jonathan Bennett
parent 7a38368494
commit 8ba98ae873
3 changed files with 9 additions and 8 deletions

View File

@ -8,7 +8,7 @@ UpDownInterruptBase::UpDownInterruptBase(const char *name) : concurrency::OSThre
void UpDownInterruptBase::init(uint8_t pinDown, uint8_t pinUp, uint8_t pinPress, input_broker_event eventDown, void UpDownInterruptBase::init(uint8_t pinDown, uint8_t pinUp, uint8_t pinPress, input_broker_event eventDown,
input_broker_event eventUp, input_broker_event eventPressed, void (*onIntDown)(), input_broker_event eventUp, input_broker_event eventPressed, void (*onIntDown)(),
void (*onIntUp)(), void (*onIntPress)()) void (*onIntUp)(), void (*onIntPress)(), unsigned long updownDebounceMs)
{ {
this->_pinDown = pinDown; this->_pinDown = pinDown;
this->_pinUp = pinUp; this->_pinUp = pinUp;
@ -35,19 +35,19 @@ int32_t UpDownInterruptBase::runOnce()
e.inputEvent = INPUT_BROKER_NONE; e.inputEvent = INPUT_BROKER_NONE;
unsigned long now = millis(); unsigned long now = millis();
if (this->action == UPDOWN_ACTION_PRESSED) { if (this->action == UPDOWN_ACTION_PRESSED) {
if (now - lastPressKeyTime >= PRESS_DEBOUNCE_MS) { if (now - lastPressKeyTime >= pressDebounceMs) {
lastPressKeyTime = now; 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) { if (now - lastUpKeyTime >= updownDebounceMs) {
lastUpKeyTime = now; 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) { if (now - lastDownKeyTime >= updownDebounceMs) {
lastDownKeyTime = now; lastDownKeyTime = now;
LOG_DEBUG("GPIO event Down"); LOG_DEBUG("GPIO event Down");
e.inputEvent = this->_eventDown; e.inputEvent = this->_eventDown;

View File

@ -8,7 +8,7 @@ class UpDownInterruptBase : public Observable<const InputEvent *>, public concur
public: public:
explicit UpDownInterruptBase(const char *name); explicit UpDownInterruptBase(const char *name);
void init(uint8_t pinDown, uint8_t pinUp, uint8_t pinPress, input_broker_event eventDown, input_broker_event eventUp, void init(uint8_t pinDown, uint8_t pinUp, uint8_t pinPress, input_broker_event eventDown, input_broker_event eventUp,
input_broker_event eventPressed, void (*onIntDown)(), void (*onIntUp)(), void (*onIntPress)()); input_broker_event eventPressed, void (*onIntDown)(), void (*onIntUp)(), void (*onIntPress)(), unsigned long updownDebounceMs = 300);
void intPressHandler(); void intPressHandler();
void intDownHandler(); void intDownHandler();
void intUpHandler(); void intUpHandler();
@ -31,6 +31,6 @@ class UpDownInterruptBase : public Observable<const InputEvent *>, public concur
unsigned long lastUpKeyTime = 0; unsigned long lastUpKeyTime = 0;
unsigned long lastDownKeyTime = 0; unsigned long lastDownKeyTime = 0;
unsigned long lastPressKeyTime = 0; unsigned long lastPressKeyTime = 0;
const unsigned long UPDOWN_DEBOUNCE_MS = 300; unsigned long updownDebounceMs = 300;
const unsigned long PRESS_DEBOUNCE_MS = 500; const unsigned long pressDebounceMs = 500;
}; };

View File

@ -21,8 +21,9 @@ bool UpDownInterruptImpl1::init()
input_broker_event eventUp = INPUT_BROKER_UP; input_broker_event eventUp = INPUT_BROKER_UP;
input_broker_event eventPressed = INPUT_BROKER_SELECT; input_broker_event eventPressed = INPUT_BROKER_SELECT;
unsigned long debounceMs = moduleConfig.canned_message.rotary1_enabled ? 100 : 300;
UpDownInterruptBase::init(pinDown, pinUp, pinPress, eventDown, eventUp, eventPressed, UpDownInterruptImpl1::handleIntDown, UpDownInterruptBase::init(pinDown, pinUp, pinPress, eventDown, eventUp, eventPressed, UpDownInterruptImpl1::handleIntDown,
UpDownInterruptImpl1::handleIntUp, UpDownInterruptImpl1::handleIntPressed); UpDownInterruptImpl1::handleIntUp, UpDownInterruptImpl1::handleIntPressed, debounceMs);
inputBroker->registerSource(this); inputBroker->registerSource(this);
return true; return true;
} }