From a994c71de41fba43d5a9d9139fb8169165e6f5ac Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 19 Feb 2023 15:28:20 +0100 Subject: [PATCH] Refactor device_present calculation --- tasmota/tasmota_support/support_button_v4.ino | 44 ++++++++++++++++--- tasmota/tasmota_support/support_tasmota.ino | 32 +++++++------- .../tasmota_xdrv_driver/xdrv_66_tm1638.ino | 16 +------ 3 files changed, 55 insertions(+), 37 deletions(-) diff --git a/tasmota/tasmota_support/support_button_v4.ino b/tasmota/tasmota_support/support_button_v4.ino index a92963270..86967e84a 100644 --- a/tasmota/tasmota_support/support_button_v4.ino +++ b/tasmota/tasmota_support/support_button_v4.ino @@ -233,6 +233,9 @@ void ButtonProbe(void) { void ButtonInit(void) { bool ac_detect = (Settings->button_debounce % 10 == 9); Button.used = 0; +/* + uint32_t last_used = 0; +*/ for (uint32_t i = 0; i < MAX_KEYS_SET; i++) { Button.last_state[i] = NOT_PRESSED; #ifdef ESP8266 @@ -261,14 +264,13 @@ void ButtonInit(void) { } #endif // USE_ADC else { + // Insert, Skip and Append virtual buttons XdrvMailbox.index = i; if (XdrvCall(FUNC_ADD_BUTTON)) { - /* - At entry: - XdrvMailbox.index = button index - At exit: - XdrvMailbox.index bit 0 = current state - */ + // At entry: + // XdrvMailbox.index = button index + // At exit: + // XdrvMailbox.index bit 0 = current state bitSet(Button.used, i); // This pin is used bool state = (XdrvMailbox.index &1); ButtonSetVirtualPinState(i, state); // Virtual hardware pin state @@ -280,8 +282,38 @@ void ButtonInit(void) { } } Button.debounced_state[i] = Button.last_state[i]; +/* + if (bitRead(Button.used, i)) { + last_used = i +1; + } +*/ } +/* + // Append virtual buttons + for (uint32_t i = last_used; i < MAX_KEYS_SET; i++) { + Button.last_state[i] = NOT_PRESSED; + + XdrvMailbox.index = i; + if (XdrvCall(FUNC_ADD_BUTTON)) { + // At entry: + // XdrvMailbox.index = button index + // At exit: + // XdrvMailbox.index bit 0 = current state + bitSet(Button.used, i); // This pin is used + bool state = (XdrvMailbox.index &1); + ButtonSetVirtualPinState(i, state); // Virtual hardware pin state + if (!state) { ButtonInvertFlag(i); } // Set inverted flag + // last_state[i] must be 1 to indicate no button pressed + Button.last_state[i] = (bitRead(Button.virtual_pin, i) != bitRead(Button.inverted_mask, i)); + + AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: Add vButton%d, State %d"), i +1, Button.last_state[i]); + } + + Button.debounced_state[i] = Button.last_state[i]; + } +*/ + // AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: vPinUsed %08X, State %08X, Invert %08X"), Button.used, Button.virtual_pin, Button.inverted_mask); if (Button.used) { // Any bit set diff --git a/tasmota/tasmota_support/support_tasmota.ino b/tasmota/tasmota_support/support_tasmota.ino index 416ee8c0e..21da21b1f 100644 --- a/tasmota/tasmota_support/support_tasmota.ino +++ b/tasmota/tasmota_support/support_tasmota.ino @@ -2196,6 +2196,22 @@ void GpioInit(void) XdrvCall(FUNC_I2C_INIT); // Init RTC TasmotaGlobal.devices_present = 0; + uint32_t bi_device = 0; + for (uint32_t i = 0; i < MAX_RELAYS; i++) { + if (PinUsed(GPIO_REL1, i)) { + TasmotaGlobal.devices_present++; +#ifdef ESP8266 + if (EXS_RELAY == TasmotaGlobal.module_type) { + if (i &1) { TasmotaGlobal.devices_present--; } + } +#endif // ESP8266 + if (bitRead(TasmotaGlobal.rel_bistable, i)) { + if (bi_device &1) { TasmotaGlobal.devices_present--; } + bi_device++; + } + } + } + TasmotaGlobal.light_type = LT_BASIC; // Use basic PWM control if SetOption15 = 0 XsnsCall(FUNC_MODULE_INIT); @@ -2225,22 +2241,6 @@ void GpioInit(void) GpioInitPwm(); - uint32_t bi_device = 0; - for (uint32_t i = 0; i < MAX_RELAYS; i++) { - if (PinUsed(GPIO_REL1, i)) { - TasmotaGlobal.devices_present++; -#ifdef ESP8266 - if (EXS_RELAY == TasmotaGlobal.module_type) { - if (i &1) { TasmotaGlobal.devices_present--; } - } -#endif // ESP8266 - if (bitRead(TasmotaGlobal.rel_bistable, i)) { - if (bi_device &1) { TasmotaGlobal.devices_present--; } - bi_device++; - } - } - } - for (uint32_t i = 0; i < MAX_LEDS; i++) { if (PinUsed(GPIO_LED1, i)) { #ifdef USE_ARILUX_RF diff --git a/tasmota/tasmota_xdrv_driver/xdrv_66_tm1638.ino b/tasmota/tasmota_xdrv_driver/xdrv_66_tm1638.ino index 41dc91d10..f693de10e 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_66_tm1638.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_66_tm1638.ino @@ -163,21 +163,7 @@ void TmInit(void) { } digitalWrite(Tm1638.strobe_pin, HIGH); - // Dirty hack to offset TM1638 leds from GPIO relays - // At this time in code sequence the number of GPIO relays has not been established - uint32_t bi_device = 0; - uint32_t devices_present = 0; - for (uint32_t i = 0; i < MAX_RELAYS; i++) { - if (PinUsed(GPIO_REL1, i)) { - devices_present++; - if (bitRead(TasmotaGlobal.rel_bistable, i)) { - if (bi_device &1) { devices_present--; } - bi_device++; - } - } - } - - Tm1638.led_offset = devices_present; + Tm1638.led_offset = TasmotaGlobal.devices_present; TasmotaGlobal.devices_present += TM1638_MAX_LEDS; Tm1638.key_offset = -1; Tm1638.detected = true;