File tree Expand file tree Collapse file tree 1 file changed +30
-31
lines changed
libraries/ESP32/examples/GPIO/FunctionalInterrupt Expand file tree Collapse file tree 1 file changed +30
-31
lines changed Original file line number Diff line number Diff line change 1212#define BUTTON2 0
1313#endif
1414
15- class Button
16- {
17- public:
18- Button (uint8_t reqPin) : PIN(reqPin) {
19- pinMode (PIN, INPUT_PULLUP);
20- attachInterrupt (PIN, std::bind (&Button::isr,this ), FALLING);
21- };
22- ~Button () {
23- detachFunctionalInterrupt (PIN);
24- }
15+ class Button {
16+ public:
17+ Button (uint8_t reqPin) : PIN(reqPin) {
18+ pinMode (PIN, INPUT_PULLUP);
19+ attachInterrupt (PIN, std::bind (&Button::isr, this ), FALLING);
20+ };
21+ ~Button () {
22+ detachFunctionalInterrupt (PIN);
23+ }
2524
2625#if defined(ESP8266)
27- void ICACHE_RAM_ATTR isr ()
26+ void ICACHE_RAM_ATTR isr ()
2827#elif defined(ESP32)
29- void IRAM_ATTR isr ()
28+ void IRAM_ATTR isr ()
3029#endif
31- {
32- numberKeyPresses += 1 ;
33- pressed = true ;
34- }
35-
36- void checkPressed () {
37- if (pressed) {
38- Serial.printf (" Button on pin %u has been pressed %u times\n " , PIN, numberKeyPresses);
39- pressed = false ;
40- }
41- }
42-
43- private:
44- const uint8_t PIN;
45- volatile uint32_t numberKeyPresses;
46- volatile bool pressed;
30+ {
31+ numberKeyPresses += 1 ;
32+ pressed = true ;
33+ }
34+
35+ void checkPressed () {
36+ if (pressed) {
37+ Serial.printf (" Button on pin %u has been pressed %u times\n " , PIN, numberKeyPresses);
38+ pressed = false ;
39+ }
40+ }
41+
42+ private:
43+ const uint8_t PIN;
44+ volatile uint32_t numberKeyPresses;
45+ volatile bool pressed;
4746};
4847
4948Button button1 (BUTTON1);
5049Button button2 (BUTTON2);
5150
5251
5352void setup () {
54- Serial.begin (115200 );
53+ Serial.begin (115200 );
5554}
5655
5756void loop () {
58- button1.checkPressed ();
59- button2.checkPressed ();
57+ button1.checkPressed ();
58+ button2.checkPressed ();
6059}
You can’t perform that action at this time.
0 commit comments