From 31f5b79bf89e5312c79de874e01fe21b0c268dc3 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Wed, 15 Feb 2023 11:06:53 +0100 Subject: [PATCH] Fix adding virtual switches --- tasmota/tasmota_support/support_switch_v4.ino | 24 +++++++++++-------- .../xdrv_16_tuyamcu_v1.ino | 7 +++--- .../xdrv_16_tuyamcu_v2.ino | 7 +++--- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/tasmota/tasmota_support/support_switch_v4.ino b/tasmota/tasmota_support/support_switch_v4.ino index 50bc7d428..fdde1bb25 100644 --- a/tasmota/tasmota_support/support_switch_v4.ino +++ b/tasmota/tasmota_support/support_switch_v4.ino @@ -74,13 +74,9 @@ void SwitchSetVirtualPinState(uint32_t index, uint32_t state) { bitWrite(Switch.virtual_pin, index, state); } -void SwitchSetState(uint32_t index, uint32_t state) { - // Set debounced pin state to be used by late detected switches - if (!bitRead(Switch.used, index)) { - bitSet(Switch.used, index); - AddLog(LOG_LEVEL_DEBUG, PSTR("SWT: Add vSwitch%d, State %d"), index +1, state); - } - Switch.debounced_state[index] = state; +uint8_t SwitchLastState(uint32_t index) { + // Get last state + return Switch.last_state[index]; } uint8_t SwitchGetState(uint32_t index) { @@ -88,9 +84,17 @@ uint8_t SwitchGetState(uint32_t index) { return Switch.debounced_state[index]; } -uint8_t SwitchLastState(uint32_t index) { - // Get last state - return Switch.last_state[index]; +void SwitchSetState(uint32_t index, uint32_t state) { + // Set debounced pin state to be used by late detected switches + if (!bitRead(Switch.used, index)) { + for (uint32_t i = 0; i <= index; i++) { + if (!bitRead(Switch.used, i)) { + bitSet(Switch.used, i); + AddLog(LOG_LEVEL_DEBUG, PSTR("SWT: Add vSwitch%d, State %d"), i +1, Switch.debounced_state[i]); + } + } + } + Switch.debounced_state[index] = state; } /*------------------------------------------------------------------------------------------*/ diff --git a/tasmota/tasmota_xdrv_driver/xdrv_16_tuyamcu_v1.ino b/tasmota/tasmota_xdrv_driver/xdrv_16_tuyamcu_v1.ino index 82bac2396..2e4386ad5 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_16_tuyamcu_v1.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_16_tuyamcu_v1.ino @@ -848,10 +848,11 @@ void TuyaProcessStatePacket(void) { if (Tuya.buffer[dpidStart + 4]) { PowerOff = true; } } } else if (fnId >= TUYA_MCU_FUNC_SWT1 && fnId <= TUYA_MCU_FUNC_SWT4) { - uint32_t switch_state = SwitchGetState(fnId - TUYA_MCU_FUNC_SWT1); - AddLog(LOG_LEVEL_DEBUG, PSTR("TYA: RX Switch-%d --> MCU State: %d Current State:%d"),fnId - TUYA_MCU_FUNC_SWT1 + 1,Tuya.buffer[dpidStart + 4], switch_state); + uint32_t switch_index = fnId - TUYA_MCU_FUNC_SWT1; + uint32_t switch_state = SwitchGetState(switch_index); + AddLog(LOG_LEVEL_DEBUG, PSTR("TYA: RX Switch-%d --> MCU State: %d Current State:%d"), switch_index +1, Tuya.buffer[dpidStart + 4], switch_state); if (switch_state != Tuya.buffer[dpidStart + 4]) { - SwitchSetState(fnId - TUYA_MCU_FUNC_SWT1, Tuya.buffer[dpidStart + 4]); + SwitchSetState(switch_index, Tuya.buffer[dpidStart + 4]); } } if (PowerOff) { Tuya.ignore_dimmer_cmd_timeout = millis() + 250; } diff --git a/tasmota/tasmota_xdrv_driver/xdrv_16_tuyamcu_v2.ino b/tasmota/tasmota_xdrv_driver/xdrv_16_tuyamcu_v2.ino index 640f5574e..84589b42c 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_16_tuyamcu_v2.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_16_tuyamcu_v2.ino @@ -1639,10 +1639,11 @@ void TuyaProcessRxedDP(uint8_t dpid, uint8_t type, uint8_t *data, int dpDataLen) if (value) { PowerOff = true; } } } else if (fnId >= TUYA_MCU_FUNC_SWT1 && fnId <= TUYA_MCU_FUNC_SWT4) { - uint32_t switch_state = SwitchGetState(fnId - TUYA_MCU_FUNC_SWT1); - AddLog(LOG_LEVEL_DEBUG, PSTR("T:fn%d Switch%d --> M%d T%d"),fnId, fnId - TUYA_MCU_FUNC_SWT1 + 1, value, switch_state); + uint32_t switch_index = fnId - TUYA_MCU_FUNC_SWT1; + uint32_t switch_state = SwitchGetState(switch_index); + AddLog(LOG_LEVEL_DEBUG, PSTR("T:fn%d Switch%d --> M%d T%d"), fnId, switch_index +1, value, switch_state); if (switch_state != value) { - SwitchSetState(fnId - TUYA_MCU_FUNC_SWT1, value); + SwitchSetState(switch_index, value); } } //if (PowerOff) { pTuya->ignore_dimmer_cmd_timeout = millis() + 250; }