From c77aed4ac4f343fb6e7e7b9bbaa36b3495eda4cc Mon Sep 17 00:00:00 2001 From: Bert Melis Date: Sat, 7 Jul 2018 11:26:58 +0200 Subject: [PATCH] Allow using argument with attachInterrupt (#1535) * Allow using argument with attachInterrupt * formatting replace tabs with spaces * fix bug more then 1 interrupt * leftover * add example * make attachInterruptArg public * update example * leftover --- cores/esp32/esp32-hal-gpio.c | 38 ++++++++++++---- cores/esp32/esp32-hal-gpio.h | 1 + .../examples/GPIOInterrupt/GPIOInterrupt.ino | 45 +++++++++++++++++++ 3 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 libraries/ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino diff --git a/cores/esp32/esp32-hal-gpio.c b/cores/esp32/esp32-hal-gpio.c index 7c5e02bd..795dba2b 100644 --- a/cores/esp32/esp32-hal-gpio.c +++ b/cores/esp32/esp32-hal-gpio.c @@ -70,7 +70,12 @@ const DRAM_ATTR esp32_gpioMux_t esp32_gpioMux[GPIO_PIN_COUNT]={ }; typedef void (*voidFuncPtr)(void); -static voidFuncPtr __pinInterruptHandlers[GPIO_PIN_COUNT] = {0,}; +typedef void (*voidFuncPtrArg)(void*); +typedef struct { + voidFuncPtr fn; + void* arg; +} InterruptHandle_t; +static InterruptHandle_t __pinInterruptHandlers[GPIO_PIN_COUNT] = {0,}; #include "driver/rtc_io.h" @@ -193,7 +198,7 @@ extern int IRAM_ATTR __digitalRead(uint8_t pin) static intr_handle_t gpio_intr_handle = NULL; -static void IRAM_ATTR __onPinInterrupt(void *arg) +static void IRAM_ATTR __onPinInterrupt() { uint32_t gpio_intr_status_l=0; uint32_t gpio_intr_status_h=0; @@ -207,8 +212,12 @@ static void IRAM_ATTR __onPinInterrupt(void *arg) if(gpio_intr_status_l) { do { if(gpio_intr_status_l & ((uint32_t)1 << pin)) { - if(__pinInterruptHandlers[pin]) { - __pinInterruptHandlers[pin](); + if(__pinInterruptHandlers[pin].fn) { + if(__pinInterruptHandlers[pin].arg){ + ((voidFuncPtrArg)__pinInterruptHandlers[pin].fn)(__pinInterruptHandlers[pin].arg); + } else { + __pinInterruptHandlers[pin].fn(); + } } } } while(++pin<32); @@ -217,15 +226,19 @@ static void IRAM_ATTR __onPinInterrupt(void *arg) pin=32; do { if(gpio_intr_status_h & ((uint32_t)1 << (pin - 32))) { - if(__pinInterruptHandlers[pin]) { - __pinInterruptHandlers[pin](); + if(__pinInterruptHandlers[pin].fn) { + if(__pinInterruptHandlers[pin].arg){ + ((voidFuncPtrArg)__pinInterruptHandlers[pin].fn)(__pinInterruptHandlers[pin].arg); + } else { + __pinInterruptHandlers[pin].fn(); + } } } } while(++pin + +struct Button { + const uint8_t PIN; + uint32_t numberKeyPresses; + bool pressed; +}; + +Button button1 = {23, 0, false}; +Button button2 = {18, 0, false}; + +void IRAM_ATTR isr(void* arg) { + Button* s = static_cast(arg); + s->numberKeyPresses += 1; + s->pressed = true; +} + +void IRAM_ATTR isr() { + button2.numberKeyPresses += 1; + button2.pressed = true; +} + +void setup() { + Serial.begin(115200); + pinMode(button1.PIN, INPUT_PULLUP); + attachInterruptArg(button1.PIN, isr, &button1, FALLING); + pinMode(button2.PIN, INPUT_PULLUP); + attachInterrupt(button2.PIN, isr, FALLING); +} + +void loop() { + if (button1.pressed) { + Serial.printf("Button 1 has been pressed %u times\n", button1.numberKeyPresses); + button1.pressed = false; + } + if (button2.pressed) { + Serial.printf("Button 2 has been pressed %u times\n", button2.numberKeyPresses); + button2.pressed = false; + } + static uint32_t lastMillis = 0; + if (millis() - lastMillis > 10000) { + lastMillis = millis(); + detachInterrupt(button1.PIN); + } +}