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