new persistence manager component

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2025-06-23 23:10:42 +02:00
parent c6f0c4572d
commit 0c8c831eea
16 changed files with 92 additions and 119 deletions

View File

@@ -60,7 +60,6 @@ else ()
${CMAKE_SOURCE_DIR}/Common.cpp
${CMAKE_SOURCE_DIR}/debug/debug_overlay.cpp
${CMAKE_SOURCE_DIR}/hal/u8x8_hal_sdl.cpp
${CMAKE_SOURCE_DIR}/manager/PersistenceManager.cpp
${CMAKE_SOURCE_DIR}/manager/ResourceManager.cpp
${CMAKE_SOURCE_DIR}/model/AppContext.cpp
${CMAKE_SOURCE_DIR}/model/Window.cpp
@@ -98,6 +97,7 @@ else ()
ImGui
insa
led-manager
persistence-manager
SDL3::SDL3
SDL3_image::SDL3_image
SDL3_ttf::SDL3_ttf

View File

@@ -4,6 +4,7 @@ if (NOT DEFINED ENV{IDF_PATH})
add_subdirectory(imgui)
add_subdirectory(insa)
add_subdirectory(led-manager)
add_subdirectory(persistence-manager)
target_link_libraries(components INTERFACE ImGui)
endif ()

View File

@@ -17,7 +17,7 @@
// Project-specific headers
#include "common/Widget.h"
#include "common/IPersistenceManager.h"
#include "../../persistence-manager/include/IPersistenceManager.h"
#include "u8g2.h"
class MenuItem;

View File

@@ -5,6 +5,7 @@ if (DEFINED ENV{IDF_PATH})
PRIV_REQUIRES
u8g2
esp_event
persistence-manager
)
return()
endif ()
@@ -23,4 +24,5 @@ target_include_directories(${PROJECT_NAME} PUBLIC include)
target_link_libraries(${PROJECT_NAME} PRIVATE
SDL3::SDL3
persistence-manager
)

View File

@@ -1,4 +1,5 @@
#pragma once
#include "SDL3/SDL_render.h"
#include <cstdint>
@@ -12,7 +13,7 @@ class Matrix
void Render() const;
SDL_WindowID windowId() const;
[[nodiscard]] SDL_WindowID windowId() const;
private:
void DrawColoredGrid() const;

View File

@@ -1,6 +1,6 @@
#pragma once
#include "stdint.h"
#include <cstdint>
enum
{
@@ -15,8 +15,8 @@ typedef struct
int value;
} led_event_data_t;
uint64_t wled_init(void);
uint64_t wled_init();
uint64_t register_handler(void);
uint64_t register_handler();
uint64_t send_event(uint32_t event, led_event_data_t *payload);

View File

@@ -1,4 +1,4 @@
#include "../../include/Matrix.h"
#include "Matrix.h"
#include "SDL3/SDL.h"
@@ -36,7 +36,7 @@ void Matrix::DrawColoredGrid() const
h = m_rows - 1 - h_raw;
}
const auto rectSize = cellSize - 2.0f * spacing;
constexpr auto rectSize = cellSize - 2.0f * spacing;
const auto x = static_cast<float>(w) * cellSize + spacing;
const auto y = static_cast<float>(h) * cellSize + spacing;

View File

@@ -0,0 +1,24 @@
if (DEFINED ENV{IDF_PATH})
idf_component_register(SRCS
src/hal_esp32/PersistenceManager.cpp
INCLUDE_DIRS "include"
REQUIRES
nvs_flash
)
return()
endif ()
cmake_minimum_required(VERSION 3.30)
project(persistence-manager)
add_library(${PROJECT_NAME} STATIC
src/hal_native/PersistenceManager.cpp
)
include_directories(include)
target_include_directories(${PROJECT_NAME} PUBLIC include)
target_link_libraries(${PROJECT_NAME} PRIVATE
SDL3::SDL3
)

View File

