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.

354 lines
9.7 KiB

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