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.
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:trueif the value was successfully saved,falseotherwise.
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:trueif the value was successfully saved,falseotherwise.
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:trueif the variable was successfully deleted,falseotherwise.
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:
NotInitedInitedLoadedDeleted
Comments