94 lines
2.4 KiB
C
94 lines
2.4 KiB
C
// SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries
|
|
//
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Process button input and return button activity
|
|
// Retained button code from RGB Shades though just using one button
|
|
|
|
#define NUMBUTTONS 1
|
|
#define MODEBUTTON 2 //define the pin the button is connected to
|
|
|
|
#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] = {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;
|
|
|
|
}
|
|
|
|
// Check the mode button (for switching between effects)
|
|
void doButtons() {
|
|
switch (buttonStatus(0)) {
|
|
|
|
case BTNRELEASED: // short button press
|
|
cycleMillis = currentMillis;
|
|
if (++currentEffect >= numEffects) currentEffect = 0; // loop to start of effect list
|
|
effectInit = false; // trigger effect initialization when new effect is selected
|
|
break;
|
|
|
|
case BTNLONGPRESS: // long button press
|
|
currentBrightness += 51; // increase the brightness (wraps to lowest)
|
|
FastLED.setBrightness(scale8(currentBrightness, MAXBRIGHTNESS));
|
|
break;
|
|
}
|
|
}
|