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.

255 lines
5.9 KiB

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