From 48d55844911e48a978fdcf4f55cb1b9cea72eb91 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Mon, 12 Apr 2021 00:45:33 +0200 Subject: [PATCH] Add switch support --- CHANGELOG.md | 6 ++++++ wled00/button.cpp | 38 +++++++++++++++++++++++++++++++++-- wled00/cfg.cpp | 4 ++-- wled00/const.h | 2 +- wled00/data/settings_sync.htm | 7 ++++++- wled00/html_settings.h | 13 ++++++------ wled00/set.cpp | 2 +- wled00/wled.cpp | 4 ++-- wled00/wled.h | 4 ++-- wled00/wled_eeprom.cpp | 2 +- wled00/xml.cpp | 2 +- 11 files changed, 65 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03bde2e..72c41c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ### Builds after release 0.12.0 +#### Build 2104120 + +- Added switch support (button macro is switch closing action, long press macro switch opening) +- Replaced Circus effect with new Running Dual effect (Circus is Tricolor Chase with Red/White/Black) +- Fixed ledmap with multiple segments (PR #1864) + #### Build 2104030 - Fixed ESP32 crash on Drip effect with reversed segment (#1854) diff --git a/wled00/button.cpp b/wled00/button.cpp index 22e71a6..c263a66 100644 --- a/wled00/button.cpp +++ b/wled00/button.cpp @@ -4,6 +4,8 @@ * Physical IO */ +#define WLED_DEBOUNCE_THRESHOLD 50 //only consider button input of at least 50ms as valid (debouncing) + void shortPressAction() { if (!macroButton) @@ -25,10 +27,42 @@ bool isButtonPressed() } +void handleSwitch() +{ + if (buttonPressedBefore != isButtonPressed()) { + buttonPressedTime = millis(); + buttonPressedBefore = !buttonPressedBefore; + } + + if (buttonLongPressed == buttonPressedBefore) return; + + if (millis() - buttonPressedTime > WLED_DEBOUNCE_THRESHOLD) { //fire edge event only after 50ms without change (debounce) + if (buttonPressedBefore) { //LOW, falling edge, switch closed + if (macroButton) applyPreset(macroButton); + else { //turn on + if (!bri) {toggleOnOff(); colorUpdated(NOTIFIER_CALL_MODE_BUTTON);} + } + } else { //HIGH, rising edge, switch opened + if (macroLongPress) applyPreset(macroLongPress); + else { //turn off + if (bri) {toggleOnOff(); colorUpdated(NOTIFIER_CALL_MODE_BUTTON);} + } + } + buttonLongPressed = buttonPressedBefore; //save the last "long term" switch state + } +} + + void handleButton() { - if (btnPin<0 || !buttonEnabled) return; + if (btnPin<0 || buttonType < BTN_TYPE_PUSH) return; + + if (buttonType == BTN_TYPE_SWITCH) { //button is not momentary, but switch. This is only suitable on pins whose on-boot state does not matter (NO gpio0) + handleSwitch(); return; + } + + //momentary button logic if (isButtonPressed()) //pressed { if (!buttonPressedBefore) buttonPressedTime = millis(); @@ -48,7 +82,7 @@ void handleButton() else if (!isButtonPressed() && buttonPressedBefore) //released { long dur = millis() - buttonPressedTime; - if (dur < 50) {buttonPressedBefore = false; return;} //too short "press", debounce + if (dur < WLED_DEBOUNCE_THRESHOLD) {buttonPressedBefore = false; return;} //too short "press", debounce bool doublePress = buttonWaitTime; buttonWaitTime = 0; diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index 1261140..4955eec 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -137,7 +137,7 @@ void deserializeConfig() { if (hw_led["rev"]) busses.getBus(0)->reversed = true; //set 0.11 global reversed setting for first bus JsonObject hw_btn_ins_0 = hw[F("btn")][F("ins")][0]; - CJSON(buttonEnabled, hw_btn_ins_0["type"]); + CJSON(buttonType, hw_btn_ins_0["type"]); int hw_btn_pin = hw_btn_ins_0[F("pin")][0]; if (pinManager.allocatePin(hw_btn_pin,false)) { btnPin = hw_btn_pin; @@ -477,7 +477,7 @@ void serializeConfig() { // button BTNPIN JsonObject hw_btn_ins_0 = hw_btn_ins.createNestedObject(); - hw_btn_ins_0["type"] = (buttonEnabled) ? BTN_TYPE_PUSH : BTN_TYPE_NONE; + hw_btn_ins_0["type"] = buttonType; JsonArray hw_btn_ins_0_pin = hw_btn_ins_0.createNestedArray("pin"); hw_btn_ins_0_pin.add(btnPin); diff --git a/wled00/const.h b/wled00/const.h index e6d6d1f..89492a1 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -135,7 +135,7 @@ #define BTN_TYPE_RESERVED 1 #define BTN_TYPE_PUSH 2 #define BTN_TYPE_PUSH_ACT_HIGH 3 //not implemented -#define BTN_TYPE_SWITCH 4 //not implemented +#define BTN_TYPE_SWITCH 4 #define BTN_TYPE_SWITCH_ACT_HIGH 5 //not implemented //Ethernet board types diff --git a/wled00/data/settings_sync.htm b/wled00/data/settings_sync.htm index 37b56e6..c1e2819 100644 --- a/wled00/data/settings_sync.htm +++ b/wled00/data/settings_sync.htm @@ -16,7 +16,12 @@ function GetV(){var d=document;}