@@ -35,49 +35,21 @@ public:
* @brief Template method for type-safe retrieval of values
* @tparam T The type of value to retrieve
* @param key The key to look up
* @param defaultValue The default value to return if key is not found
* @return The stored value or default value if key doesn't exist
* @param defaultValue The default value to return if the key is not found
* @return The stored value or default value if the key doesn't exist
*/
template<typename T>
T GetValue(const std::string& key, const T& defaultValue = T{}) const {
[[nodiscard]] T GetValue(const std::string& key, const T& defaultValue = T{}) const {
return GetValueImpl<T>(key, defaultValue);
}
/**
* @brief Convenience methods for setting specific types
*/
void SetBool(const std::string& key, bool value) { SetValue(key, value); }
void SetInt(const std::string& key, int value) { SetValue(key, value); }
void SetFloat(const std::string& key, float value) { SetValue(key, value); }
void SetDouble(const std::string& key, double value) { SetValue(key, value); }
void SetString(const std::string& key, const std::string& value) { SetValue(key, value); }
/**
* @brief Convenience methods for getting specific types with default values
*/
bool GetBool(const std::string& key, bool defaultValue = false) const {
return GetValue(key, defaultValue);
}
int GetInt(const std::string& key, int defaultValue = 0) const {
return GetValue(key, defaultValue);
}
float GetFloat(const std::string& key, float defaultValue = 0.0f) const {
return GetValue(key, defaultValue);
}
double GetDouble(const std::string& key, double defaultValue = 0.0) const {
return GetValue(key, defaultValue);
}
std::string GetString(const std::string& key, const std::string& defaultValue = "") const {
return GetValue(key, defaultValue);
}
/**
* @brief Utility methods for key management
*/
virtual bool HasKey(const std::string& key) const = 0; ///< Check if a key exists
[[nodiscard]] virtual bool HasKey(const std::string& key) const = 0; ///< Check if a key exists
virtual void RemoveKey(const std::string& key) = 0; ///< Remove a key-value pair
virtual void Clear() = 0; ///< Clear all stored data
virtual size_t GetKeyCount() const = 0; ///< Get the number of stored keys
[[nodiscard]] virtual size_t GetKeyCount() const = 0; ///< Get the number of stored keys
/**
* @brief Persistence operations
@@ -96,11 +68,11 @@ protected:
virtual void SetValueImpl(const std::string& key, double value) = 0;
virtual void SetValueImpl(const std::string& key, const std::string& value) = 0;
virtual bool GetValueImpl(const std::string& key, bool defaultValue) const = 0;
virtual int GetValueImpl(const std::string& key, int defaultValue) const = 0;
virtual float GetValueImpl(const std::string& key, float defaultValue) const = 0;
virtual double GetValueImpl(const std::string& key, double defaultValue) const = 0;
virtual std::string GetValueImpl(const std::string& key, const std::string& defaultValue) const = 0;
[[nodiscard]] virtual bool GetValueImpl(const std::string& key, bool defaultValue) const = 0;
[[nodiscard]] virtual int GetValueImpl(const std::string& key, int defaultValue) const = 0;
[[nodiscard]] virtual float GetValueImpl(const std::string& key, float defaultValue) const = 0;
[[nodiscard]] virtual double GetValueImpl(const std::string& key, double defaultValue) const = 0;
[[nodiscard]] virtual std::string GetValueImpl(const std::string& key, const std::string& defaultValue) const = 0;
private:
/**
@@ -111,18 +83,18 @@ private:
* @return The retrieved value or default if not found
*/
template<typename T>
T GetValueImpl(const std::string& key, const T& defaultValue) const
[[nodiscard]] T GetValueImpl(const std::string& key, const T& defaultValue) const
{
if constexpr (std::is_same_v<T, bool>) {
return GetValueImpl(key, defaultValue);
return GetValueImpl(key, static_cast<bool>(defaultValue));
} else if constexpr (std::is_same_v<T, int>) {
return GetValueImpl(key, defaultValue);
return GetValueImpl(key, static_cast<int>(defaultValue));
} else if constexpr (std::is_same_v<T, float>) {
return GetValueImpl(key, defaultValue);
return GetValueImpl(key, static_cast<float>(defaultValue));
} else if constexpr (std::is_same_v<T, double>) {
return GetValueImpl(key, defaultValue);
return GetValueImpl(key, static_cast<double>(defaultValue));
} else if constexpr (std::is_same_v<T, std::string>) {
return GetValueImpl(key, defaultValue);
return GetValueImpl(key, static_cast<const std::string&>(defaultValue));
} else {
static_assert(std::is_same_v<T, bool> ||
std::is_same_v<T, int> ||
@@ -130,7 +102,7 @@ private:
std::is_same_v<T, double> ||
std::is_same_v<T, std::string>,
"Unsupported type for IPersistenceManager");
return defaultValue; // This line will never be reached, but satisfies compiler
return defaultValue; // This line will never be reached, but satisfies the compiler
}
}
};

