Move UtilitiesGroups to PSRAM

This commit is contained in:
Edward Firmo
2024-04-08 20:36:23 +02:00
parent 3a2dd23f86
commit 6be727d4af
2 changed files with 34 additions and 9 deletions

View File

@@ -2,13 +2,28 @@
#include "utilities.h" #include "utilities.h"
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#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 { namespace nspanel_ha_blueprint {
UtilitiesGroupValues UtilitiesGroups[8]; UtilitiesGroupValues *UtilitiesGroups = nullptr;
void resetUtilitiesGroups() { void resetUtilitiesGroups() {
// Temporary structure to hold the initial values // Dynamically allocate the UtilitiesGroups array in PSRAM
#ifdef USE_ESP_IDF
UtilitiesGroups = static_cast<UtilitiesGroupValues*>(heap_caps_malloc(8 * sizeof(UtilitiesGroupValues), MALLOC_CAP_SPIRAM));
#elif defined(USE_ARDUINO)
UtilitiesGroups = static_cast<UtilitiesGroupValues*>(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] = { const UtilitiesGroupValues initialUtilitiesGroups[8] = {
{ "grid", "\0", "\0", 0 }, { "grid", "\0", "\0", 0 },
{ "group01", "\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) { uint8_t findUtilitiesGroupIndex(const char* group_id) {
int low = 0; 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) { while (low <= high) {
int mid = low + (high - low) / 2; 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) { if (cmp < 0) {
low = mid + 1; low = mid + 1;
} else if (cmp > 0) { } else if (cmp > 0) {
high = mid - 1; high = mid - 1;
} else { } else {
return static_cast<uint8_t>(mid); // Found return static_cast<uint8_t>(mid); // Found
} }
} }
return UINT8_MAX; // Not found return UINT8_MAX; // Not found
} }
} // namespace nspanel_ha_blueprint } // namespace nspanel_ha_blueprint

View File

@@ -174,8 +174,10 @@ time:
on_time_sync: on_time_sync:
then: then:
- logger.log: "System clock synchronized" - lambda: |-
- script.execute: refresh_datetime 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 json: # Can be replaced by web_server
@@ -2541,7 +2543,7 @@ script:
- id: page_utilities - id: page_utilities
mode: restart mode: restart
then: then:
- lambda: resetUtilitiesGroups(); - lambda: if (UtilitiesGroups == nullptr) resetUtilitiesGroups();
- id: page_weather - id: page_weather
mode: restart mode: restart
@@ -2879,6 +2881,7 @@ script:
mode: restart mode: restart
then: then:
- lambda: |- - lambda: |-
cleanupUtilitiesGroups();
boot_event->stop(); boot_event->stop();
boot_progress->stop(); boot_progress->stop();
change_climate_state->stop(); change_climate_state->stop();