forked from macetech/RGBShadesAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.h
151 lines (121 loc) · 3.96 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
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
// 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 BTNGUARDTIME 6
#define BTNDEBOUNCETIME 30
#define BTNLONGPRESSTIME 1500
unsigned long buttonEvents[NUMBUTTONS];
byte buttonStatuses[NUMBUTTONS];
byte buttonmap[NUMBUTTONS] = {BRIGHTNESSBUTTON, MODEBUTTON};
extern const byte numEffectsAudio;
extern const byte numEffectsNoAudio;
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;
case BTNGUARDTIME:
if (digitalRead(buttonmap[0]) == HIGH && digitalRead(buttonmap[1]) == HIGH) {
buttonStatuses[i] = BTNIDLE;
}
}
}
}
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() {
if ((buttonStatuses[0] == BTNPRESSED) && (buttonStatuses[1] == BTNPRESSED)) {
audioEnabled = !audioEnabled; // toggle audio mode
switch (audioEnabled) {
case true:
numEffects = numEffectsAudio;
break;
case false:
numEffects = numEffectsNoAudio;
break;
}
currentEffect = 0;
effectInit = false;
confirmBlink(CRGB::DarkGreen, 3);
buttonStatuses[0] = BTNGUARDTIME;
buttonStatuses[1] = BTNGUARDTIME;
} else {
// 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
// one blue blink: auto mode. two red blinks: manual mode.
if (autoCycle) {
confirmBlink(CRGB::Blue, 1);
} else {
confirmBlink(CRGB::Red, 2);
}
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;
}
}
}