From 9ca5e9471515d4a626ebf20dff892ff6e4c9c595 Mon Sep 17 00:00:00 2001 From: RemiDing Date: Tue, 4 Aug 2020 22:53:44 +0200 Subject: [PATCH 1/3] detect double click --- tasmota/support_switch.ino | 42 ++++++++++++++++++++++++++++---------- tasmota/tasmota.h | 2 +- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/tasmota/support_switch.ino b/tasmota/support_switch.ino index 9fc3b57c3..e0c4049ab 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]) { + if (Switch.hold_timer[i] & (((Settings.switchmode[i] == PUSHHOLDMULTI) | (Settings.switchmode[i] == PUSHHOLDMULTI_INV)) ? 0x3F: 0xFF)) { Switch.hold_timer[i]--; - if (Switch.hold_timer[i] == loops_per_second * Settings.param[P_HOLD_TIME] / 25) { + if ((Switch.hold_timer[i] & 0x3F) == 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]) { + if (0 == (Switch.hold_timer[i] & (((Settings.switchmode[i] == PUSHHOLDMULTI) | (Settings.switchmode[i] == PUSHHOLDMULTI_INV)) ? 0x3F: 0xFF))) { switch (Settings.switchmode[i]) { case TOGGLEMULTI: switchflag = POWER_TOGGLE; // Toggle after hold @@ -257,6 +257,7 @@ void SwitchHandler(uint8_t mode) 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 { + Switch.hold_timer[i]= 0; SendKey(KEY_SWITCH, i +1, POWER_CLEAR); // Execute command via MQTT } break; @@ -265,6 +266,7 @@ void SwitchHandler(uint8_t mode) 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 { + Switch.hold_timer[i]= 0; SendKey(KEY_SWITCH, i +1, POWER_CLEAR); // Execute command via MQTT } break; @@ -327,31 +329,49 @@ void SwitchHandler(uint8_t mode) break; case PUSHHOLDMULTI: if (NOT_PRESSED == button) { - if (Switch.hold_timer[i] != 0) { + if ((Switch.hold_timer[i] & 0x3F) != 0) { + Switch.hold_timer[i] = ((Switch.hold_timer[i] & 0xc0) == 0x40) ? 0x80 : 0; SendKey(KEY_SWITCH, i +1, POWER_INV); // Execute command via MQTT } } else { - if (Switch.hold_timer[i] > loops_per_second * Settings.param[P_HOLD_TIME] / 25) { - switchflag = POWER_TOGGLE; // Toggle with pushbutton + 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; + switchflag = POWER_TOGGLE; // Toggle with pushbutton + } + else{ + SendKey(KEY_SWITCH, i +1, POWER_100); // Execute command via MQTT + Switch.hold_timer[i]= 0; + } } else { + Switch.hold_timer[i]= 0; SendKey(KEY_SWITCH, i +1, POWER_RELEASE); // Execute command via MQTT } } - Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 10; + Switch.hold_timer[i] = (Switch.hold_timer[i] & 0xC0) | loops_per_second * Settings.param[P_HOLD_TIME] / 10; break; case PUSHHOLDMULTI_INV: if (PRESSED == button) { - if (Switch.hold_timer[i] != 0) { + if ((Switch.hold_timer[i] & 0x3F) != 0) { + Switch.hold_timer[i] = ((Switch.hold_timer[i] & 0xc0) == 0x40) ? 0x80 : 0; SendKey(KEY_SWITCH, i +1, POWER_INV); // Execute command via MQTT } } else { - if (Switch.hold_timer[i] > loops_per_second * Settings.param[P_HOLD_TIME] / 25) { - switchflag = POWER_TOGGLE; // Toggle with pushbutton + 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; + switchflag = POWER_TOGGLE; // Toggle with pushbutton + } + else{ + SendKey(KEY_SWITCH, i +1, POWER_100); // Execute command via MQTT + Switch.hold_timer[i]= 0; + } } else { + Switch.hold_timer[i]= 0; SendKey(KEY_SWITCH, i +1, POWER_RELEASE); // Execute command via MQTT } } - Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 10; + Switch.hold_timer[i] = (Switch.hold_timer[i] & 0xC0) | 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 21a7b5b00..d215b96f2 100644 --- a/tasmota/tasmota.h +++ b/tasmota/tasmota.h @@ -234,7 +234,7 @@ enum TopicOptions { CMND, STAT, TELE, nu1, RESULT_OR_CMND, RESULT_OR_STAT, RESUL enum ExecuteCommandPowerOptions { POWER_OFF, POWER_ON, POWER_TOGGLE, POWER_BLINK, POWER_BLINK_STOP, POWER_OFF_NO_STATE = 8, POWER_ON_NO_STATE, POWER_TOGGLE_NO_STATE, POWER_SHOW_STATE = 16 }; -enum SendKeyPowerOptions { POWER_HOLD = 3, POWER_INCREMENT = 4, POWER_INV = 5, POWER_CLEAR = 6, POWER_RELEASE = 7, CLEAR_RETAIN = 9 }; +enum SendKeyPowerOptions { POWER_HOLD = 3, POWER_INCREMENT = 4, POWER_INV = 5, POWER_CLEAR = 6, POWER_RELEASE = 7, POWER_100 = 8, CLEAR_RETAIN = 9 }; enum SendKeyOptions { KEY_BUTTON, KEY_SWITCH }; enum SendKeyMultiClick { SINGLE = 10, DOUBLE = 11, TRIPLE = 12, QUAD = 13, PENTA = 14}; From 2a8d8f98746b51b3846e8c65bb1fbcd6a911c850 Mon Sep 17 00:00:00 2001 From: RemiDing Date: Sun, 9 Aug 2020 08:14:09 +0200 Subject: [PATCH 2/3] 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 \*********************************************************************************************/ From 9f4d90ec133331fe1525d29877ba31b0491f1589 Mon Sep 17 00:00:00 2001 From: RemiDing Date: Sun, 9 Aug 2020 08:34:03 +0200 Subject: [PATCH 3/3] removed compile errors --- tasmota/support_switch.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasmota/support_switch.ino b/tasmota/support_switch.ino index eea4a1aee..bd127d6c5 100644 --- a/tasmota/support_switch.ino +++ b/tasmota/support_switch.ino @@ -330,7 +330,7 @@ void SwitchHandler(uint8_t mode) case PUSHHOLDMULTI: if (NOT_PRESSED == button) { 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; + 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 {