forked from tonuino/TonUINO-TNG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttons.cpp
183 lines (159 loc) · 4.89 KB
/
buttons.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "buttons.hpp"
#include "constants.hpp"
#include "logger.hpp"
namespace {
constexpr bool buttonPinIsActiveLow = (buttonPinType == levelType::activeLow);
}
Buttons::Buttons()
: CommandSource()
// pin dbTime puEnable invert
, buttonPause(buttonPausePin, buttonDbTime, buttonPinIsActiveLow, buttonPinIsActiveLow)
, buttonUp (buttonUpPin , buttonDbTime, buttonPinIsActiveLow, buttonPinIsActiveLow)
, buttonDown (buttonDownPin , buttonDbTime, buttonPinIsActiveLow, buttonPinIsActiveLow)
#ifdef FIVEBUTTONS
, buttonFour (buttonFourPin , buttonDbTime, buttonPinIsActiveLow, buttonPinIsActiveLow)
, buttonFive (buttonFivePin , buttonDbTime, buttonPinIsActiveLow, buttonPinIsActiveLow)
#endif
{
buttonPause.begin();
buttonUp .begin();
buttonDown .begin();
#ifdef FIVEBUTTONS
buttonFour .begin();
buttonFive .begin();
#endif
}
commandRaw Buttons::getCommandRaw() {
commandRaw ret = commandRaw::none;
readButtons();
if ((ignoreRelease || ignoreAll) && isNoButton()) {
ignoreAll = false;
ignoreRelease = false;
longPressFactor = 0;
return commandRaw::none;
}
if (ignoreAll) {
return commandRaw::none;
}
if (( buttonPause.pressedFor(buttonLongPress)
|| buttonUp .pressedFor(buttonLongPress)
|| buttonDown .pressedFor(buttonLongPress)
)
&& buttonPause.isPressed()
&& buttonUp .isPressed()
&& buttonDown .isPressed()) {
ret = commandRaw::allLong;
ignoreAll = true;
}
#ifdef FIVEBUTTONS
else if (( buttonPause.pressedFor(buttonLongPress)
|| buttonFour .pressedFor(buttonLongPress)
|| buttonFive .pressedFor(buttonLongPress)
)
&& buttonPause.isPressed()
&& buttonFour .isPressed()
&& buttonFive .isPressed()) {
ret = commandRaw::allLong;
ignoreAll = true;
}
#endif
else if (( buttonUp .pressedFor(buttonLongPress)
|| buttonDown .pressedFor(buttonLongPress)
)
&& buttonUp .isPressed()
&& buttonDown .isPressed()) {
ret = commandRaw::updownLong;
ignoreAll = true;
}
else if (buttonPause.wasReleased() && not ignoreRelease) {
ret = commandRaw::pause;
}
else if (buttonPause.pressedFor(buttonLongPress + longPressFactor * buttonLongPressRepeat)) {
if (longPressFactor == 0)
ret = commandRaw::pauseLong;
}
else if (buttonUp.wasReleased() && not ignoreRelease) {
ret = commandRaw::up;
}
else if (buttonUp.pressedFor(buttonLongPress + longPressFactor * buttonLongPressRepeat)) {
if (longPressFactor == 0)
ret = commandRaw::upLong;
else
ret = commandRaw::upLongRepeat;
}
else if (buttonDown.wasReleased() && not ignoreRelease) {
ret = commandRaw::down;
}
else if (buttonDown.pressedFor(buttonLongPress + longPressFactor * buttonLongPressRepeat)) {
if (longPressFactor == 0)
ret = commandRaw::downLong;
else
ret = commandRaw::downLongRepeat;
}
#ifdef FIVEBUTTONS
else if (buttonFour.wasReleased() && not ignoreRelease) {
ret = commandRaw::four;
}
else if (buttonFour.pressedFor(buttonLongPress + longPressFactor * buttonLongPressRepeat)) {
if (longPressFactor == 0)
ret = commandRaw::fourLong;
else
ret = commandRaw::fourLongRepeat;
}
else if (buttonFive.wasReleased() && not ignoreRelease) {
ret = commandRaw::five;
}
else if (buttonFive.pressedFor(buttonLongPress + longPressFactor * buttonLongPressRepeat)) {
if (longPressFactor == 0)
ret = commandRaw::fiveLong;
else
ret = commandRaw::fiveLongRepeat;
}
#endif
switch (ret) {
case commandRaw::pauseLong :
case commandRaw::upLong :
case commandRaw::upLongRepeat :
case commandRaw::downLong :
case commandRaw::downLongRepeat:
#ifdef FIVEBUTTONS
case commandRaw::fourLong :
case commandRaw::fourLongRepeat:
case commandRaw::fiveLong :
case commandRaw::fiveLongRepeat:
#endif
++longPressFactor;
ignoreRelease = true;
break;
default : break;
}
if (ret != commandRaw::none) {
LOG(button_log, s_debug, F("Button raw: "), static_cast<uint8_t>(ret));
}
return ret;
}
bool Buttons::isNoButton() {
return not buttonPause.isPressed()
&& not buttonUp .isPressed()
&& not buttonDown .isPressed()
#ifdef FIVEBUTTONS
&& not buttonFour .isPressed()
&& not buttonFive .isPressed()
#endif
;
}
bool Buttons::isReset() {
constexpr int buttonActiveLevel = getLevel(buttonPinType, level::active);
return (digitalRead(buttonPausePin) == buttonActiveLevel &&
digitalRead(buttonUpPin ) == buttonActiveLevel &&
digitalRead(buttonDownPin ) == buttonActiveLevel );
}
void Buttons::readButtons() {
buttonPause.read();
buttonUp .read();
buttonDown .read();
#ifdef FIVEBUTTONS
buttonFour .read();
buttonFive .read();
#endif
}