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
:true
if the variable was successfully loaded,false
otherwise.
Loaded
​
bool Loaded();
Checks if the variable is loaded from persistent storage.
Returns:
bool
:true
if the variable is loaded,false
otherwise.
Set
​
bool Set(T new_value);
Sets a new value for the variable and saves it to persistent storage.
Parameters:
new_value
(T
): The new value to set.
Returns:
bool
:true
if the value was successfully saved,false
otherwise.
TempSet
​
bool TempSet(T new_value);
Temporarily updates the variable without saving it to persistent storage.
Parameters:
new_value
(T
): The new value to set.
Returns:
bool
: Always returnstrue
.
Save
​
bool Save();
Saves the current value to persistent storage.
Returns:
bool
:true
if the value was successfully saved,false
otherwise.
Get
​
T& Get();
Retrieves the value of the variable, loading it from persistent storage if not already loaded.
Returns:
T&
: A reference to the variable's value.
Delete
​
bool Delete();
Deletes the variable from persistent storage.
Returns:
bool
:true
if the variable was successfully deleted,false
otherwise.
Operators​
The SavedVariable
class provides various operators for arithmetic, comparison, and assignment. The operators automatically update the value in persistent storage unless explicitly stated.
Assignment​
bool operator=(T new_value);
Comparison​
bool operator==(T new_value);
bool operator!=(T new_value);
bool operator>(T new_value);
bool operator<(T new_value);
bool operator>=(T new_value);
bool operator<=(T new_value);
Arithmetic​
T operator+(T operation_value);
T operator-(T operation_value);
T operator*(T operation_value);
T operator/(T operation_value);
T operator%(T operation_value);
Increment/Decrement​
T& operator++(); // Pre-increment
T operator++(int); // Post-increment
T& operator--(); // Pre-decrement
T operator--(int); // Post-decrement
Pointer and Type Conversion​
T* operator&();
operator T();
operator T*();
Enums​
SavedVariableState
​
Defines the possible states of a SavedVariable
:
NotInited
Inited
Loaded
Deleted
Comments