Optimize main loop handling

This commit is contained in:
fvanroie 2020-03-25 13:48:07 +01:00
parent 336ec76f08
commit df9af1cc3a
3 changed files with 41 additions and 25 deletions

View File

@ -6,7 +6,7 @@
void httpSetup(const JsonObject & settings); void httpSetup(const JsonObject & settings);
void httpLoop(void); void httpLoop(void);
void httpEverySecond(void); void httpEvery5Seconds(void);
void httpReconnect(void); void httpReconnect(void);
bool httpGetConfig(const JsonObject & settings); bool httpGetConfig(const JsonObject & settings);

View File

@ -4,7 +4,8 @@
#include "ArduinoJson.h" #include "ArduinoJson.h"
void mqttSetup(const JsonObject & settings); void mqttSetup(const JsonObject & settings);
void mqttLoop(bool wifiIsConnected); void mqttLoop();
void mqttEvery5Seconds(bool wifiIsConnected);
void mqttStop(); void mqttStop();
void mqttReconnect(); void mqttReconnect();

View File

@ -2,16 +2,12 @@
#include <Arduino.h> #include <Arduino.h>
#include "ArduinoJson.h" #include "ArduinoJson.h"
#include "TFT_eSPI.h"
#include "hasp_debug.h" #include "hasp_debug.h"
#include "hasp_spiffs.h" #include "hasp_spiffs.h"
#include "hasp_config.h" #include "hasp_config.h"
#include "hasp_tft.h"
#include "hasp_gui.h" #include "hasp_gui.h"
#include "hasp_ota.h"
//#include "hasp_ota.h"
#include "hasp.h" #include "hasp.h"
#include "hasp_conf.h"
#if HASP_USE_SPIFFS #if HASP_USE_SPIFFS
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
@ -48,8 +44,13 @@
#include "hasp_button.h" #include "hasp_button.h"
#endif #endif
#if HASP_USE_OTA
#include "hasp_ota.h"
#endif
bool isConnected; bool isConnected;
uint8_t mainLoopCounter = 0; uint8_t mainLoopCounter = 0;
unsigned long mainLastLoopTime = 0;
void setup() void setup()
{ {
@ -62,24 +63,24 @@ void setup()
#if HASP_USE_EEPROM #if HASP_USE_EEPROM
eepromSetup(); eepromSetup();
#endif #endif
#if HASP_USE_SPIFFS #if HASP_USE_SPIFFS
spiffsSetup(); spiffsSetup();
#endif #endif
/* Read Config File */ /* Read Config File */
DynamicJsonDocument settings(1024); DynamicJsonDocument settings(1024 + 512);
configSetup(settings); configSetup(settings);
#if HASP_USE_SDCARD #if HASP_USE_SDCARD
sdcardSetup(); sdcardSetup();
#endif #endif
// debugSetup(settings[F("debug")]); debugSetup(settings[F("debug")]);
/* Init Graphics */ /* Init Graphics */
TFT_eSPI screen = TFT_eSPI(); // TFT_eSPI screen = TFT_eSPI();
guiSetup(screen, settings[F("gui")]); guiSetup(settings[F("gui")]);
tftSetup(screen, settings[F("tft")]);
/* Init GUI Application */ /* Init GUI Application */
haspSetup(settings[F("hasp")]); haspSetup(settings[F("hasp")]);
@ -108,8 +109,11 @@ void setup()
buttonSetup(); buttonSetup();
#endif #endif
#if HASP_USE_OTA
otaSetup(settings[F("ota")]); otaSetup(settings[F("ota")]);
#endif #endif
#endif // WIFI
} }
void loop() void loop()
@ -134,43 +138,54 @@ void loop()
/* Network Services Loops */ /* Network Services Loops */
#if HASP_USE_WIFI #if HASP_USE_WIFI
isConnected = wifiLoop();
#if HASP_USE_MQTT #if HASP_USE_MQTT
mqttLoop(isConnected); mqttLoop();
#endif #endif // MQTT
#if HASP_USE_HTTP #if HASP_USE_HTTP
httpLoop(); httpLoop();
#endif #endif // HTTP
#if HASP_USE_TELNET #if HASP_USE_TELNET
telnetLoop(); telnetLoop();
#endif #endif // TELNET
#if HASP_USE_MDNS #if HASP_USE_MDNS
mdnsLoop(); mdnsLoop();
#endif #endif // MDNS
#if HASP_USE_BUTTON #if HASP_USE_BUTTON
buttonLoop(); buttonLoop();
#endif #endif // BUTTON
#if HASP_USE_OTA
otaLoop(); otaLoop();
debugLoop(); #endif // OTA
#endif
static unsigned long mainLastLoopTime = 0; #endif // WIFI
// Every Second Loop // Every Second Loop
if(millis() - mainLastLoopTime >= 1000) { if(millis() - mainLastLoopTime >= 1000) {
/* Update counters */
mainLastLoopTime += 1000; mainLastLoopTime += 1000;
httpEverySecond();
otaEverySecond();
mainLoopCounter++; mainLoopCounter++;
if(mainLoopCounter >= 10) { if(mainLoopCounter >= 10) {
mainLoopCounter = 0; mainLoopCounter = 0;
} }
/* Run Every Second */
#if HASP_USE_OTA
otaEverySecond();
#endif
debugEverySecond();
/* Run Every 5 Seconds */
if(mainLoopCounter == 0 || mainLoopCounter == 5) {
httpEvery5Seconds();
isConnected = wifiLoop();
mqttEvery5Seconds(isConnected);
}
} }
// delay(1); // delay(1);