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.

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