|
1 | 1 | #include <Arduino.h>
|
2 |
| -#include <FunctionalInterrupt.h> |
3 | 2 |
|
4 | 3 | #define BUTTON1 16
|
5 | 4 | #define BUTTON2 17
|
6 | 5 |
|
7 |
| -class Button |
8 |
| -{ |
9 |
| -public: |
10 |
| - Button(uint8_t reqPin) : PIN(reqPin){ |
11 |
| - pinMode(PIN, INPUT_PULLUP); |
12 |
| - attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING); |
13 |
| - }; |
14 |
| - ~Button() { |
15 |
| - detachInterrupt(PIN); |
16 |
| - } |
17 |
| - |
18 |
| - void ARDUINO_ISR_ATTR isr() { |
19 |
| - numberKeyPresses += 1; |
20 |
| - pressed = true; |
21 |
| - } |
22 |
| - |
23 |
| - void checkPressed() { |
24 |
| - if (pressed) { |
25 |
| - Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses); |
26 |
| - pressed = false; |
27 |
| - } |
28 |
| - } |
29 |
| - |
30 |
| -private: |
31 |
| - const uint8_t PIN; |
32 |
| - volatile uint32_t numberKeyPresses; |
33 |
| - volatile bool pressed; |
| 6 | +struct Button { |
| 7 | + uint8_t PIN; |
| 8 | + volatile uint32_t numberKeyPresses; |
| 9 | + volatile int pressed; |
| 10 | + void (*isr)(struct Button*); |
34 | 11 | };
|
35 | 12 |
|
36 |
| -Button button1(BUTTON1); |
37 |
| -Button button2(BUTTON2); |
| 13 | +void isr(struct Button* button) { |
| 14 | + button->numberKeyPresses += 1; |
| 15 | + button->pressed = 1; |
| 16 | +} |
| 17 | + |
| 18 | +void checkPressed(struct Button* button) { |
| 19 | + if(button->pressed) { |
| 20 | + Serial.printf("Button on pin %u has been pressed %u times\n", button->PIN, button->numberKeyPresses); |
| 21 | + button->pressed = 0; |
| 22 | + } |
| 23 | +} |
38 | 24 |
|
| 25 | +struct Button button1 = {BUTTON1, 0, 0, isr}; |
| 26 | +struct Button button2 = {BUTTON2, 0, 0, isr}; |
39 | 27 |
|
40 | 28 | void setup() {
|
41 |
| - Serial.begin(115200); |
| 29 | + Serial.begin(115200); |
| 30 | + pinMode(button1.PIN, INPUT_PULLUP); |
| 31 | + pinMode(button2.PIN, INPUT_PULLUP); |
| 32 | + attachInterrupt(button1.PIN, &isr, FALLING); |
| 33 | + attachInterrupt(button2.PIN, &isr, FALLING); |
42 | 34 | }
|
43 | 35 |
|
44 | 36 | void loop() {
|
45 |
| - button1.checkPressed(); |
46 |
| - button2.checkPressed(); |
| 37 | + checkPressed(&button1); |
| 38 | + checkPressed(&button2); |
47 | 39 | }
|
0 commit comments