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.

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