View File

@@ -1,6 +1,6 @@
#pragma once
#include "../components/insa/include/common/IPersistenceManager.h"
#include "IPersistenceManager.h"
#include <string>
#include <unordered_map>
@@ -14,7 +14,7 @@
* in flash memory, providing a platform-optimized solution for
* embedded systems.
*/
class PersistenceManager : public IPersistenceManager
class PersistenceManager final : public IPersistenceManager
{
private:
nvs_handle_t nvs_handle_;
@@ -25,7 +25,6 @@ class PersistenceManager : public IPersistenceManager
explicit PersistenceManager(const std::string &nvs_namespace = "config");
~PersistenceManager() override;
// IPersistenceManager implementation
bool HasKey(const std::string &key) const override;
void RemoveKey(const std::string &key) override;
void Clear() override;
@@ -34,7 +33,6 @@ class PersistenceManager : public IPersistenceManager
bool Save() override;
bool Load() override;
// ESP32-specific methods
bool Initialize();
void Deinitialize();
bool IsInitialized() const
@@ -43,7 +41,6 @@ class PersistenceManager : public IPersistenceManager
}
protected:
// Template-spezifische Implementierungen
void SetValueImpl(const std::string &key, bool value) override;
void SetValueImpl(const std::string &key, int value) override;
void SetValueImpl(const std::string &key, float value) override;

View File

@@ -1,6 +1,6 @@
#pragma once
#include "common/IPersistenceManager.h"
#include "../IPersistenceManager.h"
#include <SDL3/SDL.h>
#include <unordered_map>
#include <variant>
@@ -16,42 +16,38 @@ public:
>;
private:
std::unordered_map<std::string, ValueType> data_;
std::string filename_;
std::unordered_map<std::string, ValueType> m_data;
std::string m_filename;
public:
explicit PersistenceManager(std::string filename = "settings.dat");
~PersistenceManager() override;
// IPersistenceManager implementation
bool HasKey(const std::string& key) const override;
[[nodiscard]] bool HasKey(const std::string& key) const override;
void RemoveKey(const std::string& key) override;
void Clear() override;
size_t GetKeyCount() const override { return data_.size(); }
[[nodiscard]] size_t GetKeyCount() const override { return m_data.size(); }
bool Save() override;
bool Load() override;
// Erweiterte SDL3-spezifische Methoden
bool SaveToFile(const std::string& filename);
bool LoadFromFile(const std::string& filename);
protected:
// Template-spezifische Implementierungen
void SetValueImpl(const std::string& key, bool value) override;
void SetValueImpl(const std::string& key, int value) override;
void SetValueImpl(const std::string& key, float value) override;
void SetValueImpl(const std::string& key, double value) override;
void SetValueImpl(const std::string& key, const std::string& value) override;
bool GetValueImpl(const std::string& key, bool defaultValue) const override;
int GetValueImpl(const std::string& key, int defaultValue) const override;
float GetValueImpl(const std::string& key, float defaultValue) const override;
double GetValueImpl(const std::string& key, double defaultValue) const override;
std::string GetValueImpl(const std::string& key, const std::string& defaultValue) const override;
[[nodiscard]] bool GetValueImpl(const std::string& key, bool defaultValue) const override;
[[nodiscard]] int GetValueImpl(const std::string& key, int defaultValue) const override;
[[nodiscard]] float GetValueImpl(const std::string& key, float defaultValue) const override;
[[nodiscard]] double GetValueImpl(const std::string& key, double defaultValue) const override;
[[nodiscard]] std::string GetValueImpl(const std::string& key, const std::string& defaultValue) const override;
private:
// Interne Hilfsmethoden für Serialisierung
static bool WriteValueToStream(SDL_IOStream* stream, const ValueType& value) ;
static bool ReadValueFromStream(SDL_IOStream* stream, ValueType& value) ;

View File

@@ -1,4 +1,4 @@
#include "PersistenceManager.h"
#include "hal_esp32/PersistenceManager.h"
#include <cstring>
#include <esp_log.h>

View File

