forked from schreibfaul1/ESP32-audioI2S
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathButton.h
50 lines (42 loc) · 1.04 KB
/
Button.h
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
48
49
50
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
struct Button;
std::vector<Button*> buttonList;
struct Button {
explicit Button(int pin)
: _pin(pin) {
buttonList.push_back(this);
}
void setup() {
pinMode(_pin, INPUT_PULLUP);
}
bool isCommandPending() {
portENTER_CRITICAL_ISR(&timerMux);
bool ret = _commandPending;
_commandPending = false;
portEXIT_CRITICAL_ISR(&timerMux);
return ret;
}
void onTimer() {
if(digitalRead(_pin) == HIGH) {
// button not pressed
_pressed = false;
return;
}
if(_pressed) {
return;
// no repeat for easier control by toddlers...
//++_repeatCount;
//if(_repeatCount < 50) return;
}
_repeatCount = 0;
_pressed = true;
portENTER_CRITICAL_ISR(&timerMux);
_commandPending = true;
portEXIT_CRITICAL_ISR(&timerMux);
}
private:
int _pin;
bool _commandPending{false};
bool _pressed{false};
size_t _repeatCount{0};
};