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.

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