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.

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