-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathButtonManager.h
97 lines (77 loc) · 2.37 KB
/
ButtonManager.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef BUTTON_HELPER_H
#define BUTTON_HELPER_H
#include <ArduinoLog.h>
// https://github.com/thijse/Arduino-Log/blob/master/examples/Log/Log.ino
#include <JC_Button.h>
class ButtonManager {
public:
ButtonManager(uint32_t longPressDelay, uint8_t btDownPin, uint8_t btPlayPin, uint8_t btUpPin) :
LONG_PRESS(longPressDelay),
downButton(btDownPin),
playButton(btPlayPin),
upButton(btUpPin) {
pinMode(btPlayPin, INPUT_PULLUP);
pinMode(btUpPin, INPUT_PULLUP);
pinMode(btDownPin, INPUT_PULLUP);
}
void readAllButtons() {
playButton.read();
upButton.read();
downButton.read();
}
/**
* Reset button delay for long push
*/
void resetButtonDelayForLongPress() {
if (!downButton.isPressed() && !upButton.isPressed() && !playButton.isPressed()){
buttonDelayFactor = 1;
}
}
void waitForButtonToBeReleased(Button b) {
do { b.read(); }
while (!b.wasReleased());
}
void restartOption() {
if (upButton.pressedFor(LONG_PRESS) && downButton.pressedFor(LONG_PRESS)) {
Log.notice(F("Restart Arduino!" CR));
asm volatile (" jmp 0");
}
}
bool isPlayButtonPressedForLong() {
return playButton.pressedFor(LONG_PRESS);
}
void waitForPlayButtonToBeReleased() {
waitForButtonToBeReleased(playButton);
}
bool wasButtonReleased(Button b) {
return buttonDelayFactor == 1 && b.wasReleased();
}
bool wasPlayButtonReleased() {
return wasButtonReleased(playButton);
}
bool isButtonPressedForLong(Button btn) {
bool result = btn.pressedFor(LONG_PRESS * buttonDelayFactor);
if (result) buttonDelayFactor++;
return result;
}
bool isUpButtonPressedForLong() {
return isButtonPressedForLong(upButton);
}
bool wasUpButtonReleased() {
return wasButtonReleased(upButton);
}
bool isDownButtonPressedForLong() {
return isButtonPressedForLong(downButton);
}
bool wasDownButtonReleased() {
return wasButtonReleased(downButton);
}
private:
Button playButton;
Button upButton;
Button downButton;
uint32_t LONG_PRESS;
// if button is press it will increasing or decreasing the volume in steps by a delay of LONG_PRESS
uint8_t buttonDelayFactor = 1;
};
#endif