From a149f2e6b63bac0194f71d5209f371cc06157aab Mon Sep 17 00:00:00 2001 From: technyon Date: Sun, 20 Oct 2024 09:51:45 +0200 Subject: [PATCH] refactor GPIO detection code --- src/Config.h | 2 +- src/Gpio.cpp | 61 +++++++++++++++++++++++++-------------------- src/Gpio.h | 6 ++--- src/NukiWrapper.cpp | 2 +- 4 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/Config.h b/src/Config.h index 75661ba..ef6633d 100644 --- a/src/Config.h +++ b/src/Config.h @@ -4,7 +4,7 @@ #define NUKI_HUB_VERSION "9.02" #define NUKI_HUB_BUILD "unknownbuildnr" -#define NUKI_HUB_DATE "2024-10-19" +#define NUKI_HUB_DATE "2024-10-20" #define GITHUB_LATEST_RELEASE_URL (char*)"https://github.com/technyon/nuki_hub/releases/latest" #define GITHUB_OTA_MANIFEST_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/manifest.json" diff --git a/src/Gpio.cpp b/src/Gpio.cpp index eca41c2..80189ac 100644 --- a/src/Gpio.cpp +++ b/src/Gpio.cpp @@ -10,7 +10,6 @@ #include "networkDevices/W5500Definitions.h" Gpio* Gpio::_inst = nullptr; -const uint Gpio::_debounceTime = GPIO_DEBOUNCE_TIME; Gpio::Gpio(Preferences* preferences) : _preferences(preferences) @@ -29,42 +28,50 @@ Gpio::Gpio(Preferences* preferences) bool Gpio::isTriggered(const PinEntry& entry) { +// Log->println(" ------------ "); + const int threshold = 3; - int state = digitalRead(entry.pin); + uint8_t state = digitalRead(entry.pin); + uint8_t lastState = (_triggerState[entry.pin] & 0x80) >> 7; - if(entry.role == PinRole::GeneralInputPullDown) + uint8_t pinState = _triggerState[entry.pin] & 0x7f; + pinState = pinState << 1 | state; + _triggerState[entry.pin] = (pinState & 0x7f) | lastState << 7; +// Log->print("Trigger state: "); +// Log->println(_triggerState[entry.pin], 2); + + pinState = pinState & 0x07; +// Log->print("Val: "); +// Log->println(pinState); + + + if(pinState != 0x00 && pinState != 0x07) { - state = 1 - state; + return false; } +// Log->print("Last State: "); +// Log->println(lastState); +// Log->print("State: "); +// Log->println(state); - if(state == LOW) + if(state != lastState) { - if (_triggerCount[entry.pin] >= 0) - { - _triggerCount[entry.pin]++; - } - - if (_triggerCount[entry.pin] >= threshold) - { - _triggerCount[entry.pin] = -1; - return true; - } +// Log->print("State changed: "); +// Log->println(state); + _triggerState[entry.pin] = (pinState & 0x7f) | state << 7; } else { - if (_triggerCount[entry.pin] < 0) - { - _triggerCount[entry.pin]--; - - if(_triggerCount[entry.pin] <= -threshold) - { - _triggerCount[entry.pin] = 0; - } - } + return false; } - return false; + if(entry.role == PinRole::GeneralInputPullDown || entry.role == PinRole::GeneralInputPullUp) + { + return true; + } + + return state == LOW; } void Gpio::onTimer() @@ -113,10 +120,10 @@ void Gpio::isrOnTimer() void Gpio::init() { - _inst->_triggerCount.reserve(_inst->availablePins().size()); + _inst->_triggerState.reserve(_inst->availablePins().size()); for(int i=0; i<_inst->availablePins().size(); i++) { - _inst->_triggerCount.push_back(0); + _inst->_triggerState.push_back(0); } bool hasInputPin = false; diff --git a/src/Gpio.h b/src/Gpio.h index 2607df9..be54d16 100644 --- a/src/Gpio.h +++ b/src/Gpio.h @@ -84,6 +84,7 @@ private: void IRAM_ATTR onTimer(); bool IRAM_ATTR isTriggered(const PinEntry& pinEntry); GpioAction IRAM_ATTR getGpioAction(const PinRole& role) const; + static void IRAM_ATTR isrOnTimer(); #if defined(CONFIG_IDF_TARGET_ESP32C3) //Based on https://docs.espressif.com/projects/esp-idf/en/stable/esp32c3/api-reference/peripherals/gpio.html and https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf @@ -127,15 +128,12 @@ private: }; std::vector _pinConfiguration; - static const uint _debounceTime; - - static void IRAM_ATTR isrOnTimer(); std::vector> _callbacks; static Gpio* _inst; - std::vector _triggerCount; + std::vector _triggerState; hw_timer_t* timer = nullptr; Preferences* _preferences = nullptr; diff --git a/src/NukiWrapper.cpp b/src/NukiWrapper.cpp index 8ed2900..4167aef 100644 --- a/src/NukiWrapper.cpp +++ b/src/NukiWrapper.cpp @@ -268,7 +268,7 @@ void NukiWrapper::update() if(_nukiOfficial->getOffCommandExecutedTs() > 0 && ts >= _nukiOfficial->getOffCommandExecutedTs()) { - nukiInst->_nextLockAction = _offCommand; + _nextLockAction = _offCommand; _nukiOfficial->clearOffCommandExecutedTs(); } if(_nextLockAction != (NukiLock::LockAction)0xff)