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.

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