Move UtilitiesGroups to PSRAM
This commit is contained in:
@@ -2,13 +2,28 @@
|
||||
#include "utilities.h"
|
||||
#include <cstdint>
|
||||
#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 {
|
||||
|
||||
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<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] = {
|
||||
{ "grid", "\0", "\0", 0 },
|
||||
{ "group01", "\0", "\0", 0 },
|
||||
@@ -28,13 +43,20 @@ 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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user