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.

316 lines
7.0 KiB

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