-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuttons.cpp
79 lines (71 loc) · 1.94 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
/*
* Copyright Ben XO https://github.com/ben-xo All rights reserved.
*/
#include "buttons.h"
#include "ledpwm.h"
uint8_t was_button_pressed() {
bool pins = button_pin.read();
uint16_t now = (uint16_t) millis();
if(!F.is_down && pins) {
// pressed
F.is_down = true;
F.last_push = now;
F.clicks++;
return NO_PUSH;
}
if(!F.is_down && !pins) {
// not pressed
if(F.clicks) {
// but was pressed recently
uint8_t retval = NO_PUSH;
if(now - F.last_push > BUTTON_REALLY_LONG_PUSH_SPEED) { // default 4000ms
retval = REALLY_LONG_PUSH;
} else if(now - F.last_push > BUTTON_LONG_PUSH_SPEED) { // default 2000ms
retval = LONG_PUSH;
} else if(now - F.last_push > BUTTON_CLICK_SPEED) { // default 300ms
switch(F.clicks) {
case 1:
retval = SINGLE_CLICK;
break;
case 2:
retval = DOUBLE_CLICK;
break;
case 3:
retval = TRIPLE_CLICK;
break;
case 4:
retval = QUADRUPLE_CLICK;
break;
case 5:
default:
// 5 or more
retval = QUINTUPLE_CLICK;
break;
}
}
if(retval) F.clicks = 0;
return retval;
}
}
if(F.is_down && !pins) {
// released
if(F.clicks && now - F.last_push > BUTTON_LONG_PUSH_SPEED) {
clear_status_leds();
}
F.is_down = false;
}
if(F.is_down && pins) {
// still down
if(F.clicks) {
if(now - F.last_push > BUTTON_REALLY_LONG_PUSH_SPEED) {
// full bright as really long press (usually reset)
set_status_leds_front_buffer_only(seven_seg(12));
} else if(now - F.last_push > BUTTON_LONG_PUSH_SPEED) {
// half bright at long press
set_status_leds_front_buffer_only((F.frame_counter & 1) ? 0 : seven_seg(12));
}
}
}
// no push yet, although one may be in progress.
return NO_PUSH;
}