diff --git a/libraries/esp8266/examples/GPIO/FunctionalInterrupt/FunctionalInterrupt.ino b/libraries/esp8266/examples/GPIO/FunctionalInterrupt/FunctionalInterrupt.ino index b386b1d83f..aae06082eb 100644 --- a/libraries/esp8266/examples/GPIO/FunctionalInterrupt/FunctionalInterrupt.ino +++ b/libraries/esp8266/examples/GPIO/FunctionalInterrupt/FunctionalInterrupt.ino @@ -1,4 +1,5 @@ #include +#include #include "FunctionalInterrupts.h" #if defined(ESP32) @@ -16,10 +17,15 @@ class Button { public: Button(uint8_t reqPin) : PIN(reqPin) { pinMode(PIN, INPUT_PULLUP); - attachInterrupt(PIN, std::bind(&Button::isr, this), FALLING); + // Arduino C API: + attachInterruptArg(PIN, [](void* self) { static_cast(self)->isr(); }, this, FALLING); // works on ESP32; fails on ESP8266: "ISR not in IRAM" + //attachInterruptArg(PIN, reinterpret_cast(&isr_static), this, FALLING); // works on ESP32; works on ESP8266 + // FunctionalInterrupts API: + //attachInterrupt(PIN, [this]() { isr(); }, FALLING); // works on ESP32; works on ESP8266 + //attachScheduledInterrupt(PIN, [this](InterruptInfo ii) { Serial.print("Pin "); Serial.println(ii.pin); isr(); }, FALLING); // works on ESP32; works on ESP8266 }; ~Button() { - detachFunctionalInterrupt(PIN); + detachInterrupt(PIN); } #if defined(ESP8266) @@ -32,6 +38,15 @@ class Button { pressed = true; } +#if defined(ESP8266) + static void ICACHE_RAM_ATTR isr_static(Button* const self) +#elif defined(ESP32) + static void IRAM_ATTR isr_static(Button* const self) +#endif + { + self->isr(); + } + void checkPressed() { if (pressed) { Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses); @@ -41,19 +56,26 @@ class Button { private: const uint8_t PIN; - volatile uint32_t numberKeyPresses; - volatile bool pressed; + volatile uint32_t numberKeyPresses = 0; + volatile bool pressed = false; }; -Button button1(BUTTON1); -Button button2(BUTTON2); +Button* button1; +Button* button2; void setup() { Serial.begin(115200); + schedule_function([]() { Serial.println("Scheduled function"); }); + Serial.println("FunctionalInterrupt test/example"); + + button1 = new Button(BUTTON1); + button2 = new Button(BUTTON2); + + Serial.println("setup() complete"); } void loop() { - button1.checkPressed(); - button2.checkPressed(); + button1->checkPressed(); + button2->checkPressed(); } diff --git a/libraries/esp8266/examples/GPIO/FunctionalInterrupt/FunctionalInterrupts.cpp b/libraries/esp8266/examples/GPIO/FunctionalInterrupt/FunctionalInterrupts.cpp index be29554bc2..1867e7bd92 100644 --- a/libraries/esp8266/examples/GPIO/FunctionalInterrupt/FunctionalInterrupts.cpp +++ b/libraries/esp8266/examples/GPIO/FunctionalInterrupt/FunctionalInterrupts.cpp @@ -33,10 +33,7 @@ void attachInterrupt(uint8_t pin, std::function intRoutine, int mode // use the local interrupt routine which takes the ArgStructure as argument void* localArg = detachInterruptArg(pin); - if (localArg) - { - cleanupFunctional(localArg); - } + if (localArg) cleanupFunctional(localArg); FunctionInfo* fi = new FunctionInfo; fi->reqFunction = intRoutine; @@ -50,10 +47,7 @@ void attachInterrupt(uint8_t pin, std::function intRoutine, int mode void attachScheduledInterrupt(uint8_t pin, std::function scheduledIntRoutine, int mode) { void* localArg = detachInterruptArg(pin); - if (localArg) - { - cleanupFunctional(localArg); - } + if (localArg) cleanupFunctional(localArg); InterruptInfo* ii = new InterruptInfo(pin); @@ -70,8 +64,5 @@ void attachScheduledInterrupt(uint8_t pin, std::function sc void detachFunctionalInterrupt(uint8_t pin) { void* localArg = detachInterruptArg(pin); - if (localArg) - { - cleanupFunctional(localArg); - } + if (localArg) cleanupFunctional(localArg); }