From 7a38368494b7daa45a5326fd4bd74b46e142f8e8 Mon Sep 17 00:00:00 2001 From: whywilson Date: Mon, 16 Jun 2025 23:18:45 +0800 Subject: [PATCH] Optimize key event processing and add debounce logic. --- src/input/UpDownInterruptBase.cpp | 24 ++++++++++++++++-------- src/input/UpDownInterruptBase.h | 6 ++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/input/UpDownInterruptBase.cpp b/src/input/UpDownInterruptBase.cpp index 9a95323fe..44eae5dbb 100644 --- a/src/input/UpDownInterruptBase.cpp +++ b/src/input/UpDownInterruptBase.cpp @@ -33,16 +33,25 @@ int32_t UpDownInterruptBase::runOnce() { InputEvent e; e.inputEvent = INPUT_BROKER_NONE; - + unsigned long now = millis(); if (this->action == UPDOWN_ACTION_PRESSED) { - LOG_DEBUG("GPIO event Press"); - e.inputEvent = this->_eventPressed; + if (now - lastPressKeyTime >= PRESS_DEBOUNCE_MS) { + lastPressKeyTime = now; + LOG_DEBUG("GPIO event Press"); + e.inputEvent = this->_eventPressed; + } } else if (this->action == UPDOWN_ACTION_UP) { - LOG_DEBUG("GPIO event Up"); - e.inputEvent = this->_eventUp; + if (now - lastUpKeyTime >= UPDOWN_DEBOUNCE_MS) { + lastUpKeyTime = now; + LOG_DEBUG("GPIO event Up"); + e.inputEvent = this->_eventUp; + } } else if (this->action == UPDOWN_ACTION_DOWN) { - LOG_DEBUG("GPIO event Down"); - e.inputEvent = this->_eventDown; + if (now - lastDownKeyTime >= UPDOWN_DEBOUNCE_MS) { + lastDownKeyTime = now; + LOG_DEBUG("GPIO event Down"); + e.inputEvent = this->_eventDown; + } } if (e.inputEvent != INPUT_BROKER_NONE) { @@ -52,7 +61,6 @@ int32_t UpDownInterruptBase::runOnce() } this->action = UPDOWN_ACTION_NONE; - return 100; } diff --git a/src/input/UpDownInterruptBase.h b/src/input/UpDownInterruptBase.h index 4e9f591b9..57e42a76a 100644 --- a/src/input/UpDownInterruptBase.h +++ b/src/input/UpDownInterruptBase.h @@ -27,4 +27,10 @@ class UpDownInterruptBase : public Observable, public concur input_broker_event _eventUp = INPUT_BROKER_NONE; input_broker_event _eventPressed = INPUT_BROKER_NONE; 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; };