From 6c2c9ab5bd0d43f11f92b1eb094a6346e8788c97 Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Wed, 17 Apr 2024 09:40:02 +0200 Subject: [PATCH] Introduces "icons" library --- components/nspanel_ha_blueprint/icons.cpp | 61 +++++++++++++++++++++++ components/nspanel_ha_blueprint/icons.h | 32 ++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 components/nspanel_ha_blueprint/icons.cpp create mode 100644 components/nspanel_ha_blueprint/icons.h diff --git a/components/nspanel_ha_blueprint/icons.cpp b/components/nspanel_ha_blueprint/icons.cpp new file mode 100644 index 0000000..1d04058 --- /dev/null +++ b/components/nspanel_ha_blueprint/icons.cpp @@ -0,0 +1,61 @@ +// icons.cpp +#include "icons.h" +#include +#ifdef USE_PSRAM + #ifdef USE_ARDUINO + #include + #else // ESP-IDF + #include // Required for heap capabilities functions + #endif // ARDUINO vs ESP-IDF +#endif // USE_PSRAM + +namespace nspanel_ha_blueprint { + + std::vector icons; // This declaration may not be necessary; see below + + bool initializeIconsVector() { + Icon* allocatedMemory = nullptr; + + // Try to allocate the vector in PSRAM + #ifdef USE_PSRAM + #if defined(USE_ARDUINO) + if (psramFound()) + allocatedMemory = static_cast(ps_malloc(10 * sizeof(Icon))); + #else // ESP-IDF + allocatedMemory = static_cast(heap_caps_malloc(10 * sizeof(Icon), MALLOC_CAP_SPIRAM)); + #endif + + if (allocatedMemory != nullptr) { + new (&icons) std::vector(allocatedMemory, allocatedMemory + 10); // Placement new to initialize vector + return true; // Successfully allocated in PSRAM + } + #endif + + // Fallback to DRAM if PSRAM allocation fails or if PSRAM is not used + allocatedMemory = static_cast(malloc(10 * sizeof(Icon))); + if (allocatedMemory != nullptr) { + new (&icons) std::vector(allocatedMemory, allocatedMemory + 10); // Placement new to initialize vector + return true; // Successfully allocated in DRAM + } + + return false; // Allocation failed + } + + void Icon::updateIcon(const char* code, uint16_t color, bool vis) { + if (strcmp(last_icon_code, code) != 0 || last_icon_color != color || visible != vis) { + strncpy(last_icon_code, code, 4); + last_icon_code[4] = '\0'; + last_icon_color = color; + visible = vis; + } + } + + Icon* findIcon(uint8_t page_id, const char* comp) { + for (auto& icon : icons) { + if (icon.page_id == page_id && strcmp(icon.component, comp) == 0) { + return &icon; + } + } + return nullptr; + } +} diff --git a/components/nspanel_ha_blueprint/icons.h b/components/nspanel_ha_blueprint/icons.h new file mode 100644 index 0000000..18578f2 --- /dev/null +++ b/components/nspanel_ha_blueprint/icons.h @@ -0,0 +1,32 @@ +// icons.h +#pragma once + +#include +#include // For strncpy and strcmp +#include + +namespace nspanel_ha_blueprint { + + struct Icon { + uint8_t page_id; // Page ID, placed logically before the component + char component[15]; // Component name (max 14 chars + null terminator) + char last_icon_code[5]; // UTF-8 code (4 bytes + null terminator) + uint16_t last_icon_color; // Color value + bool visible; // Visibility of the icon + + // Constructor with default values + Icon(uint8_t pid, const char* comp, const char* code = "\xEF\xBF\xBF", uint16_t color = UINT16_MAX, bool vis = true) + : page_id(pid), last_icon_color(color), visible(vis) { + strncpy(const_cast(component), comp, 14); // Use const_cast to bypass const for initialization + const_cast(component)[14] = '\0'; // Ensure null termination safely + strncpy(last_icon_code, code, 4); + last_icon_code[4] = '\0'; + } + + void updateIcon(const char* code, uint16_t color, bool vis); + }; + + extern std::vector icons; // Global list of icons + + Icon* findIcon(uint8_t page_id, const char* component); +}