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.

316 lines
8.7 KiB

8 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. #ifdef DEBUG_PORT
  10. Embedis embedis(DEBUG_PORT);
  11. #else
  12. Embedis embedis(Serial);
  13. #endif
  14. bool _settings_save = false;
  15. // -----------------------------------------------------------------------------
  16. // Settings
  17. // -----------------------------------------------------------------------------
  18. size_t settingsMaxSize() {
  19. size_t size = EEPROM_SIZE;
  20. if (size > SPI_FLASH_SEC_SIZE) size = SPI_FLASH_SEC_SIZE;
  21. size = (size + 3) & (~3);
  22. return size;
  23. }
  24. unsigned long settingsSize() {
  25. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  26. while (size_t len = EEPROM.read(pos)) {
  27. pos = pos - len - 2;
  28. }
  29. return SPI_FLASH_SEC_SIZE - pos;
  30. }
  31. unsigned int settingsKeyCount() {
  32. unsigned count = 0;
  33. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  34. while (size_t len = EEPROM.read(pos)) {
  35. pos = pos - len - 2;
  36. count ++;
  37. }
  38. return count / 2;
  39. }
  40. String settingsKeyName(unsigned int index) {
  41. String s;
  42. unsigned count = 0;
  43. unsigned stop = index * 2 + 1;
  44. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  45. while (size_t len = EEPROM.read(pos)) {
  46. pos = pos - len - 2;
  47. count++;
  48. if (count == stop) {
  49. s.reserve(len);
  50. for (unsigned char i = 0 ; i < len; i++) {
  51. s += (char) EEPROM.read(pos + i + 1);
  52. }
  53. break;
  54. }
  55. }
  56. return s;
  57. }
  58. void settingsFactoryReset() {
  59. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  60. EEPROM.write(i, 0xFF);
  61. }
  62. EEPROM.commit();
  63. }
  64. void settingsSetup() {
  65. EEPROM.begin(SPI_FLASH_SEC_SIZE);
  66. Embedis::dictionary( F("EEPROM"),
  67. SPI_FLASH_SEC_SIZE,
  68. [](size_t pos) -> char { return EEPROM.read(pos); },
  69. [](size_t pos, char value) { EEPROM.write(pos, value); },
  70. #if SETTINGS_AUTOSAVE
  71. []() { _settings_save = true; }
  72. #else
  73. []() {}
  74. #endif
  75. );
  76. Embedis::hardware( F("WIFI"), [](Embedis* e) {
  77. StreamString s;
  78. WiFi.printDiag(s);
  79. e->response(s);
  80. }, 0);
  81. Embedis::command( F("RESET.WIFI"), [](Embedis* e) {
  82. wifiConfigure();
  83. wifiDisconnect();
  84. e->response(Embedis::OK);
  85. });
  86. Embedis::command( F("RESET.MQTT"), [](Embedis* e) {
  87. mqttConfigure();
  88. mqttDisconnect();
  89. e->response(Embedis::OK);
  90. });
  91. Embedis::command( F("RESET"), [](Embedis* e) {
  92. e->response(Embedis::OK);
  93. customReset(CUSTOM_RESET_TERMINAL);
  94. ESP.restart();
  95. });
  96. Embedis::command( F("FACTORY.RESET"), [](Embedis* e) {
  97. settingsFactoryReset();
  98. e->response(Embedis::OK);
  99. });
  100. Embedis::command( F("HEAP"), [](Embedis* e) {
  101. e->stream->printf("Free HEAP: %d bytes\n", ESP.getFreeHeap());
  102. e->response(Embedis::OK);
  103. });
  104. Embedis::command( F("RELAY"), [](Embedis* e) {
  105. if (e->argc < 2) {
  106. return e->response(Embedis::ARGS_ERROR);
  107. }
  108. int id = String(e->argv[1]).toInt();
  109. if (e->argc > 2) {
  110. int value = String(e->argv[2]).toInt();
  111. if (value == 2) {
  112. relayToggle(id);
  113. } else {
  114. relayStatus(id, value == 1);
  115. }
  116. }
  117. e->stream->printf("Status: %s\n", relayStatus(id) ? "true" : "false");
  118. e->response(Embedis::OK);
  119. });
  120. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  121. if (lightHasColor()) {
  122. Embedis::command( F("COLOR"), [](Embedis* e) {
  123. if (e->argc > 1) {
  124. String color = String(e->argv[1]);
  125. lightColor(color.c_str());
  126. lightUpdate(true, true);
  127. }
  128. e->stream->printf("Color: %s\n", lightColor().c_str());
  129. e->response(Embedis::OK);
  130. });
  131. Embedis::command( F("BRIGHTNESS"), [](Embedis* e) {
  132. if (e->argc > 1) {
  133. lightBrightness(String(e->argv[1]).toInt());
  134. lightUpdate(true, true);
  135. }
  136. e->stream->printf("Brightness: %d\n", lightBrightness());
  137. e->response(Embedis::OK);
  138. });
  139. Embedis::command( F("MIRED"), [](Embedis* e) {
  140. if (e->argc > 1) {
  141. String color = String("M") + String(e->argv[1]);
  142. lightColor(color.c_str());
  143. lightUpdate(true, true);
  144. }
  145. e->stream->printf("Color: %s\n", lightColor().c_str());
  146. e->response(Embedis::OK);
  147. });
  148. Embedis::command( F("KELVIN"), [](Embedis* e) {
  149. if (e->argc > 1) {
  150. String color = String("K") + String(e->argv[1]);
  151. lightColor(color.c_str());
  152. lightUpdate(true, true);
  153. }
  154. e->stream->printf("Color: %s\n", lightColor().c_str());
  155. e->response(Embedis::OK);
  156. });
  157. }
  158. Embedis::command( F("CHANNEL"), [](Embedis* e) {
  159. if (e->argc < 2) {
  160. return e->response(Embedis::ARGS_ERROR);
  161. }
  162. int id = String(e->argv[1]).toInt();
  163. if (e->argc > 2) {
  164. int value = String(e->argv[2]).toInt();
  165. lightChannel(id, value);
  166. lightUpdate(true, true);
  167. }
  168. e->stream->printf("Channel #%d: %d\n", id, lightChannel(id));
  169. e->response(Embedis::OK);
  170. });
  171. #endif
  172. Embedis::command( F("EEPROM"), [](Embedis* e) {
  173. unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize();
  174. e->stream->printf("Number of keys: %d\n", settingsKeyCount());
  175. e->stream->printf("Free EEPROM: %d bytes (%d%%)\n", freeEEPROM, 100 * freeEEPROM / SPI_FLASH_SEC_SIZE);
  176. e->response(Embedis::OK);
  177. });
  178. Embedis::command( F("DUMP"), [](Embedis* e) {
  179. unsigned int size = settingsKeyCount();
  180. for (unsigned int i=0; i<size; i++) {
  181. String key = settingsKeyName(i);
  182. String value = getSetting(key);
  183. e->stream->printf("+%s => %s\n", key.c_str(), value.c_str());
  184. }
  185. e->response(Embedis::OK);
  186. });
  187. Embedis::command( F("DUMP.RAW"), [](Embedis* e) {
  188. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  189. if (i % 16 == 0) e->stream->printf("\n[%04X] ", i);
  190. e->stream->printf("%02X ", EEPROM.read(i));
  191. }
  192. e->stream->printf("\n");
  193. e->response(Embedis::OK);
  194. });
  195. DEBUG_MSG_P(PSTR("[SETTINGS] EEPROM size: %d bytes\n"), SPI_FLASH_SEC_SIZE);
  196. DEBUG_MSG_P(PSTR("[SETTINGS] Settings size: %d bytes\n"), settingsSize());
  197. }
  198. void settingsDump() {
  199. unsigned int size = settingsKeyCount();
  200. for (unsigned int i=0; i<size; i++) {
  201. String key = settingsKeyName(i);
  202. String value = getSetting(key);
  203. DEBUG_MSG_P(PSTR("%s => %s\n"), key.c_str(), value.c_str());
  204. }
  205. }
  206. void settingsLoop() {
  207. if (_settings_save) {
  208. DEBUG_MSG_P(PSTR("[SETTINGS] Saving\n"));
  209. EEPROM.commit();
  210. _settings_save = false;
  211. }
  212. #if TERMINAL_SUPPORT
  213. embedis.process();
  214. #endif
  215. }
  216. void moveSetting(const char * from, const char * to) {
  217. String value = getSetting(from);
  218. if (value.length() > 0) setSetting(to, value);
  219. delSetting(from);
  220. }
  221. template<typename T> String getSetting(const String& key, T defaultValue) {
  222. String value;
  223. if (!Embedis::get(key, value)) value = String(defaultValue);
  224. return value;
  225. }
  226. template<typename T> String getSetting(const String& key, unsigned int index, T defaultValue) {
  227. return getSetting(key + String(index), defaultValue);
  228. }
  229. String getSetting(const String& key) {
  230. return getSetting(key, "");
  231. }
  232. bool setBoolSetting(const String& key, bool value, bool defaultValue) {
  233. if (value == defaultValue) {
  234. return delSetting(key);
  235. } else {
  236. return setSetting(key, value ? 1 : 0);
  237. }
  238. }
  239. template<typename T> bool setSetting(const String& key, T value) {
  240. return Embedis::set(key, String(value));
  241. }
  242. template<typename T> bool setSetting(const String& key, unsigned int index, T value) {
  243. return setSetting(key + String(index), value);
  244. }
  245. bool delSetting(const String& key) {
  246. return Embedis::del(key);
  247. }
  248. bool delSetting(const String& key, unsigned int index) {
  249. return delSetting(key + String(index));
  250. }
  251. bool hasSetting(const String& key) {
  252. return getSetting(key).length() != 0;
  253. }
  254. bool hasSetting(const String& key, unsigned int index) {
  255. return getSetting(key, index, "").length() != 0;
  256. }
  257. void saveSettings() {
  258. #if not SETTINGS_AUTOSAVE
  259. _settings_save = true;
  260. #endif
  261. //settingsDump();
  262. }