本项止转自github官方arduino-esp32 传在这里仅为阅读源码方便
projectuser
2019-07-05 50148ccffe21ff54262064ec9f2245900eaf18aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <Arduino.h>
#include <FunctionalInterrupt.h>
 
#define BUTTON1 16
#define BUTTON2 17
 
class Button
{
public:
    Button(uint8_t reqPin) : PIN(reqPin){
        pinMode(PIN, INPUT_PULLUP);
        attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING);
    };
    ~Button() {
        detachInterrupt(PIN);
    }
 
    void IRAM_ATTR isr() {
        numberKeyPresses += 1;
        pressed = true;
    }
 
    void checkPressed() {
        if (pressed) {
            Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
            pressed = false;
        }
    }
 
private:
    const uint8_t PIN;
    volatile uint32_t numberKeyPresses;
    volatile bool pressed;
};
 
Button button1(BUTTON1);
Button button2(BUTTON2);
 
 
void setup() {
    Serial.begin(115200);
}
 
void loop() {
    button1.checkPressed();
    button2.checkPressed();
}