diff --git a/components/nspanel_ha_blueprint/utilities.cpp b/components/nspanel_ha_blueprint/utilities.cpp index 8201118..93e1fb3 100644 --- a/components/nspanel_ha_blueprint/utilities.cpp +++ b/components/nspanel_ha_blueprint/utilities.cpp @@ -2,13 +2,28 @@ #include "utilities.h" #include #include +#ifdef USE_ESP_IDF +#include "esp_heap_caps.h" +#elif defined(USE_ARDUINO) +#include "esp32-hal-psram.h" +#endif // ESP-IDF vs ARDUINO + namespace nspanel_ha_blueprint { - UtilitiesGroupValues UtilitiesGroups[8]; + UtilitiesGroupValues *UtilitiesGroups = nullptr; void resetUtilitiesGroups() { - // Temporary structure to hold the initial values + // Dynamically allocate the UtilitiesGroups array in PSRAM + #ifdef USE_ESP_IDF + UtilitiesGroups = static_cast(heap_caps_malloc(8 * sizeof(UtilitiesGroupValues), MALLOC_CAP_SPIRAM)); + #elif defined(USE_ARDUINO) + UtilitiesGroups = static_cast(ps_malloc(8 * sizeof(UtilitiesGroupValues))); + #endif // ESP-IDF vs ARDUINO + + if (!UtilitiesGroups) UtilitiesGroups = new UtilitiesGroupValues[8]; // Fallback to internal SRAM if PSRAM is not available or not supported + if (!UtilitiesGroups) return; // Fail nicely if no memory is available + const UtilitiesGroupValues initialUtilitiesGroups[8] = { { "grid", "\0", "\0", 0 }, { "group01", "\0", "\0", 0 }, @@ -28,24 +43,31 @@ namespace nspanel_ha_blueprint { } } + void cleanupUtilitiesGroups() { + if (UtilitiesGroups != nullptr) { + free(UtilitiesGroups); // Compatible with both heap_caps_malloc and ps_malloc + UtilitiesGroups = nullptr; // Prevent dangling pointers + } + } + uint8_t findUtilitiesGroupIndex(const char* group_id) { int low = 0; - int high = sizeof(UtilitiesGroups) / sizeof(UtilitiesGroups[0]) - 1; + int high = 7; // Directly use the number of elements in UtilitiesGroups - 1 while (low <= high) { int mid = low + (high - low) / 2; - int cmp = strcmp(UtilitiesGroups[mid].group_id, group_id); + int cmp = std::strcmp(UtilitiesGroups[mid].group_id, group_id); if (cmp < 0) { low = mid + 1; } else if (cmp > 0) { high = mid - 1; } else { - return static_cast(mid); // Found + return static_cast(mid); // Found } } - return UINT8_MAX; // Not found + return UINT8_MAX; // Not found } } // namespace nspanel_ha_blueprint diff --git a/esphome/nspanel_esphome_core.yaml b/esphome/nspanel_esphome_core.yaml index 3d27984..97c0c0a 100644 --- a/esphome/nspanel_esphome_core.yaml +++ b/esphome/nspanel_esphome_core.yaml @@ -174,8 +174,10 @@ time: on_time_sync: then: - - logger.log: "System clock synchronized" - - script.execute: refresh_datetime + - lambda: |- + ESP_LOGD("time.on_time_sync", "System clock synchronized"); + ESP_LOGD("time.on_time_sync", "Timezone: %s", get_timezone().c_str()); + refresh_datetime->execute(); json: # Can be replaced by web_server @@ -2541,7 +2543,7 @@ script: - id: page_utilities mode: restart then: - - lambda: resetUtilitiesGroups(); + - lambda: if (UtilitiesGroups == nullptr) resetUtilitiesGroups(); - id: page_weather mode: restart @@ -2879,6 +2881,7 @@ script: mode: restart then: - lambda: |- + cleanupUtilitiesGroups(); boot_event->stop(); boot_progress->stop(); change_climate_state->stop();