Files
2026-02-10 12:12:11 +01:00

103 lines
2.8 KiB
C++

/**
* @file Config.h
* @brief Configuration management for the Locomotive Test Bench
*
* This module handles persistent storage of WiFi and system settings
* using ESP32's Preferences library (NVS - Non-Volatile Storage).
*
* @author Locomotive Test Bench Project
* @date 2025
*/
#ifndef CONFIG_H
#define CONFIG_H
#include <Arduino.h>
#include <Preferences.h>
/**
* @struct WiFiConfig
* @brief WiFi configuration parameters
*
* Stores both Access Point and Client mode settings.
*/
struct WiFiConfig {
String ssid; ///< WiFi network SSID (Client mode)
String password; ///< WiFi network password (Client mode)
bool isAPMode; ///< True = AP mode, False = Client mode
String apSSID; ///< Access Point SSID
String apPassword; ///< Access Point password (min 8 characters)
};
/**
* @struct SystemConfig
* @brief System operation configuration
*
* Stores current control mode and locomotive parameters.
*/
struct SystemConfig {
bool isDCCMode; ///< True = DCC digital, False = DC analog
uint16_t dccAddress; ///< DCC locomotive address (1-10239)
uint8_t speed; ///< Speed setting (0-100%)
uint8_t direction; ///< Direction: 0 = reverse, 1 = forward
uint32_t dccFunctions; ///< Bit field for DCC functions F0-F28
};
/**
* @class Config
* @brief Configuration manager with persistent storage
*
* Manages all configuration parameters and provides persistent
* storage using ESP32's NVS (Non-Volatile Storage) via Preferences.
*
* @note All settings are automatically saved to flash memory
* and persist across reboots.
*/
class Config {
public:
/**
* @brief Constructor - initializes with default values
*/
Config();
/**
* @brief Initialize preferences and load saved settings
*
* Must be called during setup() before using configuration.
* Loads previously saved settings from NVS.
*/
void begin();
/**
* @brief Save current configuration to NVS
*
* Writes all WiFi and system settings to persistent storage.
* Should be called after any configuration changes.
*/
void save();
/**
* @brief Load configuration from NVS
*
* Reads previously saved settings. Called automatically
* by begin(), but can be called manually to reload.
*/
void load();
/**
* @brief Reset all settings to defaults
*
* Clears all stored preferences and resets to factory defaults.
* Use with caution - all saved settings will be lost.
*/
void reset();
WiFiConfig wifi; ///< WiFi configuration settings
SystemConfig system; ///< System operation settings
private:
Preferences preferences; ///< ESP32 NVS preferences object
};
#endif