mirror of
https://github.com/wled/WLED.git
synced 2025-07-09 20:06:33 +00:00
Buttons: Trigger on button press (instead of release) if all configured presets are the same (#3226)
* Buttons: Trigger when pressing if all configured presets are the same * Add debounce for immediate rising-edge short press --------- Co-authored-by: Aircoookie <21045690+Aircoookie@users.noreply.github.com>
This commit is contained in:
parent
130f495fb6
commit
af88c68fca
@ -227,9 +227,8 @@ void handleButton()
|
|||||||
static unsigned long lastRun = 0UL;
|
static unsigned long lastRun = 0UL;
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
|
|
||||||
//if (strip.isUpdating()) return; // don't interfere with strip updates. Our button will still be there in 1ms (next cycle)
|
if (strip.isUpdating() && (now - lastRun < 400)) return; // don't interfere with strip update (unless strip is updating continuously, e.g. very long strips)
|
||||||
if (strip.isUpdating() && (millis() - lastRun < 400)) return; // be niced, but avoid button starvation
|
lastRun = now;
|
||||||
lastRun = millis();
|
|
||||||
|
|
||||||
for (uint8_t b=0; b<WLED_MAX_BUTTONS; b++) {
|
for (uint8_t b=0; b<WLED_MAX_BUTTONS; b++) {
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
@ -240,7 +239,7 @@ void handleButton()
|
|||||||
|
|
||||||
if (usermods.handleButton(b)) continue; // did usermod handle buttons
|
if (usermods.handleButton(b)) continue; // did usermod handle buttons
|
||||||
|
|
||||||
if (buttonType[b] == BTN_TYPE_ANALOG || buttonType[b] == BTN_TYPE_ANALOG_INVERTED) { // button is not a button but a potentiometer
|
if (buttonType[b] == BTN_TYPE_ANALOG || buttonType[b] == BTN_TYPE_ANALOG_INVERTED) { // button is not a button but a potentiometer
|
||||||
if (now - lastRead > ANALOG_BTN_READ_CYCLE) {
|
if (now - lastRead > ANALOG_BTN_READ_CYCLE) {
|
||||||
handleAnalog(b);
|
handleAnalog(b);
|
||||||
lastRead = now;
|
lastRead = now;
|
||||||
@ -248,14 +247,23 @@ void handleButton()
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//button is not momentary, but switch. This is only suitable on pins whose on-boot state does not matter (NOT gpio0)
|
// button is not momentary, but switch. This is only suitable on pins whose on-boot state does not matter (NOT gpio0)
|
||||||
if (buttonType[b] == BTN_TYPE_SWITCH || buttonType[b] == BTN_TYPE_PIR_SENSOR) {
|
if (buttonType[b] == BTN_TYPE_SWITCH || buttonType[b] == BTN_TYPE_PIR_SENSOR) {
|
||||||
handleSwitch(b);
|
handleSwitch(b);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//momentary button logic
|
// momentary button logic
|
||||||
if (isButtonPressed(b)) { //pressed
|
if (isButtonPressed(b)) { // pressed
|
||||||
|
|
||||||
|
// if all macros are the same, fire action immediately on rising edge
|
||||||
|
if (macroButton[b] && macroButton[b] == macroLongPress[b] && macroButton[b] == macroDoublePress[b]) {
|
||||||
|
if (!buttonPressedBefore[b])
|
||||||
|
shortPressAction(b);
|
||||||
|
buttonPressedBefore[b] = true;
|
||||||
|
buttonPressedTime[b] = now; // continually update (for debouncing to work in release handler)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!buttonPressedBefore[b]) buttonPressedTime[b] = now;
|
if (!buttonPressedBefore[b]) buttonPressedTime[b] = now;
|
||||||
buttonPressedBefore[b] = true;
|
buttonPressedBefore[b] = true;
|
||||||
@ -270,9 +278,15 @@ void handleButton()
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else if (!isButtonPressed(b) && buttonPressedBefore[b]) { //released
|
} else if (!isButtonPressed(b) && buttonPressedBefore[b]) { //released
|
||||||
|
|
||||||
long dur = now - buttonPressedTime[b];
|
long dur = now - buttonPressedTime[b];
|
||||||
if (dur < WLED_DEBOUNCE_THRESHOLD) {buttonPressedBefore[b] = false; continue;} //too short "press", debounce
|
|
||||||
|
// released after rising-edge short press action
|
||||||
|
if (macroButton[b] && macroButton[b] == macroLongPress[b] && macroButton[b] == macroDoublePress[b]) {
|
||||||
|
if (dur > WLED_DEBOUNCE_THRESHOLD) buttonPressedBefore[b] = false; // debounce, blocks button for 50 ms once it has been released
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dur < WLED_DEBOUNCE_THRESHOLD) {buttonPressedBefore[b] = false; continue;} // too short "press", debounce
|
||||||
bool doublePress = buttonWaitTime[b]; //did we have a short press before?
|
bool doublePress = buttonWaitTime[b]; //did we have a short press before?
|
||||||
buttonWaitTime[b] = 0;
|
buttonWaitTime[b] = 0;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user