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.

259 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. crashSetup();
  63. // Return bogus free heap value for broken devices
  64. // XXX: device is likely to trigger other bugs! tread carefuly
  65. wtfHeap(getSetting("wtfHeap", 0).toInt());
  66. // Init Serial, SPIFFS and system check
  67. systemSetup();
  68. // Init terminal features
  69. #if TERMINAL_SUPPORT
  70. terminalSetup();
  71. #endif
  72. // Hostname & board name initialization
  73. if (getSetting("hostname").length() == 0) {
  74. setDefaultHostname();
  75. }
  76. setBoardName();
  77. // Show welcome message and system configuration
  78. info();
  79. wifiSetup();
  80. otaSetup();
  81. #if TELNET_SUPPORT
  82. telnetSetup();
  83. #endif
  84. // -------------------------------------------------------------------------
  85. // Check if system is stable
  86. // -------------------------------------------------------------------------
  87. #if SYSTEM_CHECK_ENABLED
  88. if (!systemCheck()) return;
  89. #endif
  90. // -------------------------------------------------------------------------
  91. // Next modules will be only loaded if system is flagged as stable
  92. // -------------------------------------------------------------------------
  93. // Init webserver required before any module that uses API
  94. #if WEB_SUPPORT
  95. webSetup();
  96. wsSetup();
  97. #if DEBUG_WEB_SUPPORT
  98. debugWebSetup();
  99. #endif
  100. #endif
  101. #if API_SUPPORT
  102. apiSetup();
  103. #endif
  104. // lightSetup must be called before relaySetup
  105. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  106. lightSetup();
  107. #endif
  108. relaySetup();
  109. #if BUTTON_SUPPORT
  110. buttonSetup();
  111. #endif
  112. #if ENCODER_SUPPORT && (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE)
  113. encoderSetup();
  114. #endif
  115. #if LED_SUPPORT
  116. ledSetup();
  117. #endif
  118. #if MQTT_SUPPORT
  119. mqttSetup();
  120. #endif
  121. #if MDNS_SERVER_SUPPORT
  122. mdnsServerSetup();
  123. #endif
  124. #if MDNS_CLIENT_SUPPORT
  125. mdnsClientSetup();
  126. #endif
  127. #if LLMNR_SUPPORT
  128. llmnrSetup();
  129. #endif
  130. #if NETBIOS_SUPPORT
  131. netbiosSetup();
  132. #endif
  133. #if SSDP_SUPPORT
  134. ssdpSetup();
  135. #endif
  136. #if NTP_SUPPORT
  137. ntpSetup();
  138. #endif
  139. #if I2C_SUPPORT
  140. i2cSetup();
  141. #endif
  142. #if RF_SUPPORT
  143. rfbSetup();
  144. #endif
  145. #if ALEXA_SUPPORT
  146. alexaSetup();
  147. #endif
  148. #if NOFUSS_SUPPORT
  149. nofussSetup();
  150. #endif
  151. #if INFLUXDB_SUPPORT
  152. idbSetup();
  153. #endif
  154. #if THINGSPEAK_SUPPORT
  155. tspkSetup();
  156. #endif
  157. #if RFM69_SUPPORT
  158. rfm69Setup();
  159. #endif
  160. #if IR_SUPPORT
  161. irSetup();
  162. #endif
  163. #if DOMOTICZ_SUPPORT
  164. domoticzSetup();
  165. #endif
  166. #if HOMEASSISTANT_SUPPORT
  167. haSetup();
  168. #endif
  169. #if SENSOR_SUPPORT
  170. sensorSetup();
  171. #endif
  172. #if SCHEDULER_SUPPORT
  173. schSetup();
  174. #endif
  175. #if UART_MQTT_SUPPORT
  176. uartmqttSetup();
  177. #endif
  178. #ifdef FOXEL_LIGHTFOX_DUAL
  179. lightfoxSetup();
  180. #endif
  181. #if THERMOSTAT_SUPPORT
  182. thermostatSetup();
  183. #endif
  184. #if THERMOSTAT_DISPLAY_SUPPORT
  185. displaySetup();
  186. #endif
  187. // 3rd party code hook
  188. #if USE_EXTRA
  189. extraSetup();
  190. #endif
  191. // Prepare configuration for version 2.0
  192. migrate();
  193. // Set up delay() after loop callbacks are finished
  194. // Note: should be after settingsSetup()
  195. _loop_delay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  196. _loop_delay = constrain(_loop_delay, 0, 300);
  197. saveSettings();
  198. }
  199. void loop() {
  200. // Reload config before running any callbacks
  201. if (_reload_config) {
  202. _espurnaReload();
  203. _reload_config = false;
  204. }
  205. // Call registered loop callbacks
  206. for (unsigned char i = 0; i < _loop_callbacks.size(); i++) {
  207. (_loop_callbacks[i])();
  208. }
  209. // Power saving delay
  210. if (_loop_delay) delay(_loop_delay);
  211. }