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.

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