-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput.cpp
More file actions
63 lines (57 loc) · 1.45 KB
/
Copy pathinput.cpp
File metadata and controls
63 lines (57 loc) · 1.45 KB
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
#include "input.h"
#define SWITCH_PIN 4
#define DOUBLECLICK_INTERVAL 300
#define LONGPRESS_INTERVAL 1000
ButtonState bstate;
void reset_button_state(){
bstate.uptime = 0;
bstate.downtime = 0;
bstate.clicks = 0;
bstate.is_clicking = false;
bstate.timing_out = false;
bstate.longpressing = false;
}
/* Parses the button state-stream into interprettable click/double click/long-press */
byte get_input(){
unsigned long t = millis();
byte switchposition = digitalRead(SWITCH_PIN);
if (switchposition == LOW) {
// pressed
if (!bstate.is_clicking){
bstate.downtime = t;
bstate.is_clicking = true;
bstate.timing_out = false;
bstate.clicks++;
} else {
if (t - bstate.downtime > LONGPRESS_INTERVAL && !bstate.longpressing){
bstate.longpressing = true;
return longpress;
}
}
} else {
// up
if (bstate.is_clicking){
// released
bstate.uptime = t;
bstate.is_clicking = false;
bstate.timing_out = true;
} else {
// up
if (bstate.timing_out){
// In the timeout period
if (t - bstate.uptime > DOUBLECLICK_INTERVAL){
byte input;
// Exceeded doubleclick interval, interpret as is
if (bstate.longpressing){
input = NO_INPUT;
} else {
input = bstate.clicks;
}
reset_button_state();
return input;
}
}
}
}
return NO_INPUT;
}