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.

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