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.

290 lines
8.0 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. Embedis::command( F("COLOR"), [](Embedis* e) {
  116. if (e->argc > 1) {
  117. String color = String(e->argv[1]);
  118. lightColor(color.c_str());
  119. lightUpdate(true, true);
  120. }
  121. e->stream->printf("Color: %s\n", lightColor().c_str());
  122. e->response(Embedis::OK);
  123. });
  124. Embedis::command( F("MIRED"), [](Embedis* e) {
  125. if (e->argc > 1) {
  126. String color = String("M") + String(e->argv[1]);
  127. lightColor(color.c_str());
  128. lightUpdate(true, true);
  129. }
  130. e->stream->printf("Color: %s\n", lightColor().c_str());
  131. e->response(Embedis::OK);
  132. });
  133. Embedis::command( F("KELVIN"), [](Embedis* e) {
  134. if (e->argc > 1) {
  135. String color = String("K") + 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("BRIGHTNESS"), [](Embedis* e) {
  143. if (e->argc > 1) {
  144. lightBrightness(String(e->argv[1]).toInt());
  145. lightUpdate(true, true);
  146. }
  147. e->stream->printf("Brightness: %d\n", lightBrightness());
  148. e->response(Embedis::OK);
  149. });
  150. Embedis::command( F("CHANNEL"), [](Embedis* e) {
  151. if (e->argc < 2) {
  152. return e->response(Embedis::ARGS_ERROR);
  153. }
  154. int id = String(e->argv[1]).toInt();
  155. if (e->argc > 2) {
  156. int value = String(e->argv[2]).toInt();
  157. lightChannel(id, value);
  158. lightUpdate(true, true);
  159. }
  160. e->stream->printf("Channel #%d: %d\n", id, lightChannel(id));
  161. e->response(Embedis::OK);
  162. });
  163. #endif
  164. Embedis::command( F("EEPROM"), [](Embedis* e) {
  165. unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize();
  166. e->stream->printf("Number of keys: %d\n", settingsKeyCount());
  167. e->stream->printf("Free EEPROM: %d bytes (%d%%)\n", freeEEPROM, 100 * freeEEPROM / SPI_FLASH_SEC_SIZE);
  168. e->response(Embedis::OK);
  169. });
  170. Embedis::command( F("DUMP"), [](Embedis* e) {
  171. unsigned int size = settingsKeyCount();
  172. for (unsigned int i=0; i<size; i++) {
  173. String key = settingsKeyName(i);
  174. String value = getSetting(key);
  175. e->stream->printf("+%s => %s\n", key.c_str(), value.c_str());
  176. }
  177. e->response(Embedis::OK);
  178. });
  179. Embedis::command( F("DUMP.RAW"), [](Embedis* e) {
  180. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  181. if (i % 16 == 0) e->stream->printf("\n[%04X] ", i);
  182. e->stream->printf("%02X ", EEPROM.read(i));
  183. }
  184. e->stream->printf("\n");
  185. e->response(Embedis::OK);
  186. });
  187. DEBUG_MSG_P(PSTR("[SETTINGS] EEPROM size: %d bytes\n"), SPI_FLASH_SEC_SIZE);
  188. DEBUG_MSG_P(PSTR("[SETTINGS] Settings size: %d bytes\n"), settingsSize());
  189. }
  190. void settingsDump() {
  191. unsigned int size = settingsKeyCount();
  192. for (unsigned int i=0; i<size; i++) {
  193. String key = settingsKeyName(i);
  194. String value = getSetting(key);
  195. DEBUG_MSG_P(PSTR("%s => %s\n"), key.c_str(), value.c_str());
  196. }
  197. }
  198. void settingsLoop() {
  199. embedis.process();
  200. }
  201. void moveSetting(const char * from, const char * to) {
  202. String value = getSetting(from);
  203. if (value.length() > 0) setSetting(to, value);
  204. delSetting(from);
  205. }
  206. template<typename T> String getSetting(const String& key, T defaultValue) {
  207. String value;
  208. if (!Embedis::get(key, value)) value = String(defaultValue);
  209. return value;
  210. }
  211. template<typename T> String getSetting(const String& key, unsigned int index, T defaultValue) {
  212. return getSetting(key + String(index), defaultValue);
  213. }
  214. String getSetting(const String& key) {
  215. return getSetting(key, "");
  216. }
  217. template<typename T> bool setSetting(const String& key, T value) {
  218. return Embedis::set(key, String(value));
  219. }
  220. template<typename T> bool setSetting(const String& key, unsigned int index, T value) {
  221. return setSetting(key + String(index), value);
  222. }
  223. bool delSetting(const String& key) {
  224. return Embedis::del(key);
  225. }
  226. bool delSetting(const String& key, unsigned int index) {
  227. return delSetting(key + String(index));
  228. }
  229. bool hasSetting(const String& key) {
  230. return getSetting(key).length() != 0;
  231. }
  232. bool hasSetting(const String& key, unsigned int index) {
  233. return getSetting(key, index, "").length() != 0;
  234. }
  235. void saveSettings() {
  236. DEBUG_MSG_P(PSTR("[SETTINGS] Saving\n"));
  237. #if not AUTO_SAVE
  238. EEPROM.commit();
  239. #endif
  240. }