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.

105 lines
3.0 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 <functional>
  7. #include <utility>
  8. #include <vector>
  9. #include <ArduinoJson.h>
  10. #include "libs/EmbedisWrap.h"
  11. #include "settings_internal.h"
  12. // --------------------------------------------------------------------------
  13. class settings_key_t {
  14. public:
  15. settings_key_t(const char* value, unsigned char index) :
  16. value(value), index(index)
  17. {}
  18. settings_key_t(const String& value, unsigned char index) :
  19. value(value), index(index)
  20. {}
  21. settings_key_t(const char* value) :
  22. value(value), index(-1)
  23. {}
  24. settings_key_t(const String& value) :
  25. value(value), index(-1)
  26. {}
  27. settings_key_t(const __FlashStringHelper* value) :
  28. value(value), index(-1)
  29. {}
  30. String toString() const;
  31. explicit operator String () const {
  32. return toString();
  33. }
  34. private:
  35. const String value;
  36. int index;
  37. };
  38. using settings_move_key_t = std::pair<settings_key_t, settings_key_t>;
  39. using settings_filter_t = std::function<String(String& value)>;
  40. // --------------------------------------------------------------------------
  41. struct settings_cfg_t {
  42. String& setting;
  43. const char* key;
  44. const char* default_value;
  45. };
  46. using settings_cfg_list_t = std::initializer_list<settings_cfg_t>;
  47. // --------------------------------------------------------------------------
  48. void moveSetting(const String& from, const String& to);
  49. void moveSetting(const String& from, const String& to, unsigned int index);
  50. void moveSettings(const String& from, const String& to);
  51. template<typename R, settings::internal::convert_t<R> Rfunc = settings::internal::convert>
  52. R getSetting(const settings_key_t& key, R defaultValue);
  53. String getSetting(const settings_key_t& key);
  54. template<typename T>
  55. bool setSetting(const settings_key_t& key, const T& value);
  56. bool delSetting(const settings_key_t& key);
  57. bool hasSetting(const settings_key_t& key);
  58. void saveSettings();
  59. void resetSettings();
  60. void settingsGetJson(JsonObject& data);
  61. bool settingsRestoreJson(JsonObject& data);
  62. void settingsProcessConfig(const settings_cfg_list_t& config, settings_filter_t filter = nullptr);
  63. // --------------------------------------------------------------------------
  64. template <typename T>
  65. String getSetting(const String& key, unsigned char index, T defaultValue)
  66. __attribute__((deprecated("getSetting({key, index}, default) should be used instead")));
  67. template<typename T>
  68. bool setSetting(const String& key, unsigned char index, T value)
  69. __attribute__((deprecated("setSetting({key, index}, value) should be used instead")));
  70. template<typename T>
  71. bool hasSetting(const String& key, unsigned char index)
  72. __attribute__((deprecated("hasSetting({key, index}) should be used instead")));
  73. template<typename T>
  74. bool delSetting(const String& key, unsigned char index)
  75. __attribute__((deprecated("delSetting({key, index}) should be used instead")));