Move UtilitiesGroups to PSRAM
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user