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.

283 lines
6.4 KiB

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