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.

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