refactor GPIO detection code

This commit is contained in:
technyon
2024-10-20 09:51:45 +02:00
parent 7c8dbabb9f
commit a149f2e6b6
4 changed files with 38 additions and 33 deletions

View File

@@ -4,7 +4,7 @@
#define NUKI_HUB_VERSION "9.02" #define NUKI_HUB_VERSION "9.02"
#define NUKI_HUB_BUILD "unknownbuildnr" #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_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" #define GITHUB_OTA_MANIFEST_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/manifest.json"

View File

@@ -10,7 +10,6 @@
#include "networkDevices/W5500Definitions.h" #include "networkDevices/W5500Definitions.h"
Gpio* Gpio::_inst = nullptr; Gpio* Gpio::_inst = nullptr;
const uint Gpio::_debounceTime = GPIO_DEBOUNCE_TIME;
Gpio::Gpio(Preferences* preferences) Gpio::Gpio(Preferences* preferences)
: _preferences(preferences) : _preferences(preferences)
@@ -29,42 +28,50 @@ Gpio::Gpio(Preferences* preferences)
bool Gpio::isTriggered(const PinEntry& entry) bool Gpio::isTriggered(const PinEntry& entry)
{ {
// Log->println(" ------------ ");
const int threshold = 3; 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) // Log->print("State changed: ");
{ // Log->println(state);
_triggerCount[entry.pin]++; _triggerState[entry.pin] = (pinState & 0x7f) | state << 7;
}
if (_triggerCount[entry.pin] >= threshold)
{
_triggerCount[entry.pin] = -1;
return true;
}
} }
else else
{ {
if (_triggerCount[entry.pin] < 0) return false;
{
_triggerCount[entry.pin]--;
if(_triggerCount[entry.pin] <= -threshold)
{
_triggerCount[entry.pin] = 0;
}
}
} }
return false; if(entry.role == PinRole::GeneralInputPullDown || entry.role == PinRole::GeneralInputPullUp)
{
return true;
}
return state == LOW;
} }
void Gpio::onTimer() void Gpio::onTimer()
@@ -113,10 +120,10 @@ void Gpio::isrOnTimer()
void Gpio::init() void Gpio::init()
{ {
_inst->_triggerCount.reserve(_inst->availablePins().size()); _inst->_triggerState.reserve(_inst->availablePins().size());
for(int i=0; i<_inst->availablePins().size(); i++) for(int i=0; i<_inst->availablePins().size(); i++)
{ {
_inst->_triggerCount.push_back(0); _inst->_triggerState.push_back(0);
} }
bool hasInputPin = false; bool hasInputPin = false;

View File

@@ -84,6 +84,7 @@ private:
void IRAM_ATTR onTimer(); void IRAM_ATTR onTimer();
bool IRAM_ATTR isTriggered(const PinEntry& pinEntry); bool IRAM_ATTR isTriggered(const PinEntry& pinEntry);
GpioAction IRAM_ATTR getGpioAction(const PinRole& role) const; GpioAction IRAM_ATTR getGpioAction(const PinRole& role) const;
static void IRAM_ATTR isrOnTimer();
#if defined(CONFIG_IDF_TARGET_ESP32C3) #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 //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<PinEntry> _pinConfiguration; std::vector<PinEntry> _pinConfiguration;
static const uint _debounceTime;
static void IRAM_ATTR isrOnTimer();
std::vector<std::function<void(const GpioAction&, const int&)>> _callbacks; std::vector<std::function<void(const GpioAction&, const int&)>> _callbacks;
static Gpio* _inst; static Gpio* _inst;
std::vector<int8_t> _triggerCount; std::vector<uint8_t> _triggerState;
hw_timer_t* timer = nullptr; hw_timer_t* timer = nullptr;
Preferences* _preferences = nullptr; Preferences* _preferences = nullptr;

View File

@@ -268,7 +268,7 @@ void NukiWrapper::update()
if(_nukiOfficial->getOffCommandExecutedTs() > 0 && ts >= _nukiOfficial->getOffCommandExecutedTs()) if(_nukiOfficial->getOffCommandExecutedTs() > 0 && ts >= _nukiOfficial->getOffCommandExecutedTs())
{ {
nukiInst->_nextLockAction = _offCommand; _nextLockAction = _offCommand;
_nukiOfficial->clearOffCommandExecutedTs(); _nukiOfficial->clearOffCommandExecutedTs();
} }
if(_nextLockAction != (NukiLock::LockAction)0xff) if(_nextLockAction != (NukiLock::LockAction)0xff)