Skip to content

Commit 77609de

Browse files
committed
Split up example into standalone scheduled interrupt sketch.
1 parent 9d20ef5 commit 77609de

File tree

2 files changed

+79
-12
lines changed

2 files changed

+79
-12
lines changed

libraries/FunctionalInterrupt/examples/Functional/Functional.ino

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,8 @@ class Button {
1919
public:
2020
Button(uint8_t reqPin) : PIN(reqPin) {
2121
pinMode(PIN, INPUT_PULLUP);
22-
// Arduino API:
23-
//attachInterruptArg(PIN, [](void* self) {
24-
// static_cast<Button*>(self)->buttonIsr();
25-
//}, this, FALLING); // fails on ESP8266: "buttonIsr not in IRAM"
26-
//attachInterruptArg(PIN, reinterpret_cast<void(*)(void*)>(&buttonIsr_static), this, FALLING); // works on ESP8266
27-
attachInterrupt(PIN, std::bind(&Button::buttonIsr, this), FALLING); // works on ESP8266
28-
// FunctionalInterrupt.h API:
29-
//attachScheduledInterrupt(PIN, [this](const InterruptInfo & ii) {
30-
// Serial.print("Pin ");
31-
// Serial.println(ii.pin);
32-
// buttonIsr();
33-
//}, FALLING); // works on ESP8266
22+
//attachInterruptArg(PIN, reinterpret_cast<void(*)(void*)>(&buttonIsr_static), this, FALLING);
23+
attachInterrupt(PIN, std::bind(&Button::buttonIsr, this), FALLING);
3424
};
3525
~Button() {
3626
detachInterrupt(PIN);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
{
34+
numberKeyPresses += 1;
35+
pressed = true;
36+
}
37+
38+
static void IRAM_ATTR buttonIsr_static(Button* const self)
39+
{
40+
self->buttonIsr();
41+
}
42+
43+
uint32_t checkPressed() {
44+
if (pressed) {
45+
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
46+
pressed = false;
47+
}
48+
return numberKeyPresses;
49+
}
50+
51+
private:
52+
const uint8_t PIN;
53+
volatile uint32_t numberKeyPresses = 0;
54+
volatile bool pressed = false;
55+
};
56+
57+
Button* button1;
58+
Button* button2;
59+
60+
61+
void setup() {
62+
Serial.begin(115200);
63+
Serial.println("FunctionalInterrupt test/example");
64+
65+
button1 = new Button(BUTTON1);
66+
button2 = new Button(BUTTON2);
67+
68+
Serial.println("setup() complete");
69+
}
70+
71+
void loop() {
72+
button1->checkPressed();
73+
if (nullptr != button2 && 10 < button2->checkPressed()) {
74+
delete button2;
75+
button2 = nullptr;
76+
}
77+
}

0 commit comments

Comments
 (0)