nvs.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifndef KBF_NVS_H
  2. #define KBF_NVS_H
  3. #include <string>
  4. #include <atomic>
  5. #include <esp_log.h>
  6. #include <nvs_flash.h>
  7. using std::string;
  8. namespace kbf::nvs {
  9. /**
  10. * @brief Non-Volatile Storage read / write functions.
  11. */
  12. class NVS {
  13. public:
  14. /**
  15. * @brief Constructor. Calls init() if it hasn't been called yet.
  16. *
  17. * @param name NVS namespace name; default is "storage"
  18. */
  19. explicit NVS(const string &name = "storage");
  20. ~NVS();
  21. /**
  22. * @brief Reads a numeric value from NVS.
  23. *
  24. * @tparam T value type
  25. * @param key name
  26. * @param value value
  27. * @return true if successful; false if key not found
  28. */
  29. template<typename T>
  30. bool read(const string &key, T &value);
  31. /**
  32. * @brief Writes a numeric value to NVS.
  33. *
  34. * @tparam T value type
  35. * @param key name
  36. * @param value value
  37. */
  38. template<typename T>
  39. void write(const string &key, T &value);
  40. /** @brief NVS namespace name */
  41. const string name;
  42. private:
  43. nvs_handle_t handle{};
  44. };
  45. /**
  46. * @brief Initializes NVS. Will be called from NVS class ctor if it hasn't been called yet.
  47. */
  48. void init();
  49. /**
  50. * Erases NVS partition.
  51. */
  52. void erase();
  53. }
  54. #endif //KBF_NVS_H