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.

396 lines
11 KiB

7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 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 <EEPROM.h>
  6. #include "spi_flash.h"
  7. #include "libs/EmbedisWrap.h"
  8. #include <StreamString.h>
  9. #if TELNET_SUPPORT
  10. #include "libs/StreamInjector.h"
  11. #ifdef DEBUG_PORT
  12. StreamInjector _serial = StreamInjector(DEBUG_PORT);
  13. #else
  14. StreamInjector _serial = StreamInjector(Serial);
  15. #endif
  16. EmbedisWrap embedis(_serial);
  17. #else
  18. #ifdef DEBUG_PORT
  19. EmbedisWrap embedis(DEBUG_PORT);
  20. #else
  21. EmbedisWrap 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. len = EEPROM.read(pos);
  52. pos = pos - len - 2;
  53. count ++;
  54. }
  55. return count;
  56. }
  57. String settingsKeyName(unsigned int index) {
  58. String s;
  59. unsigned count = 0;
  60. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  61. while (size_t len = EEPROM.read(pos)) {
  62. pos = pos - len - 2;
  63. if (count == index) {
  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. count++;
  71. len = EEPROM.read(pos);
  72. pos = pos - len - 2;
  73. }
  74. return s;
  75. }
  76. void settingsFactoryReset() {
  77. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  78. EEPROM.write(i, 0xFF);
  79. }
  80. EEPROM.commit();
  81. }
  82. void settingsHelp() {
  83. unsigned char len = embedis.getCommandsCount();
  84. DEBUG_MSG_P(PSTR("\nAvailable commands:\n\n"));
  85. for (unsigned char i=0; i<len; i++) {
  86. DEBUG_MSG_P(PSTR("* %s\n"), embedis.getCommandName(i).c_str());
  87. if (embedis.getCommandName(i).equals("WRITE")) {
  88. DEBUG_MSG_P(PSTR("\n"));
  89. }
  90. }
  91. DEBUG_MSG_P(PSTR("\n"));
  92. }
  93. void settingsSetup() {
  94. EEPROM.begin(SPI_FLASH_SEC_SIZE);
  95. #if TELNET_SUPPORT
  96. _serial.callback([](uint8_t ch) {
  97. telnetWrite(ch);
  98. });
  99. #endif
  100. Embedis::dictionary( F("EEPROM"),
  101. SPI_FLASH_SEC_SIZE,
  102. [](size_t pos) -> char { return EEPROM.read(pos); },
  103. [](size_t pos, char value) { EEPROM.write(pos, value); },
  104. #if SETTINGS_AUTOSAVE
  105. []() { _settings_save = true; }
  106. #else
  107. []() {}
  108. #endif
  109. );
  110. Embedis::hardware( F("WIFI"), [](Embedis* e) {
  111. StreamString s;
  112. WiFi.printDiag(s);
  113. DEBUG_MSG(s.c_str());
  114. }, 0);
  115. // -------------------------------------------------------------------------
  116. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  117. Embedis::command( F("BRIGHTNESS"), [](Embedis* e) {
  118. if (e->argc > 1) {
  119. lightBrightness(String(e->argv[1]).toInt());
  120. lightUpdate(true, true);
  121. }
  122. DEBUG_MSG_P(PSTR("Brightness: %d\n"), lightBrightness());
  123. DEBUG_MSG_P(PSTR("+OK\n"));
  124. });
  125. #endif
  126. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  127. Embedis::command( F("CHANNEL"), [](Embedis* e) {
  128. if (e->argc < 2) {
  129. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  130. }
  131. int id = String(e->argv[1]).toInt();
  132. if (e->argc > 2) {
  133. int value = String(e->argv[2]).toInt();
  134. lightChannel(id, value);
  135. lightUpdate(true, true);
  136. }
  137. DEBUG_MSG_P(PSTR("Channel #%d: %d\n"), id, lightChannel(id));
  138. DEBUG_MSG_P(PSTR("+OK\n"));
  139. });
  140. #endif
  141. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  142. Embedis::command( F("COLOR"), [](Embedis* e) {
  143. if (e->argc > 1) {
  144. String color = String(e->argv[1]);
  145. lightColor(color.c_str());
  146. lightUpdate(true, true);
  147. }
  148. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  149. DEBUG_MSG_P(PSTR("+OK\n"));
  150. });
  151. #endif
  152. #if DEBUG_SUPPORT
  153. Embedis::command( F("CRASH"), [](Embedis* e) {
  154. debugDumpCrashInfo();
  155. DEBUG_MSG_P(PSTR("+OK\n"));
  156. });
  157. #endif
  158. Embedis::command( F("DUMP"), [](Embedis* e) {
  159. unsigned int size = settingsKeyCount();
  160. for (unsigned int i=0; i<size; i++) {
  161. String key = settingsKeyName(i);
  162. String value = getSetting(key);
  163. DEBUG_MSG_P(PSTR("+%s => %s\n"), key.c_str(), value.c_str());
  164. }
  165. DEBUG_MSG_P(PSTR("+OK\n"));
  166. });
  167. Embedis::command( F("DUMP.RAW"), [](Embedis* e) {
  168. bool ascii = false;
  169. if (e->argc == 2) ascii = String(e->argv[1]).toInt() == 1;
  170. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  171. if (i % 16 == 0) DEBUG_MSG_P(PSTR("\n[%04X] "), i);
  172. byte c = EEPROM.read(i);
  173. if (ascii && 32 <= c && c <= 126) {
  174. DEBUG_MSG_P(PSTR(" %c "), c);
  175. } else {
  176. DEBUG_MSG_P(PSTR("%02X "), c);
  177. }
  178. }
  179. DEBUG_MSG_P(PSTR("\n+OK\n"));
  180. });
  181. Embedis::command( F("EEPROM"), [](Embedis* e) {
  182. unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize();
  183. DEBUG_MSG_P(PSTR("Number of keys: %d\n"), settingsKeyCount());
  184. DEBUG_MSG_P(PSTR("Free EEPROM: %d bytes (%d%%)\n"), freeEEPROM, 100 * freeEEPROM / SPI_FLASH_SEC_SIZE);
  185. DEBUG_MSG_P(PSTR("+OK\n"));
  186. });
  187. Embedis::command( F("ERASE.CONFIG"), [](Embedis* e) {
  188. DEBUG_MSG_P(PSTR("+OK\n"));
  189. resetReason(CUSTOM_RESET_TERMINAL);
  190. ESP.eraseConfig();
  191. *((int*) 0) = 0; // see https://github.com/esp8266/Arduino/issues/1494
  192. });
  193. Embedis::command( F("FACTORY.RESET"), [](Embedis* e) {
  194. settingsFactoryReset();
  195. DEBUG_MSG_P(PSTR("+OK\n"));
  196. });
  197. Embedis::command( F("HEAP"), [](Embedis* e) {
  198. DEBUG_MSG_P(PSTR("Free HEAP: %d bytes\n"), getFreeHeap());
  199. DEBUG_MSG_P(PSTR("+OK\n"));
  200. });
  201. Embedis::command( F("HELP"), [](Embedis* e) {
  202. settingsHelp();
  203. DEBUG_MSG_P(PSTR("+OK\n"));
  204. });
  205. Embedis::command( F("INFO"), [](Embedis* e) {
  206. welcome();
  207. wifiStatus();
  208. DEBUG_MSG_P(PSTR("+OK\n"));
  209. });
  210. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  211. Embedis::command( F("KELVIN"), [](Embedis* e) {
  212. if (e->argc > 1) {
  213. String color = String("K") + String(e->argv[1]);
  214. lightColor(color.c_str());
  215. lightUpdate(true, true);
  216. }
  217. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  218. DEBUG_MSG_P(PSTR("+OK\n"));
  219. });
  220. #endif
  221. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  222. Embedis::command( F("MIRED"), [](Embedis* e) {
  223. if (e->argc > 1) {
  224. String color = String("M") + String(e->argv[1]);
  225. lightColor(color.c_str());
  226. lightUpdate(true, true);
  227. }
  228. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  229. DEBUG_MSG_P(PSTR("+OK\n"));
  230. });
  231. #endif
  232. #if NOFUSS_SUPPORT
  233. Embedis::command( F("NOFUSS"), [](Embedis* e) {
  234. DEBUG_MSG_P(PSTR("+OK\n"));
  235. nofussRun();
  236. });
  237. #endif
  238. Embedis::command( F("RELAY"), [](Embedis* e) {
  239. if (e->argc < 2) {
  240. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  241. }
  242. int id = String(e->argv[1]).toInt();
  243. if (e->argc > 2) {
  244. int value = String(e->argv[2]).toInt();
  245. if (value == 2) {
  246. relayToggle(id);
  247. } else {
  248. relayStatus(id, value == 1);
  249. }
  250. }
  251. DEBUG_MSG_P(PSTR("Status: %s\n"), relayStatus(id) ? "true" : "false");
  252. DEBUG_MSG_P(PSTR("+OK\n"));
  253. });
  254. Embedis::command( F("RESET"), [](Embedis* e) {
  255. DEBUG_MSG_P(PSTR("+OK\n"));
  256. deferredReset(100, CUSTOM_RESET_TERMINAL);
  257. });
  258. #if MQTT_SUPPORT
  259. Embedis::command( F("RESET.MQTT"), [](Embedis* e) {
  260. mqttConfigure();
  261. mqttDisconnect();
  262. DEBUG_MSG_P(PSTR("+OK\n"));
  263. });
  264. #endif
  265. Embedis::command( F("RESET.WIFI"), [](Embedis* e) {
  266. wifiConfigure();
  267. wifiDisconnect();
  268. DEBUG_MSG_P(PSTR("+OK\n"));
  269. });
  270. Embedis::command( F("UPTIME"), [](Embedis* e) {
  271. DEBUG_MSG_P(PSTR("Uptime: %d seconds\n"), getUptime());
  272. DEBUG_MSG_P(PSTR("+OK\n"));
  273. });
  274. DEBUG_MSG_P(PSTR("[SETTINGS] EEPROM size: %d bytes\n"), SPI_FLASH_SEC_SIZE);
  275. DEBUG_MSG_P(PSTR("[SETTINGS] Settings size: %d bytes\n"), settingsSize());
  276. }
  277. void settingsDump() {
  278. unsigned int size = settingsKeyCount();
  279. for (unsigned int i=0; i<size; i++) {
  280. String key = settingsKeyName(i);
  281. String value = getSetting(key);
  282. DEBUG_MSG_P(PSTR("%s => %s\n"), key.c_str(), value.c_str());
  283. }
  284. }
  285. void settingsLoop() {
  286. if (_settings_save) {
  287. //DEBUG_MSG_P(PSTR("[SETTINGS] Saving\n"));
  288. EEPROM.commit();
  289. _settings_save = false;
  290. }
  291. #if TERMINAL_SUPPORT
  292. embedis.process();
  293. #endif
  294. }
  295. void saveSettings() {
  296. #if not SETTINGS_AUTOSAVE
  297. _settings_save = true;
  298. #endif
  299. //settingsDump();
  300. }
  301. // -----------------------------------------------------------------------------
  302. void moveSetting(const char * from, const char * to) {
  303. String value = getSetting(from);
  304. if (value.length() > 0) setSetting(to, value);
  305. delSetting(from);
  306. }
  307. template<typename T> String getSetting(const String& key, T defaultValue) {
  308. String value;
  309. if (!Embedis::get(key, value)) value = String(defaultValue);
  310. return value;
  311. }
  312. template<typename T> String getSetting(const String& key, unsigned int index, T defaultValue) {
  313. return getSetting(key + String(index), defaultValue);
  314. }
  315. String getSetting(const String& key) {
  316. return getSetting(key, "");
  317. }
  318. template<typename T> bool setSetting(const String& key, T value) {
  319. return Embedis::set(key, String(value));
  320. }
  321. template<typename T> bool setSetting(const String& key, unsigned int index, T value) {
  322. return setSetting(key + String(index), value);
  323. }
  324. bool delSetting(const String& key) {
  325. return Embedis::del(key);
  326. }
  327. bool delSetting(const String& key, unsigned int index) {
  328. return delSetting(key + String(index));
  329. }
  330. bool hasSetting(const String& key) {
  331. return getSetting(key).length() != 0;
  332. }
  333. bool hasSetting(const String& key, unsigned int index) {
  334. return getSetting(key, index, "").length() != 0;
  335. }