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.

163 lines
4.0 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. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  16. while (size_t len = EEPROM.read(pos)) {
  17. pos = pos - len - 2;
  18. }
  19. return SPI_FLASH_SEC_SIZE - pos;
  20. }
  21. unsigned int settingsKeyCount() {
  22. unsigned count = 0;
  23. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  24. while (size_t len = EEPROM.read(pos)) {
  25. pos = pos - len - 2;
  26. count ++;
  27. }
  28. return count / 2;
  29. }
  30. String settingsKeyName(unsigned int index) {
  31. String s;
  32. unsigned count = 0;
  33. unsigned stop = index * 2 + 1;
  34. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  35. while (size_t len = EEPROM.read(pos)) {
  36. pos = pos - len - 2;
  37. count++;
  38. if (count == stop) {
  39. s.reserve(len);
  40. for (unsigned char i = 0 ; i < len; i++) {
  41. s += (char) EEPROM.read(pos + i + 1);
  42. }
  43. break;
  44. }
  45. }
  46. return s;
  47. }
  48. void settingsSetup() {
  49. EEPROM.begin(SPI_FLASH_SEC_SIZE);
  50. Embedis::dictionary( F("EEPROM"),
  51. SPI_FLASH_SEC_SIZE,
  52. [](size_t pos) -> char { return EEPROM.read(pos); },
  53. [](size_t pos, char value) { EEPROM.write(pos, value); },
  54. #if AUTO_SAVE
  55. []() { EEPROM.commit(); }
  56. #else
  57. []() {}
  58. #endif
  59. );
  60. Embedis::hardware( F("WIFI"), [](Embedis* e) {
  61. StreamString s;
  62. WiFi.printDiag(s);
  63. e->response(s);
  64. }, 0);
  65. Embedis::command( F("RECONNECT"), [](Embedis* e) {
  66. wifiConfigure();
  67. wifiDisconnect();
  68. e->response(Embedis::OK);
  69. });
  70. Embedis::command( F("RESET"), [](Embedis* e) {
  71. e->response(Embedis::OK);
  72. ESP.restart();
  73. });
  74. Embedis::command( F("STATUS"), [](Embedis* e) {
  75. e->stream->printf("Free heap: %d bytes\n", ESP.getFreeHeap());
  76. e->response(Embedis::OK);
  77. });
  78. Embedis::command( F("EEPROM.DUMP"), [](Embedis* e) {
  79. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  80. if (i % 16 == 0) e->stream->printf("\n[%04X] ", i);
  81. e->stream->printf("%02X ", EEPROM.read(i));
  82. }
  83. e->stream->printf("\n");
  84. e->response(Embedis::OK);
  85. });
  86. Embedis::command( F("EEPROM.ERASE"), [](Embedis* e) {
  87. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  88. EEPROM.write(i, 0xFF);
  89. }
  90. EEPROM.commit();
  91. e->response(Embedis::OK);
  92. });
  93. Embedis::command( F("SETTINGS.SIZE"), [](Embedis* e) {
  94. e->response(String(settingsSize()));
  95. });
  96. Embedis::command( F("DUMP"), [](Embedis* e) {
  97. unsigned int size = settingsKeyCount();
  98. for (unsigned int i=0; i<size; i++) {
  99. String key = settingsKeyName(i);
  100. String value = getSetting(key);
  101. e->stream->printf("+%s => %s\n", key.c_str(), value.c_str());
  102. }
  103. e->response(Embedis::OK);
  104. });
  105. DEBUG_MSG("[SETTINGS] EEPROM size: %d bytes\n", SPI_FLASH_SEC_SIZE);
  106. DEBUG_MSG("[SETTINGS] Settings size: %d bytes\n", settingsSize());
  107. }
  108. void settingsLoop() {
  109. embedis.process();
  110. }
  111. template<typename T> String getSetting(const String& key, T defaultValue) {
  112. String value;
  113. if (!Embedis::get(key, value)) value = String(defaultValue);
  114. return value;
  115. }
  116. String getSetting(const String& key) {
  117. return getSetting(key, "");
  118. }
  119. template<typename T> bool setSetting(const String& key, T value) {
  120. return Embedis::set(key, String(value));
  121. }
  122. bool delSetting(const String& key) {
  123. return Embedis::del(key);
  124. }
  125. void saveSettings() {
  126. DEBUG_MSG("[SETTINGS] Saving\n");
  127. #if not AUTO_SAVE
  128. EEPROM.commit();
  129. #endif
  130. }