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.

321 lines
8.8 KiB

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. #ifdef DEBUG_PORT
  10. Embedis embedis(DEBUG_PORT);
  11. #else
  12. Embedis embedis(Serial);
  13. #endif
  14. bool _settings_save = false;
  15. // -----------------------------------------------------------------------------
  16. // Settings
  17. // -----------------------------------------------------------------------------
  18. size_t settingsMaxSize() {
  19. size_t size = EEPROM_SIZE;
  20. if (size > SPI_FLASH_SEC_SIZE) size = SPI_FLASH_SEC_SIZE;
  21. size = (size + 3) & (~3);
  22. return size;
  23. }
  24. unsigned long settingsSize() {
  25. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  26. while (size_t len = EEPROM.read(pos)) {
  27. pos = pos - len - 2;
  28. }
  29. return SPI_FLASH_SEC_SIZE - pos;
  30. }
  31. unsigned int settingsKeyCount() {
  32. unsigned count = 0;
  33. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  34. while (size_t len = EEPROM.read(pos)) {
  35. pos = pos - len - 2;
  36. count ++;
  37. }
  38. return count / 2;
  39. }
  40. String settingsKeyName(unsigned int index) {
  41. String s;
  42. unsigned count = 0;
  43. unsigned stop = index * 2 + 1;
  44. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  45. while (size_t len = EEPROM.read(pos)) {
  46. pos = pos - len - 2;
  47. count++;
  48. if (count == stop) {
  49. s.reserve(len);
  50. for (unsigned char i = 0 ; i < len; i++) {
  51. s += (char) EEPROM.read(pos + i + 1);
  52. }
  53. break;
  54. }
  55. }
  56. return s;
  57. }
  58. void settingsFactoryReset() {
  59. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  60. EEPROM.write(i, 0xFF);
  61. }
  62. EEPROM.commit();
  63. }
  64. void settingsSetup() {
  65. EEPROM.begin(SPI_FLASH_SEC_SIZE);
  66. Embedis::dictionary( F("EEPROM"),
  67. SPI_FLASH_SEC_SIZE,
  68. [](size_t pos) -> char { return EEPROM.read(pos); },
  69. [](size_t pos, char value) { EEPROM.write(pos, value); },
  70. #if SETTINGS_AUTOSAVE
  71. []() { _settings_save = true; }
  72. #else
  73. []() {}
  74. #endif
  75. );
  76. Embedis::hardware( F("WIFI"), [](Embedis* e) {
  77. StreamString s;
  78. WiFi.printDiag(s);
  79. e->response(s);
  80. }, 0);
  81. Embedis::command( F("RESET.WIFI"), [](Embedis* e) {
  82. wifiConfigure();
  83. wifiDisconnect();
  84. e->response(Embedis::OK);
  85. });
  86. Embedis::command( F("RESET.MQTT"), [](Embedis* e) {
  87. mqttConfigure();
  88. mqttDisconnect();
  89. e->response(Embedis::OK);
  90. });
  91. Embedis::command( F("RESET"), [](Embedis* e) {
  92. e->response(Embedis::OK);
  93. customReset(CUSTOM_RESET_TERMINAL);
  94. ESP.restart();
  95. });
  96. Embedis::command( F("NOFUSS"), [](Embedis* e) {
  97. e->response(Embedis::OK);
  98. nofussRun();
  99. });
  100. Embedis::command( F("FACTORY.RESET"), [](Embedis* e) {
  101. settingsFactoryReset();
  102. e->response(Embedis::OK);
  103. });
  104. Embedis::command( F("HEAP"), [](Embedis* e) {
  105. e->stream->printf("Free HEAP: %d bytes\n", ESP.getFreeHeap());
  106. e->response(Embedis::OK);
  107. });
  108. Embedis::command( F("RELAY"), [](Embedis* e) {
  109. if (e->argc < 2) {
  110. return e->response(Embedis::ARGS_ERROR);
  111. }
  112. int id = String(e->argv[1]).toInt();
  113. if (e->argc > 2) {
  114. int value = String(e->argv[2]).toInt();
  115. if (value == 2) {
  116. relayToggle(id);
  117. } else {
  118. relayStatus(id, value == 1);
  119. }
  120. }
  121. e->stream->printf("Status: %s\n", relayStatus(id) ? "true" : "false");
  122. e->response(Embedis::OK);
  123. });
  124. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  125. if (lightHasColor()) {
  126. Embedis::command( F("COLOR"), [](Embedis* e) {
  127. if (e->argc > 1) {
  128. String color = String(e->argv[1]);
  129. lightColor(color.c_str());
  130. lightUpdate(true, true);
  131. }
  132. e->stream->printf("Color: %s\n", lightColor().c_str());
  133. e->response(Embedis::OK);
  134. });
  135. Embedis::command( F("BRIGHTNESS"), [](Embedis* e) {
  136. if (e->argc > 1) {
  137. lightBrightness(String(e->argv[1]).toInt());
  138. lightUpdate(true, true);
  139. }
  140. e->stream->printf("Brightness: %d\n", lightBrightness());
  141. e->response(Embedis::OK);
  142. });
  143. Embedis::command( F("MIRED"), [](Embedis* e) {
  144. if (e->argc > 1) {
  145. String color = String("M") + String(e->argv[1]);
  146. lightColor(color.c_str());
  147. lightUpdate(true, true);
  148. }
  149. e->stream->printf("Color: %s\n", lightColor().c_str());
  150. e->response(Embedis::OK);
  151. });
  152. Embedis::command( F("KELVIN"), [](Embedis* e) {
  153. if (e->argc > 1) {
  154. String color = String("K") + String(e->argv[1]);
  155. lightColor(color.c_str());
  156. lightUpdate(true, true);
  157. }
  158. e->stream->printf("Color: %s\n", lightColor().c_str());
  159. e->response(Embedis::OK);
  160. });
  161. }
  162. Embedis::command( F("CHANNEL"), [](Embedis* e) {
  163. if (e->argc < 2) {
  164. return e->response(Embedis::ARGS_ERROR);
  165. }
  166. int id = String(e->argv[1]).toInt();
  167. if (e->argc > 2) {
  168. int value = String(e->argv[2]).toInt();
  169. lightChannel(id, value);
  170. lightUpdate(true, true);
  171. }
  172. e->stream->printf("Channel #%d: %d\n", id, lightChannel(id));
  173. e->response(Embedis::OK);
  174. });
  175. #endif
  176. Embedis::command( F("EEPROM"), [](Embedis* e) {
  177. unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize();
  178. e->stream->printf("Number of keys: %d\n", settingsKeyCount());
  179. e->stream->printf("Free EEPROM: %d bytes (%d%%)\n", freeEEPROM, 100 * freeEEPROM / SPI_FLASH_SEC_SIZE);
  180. e->response(Embedis::OK);
  181. });
  182. Embedis::command( F("DUMP"), [](Embedis* e) {
  183. unsigned int size = settingsKeyCount();
  184. for (unsigned int i=0; i<size; i++) {
  185. String key = settingsKeyName(i);
  186. String value = getSetting(key);
  187. e->stream->printf("+%s => %s\n", key.c_str(), value.c_str());
  188. }
  189. e->response(Embedis::OK);
  190. });
  191. Embedis::command( F("DUMP.RAW"), [](Embedis* e) {
  192. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  193. if (i % 16 == 0) e->stream->printf("\n[%04X] ", i);
  194. e->stream->printf("%02X ", EEPROM.read(i));
  195. }
  196. e->stream->printf("\n");
  197. e->response(Embedis::OK);
  198. });
  199. DEBUG_MSG_P(PSTR("[SETTINGS] EEPROM size: %d bytes\n"), SPI_FLASH_SEC_SIZE);
  200. DEBUG_MSG_P(PSTR("[SETTINGS] Settings size: %d bytes\n"), settingsSize());
  201. }
  202. void settingsDump() {
  203. unsigned int size = settingsKeyCount();
  204. for (unsigned int i=0; i<size; i++) {
  205. String key = settingsKeyName(i);
  206. String value = getSetting(key);
  207. DEBUG_MSG_P(PSTR("%s => %s\n"), key.c_str(), value.c_str());
  208. }
  209. }
  210. void settingsLoop() {
  211. if (_settings_save) {
  212. DEBUG_MSG_P(PSTR("[SETTINGS] Saving\n"));
  213. EEPROM.commit();
  214. _settings_save = false;
  215. }
  216. #if TERMINAL_SUPPORT
  217. embedis.process();
  218. #endif
  219. }
  220. void moveSetting(const char * from, const char * to) {
  221. String value = getSetting(from);
  222. if (value.length() > 0) setSetting(to, value);
  223. delSetting(from);
  224. }
  225. template<typename T> String getSetting(const String& key, T defaultValue) {
  226. String value;
  227. if (!Embedis::get(key, value)) value = String(defaultValue);
  228. return value;
  229. }
  230. template<typename T> String getSetting(const String& key, unsigned int index, T defaultValue) {
  231. return getSetting(key + String(index), defaultValue);
  232. }
  233. String getSetting(const String& key) {
  234. return getSetting(key, "");
  235. }
  236. bool setBoolSetting(const String& key, bool value, bool defaultValue) {
  237. if (value == defaultValue) {
  238. return delSetting(key);
  239. } else {
  240. return setSetting(key, value ? 1 : 0);
  241. }
  242. }
  243. template<typename T> bool setSetting(const String& key, T value) {
  244. return Embedis::set(key, String(value));
  245. }
  246. template<typename T> bool setSetting(const String& key, unsigned int index, T value) {
  247. return setSetting(key + String(index), value);
  248. }
  249. bool delSetting(const String& key) {
  250. return Embedis::del(key);
  251. }
  252. bool delSetting(const String& key, unsigned int index) {
  253. return delSetting(key + String(index));
  254. }
  255. bool hasSetting(const String& key) {
  256. return getSetting(key).length() != 0;
  257. }
  258. bool hasSetting(const String& key, unsigned int index) {
  259. return getSetting(key, index, "").length() != 0;
  260. }
  261. void saveSettings() {
  262. #if not SETTINGS_AUTOSAVE
  263. _settings_save = true;
  264. #endif
  265. //settingsDump();
  266. }