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.

239 lines
5.5 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. std::vector<void (*)()> _loop_callbacks;
  18. std::vector<void (*)()> _reload_callbacks;
  19. unsigned long _loop_delay = 0;
  20. // -----------------------------------------------------------------------------
  21. // GENERAL CALLBACKS
  22. // -----------------------------------------------------------------------------
  23. void espurnaRegisterLoop(void (*callback)()) {
  24. _loop_callbacks.push_back(callback);
  25. }
  26. void espurnaRegisterReload(void (*callback)()) {
  27. _reload_callbacks.push_back(callback);
  28. }
  29. void espurnaReload() {
  30. for (unsigned char i = 0; i < _reload_callbacks.size(); i++) {
  31. (_reload_callbacks[i])();
  32. }
  33. }
  34. unsigned long espurnaLoopDelay() {
  35. return _loop_delay;
  36. }
  37. // -----------------------------------------------------------------------------
  38. // BOOTING
  39. // -----------------------------------------------------------------------------
  40. void setup() {
  41. // -------------------------------------------------------------------------
  42. // Basic modules, will always run
  43. // -------------------------------------------------------------------------
  44. // Cache initial free heap value
  45. getInitialFreeHeap();
  46. // Serial debug
  47. #if DEBUG_SUPPORT
  48. debugSetup();
  49. #endif
  50. // Init RTCMEM
  51. rtcmemSetup();
  52. // Init EEPROM
  53. eepromSetup();
  54. // Init persistance
  55. settingsSetup();
  56. // Init Serial, SPIFFS and system check
  57. systemSetup();
  58. // Init terminal features
  59. #if TERMINAL_SUPPORT
  60. terminalSetup();
  61. #endif
  62. // Hostname & board name initialization
  63. if (getSetting("hostname").length() == 0) {
  64. setDefaultHostname();
  65. }
  66. setBoardName();
  67. // Show welcome message and system configuration
  68. info();
  69. wifiSetup();
  70. otaSetup();
  71. #if TELNET_SUPPORT
  72. telnetSetup();
  73. #endif
  74. // -------------------------------------------------------------------------
  75. // Check if system is stable
  76. // -------------------------------------------------------------------------
  77. #if SYSTEM_CHECK_ENABLED
  78. if (!systemCheck()) return;
  79. #endif
  80. // -------------------------------------------------------------------------
  81. // Next modules will be only loaded if system is flagged as stable
  82. // -------------------------------------------------------------------------
  83. // Init webserver required before any module that uses API
  84. #if WEB_SUPPORT
  85. webSetup();
  86. wsSetup();
  87. #if DEBUG_WEB_SUPPORT
  88. debugWebSetup();
  89. #endif
  90. #endif
  91. #if API_SUPPORT
  92. apiSetup();
  93. #endif
  94. // lightSetup must be called before relaySetup
  95. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  96. lightSetup();
  97. #endif
  98. relaySetup();
  99. #if BUTTON_SUPPORT
  100. buttonSetup();
  101. #endif
  102. #if ENCODER_SUPPORT && (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE)
  103. encoderSetup();
  104. #endif
  105. #if LED_SUPPORT
  106. ledSetup();
  107. #endif
  108. #if MQTT_SUPPORT
  109. mqttSetup();
  110. #endif
  111. #if MDNS_SERVER_SUPPORT
  112. mdnsServerSetup();
  113. #endif
  114. #if MDNS_CLIENT_SUPPORT
  115. mdnsClientSetup();
  116. #endif
  117. #if LLMNR_SUPPORT
  118. llmnrSetup();
  119. #endif
  120. #if NETBIOS_SUPPORT
  121. netbiosSetup();
  122. #endif
  123. #if SSDP_SUPPORT
  124. ssdpSetup();
  125. #endif
  126. #if NTP_SUPPORT
  127. ntpSetup();
  128. #endif
  129. #if I2C_SUPPORT
  130. i2cSetup();
  131. #endif
  132. #if RF_SUPPORT
  133. rfbSetup();
  134. #endif
  135. #if ALEXA_SUPPORT
  136. alexaSetup();
  137. #endif
  138. #if NOFUSS_SUPPORT
  139. nofussSetup();
  140. #endif
  141. #if INFLUXDB_SUPPORT
  142. idbSetup();
  143. #endif
  144. #if THINGSPEAK_SUPPORT
  145. tspkSetup();
  146. #endif
  147. #if RFM69_SUPPORT
  148. rfm69Setup();
  149. #endif
  150. #if IR_SUPPORT
  151. irSetup();
  152. #endif
  153. #if DOMOTICZ_SUPPORT
  154. domoticzSetup();
  155. #endif
  156. #if HOMEASSISTANT_SUPPORT
  157. haSetup();
  158. #endif
  159. #if SENSOR_SUPPORT
  160. sensorSetup();
  161. #endif
  162. #if SCHEDULER_SUPPORT
  163. schSetup();
  164. #endif
  165. #if UART_MQTT_SUPPORT
  166. uartmqttSetup();
  167. #endif
  168. #ifdef FOXEL_LIGHTFOX_DUAL
  169. lightfoxSetup();
  170. #endif
  171. #if THERMOSTAT_SUPPORT
  172. thermostatSetup();
  173. #endif
  174. #if THERMOSTAT_DISPLAY_SUPPORT
  175. displaySetup();
  176. #endif
  177. // 3rd party code hook
  178. #if USE_EXTRA
  179. extraSetup();
  180. #endif
  181. // Prepare configuration for version 2.0
  182. migrate();
  183. // Set up delay() after loop callbacks are finished
  184. // Note: should be after settingsSetup()
  185. _loop_delay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  186. _loop_delay = constrain(_loop_delay, 0, 300);
  187. saveSettings();
  188. }
  189. void loop() {
  190. // Call registered loop callbacks
  191. for (unsigned char i = 0; i < _loop_callbacks.size(); i++) {
  192. (_loop_callbacks[i])();
  193. }
  194. // Power saving delay
  195. if (_loop_delay) delay(_loop_delay);
  196. }