Skip to content

Commit ae9a054

Browse files
committed
Updated examples
1 parent 0039f23 commit ae9a054

File tree

2 files changed

+89
-27
lines changed

2 files changed

+89
-27
lines changed
Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include <FunctionalInterrupt.h>
2-
#include <Arduino.h>
2+
3+
#ifndef IRAM_ATTR
4+
#define IRAM_ATTR ICACHE_RAM_ATTR
5+
#endif
36

47
#if defined(ESP32)
58
#define BUTTON1 16
@@ -16,46 +19,27 @@ class Button {
1619
public:
1720
Button(uint8_t reqPin) : PIN(reqPin) {
1821
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) {
26-
Serial.print("Pin ");
27-
Serial.println(ii.pin);
28-
isr();
29-
}, FALLING); // works on ESP32; works on ESP8266
22+
attachInterrupt(PIN, std::bind(&Button::buttonIsr, this), FALLING);
3023
};
3124
~Button() {
3225
detachInterrupt(PIN);
3326
}
3427

35-
#if defined(ESP8266)
36-
void ICACHE_RAM_ATTR isr()
37-
#elif defined(ESP32)
38-
void IRAM_ATTR isr()
39-
#endif
40-
{
28+
void IRAM_ATTR buttonIsr() {
4129
numberKeyPresses += 1;
4230
pressed = true;
4331
}
4432

45-
#if defined(ESP8266)
46-
static void ICACHE_RAM_ATTR isr_static(Button* const self)
47-
#elif defined(ESP32)
48-
static void IRAM_ATTR isr_static(Button* const self)
49-
#endif
50-
{
51-
self->isr();
33+
static void IRAM_ATTR buttonIsr_static(Button* const self) {
34+
self->buttonIsr();
5235
}
5336

54-
void checkPressed() {
37+
uint32_t checkPressed() {
5538
if (pressed) {
5639
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
5740
pressed = false;
5841
}
42+
return numberKeyPresses;
5943
}
6044

6145
private:
@@ -80,5 +64,8 @@ void setup() {
8064

8165
void loop() {
8266
button1->checkPressed();
83-
button2->checkPressed();
67+
if (nullptr != button2 && 10 < button2->checkPressed()) {
68+
delete button2;
69+
button2 = nullptr;
70+
}
8471
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <FunctionalInterrupt.h>
2+
3+
#ifndef IRAM_ATTR
4+
#define IRAM_ATTR ICACHE_RAM_ATTR
5+
#endif
6+
7+
#if defined(ESP32)
8+
#define BUTTON1 16
9+
#define BUTTON2 17
10+
#elif defined(ARDUINO_ESP8266_WEMOS_D1MINI)
11+
#define BUTTON1 D4
12+
#define BUTTON2 D3
13+
#else
14+
#define BUTTON1 2
15+
#define BUTTON2 0
16+
#endif
17+
18+
class Button {
19+
public:
20+
Button(uint8_t reqPin) : PIN(reqPin) {
21+
pinMode(PIN, INPUT_PULLUP);
22+
attachScheduledInterrupt(PIN, [this](const InterruptInfo & ii) {
23+
Serial.print("Pin ");
24+
Serial.println(ii.pin);
25+
buttonIsr();
26+
}, FALLING); // works on ESP8266
27+
};
28+
~Button() {
29+
detachInterrupt(PIN);
30+
}
31+
32+
void IRAM_ATTR buttonIsr() {
33+
numberKeyPresses += 1;
34+
pressed = true;
35+
}
36+
37+
static void IRAM_ATTR buttonIsr_static(Button* const self) {
38+
self->buttonIsr();
39+
}
40+
41+
uint32_t checkPressed() {
42+
if (pressed) {
43+
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
44+
pressed = false;
45+
}
46+
return numberKeyPresses;
47+
}
48+
49+
private:
50+
const uint8_t PIN;
51+
volatile uint32_t numberKeyPresses = 0;
52+
volatile bool pressed = false;
53+
};
54+
55+
Button* button1;
56+
Button* button2;
57+
58+
59+
void setup() {
60+
Serial.begin(115200);
61+
Serial.println("FunctionalInterrupt test/example");
62+
63+
button1 = new Button(BUTTON1);
64+
button2 = new Button(BUTTON2);
65+
66+
Serial.println("setup() complete");
67+
}
68+
69+
void loop() {
70+
button1->checkPressed();
71+
if (nullptr != button2 && 10 < button2->checkPressed()) {
72+
delete button2;
73+
button2 = nullptr;
74+
}
75+
}

0 commit comments

Comments
 (0)