-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttons.h
116 lines (92 loc) · 3.07 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
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
// 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;
}
void doButtons() {
// Check the mode button (for switching between effects)
switch (buttonStatus(0)) {
case BTNRELEASED: // button was pressed and released quickly
cycleMillis = currentMillis;
if (++currentEffect >= numEffects) currentEffect = 0; // loop to start of effect list
effectInit = false; // trigger effect initialization when new effect is selected
eepromMillis = currentMillis;
eepromOutdated = true;
break;
case BTNLONGPRESS: // button was held down for a while
autoCycle = !autoCycle; // toggle auto cycle mode
confirmBlink(); // one blue blink: auto mode. two red blinks: manual mode.
eepromMillis = currentMillis;
eepromOutdated = true;
break;
}
// Check the brightness adjust button
switch (buttonStatus(1)) {
case BTNRELEASED: // button was pressed and released quickly
currentBrightness += 51; // increase the brightness (wraps to lowest)
FastLED.setBrightness(scale8(currentBrightness, MAXBRIGHTNESS));
eepromMillis = currentMillis;
eepromOutdated = true;
break;
case BTNLONGPRESS: // button was held down for a while
currentBrightness = STARTBRIGHTNESS; // reset brightness to startup value
FastLED.setBrightness(scale8(currentBrightness, MAXBRIGHTNESS));
eepromMillis = currentMillis;
eepromOutdated = true;
break;
}
}