diff --git a/tasmota/include/tasmota.h b/tasmota/include/tasmota.h index 2410d0a42..6b60bcb41 100644 --- a/tasmota/include/tasmota.h +++ b/tasmota/include/tasmota.h @@ -57,6 +57,7 @@ const uint8_t MAX_INTERLOCKS = 14; // Max number of interlock groups (u const uint8_t MAX_SWITCHES = 28; // Max number of switches (up to MAX_SWITCHES_SET) const uint8_t MAX_KEYS = 28; // Max number of keys or buttons (up to 28) #endif // ESP32 +const uint8_t MAX_KEYS_SET = 28; // Max number of keys // Changes to the following MAX_ defines will impact settings layout const uint8_t MAX_INTERLOCKS_SET = 14; // Max number of interlock groups (MAX_RELAYS / 2) diff --git a/tasmota/tasmota_support/support_command.ino b/tasmota/tasmota_support/support_command.ino index 5a98f24a8..9bdf91b37 100644 --- a/tasmota/tasmota_support/support_command.ino +++ b/tasmota/tasmota_support/support_command.ino @@ -700,7 +700,7 @@ void CmndStatus(void) snprintf_P(stemp, sizeof(stemp), PSTR("%s%s\"%s\"" ), stemp, (i > 0 ? "," : ""), EscapeJSONString(SettingsText(SET_FRIENDLYNAME1 +i)).c_str()); } stemp2[0] = '\0'; - for (uint32_t i = 0; i < MAX_SWITCHES; i++) { + for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) { snprintf_P(stemp2, sizeof(stemp2), PSTR("%s%s%d" ), stemp2, (i > 0 ? "," : ""), Settings->switchmode[i]); } Response_P(PSTR("{\"" D_CMND_STATUS "\":{\"" D_CMND_MODULE "\":%d,\"" D_CMND_DEVICENAME "\":\"%s\",\"" D_CMND_FRIENDLYNAME "\":[%s],\"" D_CMND_TOPIC "\":\"%s\",\"" @@ -2137,7 +2137,7 @@ void CmndSwitchText(void) { void CmndSwitchMode(void) { - if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_SWITCHES)) { + if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_SWITCHES_SET)) { if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < MAX_SWITCH_OPTION)) { Settings->switchmode[XdrvMailbox.index -1] = XdrvMailbox.payload; } diff --git a/tasmota/tasmota_support/support_switch_v4.ino b/tasmota/tasmota_support/support_switch_v4.ino index a4546b66d..81a49f4e4 100644 --- a/tasmota/tasmota_support/support_switch_v4.ino +++ b/tasmota/tasmota_support/support_switch_v4.ino @@ -44,15 +44,15 @@ const char kSwitchPressStates[] PROGMEM = Ticker TickerSwitch; struct SWITCH { - uint32_t debounce = 0; // Switch debounce timer - uint32_t no_pullup_mask = 0; // Switch pull-up bitmask flags - uint32_t pulldown_mask = 0; // Switch pull-down bitmask flags - uint32_t virtual_pin_used = 0; // Switch used bitmask - uint32_t virtual_pin = 0; // Switch state bitmask - uint8_t state[MAX_SWITCHES] = { 0 }; - uint8_t last_state[MAX_SWITCHES]; // Last wall switch states - uint8_t hold_timer[MAX_SWITCHES] = { 0 }; // Timer for wallswitch push button hold - uint8_t debounced_state[MAX_SWITCHES]; // Switch debounced states + uint32_t debounce = 0; // Switch debounce timer + uint32_t no_pullup_mask = 0; // Switch pull-up bitmask flags + uint32_t pulldown_mask = 0; // Switch pull-down bitmask flags + uint32_t virtual_pin_used = 0; // Switch used bitmask + uint32_t virtual_pin = 0; // Switch state bitmask + uint8_t state[MAX_SWITCHES_SET] = { 0 }; + uint8_t last_state[MAX_SWITCHES_SET]; // Last wall switch states + uint8_t hold_timer[MAX_SWITCHES_SET] = { 0 }; // Timer for wallswitch push button hold + uint8_t debounced_state[MAX_SWITCHES_SET]; // Switch debounced states uint8_t first_change = 0; uint8_t present = 0; bool probe_mutex; @@ -68,6 +68,10 @@ void SwitchPulldownFlag(uint32 switch_bit) { bitSet(Switch.pulldown_mask, switch_bit); } +bool SwitchUsed(uint32_t index) { + return (PinUsed(GPIO_SWT1, index) || bitRead(Switch.virtual_pin_used, index)); +} + // Preffered virtual switch support since v12.3.1.4 void SwitchSetVirtualPinState(uint32_t index, uint32_t state) { bitWrite(Switch.virtual_pin, index, state); @@ -127,7 +131,7 @@ void SwitchProbe(void) { } uint32_t not_activated; - for (uint32_t i = 0; i < MAX_SWITCHES; i++) { + for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) { if (PinUsed(GPIO_SWT1, i)) { not_activated = digitalRead(Pin(GPIO_SWT1, i)); } @@ -220,7 +224,7 @@ void SwitchInit(void) { Switch.present = 0; Switch.virtual_pin_used = 0; - for (uint32_t i = 0; i < MAX_SWITCHES; i++) { + for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) { Switch.last_state[i] = NOT_PRESSED; // Init global to virtual switch state; bool used = false; @@ -280,8 +284,9 @@ void SwitchHandler(uint32_t mode) { uint32_t loops_per_second = 1000 / Settings->switch_debounce; - for (uint32_t i = 0; i < MAX_SWITCHES; i++) { - if (PinUsed(GPIO_SWT1, i) || bitRead(Switch.virtual_pin_used, i)) { + for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) { +// if (PinUsed(GPIO_SWT1, i) || bitRead(Switch.virtual_pin_used, i)) { + if (SwitchUsed(i)) { uint32_t button = Switch.debounced_state[i]; uint32_t switchflag = POWER_TOGGLE +1; uint32_t mqtt_action = POWER_NONE; diff --git a/tasmota/tasmota_support/support_tasmota.ino b/tasmota/tasmota_support/support_tasmota.ino index 0e8f7f7fd..54ecd4a29 100644 --- a/tasmota/tasmota_support/support_tasmota.ino +++ b/tasmota/tasmota_support/support_tasmota.ino @@ -962,11 +962,11 @@ bool MqttShowSensor(bool call_show_sensor) { ResponseAppendTime(); int json_data_start = ResponseLength(); - for (uint32_t i = 0; i < MAX_SWITCHES; i++) { + for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) { #ifdef USE_TM1638 if (PinUsed(GPIO_SWT1, i) || (PinUsed(GPIO_TM1638CLK) && PinUsed(GPIO_TM1638DIO) && PinUsed(GPIO_TM1638STB))) { #else - if (PinUsed(GPIO_SWT1, i)) { + if (SwitchUsed(i)) { #endif // USE_TM1638 ResponseAppend_P(PSTR(",\"%s\":\"%s\""), GetSwitchText(i).c_str(), GetStateText(SwitchState(i))); } diff --git a/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino b/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino index 0454646a0..6ad3cc646 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino @@ -964,11 +964,11 @@ void RulesEvery50ms(void) RulesProcessEvent(json_event); } // Boot time SWITCHES Status - for (uint32_t i = 0; i < MAX_SWITCHES; i++) { + for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) { #ifdef USE_TM1638 if (PinUsed(GPIO_SWT1, i) || (PinUsed(GPIO_TM1638CLK) && PinUsed(GPIO_TM1638DIO) && PinUsed(GPIO_TM1638STB))) { #else - if (PinUsed(GPIO_SWT1, i)) { + if (SwitchUsed(i)) { #endif // USE_TM1638 snprintf_P(json_event, sizeof(json_event), PSTR("{\"%s\":{\"Boot\":%d}}"), GetSwitchText(i).c_str(), (SwitchState(i))); RulesProcessEvent(json_event); diff --git a/tasmota/tasmota_xdrv_driver/xdrv_12_discovery.ino b/tasmota/tasmota_xdrv_driver/xdrv_12_discovery.ino index a7cbe6c6e..9d58aaae5 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_12_discovery.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_12_discovery.ino @@ -144,18 +144,18 @@ void TasDiscoverMessage(void) { "\"swc\":[")); // Switch modes (start) // Enable Discovery for Switches only if SetOption114 is enabled - for (uint32_t i = 0; i < MAX_SWITCHES; i++) { - ResponseAppend_P(PSTR("%s%d"), (i > 0 ? "," : ""), (PinUsed(GPIO_SWT1, i) && Settings->flag5.mqtt_switches) ? Settings->switchmode[i] : -1); + for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) { + ResponseAppend_P(PSTR("%s%d"), (i > 0 ? "," : ""), (SwitchUsed(i) && Settings->flag5.mqtt_switches) ? Settings->switchmode[i] : -1); } ResponseAppend_P(PSTR("]," // Switch modes (end) "\"swn\":[")); // Switch names (start) // Enable Discovery for Switches only if SetOption114 is enabled - for (uint32_t i = 0; i < MAX_SWITCHES; i++) { + for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) { char sname[TOPSZ]; snprintf_P(sname, sizeof(sname), PSTR("\"%s\""), GetSwitchText(i).c_str()); - ResponseAppend_P(PSTR("%s%s"), (i > 0 ? "," : ""), (PinUsed(GPIO_SWT1, i) && Settings->flag5.mqtt_switches) ? sname : PSTR("null")); + ResponseAppend_P(PSTR("%s%s"), (i > 0 ? "," : ""), (SwitchUsed(i) && Settings->flag5.mqtt_switches) ? sname : PSTR("null")); } ResponseAppend_P(PSTR("]," // Switch names (end) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_12_home_assistant.ino b/tasmota/tasmota_xdrv_driver/xdrv_12_home_assistant.ino index 531d8463e..998715498 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_12_home_assistant.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_12_home_assistant.ino @@ -338,18 +338,18 @@ void HassDiscoverMessage(void) { "\"swc\":[")); // Switch modes (start) // Enable Discovery for Switches only if SetOption114 is enabled - for (uint32_t i = 0; i < MAX_SWITCHES; i++) { - ResponseAppend_P(PSTR("%s%d"), (i > 0 ? "," : ""), (PinUsed(GPIO_SWT1, i) && Settings->flag5.mqtt_switches) ? Settings->switchmode[i] : -1); + for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) { + ResponseAppend_P(PSTR("%s%d"), (i > 0 ? "," : ""), (SwitchUsed(i) && Settings->flag5.mqtt_switches) ? Settings->switchmode[i] : -1); } ResponseAppend_P(PSTR("]," // Switch modes (end) "\"swn\":[")); // Switch names (start) // Enable Discovery for Switches only if SetOption114 is enabled - for (uint32_t i = 0; i < MAX_SWITCHES; i++) { + for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) { char sname[TOPSZ]; snprintf_P(sname, sizeof(sname), PSTR("\"%s\""), GetSwitchText(i).c_str()); - ResponseAppend_P(PSTR("%s%s"), (i > 0 ? "," : ""), (PinUsed(GPIO_SWT1, i) && Settings->flag5.mqtt_switches) ? sname : PSTR("null")); + ResponseAppend_P(PSTR("%s%s"), (i > 0 ? "," : ""), (SwitchUsed(i) && Settings->flag5.mqtt_switches) ? sname : PSTR("null")); } ResponseAppend_P(PSTR("]," // Switch names (end) @@ -790,7 +790,7 @@ void HAssAnnouncerBinSensors(uint8_t device, uint8_t present, uint8_t dual, uint void HAssAnnounceSwitches(void) { - for (uint32_t switch_index = 0; switch_index < MAX_SWITCHES; switch_index++) + for (uint32_t switch_index = 0; switch_index < MAX_SWITCHES_SET; switch_index++) { uint8_t switch_present = 0; uint8_t dual = 0; @@ -798,7 +798,7 @@ void HAssAnnounceSwitches(void) uint8_t hold = 0; uint8_t pir = 0; - if (PinUsed(GPIO_SWT1, switch_index)) { switch_present = 1; } + if (SwitchUsed(switch_index)) { switch_present = 1; } if (KeyTopicActive(1) && strcmp(SettingsText(SET_MQTT_SWITCH_TOPIC), TasmotaGlobal.mqtt_topic)) // Enable Discovery for Switches only if SwitchTopic is set to a custom name { diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_tasmota.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_tasmota.ino index 5bb9488bc..0f7deff83 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_tasmota.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_tasmota.ino @@ -157,7 +157,7 @@ extern "C" { } be_raise(vm, kTypeError, nullptr); } - + // Berry: tasmota.locale() -> string // int32_t l_locale(struct bvm *vm); @@ -586,8 +586,8 @@ extern "C" { int32_t l_getswitch(bvm *vm); int32_t l_getswitch(bvm *vm) { be_newobject(vm, "list"); - for (uint32_t i = 0; i < MAX_SWITCHES; i++) { - if (PinUsed(GPIO_SWT1, i)) { + for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) { + if (SwitchUsed(i)) { be_pushbool(vm, SwitchGetVirtual(i) == PRESSED); be_data_push(vm, -2); be_pop(vm, 1);