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.

266 lines
6.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
6 years ago
6 years ago
7 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. #if OTA_ARDUINOOTA_SUPPORT
  83. arduinoOtaSetup();
  84. #endif
  85. #if TELNET_SUPPORT
  86. telnetSetup();
  87. #endif
  88. #if OTA_CLIENT != OTA_CLIENT_NONE
  89. otaClientSetup();
  90. #endif
  91. // -------------------------------------------------------------------------
  92. // Check if system is stable
  93. // -------------------------------------------------------------------------
  94. #if SYSTEM_CHECK_ENABLED
  95. if (!systemCheck()) return;
  96. #endif
  97. // -------------------------------------------------------------------------
  98. // Next modules will be only loaded if system is flagged as stable
  99. // -------------------------------------------------------------------------
  100. // Init webserver required before any module that uses API
  101. #if WEB_SUPPORT
  102. webSetup();
  103. wsSetup();
  104. #if DEBUG_WEB_SUPPORT
  105. debugWebSetup();
  106. #endif
  107. #endif
  108. #if API_SUPPORT
  109. apiSetup();
  110. #endif
  111. // lightSetup must be called before relaySetup
  112. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  113. lightSetup();
  114. #endif
  115. relaySetup();
  116. #if BUTTON_SUPPORT
  117. buttonSetup();
  118. #endif
  119. #if ENCODER_SUPPORT && (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE)
  120. encoderSetup();
  121. #endif
  122. #if LED_SUPPORT
  123. ledSetup();
  124. #endif
  125. #if MQTT_SUPPORT
  126. mqttSetup();
  127. #endif
  128. #if MDNS_SERVER_SUPPORT
  129. mdnsServerSetup();
  130. #endif
  131. #if MDNS_CLIENT_SUPPORT
  132. mdnsClientSetup();
  133. #endif
  134. #if LLMNR_SUPPORT
  135. llmnrSetup();
  136. #endif
  137. #if NETBIOS_SUPPORT
  138. netbiosSetup();
  139. #endif
  140. #if SSDP_SUPPORT
  141. ssdpSetup();
  142. #endif
  143. #if NTP_SUPPORT
  144. ntpSetup();
  145. #endif
  146. #if I2C_SUPPORT
  147. i2cSetup();
  148. #endif
  149. #if RF_SUPPORT
  150. rfbSetup();
  151. #endif
  152. #if ALEXA_SUPPORT
  153. alexaSetup();
  154. #endif
  155. #if NOFUSS_SUPPORT
  156. nofussSetup();
  157. #endif
  158. #if SENSOR_SUPPORT
  159. sensorSetup();
  160. #endif
  161. #if INFLUXDB_SUPPORT
  162. idbSetup();
  163. #endif
  164. #if THINGSPEAK_SUPPORT
  165. tspkSetup();
  166. #endif
  167. #if RFM69_SUPPORT
  168. rfm69Setup();
  169. #endif
  170. #if IR_SUPPORT
  171. irSetup();
  172. #endif
  173. #if DOMOTICZ_SUPPORT
  174. domoticzSetup();
  175. #endif
  176. #if HOMEASSISTANT_SUPPORT
  177. haSetup();
  178. #endif
  179. #if SCHEDULER_SUPPORT
  180. schSetup();
  181. #endif
  182. #if UART_MQTT_SUPPORT
  183. uartmqttSetup();
  184. #endif
  185. #ifdef FOXEL_LIGHTFOX_DUAL
  186. lightfoxSetup();
  187. #endif
  188. #if THERMOSTAT_SUPPORT
  189. thermostatSetup();
  190. #endif
  191. #if THERMOSTAT_DISPLAY_SUPPORT
  192. displaySetup();
  193. #endif
  194. // 3rd party code hook
  195. #if USE_EXTRA
  196. extraSetup();
  197. #endif
  198. // Prepare configuration for version 2.0
  199. migrate();
  200. // Set up delay() after loop callbacks are finished
  201. // Note: should be after settingsSetup()
  202. _loop_delay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  203. _loop_delay = constrain(_loop_delay, 0, 300);
  204. saveSettings();
  205. }
  206. void loop() {
  207. // Reload config before running any callbacks
  208. if (_reload_config) {
  209. _espurnaReload();
  210. _reload_config = false;
  211. }
  212. // Call registered loop callbacks
  213. for (unsigned char i = 0; i < _loop_callbacks.size(); i++) {
  214. (_loop_callbacks[i])();
  215. }
  216. // Power saving delay
  217. if (_loop_delay) delay(_loop_delay);
  218. }