Skip to content

Commit 1e26d5d

Browse files
committed
Proper detach for scheduled ISR example
1 parent b91fe87 commit 1e26d5d

File tree

1 file changed

+45
-42
lines changed

1 file changed

+45
-42
lines changed

libraries/FunctionalInterrupt/examples/Functional/Functional.ino

+45-42
Original file line numberDiff line numberDiff line change
@@ -13,68 +13,71 @@
1313
#endif
1414

1515
class Button {
16-
public:
17-
Button(uint8_t reqPin) : PIN(reqPin) {
18-
pinMode(PIN, INPUT_PULLUP);
19-
// Arduino C API:
20-
//attachInterruptArg(PIN, [](void* self) {
21-
// static_cast<Button*>(self)->isr();
22-
//}, this, FALLING); // works on ESP32; fails on ESP8266: "ISR not in IRAM"
23-
//attachInterruptArg(PIN, reinterpret_cast<void(*)(void*)>(&isr_static), this, FALLING); // works on ESP32; works on ESP8266
24-
// FunctionalInterrupts API:
25-
attachScheduledInterrupt(PIN, [this](InterruptInfo ii) { Serial.print("Pin "); Serial.println(ii.pin); isr(); }, FALLING); // works on ESP32; works on ESP8266
26-
};
27-
~Button() {
28-
detachInterrupt(PIN);
29-
}
16+
public:
17+
Button(uint8_t reqPin) : PIN(reqPin) {
18+
pinMode(PIN, INPUT_PULLUP);
19+
// Arduino C API:
20+
//attachInterruptArg(PIN, [](void* self) {
21+
// static_cast<Button*>(self)->isr();
22+
//}, this, FALLING); // works on ESP32; fails on ESP8266: "ISR not in IRAM"
23+
//attachInterruptArg(PIN, reinterpret_cast<void(*)(void*)>(&isr_static), this, FALLING); // works on ESP32; works on ESP8266
24+
// FunctionalInterrupts API:
25+
attachScheduledInterrupt(PIN, [this](InterruptInfo ii) { Serial.print("Pin "); Serial.println(ii.pin); isr(); }, FALLING); // works on ESP32; works on ESP8266
26+
};
27+
~Button() {
28+
// Arduino C API:
29+
//detachInterrupt(PIN);
30+
// FunctionalInterrupt API:
31+
detachFunctionalInterrupt(PIN);
32+
}
3033

3134
#if defined(ESP8266)
32-
void ICACHE_RAM_ATTR isr()
35+
void ICACHE_RAM_ATTR isr()
3336
#elif defined(ESP32)
34-
void IRAM_ATTR isr()
37+
void IRAM_ATTR isr()
3538
#endif
36-
{
37-
numberKeyPresses += 1;
38-
pressed = true;
39-
}
39+
{
40+
numberKeyPresses += 1;
41+
pressed = true;
42+
}
4043

4144
#if defined(ESP8266)
42-
static void ICACHE_RAM_ATTR isr_static(Button* const self)
45+
static void ICACHE_RAM_ATTR isr_static(Button* const self)
4346
#elif defined(ESP32)
44-
static void IRAM_ATTR isr_static(Button* const self)
47+
static void IRAM_ATTR isr_static(Button* const self)
4548
#endif
46-
{
47-
self->isr();
48-
}
49+
{
50+
self->isr();
51+
}
4952

50-
void checkPressed() {
51-
if (pressed) {
52-
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
53-
pressed = false;
54-
}
55-
}
53+
void checkPressed() {
54+
if (pressed) {
55+
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
56+
pressed = false;
57+
}
58+
}
5659

57-
private:
58-
const uint8_t PIN;
59-
volatile uint32_t numberKeyPresses = 0;
60-
volatile bool pressed = false;
60+
private:
61+
const uint8_t PIN;
62+
volatile uint32_t numberKeyPresses = 0;
63+
volatile bool pressed = false;
6164
};
6265

6366
Button* button1;
6467
Button* button2;
6568

6669

6770
void setup() {
68-
Serial.begin(115200);
69-
Serial.println("FunctionalInterrupt test/example");
71+
Serial.begin(115200);
72+
Serial.println("FunctionalInterrupt test/example");
7073

71-
button1 = new Button(BUTTON1);
72-
button2 = new Button(BUTTON2);
74+
button1 = new Button(BUTTON1);
75+
button2 = new Button(BUTTON2);
7376

74-
Serial.println("setup() complete");
77+
Serial.println("setup() complete");
7578
}
7679

7780
void loop() {
78-
button1->checkPressed();
79-
button2->checkPressed();
81+
button1->checkPressed();
82+
button2->checkPressed();
8083
}

0 commit comments

Comments
 (0)