From 26a3a757f167b7de920ffe4f3563c40e88e9d3eb Mon Sep 17 00:00:00 2001 From: uvok Date: Sat, 19 Jul 2025 10:50:03 +0200 Subject: gpio: Re-attempt debouncing --- src/gpio.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/gpio.cpp b/src/gpio.cpp index a7927a8..84dc545 100644 --- a/src/gpio.cpp +++ b/src/gpio.cpp @@ -34,9 +34,10 @@ static unsigned long releasedTime = 0; typedef struct { - uint32_t last_change; + // uint32_t last_change; uint32_t last_level; uint32_t pressed; + uint32_t released; } InputInfo_t; static volatile InputInfo_t inputs[NUM_DIGITAL_PINS]; @@ -107,22 +108,29 @@ static void gpio_loop(void *ctx) const bool pressed = !received.state; const uint8_t pin = received.pin; LOG_F("(%u) Pin %u was %s\n", received.timestamp, pin, pressed ? "pressed" : "released"); + LOG_F(" pressed: %u, released: %u\n", inputs[pin].pressed, inputs[pin].released); } } } -static ARDUINO_ISR_ATTR void gpio_pin_irq(void *arg) +static ARDUINO_ISR_ATTR IRAM_ATTR void gpio_pin_irq(void *arg) { - uint8_t pin_no = (uint8_t)(ptrdiff_t)arg; + const uint8_t pin_no = (uint8_t)(ptrdiff_t)arg; + const int pinLevel = digitalRead(pin_no); + uint32_t now = millis(); + if (!pinLevel) + { + inputs[pin_no].pressed = now; + return; + } + inputs[pin_no].released = now; + // Elecrow inputs - the selector - are very dirty... // const bool is_dirty = now - inputs[pin_no].last_change < 50; - // inputs[pin_no].last_change = now; // if (is_dirty) // return; - int pinLevel = digitalRead(pin_no); - pin_notification_t send = {.timestamp = now, .pin = pin_no, .state = (pinLevel == HIGH)}; BaseType_t wakeup = false; xQueueSendFromISR(gpio_task_stuff.queue_handle, &send, &wakeup); -- cgit v1.2.3