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.

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