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.

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