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,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<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
|
||||
|
||||
Reference in New Issue
Block a user