forked from macetech/RGBShades
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuttons.h
73 lines (59 loc) · 1.71 KB
/
buttons.h
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
// Process button inputs and return button activity
#define NUMBUTTONS 2
#define MODEBUTTON 4
#define BRIGHTNESSBUTTON 3
#define BTNIDLE 0
#define BTNDEBOUNCING 1
#define BTNPRESSED 2
#define BTNRELEASED 3
#define BTNLONGPRESS 4
#define BTNLONGPRESSREAD 5
#define BTNDEBOUNCETIME 20
#define BTNLONGPRESSTIME 1000
unsigned long buttonEvents[NUMBUTTONS];
byte buttonStatuses[NUMBUTTONS];
byte buttonmap[NUMBUTTONS] = {BRIGHTNESSBUTTON, MODEBUTTON};
void updateButtons() {
for (byte i = 0; i < NUMBUTTONS; i++) {
switch(buttonStatuses[i]) {
case BTNIDLE:
if (digitalRead(buttonmap[i]) == LOW) {
buttonEvents[i] = currentMillis;
buttonStatuses[i] = BTNDEBOUNCING;
}
break;
case BTNDEBOUNCING:
if (currentMillis - buttonEvents[i] > BTNDEBOUNCETIME) {
if (digitalRead(buttonmap[i]) == LOW) {
buttonStatuses[i] = BTNPRESSED;
}
}
break;
case BTNPRESSED:
if (digitalRead(buttonmap[i]) == HIGH) {
buttonStatuses[i] = BTNRELEASED;
} else if (currentMillis - buttonEvents[i] > BTNLONGPRESSTIME) {
buttonStatuses[i] = BTNLONGPRESS;
}
break;
case BTNRELEASED:
break;
case BTNLONGPRESS:
break;
case BTNLONGPRESSREAD:
if (digitalRead(buttonmap[i]) == HIGH) {
buttonStatuses[i] = BTNIDLE;
}
break;
}
}
}
byte buttonStatus(byte buttonNum) {
byte tempStatus = buttonStatuses[buttonNum];
if (tempStatus == BTNRELEASED) {
buttonStatuses[buttonNum] = BTNIDLE;
} else if (tempStatus == BTNLONGPRESS) {
buttonStatuses[buttonNum] = BTNLONGPRESSREAD;
}
return tempStatus;
}