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.

303 lines
8.4 KiB

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