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.

261 lines
6.0 KiB

6 years ago
6 years ago
6 years ago
6 years ago
8 years ago
6 years ago
8 years ago
7 years ago
6 years ago
6 years ago
6 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. /*
  2. ESPurna
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "config/all.h"
  16. #include <vector>
  17. #include "libs/HeapStats.h"
  18. std::vector<void (*)()> _loop_callbacks;
  19. std::vector<void (*)()> _reload_callbacks;
  20. bool _reload_config = false;
  21. unsigned long _loop_delay = 0;
  22. // -----------------------------------------------------------------------------
  23. // GENERAL CALLBACKS
  24. // -----------------------------------------------------------------------------
  25. void espurnaRegisterLoop(void (*callback)()) {
  26. _loop_callbacks.push_back(callback);
  27. }
  28. void espurnaRegisterReload(void (*callback)()) {
  29. _reload_callbacks.push_back(callback);
  30. }
  31. void espurnaReload() {
  32. _reload_config = true;
  33. }
  34. void _espurnaReload() {
  35. for (unsigned char i = 0; i < _reload_callbacks.size(); i++) {
  36. (_reload_callbacks[i])();
  37. }
  38. }
  39. unsigned long espurnaLoopDelay() {
  40. return _loop_delay;
  41. }
  42. // -----------------------------------------------------------------------------
  43. // BOOTING
  44. // -----------------------------------------------------------------------------
  45. void setup() {
  46. // -------------------------------------------------------------------------
  47. // Basic modules, will always run
  48. // -------------------------------------------------------------------------
  49. // Cache initial free heap value
  50. setInitialFreeHeap();
  51. // Serial debug
  52. #if DEBUG_SUPPORT
  53. debugSetup();
  54. #endif
  55. // Init RTCMEM
  56. rtcmemSetup();
  57. // Init EEPROM
  58. eepromSetup();
  59. // Init persistance
  60. settingsSetup();
  61. // Init crash recorder
  62. #if DEBUG_SUPPORT
  63. crashSetup();
  64. #endif
  65. // Return bogus free heap value for broken devices
  66. // XXX: device is likely to trigger other bugs! tread carefuly
  67. wtfHeap(getSetting("wtfHeap", 0).toInt());
  68. // Init Serial, SPIFFS and system check
  69. systemSetup();
  70. // Init terminal features
  71. #if TERMINAL_SUPPORT
  72. terminalSetup();
  73. #endif
  74. // Hostname & board name initialization
  75. if (getSetting("hostname").length() == 0) {
  76. setDefaultHostname();
  77. }
  78. setBoardName();
  79. // Show welcome message and system configuration
  80. info();
  81. wifiSetup();
  82. otaSetup();
  83. #if TELNET_SUPPORT
  84. telnetSetup();
  85. #endif
  86. // -------------------------------------------------------------------------
  87. // Check if system is stable
  88. // -------------------------------------------------------------------------
  89. #if SYSTEM_CHECK_ENABLED
  90. if (!systemCheck()) return;
  91. #endif
  92. // -------------------------------------------------------------------------
  93. // Next modules will be only loaded if system is flagged as stable
  94. // -------------------------------------------------------------------------
  95. // Init webserver required before any module that uses API
  96. #if WEB_SUPPORT
  97. webSetup();
  98. wsSetup();
  99. #if DEBUG_WEB_SUPPORT
  100. debugWebSetup();
  101. #endif
  102. #endif
  103. #if API_SUPPORT
  104. apiSetup();
  105. #endif
  106. // lightSetup must be called before relaySetup
  107. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  108. lightSetup();
  109. #endif
  110. relaySetup();
  111. #if BUTTON_SUPPORT
  112. buttonSetup();
  113. #endif
  114. #if ENCODER_SUPPORT && (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE)
  115. encoderSetup();
  116. #endif
  117. #if LED_SUPPORT
  118. ledSetup();
  119. #endif
  120. #if MQTT_SUPPORT
  121. mqttSetup();
  122. #endif
  123. #if MDNS_SERVER_SUPPORT
  124. mdnsServerSetup();
  125. #endif
  126. #if MDNS_CLIENT_SUPPORT
  127. mdnsClientSetup();
  128. #endif
  129. #if LLMNR_SUPPORT
  130. llmnrSetup();
  131. #endif
  132. #if NETBIOS_SUPPORT
  133. netbiosSetup();
  134. #endif
  135. #if SSDP_SUPPORT
  136. ssdpSetup();
  137. #endif
  138. #if NTP_SUPPORT
  139. ntpSetup();
  140. #endif
  141. #if I2C_SUPPORT
  142. i2cSetup();
  143. #endif
  144. #if RF_SUPPORT
  145. rfbSetup();
  146. #endif
  147. #if ALEXA_SUPPORT
  148. alexaSetup();
  149. #endif
  150. #if NOFUSS_SUPPORT
  151. nofussSetup();
  152. #endif
  153. #if INFLUXDB_SUPPORT
  154. idbSetup();
  155. #endif
  156. #if THINGSPEAK_SUPPORT
  157. tspkSetup();
  158. #endif
  159. #if RFM69_SUPPORT
  160. rfm69Setup();
  161. #endif
  162. #if IR_SUPPORT
  163. irSetup();
  164. #endif
  165. #if DOMOTICZ_SUPPORT
  166. domoticzSetup();
  167. #endif
  168. #if HOMEASSISTANT_SUPPORT
  169. haSetup();
  170. #endif
  171. #if SENSOR_SUPPORT
  172. sensorSetup();
  173. #endif
  174. #if SCHEDULER_SUPPORT
  175. schSetup();
  176. #endif
  177. #if UART_MQTT_SUPPORT
  178. uartmqttSetup();
  179. #endif
  180. #ifdef FOXEL_LIGHTFOX_DUAL
  181. lightfoxSetup();
  182. #endif
  183. #if THERMOSTAT_SUPPORT
  184. thermostatSetup();
  185. #endif
  186. #if THERMOSTAT_DISPLAY_SUPPORT
  187. displaySetup();
  188. #endif
  189. // 3rd party code hook
  190. #if USE_EXTRA
  191. extraSetup();
  192. #endif
  193. // Prepare configuration for version 2.0
  194. migrate();
  195. // Set up delay() after loop callbacks are finished
  196. // Note: should be after settingsSetup()
  197. _loop_delay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  198. _loop_delay = constrain(_loop_delay, 0, 300);
  199. saveSettings();
  200. }
  201. void loop() {
  202. // Reload config before running any callbacks
  203. if (_reload_config) {
  204. _espurnaReload();
  205. _reload_config = false;
  206. }
  207. // Call registered loop callbacks
  208. for (unsigned char i = 0; i < _loop_callbacks.size(); i++) {
  209. (_loop_callbacks[i])();
  210. }
  211. // Power saving delay
  212. if (_loop_delay) delay(_loop_delay);
  213. }