-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathinputs.c
535 lines (506 loc) · 18.1 KB
/
inputs.c
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
* This file is part of the Mooltipass Project (https://github.com/mooltipass).
* Copyright (c) 2019 Stephan Mathieu
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*! \file inputs.c
* \brief Scroll wheel driver
* Created: 20/02/2018
* Author: Mathieu Stephan
*/
#include <asf.h>
#include "platform_defines.h"
#include "logic_device.h"
#include "driver_timer.h"
#include "platform_io.h"
#include "inputs.h"
#if !defined(PLAT_V5_SETUP) && !defined(PLAT_V6_SETUP) && !defined(PLAT_V7_SETUP) && !defined(V2_PLAT_V1_SETUP)
// Wheel machine states
uint16_t inputs_wheel_sm_states[] = {0b011, 0b001, 0b000, 0b010};
// Boot to know if we allow next increment
volatile BOOL inputs_wheel_increment_armed_up = FALSE;
volatile BOOL inputs_wheel_increment_armed_down = FALSE;
// Last wheel state machine index
volatile uint16_t inputs_last_wheel_sm;
#else
volatile uint16_t inputs_pending_a_state_counter = 0;
volatile BOOL inputs_last_a_state_on = FALSE;
volatile BOOL inputs_last_b_state_on = FALSE;
#endif
// Wheel pressed duration counter
volatile uint16_t inputs_wheel_click_duration_counter;
// Penalty for long click
volatile uint16_t inputs_wheel_long_click_penalty;
// Current wheel click return
volatile det_ret_type_te inputs_wheel_click_return;
// Wheel click counter
volatile uint16_t inputs_wheel_click_counter;
// Wheel current increment for caller
volatile int16_t inputs_wheel_cur_increment;
// To get wheel action, discard release event
BOOL inputs_discard_release_event = FALSE;
// Wheel direction reverse bool
BOOL inputs_wheel_reverse_bool = FALSE;
// Wheel debounce ms value
volatile BOOL inputs_wheel_debounce_value = 100;
// Last detection type returned (cleared when calling cleardetections)
wheel_action_ret_te inputs_last_detection_type_ret = WHEEL_ACTION_NONE;
#ifndef BOOTLOADER
volatile uint16_t inputs_force_reboot_timer = 0;
#endif
#ifndef EMULATOR_BUILD
/*! \fn inputs_scan(void)
* \brief Wheel debounce called by 1ms interrupt
*/
void inputs_scan(void)
{
#if !defined(PLAT_V5_SETUP) && !defined(PLAT_V6_SETUP) && !defined(PLAT_V7_SETUP) && !defined(V2_PLAT_V1_SETUP)
uint16_t wheel_state, wheel_sm = 0;
// Wheel encoder
wheel_state = ((PORT->Group[WHEEL_A_GROUP].IN.reg & WHEEL_A_MASK) >> WHEEL_A_PINID) | ((PORT->Group[WHEEL_B_GROUP].IN.reg & WHEEL_B_MASK) >> (WHEEL_B_PINID-1));
// Find the state matching the wheel state
for (uint16_t i = 0; i < sizeof(inputs_wheel_sm_states)/sizeof(inputs_wheel_sm_states[0]); i++)
{
if (wheel_state == inputs_wheel_sm_states[i])
{
wheel_sm = i;
}
}
if (wheel_sm == ((inputs_last_wheel_sm+1)&0x03))
{
if (wheel_state == 0)
{
inputs_wheel_increment_armed_up = TRUE;
}
else if ((wheel_state == 0x03) && (inputs_wheel_increment_armed_up == TRUE))
{
if (inputs_wheel_reverse_bool != FALSE)
{
inputs_wheel_cur_increment++;
}
else
{
inputs_wheel_cur_increment--;
}
inputs_wheel_increment_armed_up = FALSE;
inputs_wheel_increment_armed_down = FALSE;
}
inputs_last_wheel_sm = wheel_sm;
}
else if (wheel_sm == ((inputs_last_wheel_sm-1)&0x03))
{
if (wheel_state == 0)
{
inputs_wheel_increment_armed_down = TRUE;
}
else if ((wheel_state == 0x03) && (inputs_wheel_increment_armed_down == TRUE))
{
if (inputs_wheel_reverse_bool != FALSE)
{
inputs_wheel_cur_increment--;
}
else
{
inputs_wheel_cur_increment++;
}
inputs_wheel_increment_armed_up = FALSE;
inputs_wheel_increment_armed_down = FALSE;
}
inputs_last_wheel_sm = wheel_sm;
}
#else
BOOL cur_switch_a_state = (((PORT->Group[WHEEL_A_GROUP].IN.reg & WHEEL_A_MASK) >> WHEEL_A_PINID) == 0)? FALSE:TRUE;
BOOL cur_switch_b_state = (((PORT->Group[WHEEL_B_GROUP].IN.reg & WHEEL_B_MASK) >> WHEEL_B_PINID) == 0)? FALSE:TRUE;
/* Detect A channel transitions */
if (cur_switch_a_state != inputs_last_a_state_on)
{
/* Debouncing */
if (inputs_pending_a_state_counter++ == 5)
{
if (inputs_last_a_state_on == FALSE)
{
if (inputs_last_b_state_on == FALSE)
{
if (cur_switch_b_state == FALSE)
{
if (inputs_wheel_reverse_bool == FALSE)
{
inputs_wheel_cur_increment--;
}
else
{
inputs_wheel_cur_increment++;
}
}
else
{
if (inputs_wheel_reverse_bool == FALSE)
{
inputs_wheel_cur_increment++;
}
else
{
inputs_wheel_cur_increment--;
}
}
}
else
{
if (cur_switch_b_state == FALSE)
{
if (inputs_wheel_reverse_bool == FALSE)
{
inputs_wheel_cur_increment--;
}
else
{
inputs_wheel_cur_increment++;
}
}
else
{
if (inputs_wheel_reverse_bool == FALSE)
{
inputs_wheel_cur_increment++;
}
else
{
inputs_wheel_cur_increment--;
}
}
}
}
else
{
if (inputs_last_b_state_on == FALSE)
{
if (cur_switch_b_state == FALSE)
{
if (inputs_wheel_reverse_bool == FALSE)
{
inputs_wheel_cur_increment++;
}
else
{
inputs_wheel_cur_increment--;
}
}
else
{
if (inputs_wheel_reverse_bool == FALSE)
{
inputs_wheel_cur_increment--;
}
else
{
inputs_wheel_cur_increment++;
}
}
}
else
{
if (cur_switch_b_state == FALSE)
{
if (inputs_wheel_reverse_bool == FALSE)
{
inputs_wheel_cur_increment++;
}
else
{
inputs_wheel_cur_increment--;
}
}
else
{
if (inputs_wheel_reverse_bool == FALSE)
{
inputs_wheel_cur_increment--;
}
else
{
inputs_wheel_cur_increment++;
}
}
}
}
inputs_last_a_state_on = cur_switch_a_state;
inputs_last_b_state_on = cur_switch_b_state;
inputs_pending_a_state_counter = 0;
}
}
else
{
inputs_pending_a_state_counter = 0;
}
#endif
// Wheel click
if ((PORT->Group[WHEEL_SW_GROUP].IN.reg & WHEEL_SW_MASK) == 0)
{
#ifndef BOOTLOADER
/* Increment force reboot timer and reboot if it reached target value */
if (inputs_force_reboot_timer++ == NB_MS_WHEEL_PRESS_FOR_REBOOT)
{
/* Switch off screen wait for user to release button */
platform_io_power_down_oled();
while ((PORT->Group[WHEEL_SW_GROUP].IN.reg & WHEEL_SW_MASK) == 0);
DELAYMS(200);
platform_io_cutoff_power();
while(1);
}
#endif
/* Check that detection wasn't set to invalid */
if (inputs_wheel_click_return != RETURN_INV_DET)
{
/* Still needed even though we have hardware debouncing */
if ((inputs_wheel_click_counter == inputs_wheel_debounce_value) && (inputs_wheel_click_return != RETURN_JRELEASED))
{
inputs_wheel_click_return = RETURN_JDETECT;
}
if (inputs_wheel_click_counter != 0xFFFF)
{
inputs_wheel_click_counter++;
}
if ((inputs_wheel_click_return == RETURN_DET) || (inputs_wheel_click_return == RETURN_JDETECT))
{
inputs_wheel_click_duration_counter++;
}
}
}
else
{
#ifndef BOOTLOADER
inputs_force_reboot_timer = 0;
#endif
if (inputs_wheel_click_return == RETURN_DET)
{
inputs_wheel_click_return = RETURN_JRELEASED;
}
else if (inputs_wheel_click_return != RETURN_JRELEASED)
{
inputs_wheel_click_duration_counter = 0;
inputs_wheel_click_return = RETURN_REL;
}
inputs_wheel_click_counter = 0;
}
}
#endif
/*! \fn inputs_set_wheel_debounce_value(uint16_t debounce_value)
* \brief Set wheel debounce value
* \param debounce_value Wheel debounce value in ms
*/
void inputs_set_wheel_debounce_value(uint16_t debounce_value)
{
inputs_wheel_debounce_value = debounce_value;
}
/*! \fn inputs_set_inputs_invert_bool(BOOL invert_bool)
* \brief Set bool to invert input logic
* \param invert_bool The inversion boolean
*/
void inputs_set_inputs_invert_bool(BOOL invert_bool)
{
inputs_wheel_reverse_bool = invert_bool;
}
/*! \fn inputs_get_wheel_increment(void)
* \brief Fetch the current increment/decrement for the wheel
* \return positive or negative depending on the scrolling
*/
int16_t inputs_get_wheel_increment(void)
{
int16_t return_val = 0;
if (inputs_wheel_cur_increment != 0)
{
logic_device_activity_detected();
cpu_irq_enter_critical();
return_val = inputs_wheel_cur_increment;
inputs_wheel_cur_increment = 0;
cpu_irq_leave_critical();
}
return return_val;
}
/*! \fn inputs_raw_is_wheel_released(void)
* \brief (raw access) know if the wheel is released
* \return TRUE if the wheel is release, FALSE otherwise
* \note Know what you're doing if you're calling this, this is debounce-prone
*/
BOOL inputs_raw_is_wheel_released(void)
{
#ifndef EMULATOR_BUILD
if ((PORT->Group[WHEEL_SW_GROUP].IN.reg & WHEEL_SW_MASK) == 0)
{
return FALSE;
}
else
{
return TRUE;
}
#else
return FALSE;
#endif
}
/*! \fn inputs_is_wheel_clicked(void)
* \brief Know if the wheel is clicked
* \return just released/pressed, (non)detected
*/
det_ret_type_te inputs_is_wheel_clicked(void)
{
// This copy is an atomic operation
volatile det_ret_type_te return_val = inputs_wheel_click_return;
if ((return_val != RETURN_DET) && (return_val != RETURN_REL) && (return_val != RETURN_INV_DET))
{
logic_device_activity_detected();
cpu_irq_enter_critical();
if (inputs_wheel_click_return == RETURN_JDETECT)
{
inputs_wheel_click_return = RETURN_DET;
}
else if (inputs_wheel_click_return == RETURN_JRELEASED)
{
inputs_wheel_click_return = RETURN_REL;
}
cpu_irq_leave_critical();
}
return return_val;
}
/*! \fn inputs_clear_detections(void)
* \brief Clear current detections
*/
void inputs_clear_detections(void)
{
cpu_irq_enter_critical();
inputs_last_detection_type_ret = WHEEL_ACTION_NONE;
#if !defined(PLAT_V5_SETUP) && !defined(PLAT_V6_SETUP) && !defined(PLAT_V7_SETUP) && !defined(V2_PLAT_V1_SETUP)
inputs_wheel_increment_armed_down = FALSE;
inputs_wheel_increment_armed_up = FALSE;
#endif
inputs_wheel_click_return = RETURN_INV_DET;
inputs_wheel_click_duration_counter = 0;
inputs_discard_release_event = FALSE;
inputs_wheel_long_click_penalty = 0;
inputs_wheel_cur_increment = 0;
cpu_irq_leave_critical();
}
/*! \fn inputs_get_last_returned_action(void)
* \brief Get the last returned action to another call
* \return See wheel_action_ret_t
*/
RET_TYPE inputs_get_last_returned_action(void)
{
return inputs_last_detection_type_ret;
}
/*! \fn inputs_get_wheel_action(void)
* \brief Get current wheel action
* \param wait_for_action Set to TRUE to wait for an action
* \param ignore_incdec Ignore actions linked to wheel scrolling
* \return See wheel_action_ret_t
*/
wheel_action_ret_te inputs_get_wheel_action(BOOL wait_for_action, BOOL ignore_incdec)
{
wheel_action_ret_te return_val = WHEEL_ACTION_NONE;
int16_t inputs_wheel_cur_increment_copy = 0;
do
{
// If we want to take into account wheel scrolling
if (ignore_incdec == FALSE)
{
inputs_wheel_cur_increment_copy = inputs_wheel_cur_increment;
}
// Check for invalid state
if (inputs_wheel_click_return == RETURN_INV_DET)
{
return WHEEL_ACTION_NONE;
}
if (inputs_wheel_click_return == RETURN_JDETECT)
{
// When checking for actions we clear the just detected state
cpu_irq_enter_critical();
inputs_wheel_click_return = RETURN_DET;
cpu_irq_leave_critical();
}
if ((inputs_wheel_click_return == RETURN_JRELEASED) || (inputs_wheel_cur_increment_copy != 0) || (inputs_wheel_click_duration_counter > LONG_PRESS_MS + inputs_wheel_long_click_penalty))
{
cpu_irq_enter_critical();
if ((inputs_wheel_click_duration_counter > LONG_PRESS_MS + inputs_wheel_long_click_penalty) && (inputs_discard_release_event == FALSE))
{
return_val = WHEEL_ACTION_LONG_CLICK;
}
else if (inputs_wheel_click_return == RETURN_JRELEASED)
{
if (inputs_wheel_cur_increment_copy == 0)
{
if ((inputs_discard_release_event != FALSE) || (inputs_wheel_long_click_penalty != 0))
{
inputs_discard_release_event = FALSE;
inputs_wheel_long_click_penalty = 0;
return_val = WHEEL_ACTION_DISCARDED;
}
else
{
return_val = WHEEL_ACTION_SHORT_CLICK;
}
}
/* Acknowledge */
inputs_wheel_click_return = RETURN_REL;
}
else if (inputs_wheel_click_return == RETURN_DET)
{
if (inputs_wheel_cur_increment_copy > 0)
{
return_val = WHEEL_ACTION_CLICK_DOWN;
}
else if (inputs_wheel_cur_increment_copy < 0)
{
return_val = WHEEL_ACTION_CLICK_UP;
}
}
else
{
if (inputs_wheel_cur_increment_copy > 0)
{
return_val = WHEEL_ACTION_DOWN;
}
else if (inputs_wheel_cur_increment_copy < 0)
{
return_val = WHEEL_ACTION_UP;
}
}
// Clear detections
inputs_wheel_click_duration_counter = 0;
if (return_val == WHEEL_ACTION_LONG_CLICK)
{
inputs_discard_release_event = TRUE;
}
else if ((return_val == WHEEL_ACTION_CLICK_DOWN) || (return_val == WHEEL_ACTION_CLICK_UP))
{
inputs_wheel_long_click_penalty = LONG_PRESS_MS*2;
}
else if(return_val == WHEEL_ACTION_NONE)
{
// User has been scrolling then keeping the wheel pressed: do not do anything
}
if (ignore_incdec == FALSE)
{
inputs_wheel_cur_increment = 0;
}
cpu_irq_leave_critical();
}
}
while ((wait_for_action != FALSE) && (return_val == WHEEL_ACTION_NONE));
// Don't forget to call the activity detected routine if something happened
if (return_val != WHEEL_ACTION_NONE)
{
logic_device_activity_detected();
}
inputs_last_detection_type_ret = return_val;
return return_val;
}