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,
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->_pinUp = pinUp;
@ -35,19 +35,19 @@ int32_t UpDownInterruptBase::runOnce()
e.inputEvent = INPUT_BROKER_NONE;
unsigned long now = millis();
if (this->action == UPDOWN_ACTION_PRESSED) {
if (now - lastPressKeyTime >= PRESS_DEBOUNCE_MS) {
if (now - lastPressKeyTime >= pressDebounceMs) {
lastPressKeyTime = now;
LOG_DEBUG("GPIO event Press");
e.inputEvent = this->_eventPressed;
}
} else if (this->action == UPDOWN_ACTION_UP) {
if (now - lastUpKeyTime >= UPDOWN_DEBOUNCE_MS) {
if (now - lastUpKeyTime >= updownDebounceMs) {
lastUpKeyTime = now;
LOG_DEBUG("GPIO event Up");
e.inputEvent = this->_eventUp;
}
} else if (this->action == UPDOWN_ACTION_DOWN) {
if (now - lastDownKeyTime >= UPDOWN_DEBOUNCE_MS) {
if (now - lastDownKeyTime >= updownDebounceMs) {
lastDownKeyTime = now;
LOG_DEBUG("GPIO event Down");
e.inputEvent = this->_eventDown;

View File

@ -8,7 +8,7 @@ class UpDownInterruptBase : public Observable<const InputEvent *>, public concur
public:
explicit UpDownInterruptBase(const char *name);
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 intDownHandler();
void intUpHandler();
@ -31,6 +31,6 @@ class UpDownInterruptBase : public Observable<const InputEvent *>, public concur
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;
unsigned long updownDebounceMs = 300;
const unsigned long pressDebounceMs = 500;
};

View File

@ -21,8 +21,9 @@ bool UpDownInterruptImpl1::init()
input_broker_event eventUp = INPUT_BROKER_UP;
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,
UpDownInterruptImpl1::handleIntUp, UpDownInterruptImpl1::handleIntPressed);
UpDownInterruptImpl1::handleIntUp, UpDownInterruptImpl1::handleIntPressed, debounceMs);
inputBroker->registerSource(this);
return true;
}