Skip to content

Commit

Permalink
Create overloads to allow float values to be saved in NVS
Browse files Browse the repository at this point in the history
  • Loading branch information
SamAtBackbone committed Dec 5, 2024
1 parent 730f59b commit 19cc7e9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
56 changes: 56 additions & 0 deletions components/nvs/include/nvs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,56 @@ class Nvs : public BaseComponent {
get_or_set_var(ns_name.data(), key.data(), value, default_value, ec);
}

/// @brief Save a float variable in the NVS and commit
/// @param[in] ns_name Namespace of the variable to save
/// @param[in] key NVS Key of the variable to save
/// @param[in] value float value to save
/// @param[out] ec Saves a std::error_code representing success or failure
void set_var(std::string_view ns_name, std::string_view key, float value, std::error_code &ec) {
if(sizeof(float) != sizeof(uint32_t)) {
ec = make_error_code(NvsErrc::Value_Type_Error);
return;
}
uint32_t val_u32;
memcpy(&val_u32, &value, sizeof(uint32_t));
set_var(ns_name.data(), key.data(), val_u32, ec);
}

/// @brief Reads a float variable from the NVS
/// @param[in] ns_name Namespace of the variable to read
/// @param[in] key NVS Key of the variable to read
/// @param[out] value Float variable to read
/// @param[out] ec Saves a std::error_code representing success or failure
void get_var(std::string_view ns_name, std::string_view key, float &value, std::error_code &ec) {
if(sizeof(float) != sizeof(uint32_t)) {
ec = make_error_code(NvsErrc::Value_Type_Error);
return;
}
uint32_t val_u32;
get_var(ns_name.data(), key.data(), val_u32, ec);
if(!ec)
memcpy(&value, &val_u32, sizeof(float));
}

/// @brief Reads a float variable from the NVS
/// @param[in] ns_name Namespace of the variable to read
/// @param[in] key NVS Key of the variable to read
/// @param[out] value Float variable to read
/// @param[in] default_value If the key isn't found in the NVS, this float value is saved to NVS
/// @param[out] ec Saves a std::error_code representing success or failure
void get_or_set_var(std::string_view ns_name, std::string_view key, float &value, float default_value,
std::error_code &ec) {
if(sizeof(float) != sizeof(uint32_t)) {
ec = make_error_code(NvsErrc::Value_Type_Error);
return;
}
uint32_t val_u32;
memcpy(&val_u32, &default_value, sizeof(uint32_t));
get_or_set_var(ns_name.data(), key.data(), val_u32, val_u32, ec);
if(!ec)
memcpy(&value, &val_u32, sizeof(float));
}

/// @brief Save a variable in the NVS and commit
/// @param[in] ns_name Namespace of the variable to save
/// @param[in] key NVS Key of the variable to save
Expand Down Expand Up @@ -167,5 +217,11 @@ class Nvs : public BaseComponent {

handle.get(key, value, default_value, ec);
}

/**
* @brief overload of std::make_error_code used by custom error codes.
*/
std::error_code make_error_code(NvsErrc e) { return {static_cast<int>(e), theNvsErrCategory}; }

}; // Class Nvs
} // namespace espp
1 change: 1 addition & 0 deletions components/nvs/include/nvs_errc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum class NvsErrc {
Handle_Uninitialized,
Erase_NVS_Key_Failed,
Erase_NVS_Namespace_Failed,
Value_Type_Error,
};

struct NvsErrCategory : std::error_category {
Expand Down

0 comments on commit 19cc7e9

Please sign in to comment.