Skip to content

Commit 3d8348c

Browse files
committed
Changed example from C++ class to C struct
1 parent 714e919 commit 3d8348c

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed
Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,39 @@
11
#include <Arduino.h>
2-
#include <FunctionalInterrupt.h>
32

43
#define BUTTON1 16
54
#define BUTTON2 17
65

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*);
3411
};
3512

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+
}
3824

25+
struct Button button1 = {BUTTON1, 0, 0, isr};
26+
struct Button button2 = {BUTTON2, 0, 0, isr};
3927

4028
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);
4234
}
4335

4436
void loop() {
45-
button1.checkPressed();
46-
button2.checkPressed();
37+
checkPressed(&button1);
38+
checkPressed(&button2);
4739
}

0 commit comments

Comments
 (0)