Class: SavedVariable<T>
The SavedVariable template class provides a mechanism to manage variables with persistent storage using the MatrixOS::NVS API. It supports automatic saving, loading, and managing variable states.
The source file for this utility is located in os/framework/SavedVariable.h
Template Parameter
T: The type of the variable to store.
Macros
CreateSavedVar
#define CreateSavedVar(scope, name, type, default_value)
Creates a SavedVariable instance with a hash derived from the scope and name.
Example:
// Macro usage
CreateSavedVar("Settings", Brightness, uint8_t, 128);
// Expands to:
SavedVariable<uint8_t> Brightness("Settings", "Brightness", 128);
Members
hash
uint32_t hash;
- Type:
uint32_t - Description: The unique hash identifier for the saved variable.
state
SavedVariableState state;
- Type:
SavedVariableState - Description: Tracks the state of the variable. Possible states are:
NotInited: The variable is not initialized.Inited: The variable is initialized.Loaded: The variable is loaded from persistent storage.Deleted: The variable has been deleted.
value
T value;
- Type:
T - Description: The value of the saved variable.
Constructors
Parameterized Constructor (Scope and Name)
SavedVariable(string scope, string name, T default_value);
Initializes the variable with a unique hash derived from its scope and name.
Parameters:
scope(string): A namespace or grouping for the variable.name(string): The name of the variable.default_value(T): The default value for the variable.
Parameterized Constructor (Hash)
SavedVariable(uint32_t hash, T default_value);
Initializes the variable with a precomputed hash.
Parameters:
hash(uint32_t): The unique hash for the variable.default_value(T): The default value for the variable.
Methods
Load
bool Load();
Loads the variable value from persistent storage.
Returns:
bool:trueif the variable was successfully loaded,falseotherwise.
Loaded
bool Loaded();
Checks if the variable is loaded from persistent storage.
Returns:
bool:trueif the variable is loaded,falseotherwise.