checking persistence on desktop and esp32
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
|
||||
// Project-specific headers
|
||||
#include "common/Widget.h"
|
||||
#include "common/IPersistenceManager.h"
|
||||
#include "u8g2.h"
|
||||
|
||||
class MenuItem;
|
||||
@@ -25,102 +26,49 @@ class MenuItem;
|
||||
* @struct menu_options_t
|
||||
* @brief Configuration structure for menu widgets containing display context and callbacks
|
||||
* @details This structure serves as a configuration container that provides menu widgets
|
||||
* with access to the display system, screen management functions, and input
|
||||
* handling callbacks. It acts as the bridge between individual menu widgets
|
||||
* and the broader application framework.
|
||||
*
|
||||
* The structure contains:
|
||||
* - Display context for rendering operations
|
||||
* - Screen management callbacks for navigation
|
||||
* - Input handling callback for button events
|
||||
*
|
||||
* All callback functions use std::function for type safety and flexibility,
|
||||
* allowing both function pointers and lambda expressions to be used.
|
||||
*
|
||||
* @note This structure should be initialized by the application framework
|
||||
* and passed to menu widgets during construction.
|
||||
* with access to the display system, screen management functions, input
|
||||
* handling callbacks, and persistent storage.
|
||||
*
|
||||
* @see Widget
|
||||
* @see ButtonType
|
||||
* @see IPersistenceManager
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
/**
|
||||
* @brief Pointer to u8g2 display context for graphics output operations
|
||||
* @details This pointer provides access to the u8g2 graphics library functions
|
||||
* for rendering text, shapes, and other visual elements. It must be
|
||||
* initialized and ready for drawing operations before being passed
|
||||
* to menu widgets.
|
||||
*
|
||||
* @note The menu widgets do not take ownership of this pointer and assume
|
||||
* it remains valid throughout their lifetime. Ensure the u8g2 context
|
||||
* is properly managed by the application framework.
|
||||
*
|
||||
* @warning Must not be nullptr when passed to menu widgets
|
||||
*/
|
||||
u8g2_t *u8g2;
|
||||
|
||||
/**
|
||||
* @brief Callback function to set the current active screen
|
||||
* @param screen Smart pointer to the Widget that should become the active screen
|
||||
*
|
||||
* @details This callback replaces the currently active screen with the provided
|
||||
* widget. It is typically used for direct screen transitions where the
|
||||
* previous screen should be completely replaced rather than stacked.
|
||||
*
|
||||
* @note The callback takes ownership of the provided Widget through the shared_ptr.
|
||||
* The previous screen will be destroyed unless other references exist.
|
||||
*
|
||||
* @see pushScreen for adding screens to a navigation stack
|
||||
*/
|
||||
std::function<void(std::shared_ptr<Widget>)> setScreen;
|
||||
|
||||
/**
|
||||
* @brief Callback function to add a new screen to the navigation stack
|
||||
* @param screen Smart pointer to the Widget that should be pushed onto the screen stack
|
||||
*
|
||||
* @details This callback adds a new screen on top of the current screen stack,
|
||||
* allowing for hierarchical navigation where users can return to
|
||||
* previous screens. Commonly used for sub-menus, settings screens,
|
||||
* or modal dialogs.
|
||||
*
|
||||
* @note The callback takes ownership of the provided Widget through the shared_ptr.
|
||||
* The current screen remains in memory and can be returned to via popScreen().
|
||||
*
|
||||
* @see popScreen for removing screens from the navigation stack
|
||||
* @see setScreen for direct screen replacement
|
||||
*/
|
||||
std::function<void(std::shared_ptr<Widget>)> pushScreen;
|
||||
|
||||
/**
|
||||
* @brief Callback function to remove the top screen from the navigation stack
|
||||
* @details This callback removes the currently active screen and returns to the
|
||||
* previous screen in the navigation stack. It is typically used for
|
||||
* "back" or "cancel" operations in hierarchical menu systems.
|
||||
*
|
||||
* @note If the navigation stack is empty or contains only one screen, the
|
||||
* behavior is implementation-dependent and should be handled gracefully
|
||||
* by the application framework.
|
||||
*
|
||||
* @see pushScreen for adding screens to the navigation stack
|
||||
*/
|
||||
std::function<void()> popScreen;
|
||||
|
||||
/**
|
||||
* @brief Callback function to handle button press events
|
||||
* @param button The type of button that was pressed
|
||||
*
|
||||
* @details This callback is invoked when a button press event occurs that is
|
||||
* not handled directly by the menu widget. It allows the application
|
||||
* framework to implement global button handling logic, such as
|
||||
* system-wide shortcuts or fallback behavior.
|
||||
*
|
||||
* @note This callback is typically used for buttons that have application-wide
|
||||
* meaning (e.g., home button, menu button) rather than widget-specific
|
||||
* navigation which is handled internally by the widgets.
|
||||
*
|
||||
* @see ButtonType for available button types
|
||||
* @see Widget::onButtonClicked for widget-specific button handling
|
||||
*/
|
||||
std::function<void(ButtonType button)> onButtonClicked;
|
||||
|
||||
/**
|
||||
* @brief Shared pointer to platform-independent persistence manager
|
||||
* @details This provides access to persistent key-value storage across different
|
||||
* platforms. The actual implementation (SDL3 or ESP32/NVS) is determined
|
||||
* at compile time based on the target platform.
|
||||
*
|
||||
* @note The persistence manager is shared across all menu widgets and maintains
|
||||
* its state throughout the application lifecycle.
|
||||
*/
|
||||
std::shared_ptr<IPersistenceManager> persistenceManager;
|
||||
|
||||
} menu_options_t;
|
136
components/insa/include/common/IPersistenceManager.h
Normal file
136
components/insa/include/common/IPersistenceManager.h
Normal file
@@ -0,0 +1,136 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
/**
|
||||
* @interface IPersistenceManager
|
||||
* @brief Abstract interface for platform-independent persistence management
|
||||
* @details This interface defines the contract for key-value storage and retrieval
|
||||
* systems across different platforms (Desktop/SDL3 and ESP32).
|
||||
*/
|
||||
class IPersistenceManager
|
||||
{
|
||||
public:
|
||||
virtual ~IPersistenceManager() = default;
|
||||
|
||||
/**
|
||||
* @brief Template methods for type-safe setting and retrieving of values
|
||||
* @tparam T The type of value to set (must be one of: bool, int, float, double, std::string)
|
||||
* @param key The key to associate with the value
|
||||
* @param value The value to store
|
||||
*/
|
||||
template<typename T>
|
||||
void SetValue(const std::string& key, const T& value) {
|
||||
static_assert(std::is_same_v<T, bool> ||
|
||||
std::is_same_v<T, int> ||
|
||||
std::is_same_v<T, float> ||
|
||||
std::is_same_v<T, double> ||
|
||||
std::is_same_v<T, std::string>,
|
||||
"Unsupported type for IPersistenceManager");
|
||||
SetValueImpl(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
template<typename T>
|
||||
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
|
||||
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
|
||||
|
||||
/**
|
||||
* @brief Persistence operations
|
||||
*/
|
||||
virtual bool Save() = 0; ///< Save data to persistent storage
|
||||
virtual bool Load() = 0; ///< Load data from persistent storage
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @brief Template-specific implementations that must be overridden by derived classes
|
||||
* @details These methods handle the actual storage and retrieval of different data types
|
||||
*/
|
||||
virtual void SetValueImpl(const std::string& key, bool value) = 0;
|
||||
virtual void SetValueImpl(const std::string& key, int value) = 0;
|
||||
virtual void SetValueImpl(const std::string& key, float value) = 0;
|
||||
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;
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Template dispatch methods for type-safe value retrieval
|
||||
* @tparam T The type to retrieve
|
||||
* @param key The key to look up
|
||||
* @param defaultValue The default value to return
|
||||
* @return The retrieved value or default if not found
|
||||
*/
|
||||
template<typename T>
|
||||
T GetValueImpl(const std::string& key, const T& defaultValue) const
|
||||
{
|
||||
if constexpr (std::is_same_v<T, bool>) {
|
||||
return GetValueImpl(key, defaultValue);
|
||||
} else if constexpr (std::is_same_v<T, int>) {
|
||||
return GetValueImpl(key, defaultValue);
|
||||
} else if constexpr (std::is_same_v<T, float>) {
|
||||
return GetValueImpl(key, defaultValue);
|
||||
} else if constexpr (std::is_same_v<T, double>) {
|
||||
return GetValueImpl(key, defaultValue);
|
||||
} else if constexpr (std::is_same_v<T, std::string>) {
|
||||
return GetValueImpl(key, defaultValue);
|
||||
} else {
|
||||
static_assert(std::is_same_v<T, bool> ||
|
||||
std::is_same_v<T, int> ||
|
||||
std::is_same_v<T, float> ||
|
||||
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
|
||||
}
|
||||
}
|
||||
};
|
61
components/insa/include/common/PersistenceManager.h
Normal file
61
components/insa/include/common/PersistenceManager.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include "IPersistenceManager.h"
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <nvs.h>
|
||||
#include <nvs_flash.h>
|
||||
|
||||
/**
|
||||
* @class PersistenceManager
|
||||
* @brief ESP32-specific implementation using NVS (Non-Volatile Storage)
|
||||
* @details This implementation uses ESP32's NVS API for persistent storage
|
||||
* in flash memory, providing a platform-optimized solution for
|
||||
* embedded systems.
|
||||
*/
|
||||
class PersistenceManager : public IPersistenceManager
|
||||
{
|
||||
private:
|
||||
nvs_handle_t nvs_handle_;
|
||||
std::string namespace_;
|
||||
bool initialized_;
|
||||
|
||||
public:
|
||||
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;
|
||||
size_t GetKeyCount() const override;
|
||||
|
||||
bool Save() override;
|
||||
bool Load() override;
|
||||
|
||||
// ESP32-specific methods
|
||||
bool Initialize();
|
||||
void Deinitialize();
|
||||
bool IsInitialized() const
|
||||
{
|
||||
return initialized_;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
private:
|
||||
bool EnsureInitialized() const;
|
||||
};
|
@@ -29,5 +29,7 @@ private:
|
||||
*/
|
||||
void onButtonPressed(const MenuItem& menuItem, ButtonType button) override;
|
||||
|
||||
static std::string CreateKey(int index);
|
||||
|
||||
menu_options_t *m_options; ///< Pointer to menu configuration options
|
||||
};
|
Reference in New Issue
Block a user