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.

122 lines
2.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /*
  2. SETTINGS MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include "Embedis.h"
  6. #include <EEPROM.h>
  7. #include "spi_flash.h"
  8. #include <StreamString.h>
  9. #define AUTO_SAVE 1
  10. Embedis embedis(Serial);
  11. // -----------------------------------------------------------------------------
  12. // Settings
  13. // -----------------------------------------------------------------------------
  14. unsigned long settingsSize() {
  15. bool zero = false;
  16. unsigned long size = 0;
  17. for (unsigned int i = SPI_FLASH_SEC_SIZE; i>0; i--) {
  18. size++;
  19. if (EEPROM.read(i) == 0) {
  20. if (zero) break;
  21. zero = true;
  22. } else {
  23. zero = false;
  24. }
  25. }
  26. return size-2;
  27. }
  28. void settingsSetup() {
  29. EEPROM.begin(SPI_FLASH_SEC_SIZE);
  30. Embedis::dictionary( F("EEPROM"),
  31. SPI_FLASH_SEC_SIZE,
  32. [](size_t pos) -> char { return EEPROM.read(pos); },
  33. [](size_t pos, char value) { EEPROM.write(pos, value); },
  34. #if AUTO_SAVE
  35. []() { EEPROM.commit(); }
  36. #else
  37. []() {}
  38. #endif
  39. );
  40. Embedis::hardware( F("WIFI"), [](Embedis* e) {
  41. StreamString s;
  42. WiFi.printDiag(s);
  43. e->response(s);
  44. }, 0);
  45. Embedis::command( F("RECONNECT"), [](Embedis* e) {
  46. wifiConfigure();
  47. wifiDisconnect();
  48. e->response(Embedis::OK);
  49. });
  50. Embedis::command( F("RESET"), [](Embedis* e) {
  51. e->response(Embedis::OK);
  52. ESP.reset();
  53. });
  54. Embedis::command( F("EEPROM.DUMP"), [](Embedis* e) {
  55. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  56. if (i % 16 == 0) Serial.printf("\n[%04X] ", i);
  57. Serial.printf("%02X ", EEPROM.read(i));
  58. }
  59. Serial.printf("\n");
  60. e->response(Embedis::OK);
  61. });
  62. Embedis::command( F("EEPROM.ERASE"), [](Embedis* e) {
  63. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  64. EEPROM.write(i, 0xFF);
  65. }
  66. EEPROM.commit();
  67. e->response(Embedis::OK);
  68. });
  69. Embedis::command( F("SETTINGS.SIZE"), [](Embedis* e) {
  70. e->response(String(settingsSize()));
  71. });
  72. DEBUG_MSG("[SETTINGS] EEPROM size: %d bytes\n", SPI_FLASH_SEC_SIZE);
  73. DEBUG_MSG("[SETTINGS] Settings size: %d bytes\n", settingsSize());
  74. }
  75. void settingsLoop() {
  76. embedis.process();
  77. }
  78. template<typename T> String getSetting(const String& key, T defaultValue) {
  79. String value;
  80. if (!Embedis::get(key, value)) value = String(defaultValue);
  81. return value;
  82. }
  83. String getSetting(const String& key) {
  84. return getSetting(key, "");
  85. }
  86. template<typename T> bool setSetting(const String& key, T value) {
  87. return Embedis::set(key, String(value));
  88. }
  89. bool delSetting(const String& key) {
  90. return Embedis::del(key);
  91. }
  92. void saveSettings() {
  93. DEBUG_MSG("[SETTINGS] Saving\n");
  94. #if not AUTO_SAVE
  95. EEPROM.commit();
  96. #endif
  97. }