-
Notifications
You must be signed in to change notification settings - Fork 13
/
ButtonHandler.cpp
299 lines (240 loc) · 6.94 KB
/
ButtonHandler.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "ButtonHandler.h"
#include "SwitchEndpoint.h"
extern "C"
{
#include "dbg.h"
#include "AppHardwareApi.h"
}
ButtonHandler::ButtonHandler()
{
endpoint = NULL;
currentState = INVALID;
currentStateDuration = 0;
switchMode = SWITCH_MODE_TOGGLE;
relayMode = RELAY_MODE_FRONT;
maxPause = 250/ButtonPollCycle;
longPressDuration = 1000/ButtonPollCycle;
}
void ButtonHandler::setEndpoint(SwitchEndpoint * ep)
{
endpoint = ep;
}
const char * ButtonHandler::getStateName(ButtonState state)
{
switch(state)
{
case IDLE: return "IDLE";
case PRESSED1: return "PRESSED1";
case PAUSE1: return "PAUSE1";
case PRESSED2: return "PRESSED2";
case PAUSE2: return "PAUSE2";
case PRESSED3: return "PRESSED3";
case LONG_PRESS: return "LONG_PRESS";
case INVALID: return "INVALID";
}
// Should never happen
return "";
}
void ButtonHandler::setConfiguration(SwitchMode switchMode, RelayMode relayMode, uint16 maxPause, uint16 minLongPress)
{
// This function does the same as 4 functions below all together, but as a single transaction.
// This is needed to avoid cluttering log with multiple changeState messages
switchMode = switchMode;
relayMode = relayMode;
maxPause = maxPause/ButtonPollCycle;
longPressDuration = minLongPress/ButtonPollCycle;
changeState(INVALID, true);
}
void ButtonHandler::setSwitchMode(SwitchMode mode)
{
switchMode = mode;
changeState(INVALID);
}
void ButtonHandler::setRelayMode(RelayMode mode)
{
relayMode = mode;
changeState(INVALID);
}
void ButtonHandler::setMaxPause(uint16 value)
{
maxPause = value/ButtonPollCycle;
changeState(INVALID);
}
void ButtonHandler::setMinLongPress(uint16 value)
{
longPressDuration = value/ButtonPollCycle;
changeState(INVALID);
}
void ButtonHandler::changeState(ButtonState state, bool suppressLogging)
{
currentState = state;
currentStateDuration = 0;
// TODO: Avoid dumping multiple changeState() calls during initial initialization
if(!suppressLogging)
DBG_vPrintf(TRUE, "Switching button %d state to %s\n", endpoint->getEndpointId(), getStateName(state));
}
void ButtonHandler::buttonStateMachineToggle(bool pressed)
{
// The state machine
switch(currentState)
{
case IDLE:
if(pressed)
{
changeState(PRESSED1);
endpoint->reportAction(BUTTON_ACTION_SINGLE);
if(relayMode != RELAY_MODE_UNLINKED)
endpoint->toggle();
}
break;
case PRESSED1:
if(!pressed)
changeState(IDLE);
break;
default:
changeState(IDLE); // How did we get here?
break;
}
}
void ButtonHandler::buttonStateMachineMomentary(bool pressed)
{
// The state machine
switch(currentState)
{
case IDLE:
if(pressed)
{
changeState(PRESSED1);
endpoint->reportAction(BUTTON_PRESSED);
if(relayMode != RELAY_MODE_UNLINKED)
endpoint->switchOn();
endpoint->reportLongPress(true);
}
break;
case PRESSED1:
if(!pressed)
{
changeState(IDLE);
endpoint->reportAction(BUTTON_RELEASED);
if(relayMode != RELAY_MODE_UNLINKED)
endpoint->switchOff();
endpoint->reportLongPress(false);
}
break;
default:
changeState(IDLE); // How did we get here?
break;
}
}
void ButtonHandler::buttonStateMachineMultistate(bool pressed)
{
// The state machine
switch(currentState)
{
case IDLE:
if(pressed)
{
changeState(PRESSED1);
if(relayMode == RELAY_MODE_FRONT)
endpoint->toggle();
}
break;
case PRESSED1:
if(pressed && (currentStateDuration > longPressDuration))
{
changeState(LONG_PRESS);
endpoint->reportAction(BUTTON_PRESSED);
if(relayMode == RELAY_MODE_LONG)
endpoint->toggle();
endpoint->reportLongPress(true);
}
if(!pressed)
{
changeState(PAUSE1);
}
break;
case PAUSE1:
if(!pressed && currentStateDuration > maxPause)
{
changeState(IDLE);
endpoint->reportAction(BUTTON_ACTION_SINGLE);
if(relayMode == RELAY_MODE_SINGLE)
endpoint->toggle();
}
if(pressed)
changeState(PRESSED2);
break;
case PRESSED2:
if(!pressed)
{
changeState(PAUSE2);
}
break;
case PAUSE2:
if(!pressed && currentStateDuration > maxPause)
{
changeState(IDLE);
endpoint->reportAction(BUTTON_ACTION_DOUBLE);
if(relayMode == RELAY_MODE_DOUBLE)
endpoint->toggle();
}
if(pressed)
{
changeState(PRESSED3);
}
break;
case PRESSED3:
if(!pressed)
{
changeState(IDLE);
if(relayMode == RELAY_MODE_TRIPPLE)
endpoint->toggle();
endpoint->reportAction(BUTTON_ACTION_TRIPPLE);
}
break;
case LONG_PRESS:
if(!pressed)
{
changeState(IDLE);
endpoint->reportAction(BUTTON_RELEASED);
endpoint->reportLongPress(false);
}
break;
default: break;
}
}
void ButtonHandler::handleButtonState(bool pressed)
{
// Let at least 20ms to stabilize button value, do not make any early decisions
// When button state is stabilized - go through the corresponding state machine
currentStateDuration++;
if(currentStateDuration < 2)
return;
// On a mode change the state is set to INVALID. This is needed to avoid immediate handling of a pressed button (if any).
// This check performs exit from INVALID state to IDLE upon button release
if(currentState == INVALID)
{
if(!pressed)
changeState(IDLE);
return;
}
// Handle the button according to the current mode
switch(switchMode)
{
case SWITCH_MODE_TOGGLE:
buttonStateMachineToggle(pressed);
break;
case SWITCH_MODE_MOMENTARY:
buttonStateMachineMomentary(pressed);
break;
case SWITCH_MODE_MULTIFUNCTION:
buttonStateMachineMultistate(pressed);
break;
default:
break;
}
}
void ButtonHandler::resetButtonStateMachine()
{
changeState(INVALID);
}