From d043ac770d47801dc20a59066ee96e30250c54d4 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sat, 14 Mar 2020 13:13:33 +0100 Subject: [PATCH] Refactor support_switche.ino - Add commands ``SwitchMode 13`` PushOn and ``SwitchMode 14`` PushOnInverted (#7912) - Refactor support_switche.ino --- RELEASENOTES.md | 1 + tasmota/CHANGELOG.md | 1 + tasmota/support_switch.ino | 73 ++++++++++++++++++++++++-------------- tasmota/tasmota.h | 4 +-- 4 files changed, 50 insertions(+), 29 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index c318212f9..a2757b96f 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -87,6 +87,7 @@ The following binary downloads have been compiled with ESP8266/Arduino library c - Add command ``ShutterButton `` to control shutter(s) by to-scho (#7403) - Add commands ``SwitchMode 8`` ToggleMulti, ``SwitchMode 9`` FollowMulti and ``SwitchMode 10`` FollowMultiInverted (#7522) - Add commands ``SwitchMode 11`` PushHoldMulti and ``SwitchMode 12`` PushHoldInverted (#7603) +- Add commands ``SwitchMode 13`` PushOn and ``SwitchMode 14`` PushOnInverted (#7912) - Add command ``Buzzer -1`` for infinite mode and command ``Buzzer -2`` for following led mode (#7623) - Add SerialConfig to ``Status 1`` - Add WifiPower to ``Status 5`` diff --git a/tasmota/CHANGELOG.md b/tasmota/CHANGELOG.md index cf8694d55..2c23efc42 100644 --- a/tasmota/CHANGELOG.md +++ b/tasmota/CHANGELOG.md @@ -4,6 +4,7 @@ - Add HAss Discovery support for Button and Switch triggers by Federico Leoni (#7901) - Add support for HDC1080 Temperature and Humidity sensor by Luis Teixeira (#7888) +- Add commands ``SwitchMode 13`` PushOn and ``SwitchMode 14`` PushOnInverted (#7912) ### 8.1.0.10 20200227 diff --git a/tasmota/support_switch.ino b/tasmota/support_switch.ino index e0be915e9..7acdda409 100644 --- a/tasmota/support_switch.ino +++ b/tasmota/support_switch.ino @@ -17,8 +17,8 @@ along with this program. If not, see . */ -#define SWITCH_V2 -#ifdef SWITCH_V2 +#define SWITCH_V3 +#ifdef SWITCH_V3 /*********************************************************************************************\ * Switch support with input filter * @@ -166,20 +166,20 @@ void SwitchHandler(uint8_t mode) switchflag = ~button &1; // Follow inverted wall switch state after hold break; case PUSHHOLDMULTI: - if (NOT_PRESSED == button){ + if (NOT_PRESSED == button) { Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 25; SendKey(KEY_SWITCH, i +1, POWER_INCREMENT); // Execute command via MQTT - } - else + } else { SendKey(KEY_SWITCH, i +1, POWER_CLEAR); // Execute command via MQTT + } break; case PUSHHOLDMULTI_INV: - if (PRESSED == button){ + if (PRESSED == button) { Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 25; SendKey(KEY_SWITCH, i +1, POWER_INCREMENT); // Execute command via MQTT - } - else + } else { SendKey(KEY_SWITCH, i +1, POWER_CLEAR); // Execute command via MQTT + } break; default: SendKey(KEY_SWITCH, i +1, POWER_HOLD); // Execute command via MQTT @@ -188,9 +188,7 @@ void SwitchHandler(uint8_t mode) } } -// enum SwitchModeOptions {TOGGLE, FOLLOW, FOLLOW_INV, PUSHBUTTON, PUSHBUTTON_INV, PUSHBUTTONHOLD, PUSHBUTTONHOLD_INV, PUSHBUTTON_TOGGLE, TOGGLEMULTI, FOLLOWMULTI, FOLLOWMULTI_INV, PUSHHOLDMULTI, PUSHHOLDMULTI_INV, PUSHON, MAX_SWITCH_OPTION}; - - if (button != Switch.last_state[i]) { + if (button != Switch.last_state[i]) { // This implies if ((PRESSED == button) then (NOT_PRESSED == Switch.last_state[i])) switch (Settings.switchmode[i]) { case TOGGLE: case PUSHBUTTON_TOGGLE: @@ -203,29 +201,35 @@ void SwitchHandler(uint8_t mode) switchflag = ~button &1; // Follow inverted wall switch state break; case PUSHBUTTON: - if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i])) { +// if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i])) { + if (PRESSED == button) { switchflag = POWER_TOGGLE; // Toggle with pushbutton to Gnd } break; case PUSHBUTTON_INV: - if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i])) { +// if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i])) { + if (NOT_PRESSED == button) { switchflag = POWER_TOGGLE; // Toggle with releasing pushbutton from Gnd } break; case PUSHBUTTONHOLD: - if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i])) { +// if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i])) { + if (PRESSED == button) { Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 10; // Start timer on button press } - if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i]) && (Switch.hold_timer[i])) { +// if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i]) && (Switch.hold_timer[i])) { + if ((NOT_PRESSED == button) && (Switch.hold_timer[i])) { Switch.hold_timer[i] = 0; // Button released and hold timer not expired : stop timer... switchflag = POWER_TOGGLE; // ...and Toggle } break; case PUSHBUTTONHOLD_INV: - if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i])) { +// if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i])) { + if (NOT_PRESSED == button) { Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 10; // Start timer on button press... } - if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i]) && (Switch.hold_timer[i])) { +// if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i]) && (Switch.hold_timer[i])) { + if ((PRESSED == button) && (Switch.hold_timer[i])) { Switch.hold_timer[i] = 0; // Button released and hold timer not expired : stop timer. switchflag = POWER_TOGGLE; // ...and Toggle } @@ -253,31 +257,46 @@ void SwitchHandler(uint8_t mode) } break; case PUSHHOLDMULTI: - if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i])) { - if(Switch.hold_timer[i]!=0) +// if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i])) { + if (NOT_PRESSED == button) { + if (Switch.hold_timer[i] != 0) { SendKey(KEY_SWITCH, i +1, POWER_INV); // Execute command via MQTT + } Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 10; } - if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i])) { - if(Switch.hold_timer[i] > loops_per_second * Settings.param[P_HOLD_TIME] / 25) +// if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i])) { + if (PRESSED == button) { + if (Switch.hold_timer[i] > loops_per_second * Settings.param[P_HOLD_TIME] / 25) { switchflag = POWER_TOGGLE; // Toggle with pushbutton + } Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 10; } break; case PUSHHOLDMULTI_INV: - if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i])) { - if(Switch.hold_timer[i]!=0) +// if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i])) { + if (PRESSED == button) { + if (Switch.hold_timer[i] != 0) { SendKey(KEY_SWITCH, i +1, POWER_INV); // Execute command via MQTT + } Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 10; } - if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i])) { - if(Switch.hold_timer[i] > loops_per_second * Settings.param[P_HOLD_TIME] / 25) +// if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i])) { + if (NOT_PRESSED == button) { + if (Switch.hold_timer[i] > loops_per_second * Settings.param[P_HOLD_TIME] / 25) { switchflag = POWER_TOGGLE; // Toggle with pushbutton + } Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 10; } break; case PUSHON: - if (PRESSED == button) switchflag = POWER_ON; // Power ON with pushbutton to Gnd + if (PRESSED == button) { + switchflag = POWER_ON; // Power ON with pushbutton to Gnd + } + break; + case PUSHON_INV: + if (NOT_PRESSED == button) { + switchflag = POWER_ON; // Power ON with releasing pushbutton from Gnd + } break; } Switch.last_state[i] = button; @@ -301,4 +320,4 @@ void SwitchLoop(void) } } -#endif // SWITCH_V2 +#endif // SWITCH_V3 diff --git a/tasmota/tasmota.h b/tasmota/tasmota.h index 919193f09..6e9c8ac29 100644 --- a/tasmota/tasmota.h +++ b/tasmota/tasmota.h @@ -233,7 +233,7 @@ enum LoggingLevels {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_D enum WifiConfigOptions {WIFI_RESTART, EX_WIFI_SMARTCONFIG, WIFI_MANAGER, EX_WIFI_WPSCONFIG, WIFI_RETRY, WIFI_WAIT, WIFI_SERIAL, WIFI_MANAGER_RESET_ONLY, MAX_WIFI_OPTION}; enum SwitchModeOptions {TOGGLE, FOLLOW, FOLLOW_INV, PUSHBUTTON, PUSHBUTTON_INV, PUSHBUTTONHOLD, PUSHBUTTONHOLD_INV, PUSHBUTTON_TOGGLE, TOGGLEMULTI, - FOLLOWMULTI, FOLLOWMULTI_INV, PUSHHOLDMULTI, PUSHHOLDMULTI_INV, PUSHON, MAX_SWITCH_OPTION}; + FOLLOWMULTI, FOLLOWMULTI_INV, PUSHHOLDMULTI, PUSHHOLDMULTI_INV, PUSHON, PUSHON_INV, MAX_SWITCH_OPTION}; enum LedStateOptions {LED_OFF, LED_POWER, LED_MQTTSUB, LED_POWER_MQTTSUB, LED_MQTTPUB, LED_POWER_MQTTPUB, LED_MQTT, LED_POWER_MQTT, MAX_LED_OPTION}; @@ -317,7 +317,7 @@ enum DeviceGroupItem { DGR_ITEM_EOL, DGR_ITEM_STATUS, DGR_ITEM_LAST_16BIT, DGR_ITEM_MAX_16BIT = 127, DGR_ITEM_POWER, DGR_ITEM_DIMMER_RANGE, // Add new 32-bit items before this line - DGR_ITEM_LAST_32BIT, DGR_ITEM_MAX_32BIT = 191, + DGR_ITEM_LAST_32BIT, DGR_ITEM_MAX_32BIT = 191, // Add new string items before this line DGR_ITEM_LAST_STRING, DGR_ITEM_MAX_STRING = 223, DGR_ITEM_LIGHT_CHANNELS };