Skip to content

Commit

Permalink
Modified example to showcase how general FunctionalInterrupt uses vio…
Browse files Browse the repository at this point in the history
…late the ISR in

ICACHE_RAM rule.
  • Loading branch information
dok-net committed May 3, 2019
1 parent 9e239ec commit f89b22c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Arduino.h>
#include <Schedule.h>
#include "FunctionalInterrupts.h"

#if defined(ESP32)
Expand All @@ -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<Button*>(self)->isr(); }, this, FALLING); // works on ESP32; fails on ESP8266: "ISR not in IRAM"
//attachInterruptArg(PIN, reinterpret_cast<void(*)(void*)>(&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)
Expand All @@ -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);
Expand All @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ void attachInterrupt(uint8_t pin, std::function<void(void)> 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;
Expand All @@ -50,10 +47,7 @@ void attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode
void attachScheduledInterrupt(uint8_t pin, std::function<void(InterruptInfo)> scheduledIntRoutine, int mode)
{
void* localArg = detachInterruptArg(pin);
if (localArg)
{
cleanupFunctional(localArg);
}
if (localArg) cleanupFunctional(localArg);

InterruptInfo* ii = new InterruptInfo(pin);

Expand All @@ -70,8 +64,5 @@ void attachScheduledInterrupt(uint8_t pin, std::function<void(InterruptInfo)> sc
void detachFunctionalInterrupt(uint8_t pin)
{
void* localArg = detachInterruptArg(pin);
if (localArg)
{
cleanupFunctional(localArg);
}
if (localArg) cleanupFunctional(localArg);
}

0 comments on commit f89b22c

Please sign in to comment.