Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
5.5 KiB

  1. /*
  2. SETTINGS MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #pragma once
  6. #include <Arduino.h>
  7. #include <functional>
  8. #include <utility>
  9. #include <vector>
  10. #include <ArduinoJson.h>
  11. #include "espurna.h"
  12. #include "libs/EmbedisWrap.h"
  13. // --------------------------------------------------------------------------
  14. class settings_key_t {
  15. public:
  16. settings_key_t(const char* value, unsigned char index) :
  17. value(value), index(index)
  18. {}
  19. settings_key_t(const String& value, unsigned char index) :
  20. value(value), index(index)
  21. {}
  22. settings_key_t(const char* value) :
  23. value(value), index(-1)
  24. {}
  25. settings_key_t(const String& value) :
  26. value(value), index(-1)
  27. {}
  28. settings_key_t(const __FlashStringHelper* value) :
  29. value(value), index(-1)
  30. {}
  31. String toString() const;
  32. explicit operator String () const {
  33. return toString();
  34. }
  35. private:
  36. const String value;
  37. int index;
  38. };
  39. using settings_move_key_t = std::pair<settings_key_t, settings_key_t>;
  40. using settings_filter_t = std::function<String(String& value)>;
  41. // --------------------------------------------------------------------------
  42. struct settings_cfg_t {
  43. String& setting;
  44. const char* key;
  45. const char* default_value;
  46. };
  47. using settings_cfg_list_t = std::initializer_list<settings_cfg_t>;
  48. // --------------------------------------------------------------------------
  49. namespace settings {
  50. namespace internal {
  51. uint32_t u32fromString(const String& string, int base);
  52. template <typename T>
  53. using convert_t = T(*)(const String& value);
  54. template <typename T>
  55. T convert(const String& value);
  56. // --------------------------------------------------------------------------
  57. template <>
  58. float convert(const String& value);
  59. template <>
  60. double convert(const String& value);
  61. template <>
  62. int convert(const String& value);
  63. template <>
  64. long convert(const String& value);
  65. template <>
  66. bool convert(const String& value);
  67. template <>
  68. unsigned long convert(const String& value);
  69. template <>
  70. unsigned int convert(const String& value);
  71. template <>
  72. unsigned short convert(const String& value);
  73. template <>
  74. unsigned char convert(const String& value);
  75. } // namespace settings::internal
  76. } // namespace settings
  77. // --------------------------------------------------------------------------
  78. void moveSetting(const String& from, const String& to);
  79. void moveSetting(const String& from, const String& to, unsigned int index);
  80. void moveSettings(const String& from, const String& to);
  81. #if 1
  82. template<typename R, settings::internal::convert_t<R> Rfunc = settings::internal::convert>
  83. R getSetting(const settings_key_t& key, R defaultValue) __attribute__((noinline));
  84. #endif
  85. template<typename R, settings::internal::convert_t<R> Rfunc = settings::internal::convert>
  86. R getSetting(const settings_key_t& key, R defaultValue) {
  87. String value;
  88. if (!Embedis::get(key.toString(), value)) {
  89. return defaultValue;
  90. }
  91. return Rfunc(value);
  92. }
  93. template<>
  94. String getSetting(const settings_key_t& key, String defaultValue);
  95. String getSetting(const settings_key_t& key);
  96. String getSetting(const settings_key_t& key, const char* defaultValue);
  97. String getSetting(const settings_key_t& key, const __FlashStringHelper* defaultValue);
  98. template<typename T>
  99. bool setSetting(const settings_key_t& key, const T& value) {
  100. return Embedis::set(key.toString(), String(value));
  101. }
  102. template<>
  103. bool setSetting(const settings_key_t& key, const String& value);
  104. bool delSetting(const settings_key_t& key);
  105. bool hasSetting(const settings_key_t& key);
  106. void saveSettings();
  107. void resetSettings();
  108. void settingsGetJson(JsonObject& data);
  109. bool settingsRestoreJson(char* json_string, size_t json_buffer_size = 1024);
  110. bool settingsRestoreJson(JsonObject& data);
  111. size_t settingsKeyCount();
  112. String settingsKeyName(unsigned int index);
  113. std::vector<String> settingsKeys();
  114. void settingsProcessConfig(const settings_cfg_list_t& config, settings_filter_t filter = nullptr);
  115. unsigned long settingsSize();
  116. void migrate();
  117. void settingsSetup();
  118. // -----------------------------------------------------------------------------
  119. // Deprecated implementation
  120. // -----------------------------------------------------------------------------
  121. template <typename T>
  122. String getSetting(const String& key, unsigned char index, T defaultValue)
  123. __attribute__((deprecated("getSetting({key, index}, default) should be used instead")));
  124. template<typename T>
  125. bool setSetting(const String& key, unsigned char index, T value)
  126. __attribute__((deprecated("setSetting({key, index}, value) should be used instead")));
  127. template<typename T>
  128. bool hasSetting(const String& key, unsigned char index)
  129. __attribute__((deprecated("hasSetting({key, index}) should be used instead")));
  130. template<typename T>
  131. bool delSetting(const String& key, unsigned char index)
  132. __attribute__((deprecated("delSetting({key, index}) should be used instead")));
  133. // --------------------------------------------------------------------------
  134. template<typename T>
  135. String getSetting(const String& key, unsigned char index, T defaultValue) {
  136. return getSetting({key, index}, defaultValue);
  137. }
  138. template<typename T>
  139. bool setSetting(const String& key, unsigned char index, T value) {
  140. return setSetting({key, index}, value);
  141. }
  142. template<typename T>
  143. bool hasSetting(const String& key, unsigned char index) {
  144. return hasSetting({key, index});
  145. }
  146. template<typename T>
  147. bool delSetting(const String& key, unsigned char index) {
  148. return delSetting({key, index});
  149. }