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.

77 lines
1.6 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
  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. void settingsSetup() {
  16. EEPROM.begin(SPI_FLASH_SEC_SIZE);
  17. Embedis::dictionary( F("EEPROM"),
  18. SPI_FLASH_SEC_SIZE,
  19. [](size_t pos) -> char { return EEPROM.read(pos); },
  20. [](size_t pos, char value) { EEPROM.write(pos, value); },
  21. #if AUTO_SAVE
  22. []() { EEPROM.commit(); }
  23. #else
  24. []() {}
  25. #endif
  26. );
  27. Embedis::hardware( F("wifi"), [](Embedis* e) {
  28. StreamString s;
  29. WiFi.printDiag(s);
  30. e->response(s);
  31. }, 0);
  32. Embedis::command( F("reconnect"), [](Embedis* e) {
  33. wifiConfigure();
  34. wifiDisconnect();
  35. e->response(Embedis::OK);
  36. });
  37. DEBUG_MSG("[SETTINGS] Initialized\n");
  38. }
  39. void settingsLoop() {
  40. embedis.process();
  41. }
  42. String getSetting(const String& key, String defaultValue) {
  43. String value;
  44. if (!Embedis::get(key, value)) value = defaultValue;
  45. return value;
  46. }
  47. bool setSetting(const String& key, String& value) {
  48. return Embedis::set(key, value);
  49. }
  50. bool delSetting(const String& key) {
  51. return Embedis::del(key);
  52. }
  53. void saveSettings() {
  54. DEBUG_MSG("[SETTINGS] Saving\n");
  55. #if not AUTO_SAVE
  56. EEPROM.commit();
  57. #endif
  58. }