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.

310 lines
8.6 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. 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 AUTO_SAVE
  71. []() { EEPROM.commit(); }
  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("RECONNECT"), [](Embedis* e) {
  82. wifiConfigure();
  83. wifiDisconnect();
  84. e->response(Embedis::OK);
  85. });
  86. Embedis::command( F("RESTART"), [](Embedis* e) {
  87. e->response(Embedis::OK);
  88. customReset(CUSTOM_RESET_TERMINAL);
  89. ESP.restart();
  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. embedis.process();
  208. }
  209. void moveSetting(const char * from, const char * to) {
  210. String value = getSetting(from);
  211. if (value.length() > 0) setSetting(to, value);
  212. delSetting(from);
  213. }
  214. template<typename T> String getSetting(const String& key, T defaultValue) {
  215. String value;
  216. if (!Embedis::get(key, value)) value = String(defaultValue);
  217. return value;
  218. }
  219. template<typename T> String getSetting(const String& key, unsigned int index, T defaultValue) {
  220. return getSetting(key + String(index), defaultValue);
  221. }
  222. String getSetting(const String& key) {
  223. return getSetting(key, "");
  224. }
  225. bool setBoolSetting(const String& key, bool value, bool defaultValue) {
  226. if (value == defaultValue) {
  227. return delSetting(key);
  228. } else {
  229. return setSetting(key, value ? 1 : 0);
  230. }
  231. }
  232. template<typename T> bool setSetting(const String& key, T value) {
  233. return Embedis::set(key, String(value));
  234. }
  235. template<typename T> bool setSetting(const String& key, unsigned int index, T value) {
  236. return setSetting(key + String(index), value);
  237. }
  238. bool delSetting(const String& key) {
  239. return Embedis::del(key);
  240. }
  241. bool delSetting(const String& key, unsigned int index) {
  242. return delSetting(key + String(index));
  243. }
  244. bool hasSetting(const String& key) {
  245. return getSetting(key).length() != 0;
  246. }
  247. bool hasSetting(const String& key, unsigned int index) {
  248. return getSetting(key, index, "").length() != 0;
  249. }
  250. void saveSettings() {
  251. DEBUG_MSG_P(PSTR("[SETTINGS] Saving\n"));
  252. #if not AUTO_SAVE
  253. EEPROM.commit();
  254. #endif
  255. //settingsDump();
  256. }