Sync setup

Button setup

-On/Off button enabled:
+Button type: +
Infrared remote:
Infrared remote: -

+Infrared remote:
IR info

WLED Broadcast

UDP Port:
2nd Port: hasArg(F("BT")); + buttonType = request->arg(F("BT")).toInt(); irEnabled = request->arg(F("IR")).toInt(); int t = request->arg(F("UP")).toInt(); if (t > 0) udpPort = t; diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 7116edb..5d34a91 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -422,8 +422,8 @@ void WLED::beginStrip() digitalWrite(rlyPin, (rlyMde ? bri : !bri)); // disable button if it is "pressed" unintentionally - if (btnPin>=0 && isButtonPressed()) - buttonEnabled = false; + if (btnPin>=0 && buttonType == BTN_TYPE_PUSH && isButtonPressed()) + buttonType = BTN_TYPE_NONE; } void WLED::initAP(bool resetAP) diff --git a/wled00/wled.h b/wled00/wled.h index a0fa72f..75b2704 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2104030 +#define VERSION 2104120 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG @@ -254,7 +254,7 @@ WLED_GLOBAL NodesMap Nodes; WLED_GLOBAL bool nodeListEnabled _INIT(true); WLED_GLOBAL bool nodeBroadcastEnabled _INIT(true); -WLED_GLOBAL bool buttonEnabled _INIT(true); +WLED_GLOBAL byte buttonType _INIT(BTN_TYPE_PUSH); WLED_GLOBAL byte irEnabled _INIT(0); // Infrared receiver WLED_GLOBAL uint16_t udpPort _INIT(21324); // WLED notifier default port diff --git a/wled00/wled_eeprom.cpp b/wled00/wled_eeprom.cpp index cc72fd1..3247fbf 100644 --- a/wled00/wled_eeprom.cpp +++ b/wled00/wled_eeprom.cpp @@ -93,7 +93,7 @@ void loadSettingsFromEEPROM() notifyButton = EEPROM.read(230); notifyTwice = EEPROM.read(231); - buttonEnabled = EEPROM.read(232); + buttonType = EEPROM.read(232) ? BTN_TYPE_PUSH : BTN_TYPE_NONE; staticIP[0] = EEPROM.read(234); staticIP[1] = EEPROM.read(235); diff --git a/wled00/xml.cpp b/wled00/xml.cpp index dc572a0..f8fb0c5 100644 --- a/wled00/xml.cpp +++ b/wled00/xml.cpp @@ -349,7 +349,7 @@ void getSettingsJS(byte subPage, char* dest) if (subPage == 4) { - sappend('c',SET_F("BT"),buttonEnabled); + sappend('v',SET_F("BT"),buttonType); sappend('v',SET_F("IR"),irEnabled); sappend('v',SET_F("UP"),udpPort); sappend('v',SET_F("U2"),udpPort2);