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.

362 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. customReset(CUSTOM_RESET_TERMINAL);
  123. ESP.restart();
  124. });
  125. Embedis::command( F("ERASE.CONFIG"), [](Embedis* e) {
  126. DEBUG_MSG_P(PSTR("+OK\n"));
  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. DEBUG_MSG_P(PSTR("+OK\n"));
  134. nofussRun();
  135. });
  136. #endif
  137. Embedis::command( F("FACTORY.RESET"), [](Embedis* e) {
  138. settingsFactoryReset();
  139. DEBUG_MSG_P(PSTR("+OK\n"));
  140. });
  141. Embedis::command( F("HEAP"), [](Embedis* e) {
  142. DEBUG_MSG_P(PSTR("Free HEAP: %d bytes\n"), ESP.getFreeHeap());
  143. DEBUG_MSG_P(PSTR("+OK\n"));
  144. });
  145. Embedis::command( F("RELAY"), [](Embedis* e) {
  146. if (e->argc < 2) {
  147. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  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. DEBUG_MSG_P(PSTR("Status: %s\n"), relayStatus(id) ? "true" : "false");
  159. DEBUG_MSG_P(PSTR("+OK\n"));
  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. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  170. DEBUG_MSG_P(PSTR("+OK\n"));
  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. DEBUG_MSG_P(PSTR("Brightness: %d\n"), lightBrightness());
  178. DEBUG_MSG_P(PSTR("+OK\n"));
  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. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  187. DEBUG_MSG_P(PSTR("+OK\n"));
  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. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  196. DEBUG_MSG_P(PSTR("+OK\n"));
  197. });
  198. }
  199. Embedis::command( F("CHANNEL"), [](Embedis* e) {
  200. if (e->argc < 2) {
  201. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  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. DEBUG_MSG_P(PSTR("Channel #%d: %d\n"), id, lightChannel(id));
  210. DEBUG_MSG_P(PSTR("+OK\n"));
  211. });
  212. #endif
  213. Embedis::command( F("EEPROM"), [](Embedis* e) {
  214. unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize();
  215. DEBUG_MSG_P(PSTR("Number of keys: %d\n"), settingsKeyCount());
  216. DEBUG_MSG_P(PSTR("Free EEPROM: %d bytes (%d%%)\n"), freeEEPROM, 100 * freeEEPROM / SPI_FLASH_SEC_SIZE);
  217. DEBUG_MSG_P(PSTR("+OK\n"));
  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. DEBUG_MSG_P(PSTR("+%s => %s\n"), key.c_str(), value.c_str());
  225. }
  226. DEBUG_MSG_P(PSTR("+OK\n"));
  227. });
  228. #if DEBUG_SUPPORT
  229. Embedis::command( F("CRASH"), [](Embedis* e) {
  230. debugDumpCrashInfo();
  231. DEBUG_MSG_P(PSTR("+OK\n"));
  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) DEBUG_MSG_P(PSTR("\n[%04X] "), i);
  237. DEBUG_MSG_P(PSTR("%02X "), EEPROM.read(i));
  238. }
  239. DEBUG_MSG_P(PSTR("\n+OK\n"));
  240. });
  241. DEBUG_MSG_P(PSTR("[SETTINGS] EEPROM size: %d bytes\n"), SPI_FLASH_SEC_SIZE);
  242. DEBUG_MSG_P(PSTR("[SETTINGS] Settings size: %d bytes\n"), settingsSize());
  243. }
  244. void settingsDump() {
  245. unsigned int size = settingsKeyCount();
  246. for (unsigned int i=0; i<size; i++) {
  247. String key = settingsKeyName(i);
  248. String value = getSetting(key);
  249. DEBUG_MSG_P(PSTR("%s => %s\n"), key.c_str(), value.c_str());
  250. }
  251. }
  252. void settingsLoop() {
  253. if (_settings_save) {
  254. //DEBUG_MSG_P(PSTR("[SETTINGS] Saving\n"));
  255. EEPROM.commit();
  256. _settings_save = false;
  257. }
  258. #if TERMINAL_SUPPORT
  259. embedis.process();
  260. #endif
  261. }
  262. void moveSetting(const char * from, const char * to) {
  263. String value = getSetting(from);
  264. if (value.length() > 0) setSetting(to, value);
  265. delSetting(from);
  266. }
  267. template<typename T> String getSetting(const String& key, T defaultValue) {
  268. String value;
  269. if (!Embedis::get(key, value)) value = String(defaultValue);
  270. return value;
  271. }
  272. template<typename T> String getSetting(const String& key, unsigned int index, T defaultValue) {
  273. return getSetting(key + String(index), defaultValue);
  274. }
  275. String getSetting(const String& key) {
  276. return getSetting(key, "");
  277. }
  278. template<typename T> bool setSetting(const String& key, T value) {
  279. return Embedis::set(key, String(value));
  280. }
  281. template<typename T> bool setSetting(const String& key, unsigned int index, T value) {
  282. return setSetting(key + String(index), value);
  283. }
  284. bool delSetting(const String& key) {
  285. return Embedis::del(key);
  286. }
  287. bool delSetting(const String& key, unsigned int index) {
  288. return delSetting(key + String(index));
  289. }
  290. bool hasSetting(const String& key) {
  291. return getSetting(key).length() != 0;
  292. }
  293. bool hasSetting(const String& key, unsigned int index) {
  294. return getSetting(key, index, "").length() != 0;
  295. }
  296. void saveSettings() {
  297. #if not SETTINGS_AUTOSAVE
  298. _settings_save = true;
  299. #endif
  300. //settingsDump();
  301. }