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.

375 lines
10 KiB

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