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.

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