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.

282 lines
6.4 KiB

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