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.

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