From 2a8d8f98746b51b3846e8c65bb1fbcd6a911c850 Mon Sep 17 00:00:00 2001 From: RemiDing Date: Sun, 9 Aug 2020 08:14:09 +0200 Subject: [PATCH] use Switch mode definitions --- tasmota/support_switch.ino | 30 +++++++++++++++--------------- tasmota/tasmota.h | 6 ++++++ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/tasmota/support_switch.ino b/tasmota/support_switch.ino index e0c4049ab..eea4a1aee 100644 --- a/tasmota/support_switch.ino +++ b/tasmota/support_switch.ino @@ -231,9 +231,9 @@ void SwitchHandler(uint8_t mode) uint8_t button = Switch.virtual_state[i]; uint8_t switchflag = POWER_TOGGLE +1; - if (Switch.hold_timer[i] & (((Settings.switchmode[i] == PUSHHOLDMULTI) | (Settings.switchmode[i] == PUSHHOLDMULTI_INV)) ? 0x3F: 0xFF)) { + if (Switch.hold_timer[i] & (((Settings.switchmode[i] == PUSHHOLDMULTI) | (Settings.switchmode[i] == PUSHHOLDMULTI_INV)) ? SM_TIMER_MASK: SM_NO_TIMER_MASK)) { Switch.hold_timer[i]--; - if ((Switch.hold_timer[i] & 0x3F) == loops_per_second * Settings.param[P_HOLD_TIME] / 25) { + if ((Switch.hold_timer[i] & SM_TIMER_MASK) == loops_per_second * Settings.param[P_HOLD_TIME] / 25) { if ((Settings.switchmode[i] == PUSHHOLDMULTI) & (NOT_PRESSED == Switch.last_state[i])) { SendKey(KEY_SWITCH, i +1, POWER_INCREMENT); // Execute command via MQTT } @@ -241,7 +241,7 @@ void SwitchHandler(uint8_t mode) SendKey(KEY_SWITCH, i +1, POWER_INCREMENT); // Execute command via MQTT } } - if (0 == (Switch.hold_timer[i] & (((Settings.switchmode[i] == PUSHHOLDMULTI) | (Settings.switchmode[i] == PUSHHOLDMULTI_INV)) ? 0x3F: 0xFF))) { + if (0 == (Switch.hold_timer[i] & (((Settings.switchmode[i] == PUSHHOLDMULTI) | (Settings.switchmode[i] == PUSHHOLDMULTI_INV)) ? SM_TIMER_MASK: SM_NO_TIMER_MASK))) { switch (Settings.switchmode[i]) { case TOGGLEMULTI: switchflag = POWER_TOGGLE; // Toggle after hold @@ -329,14 +329,14 @@ void SwitchHandler(uint8_t mode) break; case PUSHHOLDMULTI: if (NOT_PRESSED == button) { - if ((Switch.hold_timer[i] & 0x3F) != 0) { - Switch.hold_timer[i] = ((Switch.hold_timer[i] & 0xc0) == 0x40) ? 0x80 : 0; + if ((Switch.hold_timer[i] & SM_TIMER_MASK) != 0) { + Switch.hold_timer[i] = ((Switch.hold_timer[i] & ~SM_TIMER_MASK)) == SM_FIRST_PRESS) ? SM_SECOND_PRESS : 0; SendKey(KEY_SWITCH, i +1, POWER_INV); // Execute command via MQTT } } else { - if ((Switch.hold_timer[i] & 0x3F) > loops_per_second * Settings.param[P_HOLD_TIME] / 25) { - if((Switch.hold_timer[i] & 0xC0) != 0x80) { - Switch.hold_timer[i]= 0x40; + if ((Switch.hold_timer[i] & SM_TIMER_MASK) > loops_per_second * Settings.param[P_HOLD_TIME] / 25) { + if((Switch.hold_timer[i] & ~SM_TIMER_MASK) != SM_SECOND_PRESS) { + Switch.hold_timer[i]= SM_FIRST_PRESS; switchflag = POWER_TOGGLE; // Toggle with pushbutton } else{ @@ -348,18 +348,18 @@ void SwitchHandler(uint8_t mode) SendKey(KEY_SWITCH, i +1, POWER_RELEASE); // Execute command via MQTT } } - Switch.hold_timer[i] = (Switch.hold_timer[i] & 0xC0) | loops_per_second * Settings.param[P_HOLD_TIME] / 10; + Switch.hold_timer[i] = (Switch.hold_timer[i] & ~SM_TIMER_MASK) | loops_per_second * Settings.param[P_HOLD_TIME] / 10; break; case PUSHHOLDMULTI_INV: if (PRESSED == button) { - if ((Switch.hold_timer[i] & 0x3F) != 0) { - Switch.hold_timer[i] = ((Switch.hold_timer[i] & 0xc0) == 0x40) ? 0x80 : 0; + if ((Switch.hold_timer[i] & SM_TIMER_MASK) != 0) { + Switch.hold_timer[i] = ((Switch.hold_timer[i] & ~SM_TIMER_MASK) == SM_FIRST_PRESS) ? SM_SECOND_PRESS : 0; SendKey(KEY_SWITCH, i +1, POWER_INV); // Execute command via MQTT } } else { - if ((Switch.hold_timer[i] & 0x3F)> loops_per_second * Settings.param[P_HOLD_TIME] / 25) { - if((Switch.hold_timer[i] & 0xC0) != 0x80) { - Switch.hold_timer[i]= 0x40; + if ((Switch.hold_timer[i] & SM_TIMER_MASK)> loops_per_second * Settings.param[P_HOLD_TIME] / 25) { + if((Switch.hold_timer[i] & ~SM_TIMER_MASK) != SM_SECOND_PRESS) { + Switch.hold_timer[i]= SM_FIRST_PRESS; switchflag = POWER_TOGGLE; // Toggle with pushbutton } else{ @@ -371,7 +371,7 @@ void SwitchHandler(uint8_t mode) SendKey(KEY_SWITCH, i +1, POWER_RELEASE); // Execute command via MQTT } } - Switch.hold_timer[i] = (Switch.hold_timer[i] & 0xC0) | loops_per_second * Settings.param[P_HOLD_TIME] / 10; + Switch.hold_timer[i] = (Switch.hold_timer[i] & ~SM_TIMER_MASK) | loops_per_second * Settings.param[P_HOLD_TIME] / 10; break; case PUSHON: if (PRESSED == button) { diff --git a/tasmota/tasmota.h b/tasmota/tasmota.h index 5cc304943..ba8c9e302 100644 --- a/tasmota/tasmota.h +++ b/tasmota/tasmota.h @@ -209,6 +209,12 @@ const uint32_t LOOP_SLEEP_DELAY = 50; // Lowest number of milliseconds to #define KNX_MAX_device_param 31 #define MAX_KNXTX_CMNDS 5 +// Switch Mode definietions +#define SM_TIMER_MASK 0x3F +#define SM_NO_TIMER_MASK 0xFF +#define SM_FIRST_PRESS 0x40 +#define SM_SECOND_PRESS 0x80 + /*********************************************************************************************\ * Enumeration \*********************************************************************************************/