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.

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