From b5662a6e820845dad42147e0be18718a10f47581 Mon Sep 17 00:00:00 2001 From: uvok Date: Sat, 19 Jul 2025 10:23:53 +0200 Subject: gpio: Change queue type, use digitalPinToInterrupt macro, logging --- src/gpio.cpp | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'src/gpio.cpp') diff --git a/src/gpio.cpp b/src/gpio.cpp index e76368a..a7927a8 100644 --- a/src/gpio.cpp +++ b/src/gpio.cpp @@ -1,6 +1,7 @@ #include "badge/gpio.h" #include "badge/config.h" +#include "badge/log.h" #include @@ -17,10 +18,17 @@ static void gpio_loop(void *); -typedef uint32_t pin_notification_t; +typedef struct +{ + uint32_t timestamp; + uint32_t pin : 8; + uint32_t state : 1; +} pin_notification_t; + +_Static_assert(sizeof(uint32_t) >= sizeof(unsigned long), "return type of millis"); #define GPIO_STACK 2048 -#define GPIO_QUEUE_LEN 4 +#define GPIO_QUEUE_LEN 32 static unsigned long pressedTime = 0; static unsigned long releasedTime = 0; @@ -58,7 +66,7 @@ void de::uvok::badge::gpio_init(void) { pinMode(p, GPIO_MODE_INPUT); // simply use pin number as arg - attachInterruptArg(p, gpio_pin_irq, (void *)(ptrdiff_t)p, CHANGE); + attachInterruptArg(digitalPinToInterrupt(p), gpio_pin_irq, (void *)(ptrdiff_t)p, CHANGE); inputs[p].last_level = digitalRead(p); } #endif @@ -96,10 +104,9 @@ static void gpio_loop(void *ctx) pin_notification_t received; if (xQueueReceive(gpio_task_stuff.queue_handle, &received, portMAX_DELAY) == pdTRUE) { - const int level = (received & 0x100) ? HIGH : LOW; - const bool pressed = !level; - const int pin = received & 0xff; - Serial.printf("Pin %d was %s\n", pin, pressed ? "pressed" : "released"); + 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"); } } } @@ -109,14 +116,14 @@ static ARDUINO_ISR_ATTR void gpio_pin_irq(void *arg) uint8_t pin_no = (uint8_t)(ptrdiff_t)arg; uint32_t now = millis(); // Elecrow inputs - the selector - are very dirty... - const bool is_dirty = now - inputs[pin_no].last_change < 100; - inputs[pin_no].last_change = now; - if (is_dirty) - return; + // 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 = (pinLevel << 8) | (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); if (wakeup) -- cgit v1.2.3