From ed2e083d13811c940486d91469ba1ec938f4ab7f Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Wed, 19 Jan 2022 22:15:08 +0100 Subject: [PATCH] Added button 0 doubleclick for net info. --- .../usermod_v2_four_line_display_ALT.h | 66 +++++++++++++++++++ wled00/button.cpp | 9 +-- 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/usermods/usermod_v2_four_line_display_ALT/usermod_v2_four_line_display_ALT.h b/usermods/usermod_v2_four_line_display_ALT/usermod_v2_four_line_display_ALT.h index b9f02279..3d30dc20 100644 --- a/usermods/usermod_v2_four_line_display_ALT/usermod_v2_four_line_display_ALT.h +++ b/usermods/usermod_v2_four_line_display_ALT/usermod_v2_four_line_display_ALT.h @@ -840,6 +840,72 @@ class FourLineDisplayUsermod : public Usermod { } } + /** + * handleButton() can be used to override default button behaviour. Returning true + * will prevent button working in a default way. + * Replicating button.cpp + */ + bool handleButton(uint8_t b) { + yield(); + if (!enabled + || b // butto 0 only + || buttonType[b] == BTN_TYPE_SWITCH + || buttonType[b] == BTN_TYPE_NONE + || buttonType[b] == BTN_TYPE_RESERVED + || buttonType[b] == BTN_TYPE_PIR_SENSOR + || buttonType[b] == BTN_TYPE_ANALOG + || buttonType[b] == BTN_TYPE_ANALOG_INVERTED) { + return false; + } + + unsigned long now = millis(); + static bool buttonPressedBefore = false; + static bool buttonLongPressed = false; + static unsigned long buttonPressedTime = 0; + static unsigned long buttonWaitTime = 0; + bool handled = false; + + //momentary button logic + if (isButtonPressed(b)) { //pressed + + if (!buttonPressedBefore) buttonPressedTime = now; + buttonPressedBefore = true; + + if (now - buttonPressedTime > 600) { //long press + buttonLongPressed = true; + } + + } else if (!isButtonPressed(b) && buttonPressedBefore) { //released + + long dur = now - buttonPressedTime; + if (dur < 50) { + buttonPressedBefore = false; + return true; + } //too short "press", debounce + + bool doublePress = buttonWaitTime; //did we have short press before? + buttonWaitTime = 0; + + if (!buttonLongPressed) { //short press + // if this is second release within 350ms it is a double press (buttonWaitTime!=0) + //TODO: handleButton() handles button 0 without preset in a different way for double click + if (doublePress) { + networkOverlay(PSTR("NETWORK INFO"),7000); + handled = true; + } else { + buttonWaitTime = now; + } + } + buttonPressedBefore = false; + buttonLongPressed = false; + } + // if 450ms elapsed since last press/release it is a short press + if (buttonWaitTime && now - buttonWaitTime > 350 && !buttonPressedBefore) { + buttonWaitTime = 0; + } + return handled; + } + /* * addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API. * Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI. diff --git a/wled00/button.cpp b/wled00/button.cpp index 20e70305..d7ec7609 100644 --- a/wled00/button.cpp +++ b/wled00/button.cpp @@ -257,15 +257,16 @@ void handleButton() if (b == 0 && dur > WLED_LONG_AP) { //long press on button 0 (when released) WLED::instance().initAP(true); } else if (!buttonLongPressed[b]) { //short press - if (b == 0 && !macroDoublePress[b]) { //don't wait for double press on button 0 if no double press macro set - shortPressAction(b); - } else { //double press if less than 350 ms between current press and previous short press release (buttonWaitTime!=0) + //NOTE: this interferes with double click handling in usermods so it is commented out + //if (b == 0 && !macroDoublePress[b]) { //don't wait for double press on button 0 if no double press macro set + // shortPressAction(b); + //} else { //double press if less than 350 ms between current press and previous short press release (buttonWaitTime!=0) if (doublePress) { doublePressAction(b); } else { buttonWaitTime[b] = now; } - } + //} } buttonPressedBefore[b] = false; buttonLongPressed[b] = false;