From 0cdab52418959bc7084846617bc24a08bf25b403 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Wed, 2 Jun 2021 17:13:09 +0200 Subject: [PATCH] Added. - PIS sensor switch option for button Bugfix. - proper switch high/low handling --- wled00/button.cpp | 14 +++++++++----- wled00/const.h | 1 + wled00/data/settings_leds.htm | 5 +++-- wled00/html_settings.h | 2 +- wled00/wled.h | 2 +- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/wled00/button.cpp b/wled00/button.cpp index 092ccbebe..f296ea455 100644 --- a/wled00/button.cpp +++ b/wled00/button.cpp @@ -39,6 +39,7 @@ bool isButtonPressed(uint8_t i) break; case BTN_TYPE_PUSH_ACT_HIGH: case BTN_TYPE_SWITCH_ACT_HIGH: + case BTN_TYPE_PIR_SENSOR: if (digitalRead(btnPin[i]) == HIGH) return true; break; case BTN_TYPE_TOUCH: @@ -52,6 +53,7 @@ bool isButtonPressed(uint8_t i) void handleSwitch(uint8_t b) { + // isButtonPressed() handles inverted/noninverted logic if (buttonPressedBefore[b] != isButtonPressed(b)) { buttonPressedTime[b] = millis(); buttonPressedBefore[b] = !buttonPressedBefore[b]; @@ -60,12 +62,12 @@ void handleSwitch(uint8_t b) if (buttonLongPressed[b] == buttonPressedBefore[b]) return; if (millis() - buttonPressedTime[b] > WLED_DEBOUNCE_THRESHOLD) { //fire edge event only after 50ms without change (debounce) - if (buttonPressedBefore[b] ^ (buttonType[b]==BTN_TYPE_SWITCH_ACT_HIGH)) { //LOW, falling edge, switch closed + if (!buttonPressedBefore[b]) { // on -> off if (macroButton[b]) applyPreset(macroButton[b]); else { //turn on if (!bri) {toggleOnOff(); colorUpdated(NOTIFIER_CALL_MODE_BUTTON);} } - } else { //HIGH, rising edge, switch opened + } else { // off -> on if (macroLongPress[b]) applyPreset(macroLongPress[b]); else { //turn off if (bri) {toggleOnOff(); colorUpdated(NOTIFIER_CALL_MODE_BUTTON);} @@ -75,8 +77,9 @@ void handleSwitch(uint8_t b) // publish MQTT message if (WLED_MQTT_CONNECTED) { char subuf[64]; - sprintf_P(subuf, _mqtt_topic_button, mqttDeviceTopic, (int)b); - mqtt->publish(subuf, 0, false, (buttonPressedBefore[b] ^ (buttonType[b]==BTN_TYPE_SWITCH_ACT_HIGH)) ? "on" : "off"); + if (buttonType[b] == BTN_TYPE_PIR_SENSOR) sprintf_P(subuf, PSTR("%s/motion/%d"), mqttDeviceTopic, (int)b); + else sprintf_P(subuf, _mqtt_topic_button, mqttDeviceTopic, (int)b); + mqtt->publish(subuf, 0, false, !buttonPressedBefore[b] ? "off" : "on"); } buttonLongPressed[b] = buttonPressedBefore[b]; //save the last "long term" switch state @@ -178,7 +181,8 @@ void handleButton() handleAnalog(b); continue; } - if (buttonType[b] == BTN_TYPE_SWITCH || buttonType[b] == BTN_TYPE_SWITCH_ACT_HIGH) { //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_SWITCH_ACT_HIGH || buttonType[b] == BTN_TYPE_PIR_SENSOR) { handleSwitch(b); continue; } diff --git a/wled00/const.h b/wled00/const.h index 9436d7dd9..93d860fba 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -162,6 +162,7 @@ #define BTN_TYPE_TOUCH 6 #define BTN_TYPE_ANALOG 7 #define BTN_TYPE_ANALOG_INVERTED 8 +#define BTN_TYPE_PIR_SENSOR 9 //Ethernet board types #define WLED_NUM_ETH_TYPES 5 diff --git a/wled00/data/settings_leds.htm b/wled00/data/settings_leds.htm index c2a0bd6ab..ebaaa5d2b 100644 --- a/wled00/data/settings_leds.htm +++ b/wled00/data/settings_leds.htm @@ -304,11 +304,12 @@ Reverse (rotated 180°): c += ``; c += ``; c += ``; - c += ``; - c += ``; + c += ``; + c += ``; c += ``; c += ``; c += ``; + c += ``; c += ``; c += ` ×
`; gId("btns").innerHTML = c; diff --git a/wled00/html_settings.h b/wled00/html_settings.h index 2e02050cd..d2dc5e349 100644 --- a/wled00/html_settings.h +++ b/wled00/html_settings.h @@ -74,7 +74,7 @@ Do not enable if WiFi is working correctly, increases power consumption.
LED Settings