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.

287 lines
6.5 KiB

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