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.

363 lines
9.9 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("INFO"), [](Embedis* e) {
  113. welcome();
  114. e->response(Embedis::OK);
  115. });
  116. Embedis::command( F("UPTIME"), [](Embedis* e) {
  117. e->stream->printf("Uptime: %d seconds\n", getUptime());
  118. e->response(Embedis::OK);
  119. });
  120. Embedis::command( F("RESET"), [](Embedis* e) {
  121. e->response(Embedis::OK);
  122. customReset(CUSTOM_RESET_TERMINAL);
  123. ESP.restart();
  124. });
  125. Embedis::command( F("ERASE.CONFIG"), [](Embedis* e) {
  126. e->response(Embedis::OK);
  127. customReset(CUSTOM_RESET_TERMINAL);
  128. ESP.eraseConfig();
  129. *((int*) 0) = 0; // see https://github.com/esp8266/Arduino/issues/1494
  130. });
  131. #if NOFUSS_SUPPORT
  132. Embedis::command( F("NOFUSS"), [](Embedis* e) {
  133. e->response(Embedis::OK);
  134. nofussRun();
  135. });
  136. #endif
  137. Embedis::command( F("FACTORY.RESET"), [](Embedis* e) {
  138. settingsFactoryReset();
  139. e->response(Embedis::OK);
  140. });
  141. Embedis::command( F("HEAP"), [](Embedis* e) {
  142. e->stream->printf("Free HEAP: %d bytes\n", ESP.getFreeHeap());
  143. e->response(Embedis::OK);
  144. });
  145. Embedis::command( F("RELAY"), [](Embedis* e) {
  146. if (e->argc < 2) {
  147. return e->response(Embedis::ARGS_ERROR);
  148. }
  149. int id = String(e->argv[1]).toInt();
  150. if (e->argc > 2) {
  151. int value = String(e->argv[2]).toInt();
  152. if (value == 2) {
  153. relayToggle(id);
  154. } else {
  155. relayStatus(id, value == 1);
  156. }
  157. }
  158. e->stream->printf("Status: %s\n", relayStatus(id) ? "true" : "false");
  159. e->response(Embedis::OK);
  160. });
  161. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  162. if (lightHasColor()) {
  163. Embedis::command( F("COLOR"), [](Embedis* e) {
  164. if (e->argc > 1) {
  165. String color = String(e->argv[1]);
  166. lightColor(color.c_str());
  167. lightUpdate(true, true);
  168. }
  169. e->stream->printf("Color: %s\n", lightColor().c_str());
  170. e->response(Embedis::OK);
  171. });
  172. Embedis::command( F("BRIGHTNESS"), [](Embedis* e) {
  173. if (e->argc > 1) {
  174. lightBrightness(String(e->argv[1]).toInt());
  175. lightUpdate(true, true);
  176. }
  177. e->stream->printf("Brightness: %d\n", lightBrightness());
  178. e->response(Embedis::OK);
  179. });
  180. Embedis::command( F("MIRED"), [](Embedis* e) {
  181. if (e->argc > 1) {
  182. String color = String("M") + String(e->argv[1]);
  183. lightColor(color.c_str());
  184. lightUpdate(true, true);
  185. }
  186. e->stream->printf("Color: %s\n", lightColor().c_str());
  187. e->response(Embedis::OK);
  188. });
  189. Embedis::command( F("KELVIN"), [](Embedis* e) {
  190. if (e->argc > 1) {
  191. String color = String("K") + String(e->argv[1]);
  192. lightColor(color.c_str());
  193. lightUpdate(true, true);
  194. }
  195. e->stream->printf("Color: %s\n", lightColor().c_str());
  196. e->response(Embedis::OK);
  197. });
  198. }
  199. Embedis::command( F("CHANNEL"), [](Embedis* e) {
  200. if (e->argc < 2) {
  201. return e->response(Embedis::ARGS_ERROR);
  202. }
  203. int id = String(e->argv[1]).toInt();
  204. if (e->argc > 2) {
  205. int value = String(e->argv[2]).toInt();
  206. lightChannel(id, value);
  207. lightUpdate(true, true);
  208. }
  209. e->stream->printf("Channel #%d: %d\n", id, lightChannel(id));
  210. e->response(Embedis::OK);
  211. });
  212. #endif
  213. Embedis::command( F("EEPROM"), [](Embedis* e) {
  214. unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize();
  215. e->stream->printf("Number of keys: %d\n", settingsKeyCount());
  216. e->stream->printf("Free EEPROM: %d bytes (%d%%)\n", freeEEPROM, 100 * freeEEPROM / SPI_FLASH_SEC_SIZE);
  217. e->response(Embedis::OK);
  218. });
  219. Embedis::command( F("DUMP"), [](Embedis* e) {
  220. unsigned int size = settingsKeyCount();
  221. for (unsigned int i=0; i<size; i++) {
  222. String key = settingsKeyName(i);
  223. String value = getSetting(key);
  224. e->stream->printf("+%s => %s\n", key.c_str(), value.c_str());
  225. }
  226. e->response(Embedis::OK);
  227. });
  228. #if DEBUG_SUPPORT
  229. Embedis::command( F("CRASH"), [](Embedis* e) {
  230. debugDumpCrashInfo();
  231. e->response(Embedis::OK);
  232. });
  233. #endif
  234. Embedis::command( F("DUMP.RAW"), [](Embedis* e) {
  235. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  236. if (i % 16 == 0) e->stream->printf("\n[%04X] ", i);
  237. e->stream->printf("%02X ", EEPROM.read(i));
  238. }
  239. e->stream->printf("\n");
  240. e->response(Embedis::OK);
  241. });
  242. DEBUG_MSG_P(PSTR("[SETTINGS] EEPROM size: %d bytes\n"), SPI_FLASH_SEC_SIZE);
  243. DEBUG_MSG_P(PSTR("[SETTINGS] Settings size: %d bytes\n"), settingsSize());
  244. }
  245. void settingsDump() {
  246. unsigned int size = settingsKeyCount();
  247. for (unsigned int i=0; i<size; i++) {
  248. String key = settingsKeyName(i);
  249. String value = getSetting(key);
  250. DEBUG_MSG_P(PSTR("%s => %s\n"), key.c_str(), value.c_str());
  251. }
  252. }
  253. void settingsLoop() {
  254. if (_settings_save) {
  255. //DEBUG_MSG_P(PSTR("[SETTINGS] Saving\n"));
  256. EEPROM.commit();
  257. _settings_save = false;
  258. }
  259. #if TERMINAL_SUPPORT
  260. embedis.process();
  261. #endif
  262. }
  263. void moveSetting(const char * from, const char * to) {
  264. String value = getSetting(from);
  265. if (value.length() > 0) setSetting(to, value);
  266. delSetting(from);
  267. }
  268. template<typename T> String getSetting(const String& key, T defaultValue) {
  269. String value;
  270. if (!Embedis::get(key, value)) value = String(defaultValue);
  271. return value;
  272. }
  273. template<typename T> String getSetting(const String& key, unsigned int index, T defaultValue) {
  274. return getSetting(key + String(index), defaultValue);
  275. }
  276. String getSetting(const String& key) {
  277. return getSetting(key, "");
  278. }
  279. template<typename T> bool setSetting(const String& key, T value) {
  280. return Embedis::set(key, String(value));
  281. }
  282. template<typename T> bool setSetting(const String& key, unsigned int index, T value) {
  283. return setSetting(key + String(index), value);
  284. }
  285. bool delSetting(const String& key) {
  286. return Embedis::del(key);
  287. }
  288. bool delSetting(const String& key, unsigned int index) {
  289. return delSetting(key + String(index));
  290. }
  291. bool hasSetting(const String& key) {
  292. return getSetting(key).length() != 0;
  293. }
  294. bool hasSetting(const String& key, unsigned int index) {
  295. return getSetting(key, index, "").length() != 0;
  296. }
  297. void saveSettings() {
  298. #if not SETTINGS_AUTOSAVE
  299. _settings_save = true;
  300. #endif
  301. //settingsDump();
  302. }