From d0ce4bf391f9a55574ea4dd0c897ee0546085c0e Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 21 Jun 2024 16:35:26 +0700 Subject: [PATCH] change debouce logic --- .../lib/ESPMegaPRO/DigitalInputCard.cpp | 45 ++++++++++++++----- .../lib/ESPMegaPRO/DigitalInputCard.hpp | 1 + 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp index 86ad5a1..5103c9b 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp @@ -137,25 +137,50 @@ void DigitalInputCard::handlePinChange(int pin, uint8_t ¤tBuffer, uint8_t // Handle Bank A if (((previousBuffer >> (7 - pin)) & 1) != ((currentBuffer >> (7 - pin)) & 1)) { - if (millis() - lastDebounceTime[pin] > debounceTime[pin]) - { + if (!pinChanged[pin]) { lastDebounceTime[pin] = millis(); - previousBuffer ^= (-((currentBuffer >> (7 - pin)) & 1) ^ previousBuffer) & (1UL << (7 - pin)); - for(const auto& callback : callbacks) - callback.second(virtualPin, ((currentBuffer >> (7 - pin)) & 1)); + pinChanged[pin] = true; + } else { + if (millis() - lastDebounceTime[pin] > debounceTime[pin]) + { + previousBuffer ^= (-((currentBuffer >> (7 - pin)) & 1) ^ previousBuffer) & (1UL << (7 - pin)); + for (const auto& callback : callbacks) + callback.second(virtualPin, ((currentBuffer >> (7 - pin)) & 1)); + pinChanged[pin] = false; + } } } // Handle Bank B if (((previousBuffer >> (15 - pin)) & 1) != ((currentBuffer >> (15 - pin)) & 1)) { - if (millis() - lastDebounceTime[pin] > debounceTime[pin]) - { + if (!pinChanged[pin]) { lastDebounceTime[pin] = millis(); - previousBuffer ^= (-((currentBuffer >> (15 - pin)) & 1) ^ previousBuffer) & (1UL << (15 - pin)); - for (const auto& callback : callbacks) - callback.second(virtualPin, ((currentBuffer >> (15 - pin)) & 1)); + pinChanged[pin] = true; + } else { + if (millis() - lastDebounceTime[pin] > debounceTime[pin]) + { + previousBuffer ^= (-((currentBuffer >> (15 - pin)) & 1) ^ previousBuffer) & (1UL << (15 - pin)); + for (const auto& callback : callbacks) + callback.second(virtualPin, ((currentBuffer >> (15 - pin)) & 1)); + pinChanged[pin] = false; + } } } + + + + // // Handle Bank B + // if (((previousBuffer >> (15 - pin)) & 1) != ((currentBuffer >> (15 - pin)) & 1)) + // { + // if (millis() - lastDebounceTime[pin] > debounceTime[pin]) + // { + // lastDebounceTime[pin] = millis(); + // previousBuffer ^= (-((currentBuffer >> (15 - pin)) & 1) ^ previousBuffer) & (1UL << (15 - pin)); + // for (const auto& callback : callbacks) + // callback.second(virtualPin, ((currentBuffer >> (15 - pin)) & 1)); + // } + // } + } /** diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp index a140029..3ba7805 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp @@ -54,6 +54,7 @@ class DigitalInputCard : public ExpansionCard { uint8_t previousInputBufferB; uint32_t debounceTime[16]; uint32_t lastDebounceTime[16]; + bool pinChanged[16]; // A map of the physical pin to the virtual pin uint8_t pinMap[16]; // A map of the virtual pin to the physical pin