Skip to content

Commit db67da0

Browse files
committed
Add example
1 parent 040b188 commit db67da0

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <Arduino.h>
2+
#include <FunctionalInterrupt.h>
3+
4+
#define BUTTON1 16
5+
#define BUTTON2 17
6+
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 IRAM_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;
34+
};
35+
36+
Button button1(BUTTON1);
37+
Button button2(BUTTON2);
38+
39+
40+
void setup() {
41+
Serial.begin(115200);
42+
}
43+
44+
void loop() {
45+
button1.checkPressed();
46+
button2.checkPressed();
47+
}

0 commit comments

Comments
 (0)