|
13 | 13 | #endif
|
14 | 14 |
|
15 | 15 | 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 | + } |
30 | 33 |
|
31 | 34 | #if defined(ESP8266)
|
32 |
| - void ICACHE_RAM_ATTR isr() |
| 35 | + void ICACHE_RAM_ATTR isr() |
33 | 36 | #elif defined(ESP32)
|
34 |
| - void IRAM_ATTR isr() |
| 37 | + void IRAM_ATTR isr() |
35 | 38 | #endif
|
36 |
| - { |
37 |
| - numberKeyPresses += 1; |
38 |
| - pressed = true; |
39 |
| - } |
| 39 | + { |
| 40 | + numberKeyPresses += 1; |
| 41 | + pressed = true; |
| 42 | + } |
40 | 43 |
|
41 | 44 | #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) |
43 | 46 | #elif defined(ESP32)
|
44 |
| - static void IRAM_ATTR isr_static(Button* const self) |
| 47 | + static void IRAM_ATTR isr_static(Button* const self) |
45 | 48 | #endif
|
46 |
| - { |
47 |
| - self->isr(); |
48 |
| - } |
| 49 | + { |
| 50 | + self->isr(); |
| 51 | + } |
49 | 52 |
|
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 | + } |
56 | 59 |
|
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; |
61 | 64 | };
|
62 | 65 |
|
63 | 66 | Button* button1;
|
64 | 67 | Button* button2;
|
65 | 68 |
|
66 | 69 |
|
67 | 70 | void setup() {
|
68 |
| - Serial.begin(115200); |
69 |
| - Serial.println("FunctionalInterrupt test/example"); |
| 71 | + Serial.begin(115200); |
| 72 | + Serial.println("FunctionalInterrupt test/example"); |
70 | 73 |
|
71 |
| - button1 = new Button(BUTTON1); |
72 |
| - button2 = new Button(BUTTON2); |
| 74 | + button1 = new Button(BUTTON1); |
| 75 | + button2 = new Button(BUTTON2); |
73 | 76 |
|
74 |
| - Serial.println("setup() complete"); |
| 77 | + Serial.println("setup() complete"); |
75 | 78 | }
|
76 | 79 |
|
77 | 80 | void loop() {
|
78 |
| - button1->checkPressed(); |
79 |
| - button2->checkPressed(); |
| 81 | + button1->checkPressed(); |
| 82 | + button2->checkPressed(); |
80 | 83 | }
|
0 commit comments