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.

238 lines
6.3 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. settings_key_t() :
  32. value(), index(-1)
  33. {}
  34. bool match(const char* _value) const {
  35. return (value == _value) || (toString() == _value);
  36. }
  37. bool match(const String& _value) const {
  38. return (value == _value) || (toString() == _value);
  39. }
  40. String toString() const;
  41. explicit operator String () const {
  42. return toString();
  43. }
  44. private:
  45. const String value;
  46. int index;
  47. };
  48. using settings_move_key_t = std::pair<settings_key_t, settings_key_t>;
  49. using settings_filter_t = std::function<String(String& value)>;
  50. // --------------------------------------------------------------------------
  51. struct settings_cfg_t {
  52. String& setting;
  53. const char* key;
  54. const char* default_value;
  55. };
  56. using settings_cfg_list_t = std::initializer_list<settings_cfg_t>;
  57. // --------------------------------------------------------------------------
  58. namespace settings {
  59. namespace internal {
  60. uint32_t u32fromString(const String& string, int base);
  61. template <typename T>
  62. using convert_t = T(*)(const String& value);
  63. template <typename T>
  64. T convert(const String& value);
  65. // --------------------------------------------------------------------------
  66. template <>
  67. float convert(const String& value);
  68. template <>
  69. double convert(const String& value);
  70. template <>
  71. int convert(const String& value);
  72. template <>
  73. long convert(const String& value);
  74. template <>
  75. bool convert(const String& value);
  76. template <>
  77. unsigned long convert(const String& value);
  78. template <>
  79. unsigned int convert(const String& value);
  80. template <>
  81. unsigned short convert(const String& value);
  82. template <>
  83. unsigned char convert(const String& value);
  84. template<typename T>
  85. String serialize(const T& value);
  86. template<typename T>
  87. String serialize(const T& value) {
  88. return String(value);
  89. }
  90. } // namespace settings::internal
  91. } // namespace settings
  92. // --------------------------------------------------------------------------
  93. struct settings_key_match_t {
  94. using match_f = bool(*)(const char* key);
  95. using key_f = const String(*)(const String& key);
  96. match_f match;
  97. key_f key;
  98. };
  99. void settingsRegisterDefaults(const settings_key_match_t& matcher);
  100. String settingsQueryDefaults(const String& key);
  101. // --------------------------------------------------------------------------
  102. void moveSetting(const String& from, const String& to);
  103. void moveSetting(const String& from, const String& to, unsigned int index);
  104. void moveSettings(const String& from, const String& to);
  105. template<typename R, settings::internal::convert_t<R> Rfunc = settings::internal::convert>
  106. R getSetting(const settings_key_t& key, R defaultValue) __attribute__((noinline));
  107. template<typename R, settings::internal::convert_t<R> Rfunc = settings::internal::convert>
  108. R getSetting(const settings_key_t& key, R defaultValue) {
  109. String value;
  110. if (!Embedis::get(key.toString(), value)) {
  111. return defaultValue;
  112. }
  113. return Rfunc(value);
  114. }
  115. template<>
  116. String getSetting(const settings_key_t& key, String defaultValue);
  117. String getSetting(const settings_key_t& key);
  118. String getSetting(const settings_key_t& key, const char* defaultValue);
  119. String getSetting(const settings_key_t& key, const __FlashStringHelper* defaultValue);
  120. template<typename T>
  121. bool setSetting(const settings_key_t& key, const T& value) {
  122. return Embedis::set(key.toString(), String(value));
  123. }
  124. template<>
  125. bool setSetting(const settings_key_t& key, const String& value);
  126. bool delSetting(const settings_key_t& key);
  127. bool hasSetting(const settings_key_t& key);
  128. void saveSettings();
  129. void resetSettings();
  130. void settingsGetJson(JsonObject& data);
  131. bool settingsRestoreJson(char* json_string, size_t json_buffer_size = 1024);
  132. bool settingsRestoreJson(JsonObject& data);
  133. size_t settingsKeyCount();
  134. String settingsKeyName(unsigned int index);
  135. std::vector<String> settingsKeys();
  136. void settingsProcessConfig(const settings_cfg_list_t& config, settings_filter_t filter = nullptr);
  137. unsigned long settingsSize();
  138. void migrate();
  139. void settingsSetup();
  140. // -----------------------------------------------------------------------------
  141. // Deprecated implementation
  142. // -----------------------------------------------------------------------------
  143. template <typename T>
  144. String getSetting(const String& key, unsigned char index, T defaultValue)
  145. __attribute__((deprecated("getSetting({key, index}, default) should be used instead")));
  146. template<typename T>
  147. bool setSetting(const String& key, unsigned char index, T value)
  148. __attribute__((deprecated("setSetting({key, index}, value) should be used instead")));
  149. template<typename T>
  150. bool hasSetting(const String& key, unsigned char index)
  151. __attribute__((deprecated("hasSetting({key, index}) should be used instead")));
  152. template<typename T>
  153. bool delSetting(const String& key, unsigned char index)
  154. __attribute__((deprecated("delSetting({key, index}) should be used instead")));
  155. // --------------------------------------------------------------------------
  156. template<typename T>
  157. String getSetting(const String& key, unsigned char index, T defaultValue) {
  158. return getSetting({key, index}, defaultValue);
  159. }
  160. template<typename T>
  161. bool setSetting(const String& key, unsigned char index, T value) {
  162. return setSetting({key, index}, value);
  163. }
  164. template<typename T>
  165. bool hasSetting(const String& key, unsigned char index) {
  166. return hasSetting({key, index});
  167. }
  168. template<typename T>
  169. bool delSetting(const String& key, unsigned char index) {
  170. return delSetting({key, index});
  171. }