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.

325 lines
9.0 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. // -------------------------------------------------------------------------
  82. Embedis::command( F("RESET.WIFI"), [](Embedis* e) {
  83. wifiConfigure();
  84. wifiDisconnect();
  85. e->response(Embedis::OK);
  86. });
  87. Embedis::command( F("RESET.MQTT"), [](Embedis* e) {
  88. mqttConfigure();
  89. mqttDisconnect();
  90. e->response(Embedis::OK);
  91. });
  92. Embedis::command( F("RESET"), [](Embedis* e) {
  93. e->response(Embedis::OK);
  94. customReset(CUSTOM_RESET_TERMINAL);
  95. ESP.restart();
  96. });
  97. #if NOFUSS_SUPPORT
  98. Embedis::command( F("NOFUSS"), [](Embedis* e) {
  99. e->response(Embedis::OK);
  100. nofussRun();
  101. });
  102. #endif
  103. Embedis::command( F("FACTORY.RESET"), [](Embedis* e) {
  104. settingsFactoryReset();
  105. e->response(Embedis::OK);
  106. });
  107. Embedis::command( F("HEAP"), [](Embedis* e) {
  108. e->stream->printf("Free HEAP: %d bytes\n", ESP.getFreeHeap());
  109. e->response(Embedis::OK);
  110. });
  111. Embedis::command( F("RELAY"), [](Embedis* e) {
  112. if (e->argc < 2) {
  113. return e->response(Embedis::ARGS_ERROR);
  114. }
  115. int id = String(e->argv[1]).toInt();
  116. if (e->argc > 2) {
  117. int value = String(e->argv[2]).toInt();
  118. if (value == 2) {
  119. relayToggle(id);
  120. } else {
  121. relayStatus(id, value == 1);
  122. }
  123. }
  124. e->stream->printf("Status: %s\n", relayStatus(id) ? "true" : "false");
  125. e->response(Embedis::OK);
  126. });
  127. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  128. if (lightHasColor()) {
  129. Embedis::command( F("COLOR"), [](Embedis* e) {
  130. if (e->argc > 1) {
  131. String color = String(e->argv[1]);
  132. lightColor(color.c_str());
  133. lightUpdate(true, true);
  134. }
  135. e->stream->printf("Color: %s\n", lightColor().c_str());
  136. e->response(Embedis::OK);
  137. });
  138. Embedis::command( F("BRIGHTNESS"), [](Embedis* e) {
  139. if (e->argc > 1) {
  140. lightBrightness(String(e->argv[1]).toInt());
  141. lightUpdate(true, true);
  142. }
  143. e->stream->printf("Brightness: %d\n", lightBrightness());
  144. e->response(Embedis::OK);
  145. });
  146. Embedis::command( F("MIRED"), [](Embedis* e) {
  147. if (e->argc > 1) {
  148. String color = String("M") + String(e->argv[1]);
  149. lightColor(color.c_str());
  150. lightUpdate(true, true);
  151. }
  152. e->stream->printf("Color: %s\n", lightColor().c_str());
  153. e->response(Embedis::OK);
  154. });
  155. Embedis::command( F("KELVIN"), [](Embedis* e) {
  156. if (e->argc > 1) {
  157. String color = String("K") + String(e->argv[1]);
  158. lightColor(color.c_str());
  159. lightUpdate(true, true);
  160. }
  161. e->stream->printf("Color: %s\n", lightColor().c_str());
  162. e->response(Embedis::OK);
  163. });
  164. }
  165. Embedis::command( F("CHANNEL"), [](Embedis* e) {
  166. if (e->argc < 2) {
  167. return e->response(Embedis::ARGS_ERROR);
  168. }
  169. int id = String(e->argv[1]).toInt();
  170. if (e->argc > 2) {
  171. int value = String(e->argv[2]).toInt();
  172. lightChannel(id, value);
  173. lightUpdate(true, true);
  174. }
  175. e->stream->printf("Channel #%d: %d\n", id, lightChannel(id));
  176. e->response(Embedis::OK);
  177. });
  178. #endif
  179. Embedis::command( F("EEPROM"), [](Embedis* e) {
  180. unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize();
  181. e->stream->printf("Number of keys: %d\n", settingsKeyCount());
  182. e->stream->printf("Free EEPROM: %d bytes (%d%%)\n", freeEEPROM, 100 * freeEEPROM / SPI_FLASH_SEC_SIZE);
  183. e->response(Embedis::OK);
  184. });
  185. Embedis::command( F("DUMP"), [](Embedis* e) {
  186. unsigned int size = settingsKeyCount();
  187. for (unsigned int i=0; i<size; i++) {
  188. String key = settingsKeyName(i);
  189. String value = getSetting(key);
  190. e->stream->printf("+%s => %s\n", key.c_str(), value.c_str());
  191. }
  192. e->response(Embedis::OK);
  193. });
  194. Embedis::command( F("DUMP.RAW"), [](Embedis* e) {
  195. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  196. if (i % 16 == 0) e->stream->printf("\n[%04X] ", i);
  197. e->stream->printf("%02X ", EEPROM.read(i));
  198. }
  199. e->stream->printf("\n");
  200. e->response(Embedis::OK);
  201. });
  202. DEBUG_MSG_P(PSTR("[SETTINGS] EEPROM size: %d bytes\n"), SPI_FLASH_SEC_SIZE);
  203. DEBUG_MSG_P(PSTR("[SETTINGS] Settings size: %d bytes\n"), settingsSize());
  204. }
  205. void settingsDump() {
  206. unsigned int size = settingsKeyCount();
  207. for (unsigned int i=0; i<size; i++) {
  208. String key = settingsKeyName(i);
  209. String value = getSetting(key);
  210. DEBUG_MSG_P(PSTR("%s => %s\n"), key.c_str(), value.c_str());
  211. }
  212. }
  213. void settingsLoop() {
  214. if (_settings_save) {
  215. DEBUG_MSG_P(PSTR("[SETTINGS] Saving\n"));
  216. EEPROM.commit();
  217. _settings_save = false;
  218. }
  219. #if TERMINAL_SUPPORT
  220. embedis.process();
  221. #endif
  222. }
  223. void moveSetting(const char * from, const char * to) {
  224. String value = getSetting(from);
  225. if (value.length() > 0) setSetting(to, value);
  226. delSetting(from);
  227. }
  228. template<typename T> String getSetting(const String& key, T defaultValue) {
  229. String value;
  230. if (!Embedis::get(key, value)) value = String(defaultValue);
  231. return value;
  232. }
  233. template<typename T> String getSetting(const String& key, unsigned int index, T defaultValue) {
  234. return getSetting(key + String(index), defaultValue);
  235. }
  236. String getSetting(const String& key) {
  237. return getSetting(key, "");
  238. }
  239. bool setBoolSetting(const String& key, bool value, bool defaultValue) {
  240. if (value == defaultValue) {
  241. return delSetting(key);
  242. } else {
  243. return setSetting(key, value ? 1 : 0);
  244. }
  245. }
  246. template<typename T> bool setSetting(const String& key, T value) {
  247. return Embedis::set(key, String(value));
  248. }
  249. template<typename T> bool setSetting(const String& key, unsigned int index, T value) {
  250. return setSetting(key + String(index), value);
  251. }
  252. bool delSetting(const String& key) {
  253. return Embedis::del(key);
  254. }
  255. bool delSetting(const String& key, unsigned int index) {
  256. return delSetting(key + String(index));
  257. }
  258. bool hasSetting(const String& key) {
  259. return getSetting(key).length() != 0;
  260. }
  261. bool hasSetting(const String& key, unsigned int index) {
  262. return getSetting(key, index, "").length() != 0;
  263. }
  264. void saveSettings() {
  265. #if not SETTINGS_AUTOSAVE
  266. _settings_save = true;
  267. #endif
  268. //settingsDump();
  269. }