forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFunctional.ino
81 lines (68 loc) · 1.85 KB
/
Functional.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <FunctionalInterrupt.h>
#include <Arduino.h>
#ifndef IRAM_ATTR
#define IRAM_ATTR ICACHE_RAM_ATTR
#endif
#if defined(ESP32)
#define BUTTON1 16
#define BUTTON2 17
#elif defined(ARDUINO_ESP8266_WEMOS_D1MINI)
#define BUTTON1 D4
#define BUTTON2 D3
#else
#define BUTTON1 2
#define BUTTON2 0
#endif
class Button {
public:
Button(uint8_t reqPin) : PIN(reqPin) {
pinMode(PIN, INPUT_PULLUP);
// 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:
attachScheduledInterrupt(PIN, [this](InterruptInfo ii) {
Serial.print("Pin ");
Serial.println(ii.pin);
isr();
}, FALLING); // works on ESP32; works on ESP8266
};
~Button() {
// Arduino C API:
//detachInterrupt(PIN);
// FunctionalInterrupt API:
detachFunctionalInterrupt(PIN);
}
void IRAM_ATTR isr() {
numberKeyPresses += 1;
pressed = true;
}
static void IRAM_ATTR isr_static(Button* const self) {
self->isr();
}
void checkPressed() {
if (pressed) {
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
pressed = false;
}
}
private:
const uint8_t PIN;
volatile uint32_t numberKeyPresses = 0;
volatile bool pressed = false;
};
Button* button1;
Button* button2;
void setup() {
Serial.begin(115200);
Serial.println("FunctionalInterrupt test/example");
button1 = new Button(BUTTON1);
button2 = new Button(BUTTON2);
Serial.println("setup() complete");
}
void loop() {
button1->checkPressed();
button2->checkPressed();
}