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.

127 lines
3.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. 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("STATUS"), [](Embedis* e) {
  55. Serial.printf("Free heap: %d bytes\n", ESP.getFreeHeap());
  56. e->response(Embedis::OK);
  57. });
  58. Embedis::command( F("EEPROM.DUMP"), [](Embedis* e) {
  59. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  60. if (i % 16 == 0) Serial.printf("\n[%04X] ", i);
  61. Serial.printf("%02X ", EEPROM.read(i));
  62. }
  63. Serial.printf("\n");
  64. e->response(Embedis::OK);
  65. });
  66. Embedis::command( F("EEPROM.ERASE"), [](Embedis* e) {
  67. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  68. EEPROM.write(i, 0xFF);
  69. }
  70. EEPROM.commit();
  71. e->response(Embedis::OK);
  72. });
  73. Embedis::command( F("SETTINGS.SIZE"), [](Embedis* e) {
  74. e->response(String(settingsSize()));
  75. });
  76. DEBUG_MSG("[SETTINGS] EEPROM size: %d bytes\n", SPI_FLASH_SEC_SIZE);
  77. DEBUG_MSG("[SETTINGS] Settings size: %d bytes\n", settingsSize());
  78. }
  79. void settingsLoop() {
  80. embedis.process();
  81. }
  82. template<typename T> String getSetting(const String& key, T defaultValue) {
  83. String value;
  84. if (!Embedis::get(key, value)) value = String(defaultValue);
  85. return value;
  86. }
  87. String getSetting(const String& key) {
  88. return getSetting(key, "");
  89. }
  90. template<typename T> bool setSetting(const String& key, T value) {
  91. return Embedis::set(key, String(value));
  92. }
  93. bool delSetting(const String& key) {
  94. return Embedis::del(key);
  95. }
  96. void saveSettings() {
  97. DEBUG_MSG("[SETTINGS] Saving\n");
  98. #if not AUTO_SAVE
  99. EEPROM.commit();
  100. #endif
  101. }