@@ -1,11 +1,10 @@
#include "PersistenceManager.h"
#include "hal_native/PersistenceManager.h"
#include <SDL3/SDL.h>
#include <utility>
PersistenceManager::PersistenceManager(std::string filename) : filename_(std::move(filename))
PersistenceManager::PersistenceManager(std::string filename) : m_filename(std::move(filename))
{
// Initialize SDL3 if not already done
if (!SDL_WasInit(SDL_INIT_EVENTS))
{
SDL_Init(SDL_INIT_EVENTS);
@@ -14,32 +13,32 @@ PersistenceManager::PersistenceManager(std::string filename) : filename_(std::mo
PersistenceManager::~PersistenceManager()
{
Save(); // Automatically save on destruction
Save();
}
bool PersistenceManager::HasKey(const std::string &key) const
{
return data_.contains(key);
return m_data.contains(key);
}
void PersistenceManager::RemoveKey(const std::string &key)
{
data_.erase(key);
m_data.erase(key);
}
void PersistenceManager::Clear()
{
data_.clear();
m_data.clear();
}
bool PersistenceManager::Save()
{
return SaveToFile(filename_);
return SaveToFile(m_filename);
}
bool PersistenceManager::Load()
{
return LoadFromFile(filename_);
return LoadFromFile(m_filename);
}
bool PersistenceManager::SaveToFile(const std::string &filename)
@@ -51,8 +50,7 @@ bool PersistenceManager::SaveToFile(const std::string &filename)
return false;
}
// Write number of entries
const size_t count = data_.size();
const size_t count = m_data.size();
if (SDL_WriteIO(stream, &count, sizeof(count)) != sizeof(count))
{
SDL_Log("Error writing count: %s", SDL_GetError());
@@ -60,10 +58,8 @@ bool PersistenceManager::SaveToFile(const std::string &filename)
return false;
}
// Write each entry
for (const auto &[key, value] : data_)
for (const auto &[key, value] : m_data)
{
// Write key length
size_t keyLength = key.length();
if (SDL_WriteIO(stream, &keyLength, sizeof(keyLength)) != sizeof(keyLength))
{
@@ -72,7 +68,6 @@ bool PersistenceManager::SaveToFile(const std::string &filename)
return false;
}
// Write key
if (SDL_WriteIO(stream, key.c_str(), keyLength) != keyLength)
{
SDL_Log("Error writing key: %s", SDL_GetError());
@@ -80,7 +75,6 @@ bool PersistenceManager::SaveToFile(const std::string &filename)
return false;
}
// Write value
if (!WriteValueToStream(stream, value))
{
SDL_CloseIO(stream);
@@ -101,9 +95,8 @@ bool PersistenceManager::LoadFromFile(const std::string &filename)
return false;
}
data_.clear();
m_data.clear();
// Read number of entries
size_t count;
if (SDL_ReadIO(stream, &count, sizeof(count)) != sizeof(count))
{
@@ -112,10 +105,8 @@ bool PersistenceManager::LoadFromFile(const std::string &filename)
return false;
}
// Read each entry
for (size_t i = 0; i < count; ++i)
{
// Read key length
size_t keyLength;
if (SDL_ReadIO(stream, &keyLength, sizeof(keyLength)) != sizeof(keyLength))
{
@@ -124,7 +115,6 @@ bool PersistenceManager::LoadFromFile(const std::string &filename)
return false;
}
// Read key
std::string key(keyLength, '\0');
if (SDL_ReadIO(stream, key.data(), keyLength) != keyLength)
{
@@ -133,7 +123,6 @@ bool PersistenceManager::LoadFromFile(const std::string &filename)
return false;
}
// Read value
ValueType value;
if (!ReadValueFromStream(stream, value))
{
@@ -141,42 +130,41 @@ bool PersistenceManager::LoadFromFile(const std::string &filename)
return false;
}
data_[key] = value;
m_data[key] = value;
}
SDL_CloseIO(stream);
return true;
}
// Template-specific implementations
void PersistenceManager::SetValueImpl(const std::string &key, bool value)
{
data_[key] = value;
m_data[key] = value;
}
void PersistenceManager::SetValueImpl(const std::string &key, int value)
{
data_[key] = value;
m_data[key] = value;
}
void PersistenceManager::SetValueImpl(const std::string &key, float value)
{
data_[key] = value;
m_data[key] = value;
}
void PersistenceManager::SetValueImpl(const std::string &key, double value)
{
data_[key] = value;
m_data[key] = value;
}
void PersistenceManager::SetValueImpl(const std::string &key, const std::string &value)
{
data_[key] = value;
m_data[key] = value;
}
bool PersistenceManager::GetValueImpl(const std::string &key, bool defaultValue) const
{
if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative<bool>(it->second))
if (const auto it = m_data.find(key); it != m_data.end() && std::holds_alternative<bool>(it->second))
{
return std::get<bool>(it->second);
}
@@ -185,7 +173,7 @@ bool PersistenceManager::GetValueImpl(const std::string &key, bool defaultValue)
int PersistenceManager::GetValueImpl(const std::string &key, int defaultValue) const
{
if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative<int>(it->second))
if (const auto it = m_data.find(key); it != m_data.end() && std::holds_alternative<int>(it->second))
{
return std::get<int>(it->second);
}
@@ -194,7 +182,7 @@ int PersistenceManager::GetValueImpl(const std::string &key, int defaultValue) c
float PersistenceManager::GetValueImpl(const std::string &key, float defaultValue) const
{
if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative<float>(it->second))
if (const auto it = m_data.find(key); it != m_data.end() && std::holds_alternative<float>(it->second))
{
return std::get<float>(it->second);
}
@@ -203,7 +191,7 @@ float PersistenceManager::GetValueImpl(const std::string &key, float defaultValu
double PersistenceManager::GetValueImpl(const std::string &key, double defaultValue) const
{
if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative<double>(it->second))
if (const auto it = m_data.find(key); it != m_data.end() && std::holds_alternative<double>(it->second))
{
return std::get<double>(it->second);
}
@@ -212,26 +200,23 @@ double PersistenceManager::GetValueImpl(const std::string &key, double defaultVa
std::string PersistenceManager::GetValueImpl(const std::string &key, const std::string &defaultValue) const
{
if (const auto it = data_.find(key); it != data_.end() && std::holds_alternative<std::string>(it->second))
if (const auto it = m_data.find(key); it != m_data.end() && std::holds_alternative<std::string>(it->second))
{
return std::get<std::string>(it->second);
}
return defaultValue;
}
// Private helper methods
bool PersistenceManager::WriteValueToStream(SDL_IOStream *stream, const ValueType &value)
{
const TypeId typeId = GetTypeId(value);
// Write type ID
if (SDL_WriteIO(stream, &typeId, sizeof(typeId)) != sizeof(typeId))
{
SDL_Log("Error writing type ID: %s", SDL_GetError());
return false;
}
// Write value based on type
switch (typeId)
{
case TypeId::BOOL: {
@@ -254,13 +239,11 @@ bool PersistenceManager::WriteValueToStream(SDL_IOStream *stream, const ValueTyp
const auto &str = std::get<std::string>(value);
const size_t length = str.length();
// Write string length
if (SDL_WriteIO(stream, &length, sizeof(length)) != sizeof(length))
{
return false;
}
// Write string data
return SDL_WriteIO(stream, str.c_str(), length) == length;
}
}
@@ -271,14 +254,12 @@ bool PersistenceManager::ReadValueFromStream(SDL_IOStream *stream, ValueType &va
{
TypeId typeId;
// Read type ID
if (SDL_ReadIO(stream, &typeId, sizeof(typeId)) != sizeof(typeId))
{
SDL_Log("Error reading type ID: %s", SDL_GetError());
return false;
}
// Read value based on type
switch (typeId)
{
case TypeId::BOOL: {
@@ -351,6 +332,5 @@ PersistenceManager::TypeId PersistenceManager::GetTypeId(const ValueType &value)
if (std::holds_alternative<std::string>(value))
return TypeId::STRING;
// Should never be reached
return TypeId::BOOL;
}

View File

@@ -1,16 +1,16 @@
idf_component_register(SRCS
main.cpp
app_task.cpp
PersistenceManager.cpp
../components/persistence-manager/src/hal_esp32/PersistenceManager.cpp
button_handling.c
hal/u8g2_esp32_hal.c
INCLUDE_DIRS "."
PRIV_REQUIRES
insa
led-manager
persistence-manager
u8g2
driver
esp_timer
esp_event
nvs_flash
)

View File

@@ -7,7 +7,7 @@
#include "button_handling.h"
#include "common/InactivityTracker.h"
#include "PersistenceManager.h"
#include "hal_esp32/PersistenceManager.h"
#include "ui/ScreenSaver.h"
#include "ui/SplashScreen.h"

View File

@@ -5,7 +5,7 @@
#include "MenuOptions.h"
#include "common/InactivityTracker.h"
#include "manager/PersistenceManager.h"
#include "hal_native/PersistenceManager.h"
#include "ui/ScreenSaver.h"
#include "ui/SplashScreen.h"
#include "ui/widgets/Button.h"