From d0e383e398043d60e26b5469d35419c935441a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 7 Feb 2024 17:19:35 +0100 Subject: [PATCH] Merge setup() and loop() of PC and Arduino --- include/hasp_conf.h | 2 +- src/{main_arduino.cpp => main.cpp} | 24 ++-- src/main_pc.cpp | 118 +------------------ user_setups/darwin_sdl/darwin_sdl_64bits.ini | 4 +- user_setups/linux_sdl/linux_sdl_64bits.ini | 4 +- user_setups/win32/windows_gdi_64bits.ini | 4 +- user_setups/win32/windows_sdl_64bits.ini | 4 +- 7 files changed, 27 insertions(+), 133 deletions(-) rename src/{main_arduino.cpp => main.cpp} (94%) diff --git a/include/hasp_conf.h b/include/hasp_conf.h index 723b070d..dece01d1 100644 --- a/include/hasp_conf.h +++ b/include/hasp_conf.h @@ -123,7 +123,7 @@ #endif #ifndef HASP_USE_CONSOLE -#define HASP_USE_CONSOLE 1 +#define HASP_USE_CONSOLE HASP_TARGET_ARDUINO #endif /* Filesystem */ diff --git a/src/main_arduino.cpp b/src/main.cpp similarity index 94% rename from src/main_arduino.cpp rename to src/main.cpp index b383531c..68a51af3 100644 --- a/src/main_arduino.cpp +++ b/src/main.cpp @@ -1,8 +1,6 @@ /* MIT License - Copyright (c) 2019-2024 Francis Van Roie For full license information read the LICENSE file in the project folder */ -#if HASP_TARGET_ARDUINO - /* #ifdef CORE_DEBUG_LEVEL #undef CORE_DEBUG_LEVEL @@ -28,9 +26,9 @@ #include "hasp_gui.h" #endif -bool isConnected; -uint8_t mainLoopCounter = 0; -unsigned long mainLastLoopTime = 0; +static bool isConnected; +static uint8_t mainLoopCounter = 0; +static unsigned long mainLastLoopTime = 0; #ifdef HASP_USE_STAT_COUNTER uint16_t statLoopCounter = 0; // measures the average looptime @@ -40,11 +38,18 @@ void setup() { // hal_setup(); +#if HASP_TARGET_ARDUINO esp_log_level_set("*", ESP_LOG_NONE); // set all components to ERROR level // esp_log_level_set("wifi", ESP_LOG_NONE); // enable WARN logs from WiFi stack // esp_log_level_set("dhcpc", ESP_LOG_INFO); // enable INFO logs from DHCP client // esp_log_level_set("esp_crt_bundle", ESP_LOG_VERBOSE); // enable WARN logs from WiFi stack // esp_log_level_set("esp_tls", ESP_LOG_VERBOSE); // enable WARN logs from WiFi stack +#elif HASP_TARGET_PC + // Initialize lvgl environment + lv_init(); + lv_log_register_print_cb(debugLvglLogEvent); +#endif + haspDevice.init(); /**************************** @@ -149,7 +154,7 @@ void setup() gui_setup_lvgl_task(); #endif // HASP_USE_LVGL_TASK - mainLastLoopTime = -1000; // reset loop counter + mainLastLoopTime = 0; // reset loop counter } IRAM_ATTR void loop() @@ -195,7 +200,7 @@ IRAM_ATTR void loop() /* Timer Loop */ if(millis() - mainLastLoopTime >= 1000) { - mainLastLoopTime += 1000; + mainLastLoopTime = millis(); /* Runs Every Second */ haspEverySecond(); // sleep timer & statusupdate @@ -237,10 +242,9 @@ IRAM_ATTR void loop() case 4: #if HASP_USE_WIFI > 0 || HASP_USE_ETHERNET > 0 isConnected = networkEvery5Seconds(); // Check connection - +#endif #if HASP_USE_MQTT > 0 mqttEvery5Seconds(isConnected); -#endif #endif break; @@ -270,5 +274,3 @@ IRAM_ATTR void loop() delay(2); // ms #endif } - -#endif diff --git a/src/main_pc.cpp b/src/main_pc.cpp index 5430b130..2b4f7362 100644 --- a/src/main_pc.cpp +++ b/src/main_pc.cpp @@ -26,28 +26,20 @@ #include "hasplib.h" -// #include "app_hal.h" #if USE_MONITOR #include "display/monitor.h" #endif #include "hasp_debug.h" -#include "hasp_gui.h" - -#include "dev/device.h" - -bool isConnected; - -uint8_t mainLoopCounter = 0; -unsigned long mainLastLoopTime = 0; - -#ifdef HASP_USE_STAT_COUNTER -uint16_t statLoopCounter = 0; // measures the average looptime -#endif +// hasp_gui.cpp extern uint16_t tft_width; extern uint16_t tft_height; +// main.cpp +extern void setup(); +extern void loop(); + #if defined(WINDOWS) // https://gist.github.com/kingseva/a918ec66079a9475f19642ec31276a21 void BindStdHandlesToConsole() @@ -110,106 +102,6 @@ void InitializeConsoleOutput() } #endif -void setup() -{ - // Initialize lvgl environment - lv_init(); - lv_log_register_print_cb(debugLvglLogEvent); - - // Read & Apply User Configuration -#if HASP_USE_CONFIG > 0 - configSetup(); -#endif - - haspDevice.init(); // hardware setup - // hal_setup(); - guiSetup(); - - dispatchSetup(); // for hasp and oobe - haspSetup(); - -#if HASP_USE_MQTT > 0 - mqttSetup(); // Hasp must be running - mqttStart(); -#endif - -#if HASP_USE_GPIO > 0 - gpioSetup(); -#endif - -#if defined(HASP_USE_CUSTOM) - custom_setup(); -#endif - - mainLastLoopTime = millis(); // - 1000; // reset loop counter - // delay(250); -} - -void loop() -{ - haspLoop(); -#if HASP_USE_MQTT - mqttLoop(); -#endif - - // debugLoop(); // Console - haspDevice.loop(); - guiLoop(); - -#if HASP_USE_GPIO > 0 - gpioLoop(); -#endif - -#if defined(HASP_USE_CUSTOM) - custom_loop(); -#endif - -#ifdef HASP_USE_STAT_COUNTER - statLoopCounter++; // measures the average looptime -#endif - - /* Timer Loop */ - if(millis() - mainLastLoopTime >= 1000) { - /* Runs Every Second */ - haspEverySecond(); // sleep timer - dispatchEverySecond(); // sleep timer - -#if HASP_USE_ARDUINOOTA > 0 - otaEverySecond(); // progressbar -#endif - -#if defined(HASP_USE_CUSTOM) - custom_every_second(); -#endif - - /* Runs Every 5 Seconds */ - if(mainLoopCounter == 0 || mainLoopCounter == 5) { - - haspDevice.loop_5s(); -#if HASP_USE_GPIO > 0 - gpioEvery5Seconds(); -#endif - -#if HASP_USE_MQTT - mqttEvery5Seconds(true); -#endif - -#if defined(HASP_USE_CUSTOM) - custom_every_5seconds(); -#endif - } - - /* Reset loop counter every 10 seconds */ - if(mainLoopCounter >= 9) { - mainLoopCounter = 0; - } else { - mainLoopCounter++; - } - mainLastLoopTime += 1000; - } - // delay(6); -} - void usage(const char* progName, const char* version) { std::cout diff --git a/user_setups/darwin_sdl/darwin_sdl_64bits.ini b/user_setups/darwin_sdl/darwin_sdl_64bits.ini index 1cae860e..1003bc6f 100644 --- a/user_setups/darwin_sdl/darwin_sdl_64bits.ini +++ b/user_setups/darwin_sdl/darwin_sdl_64bits.ini @@ -26,8 +26,8 @@ build_flags = -D HASP_USE_SPIFFS=0 -D HASP_USE_LITTLEFS=0 -D HASP_USE_EEPROM=0 - -D HASP_USE_GPIO=1 - -D HASP_USE_CONFIG=0 ; Standalone application, as library + -D HASP_USE_GPIO=0 + -D HASP_USE_CONFIG=1 -D HASP_USE_DEBUG=1 -D HASP_USE_PNGDECODE=1 -D HASP_USE_BMPDECODE=1 diff --git a/user_setups/linux_sdl/linux_sdl_64bits.ini b/user_setups/linux_sdl/linux_sdl_64bits.ini index dbe7f24c..c1e8d8dc 100644 --- a/user_setups/linux_sdl/linux_sdl_64bits.ini +++ b/user_setups/linux_sdl/linux_sdl_64bits.ini @@ -26,8 +26,8 @@ build_flags = -D HASP_USE_LITTLEFS=0 -D LV_USE_FS_IF=1 -D HASP_USE_EEPROM=0 - -D HASP_USE_GPIO=1 - -D HASP_USE_CONFIG=0 ; Standalone application, as library + -D HASP_USE_GPIO=0 + -D HASP_USE_CONFIG=1 -D HASP_USE_DEBUG=1 -D HASP_USE_PNGDECODE=1 -D HASP_USE_BMPDECODE=1 diff --git a/user_setups/win32/windows_gdi_64bits.ini b/user_setups/win32/windows_gdi_64bits.ini index 0344d2fc..e3a86401 100644 --- a/user_setups/win32/windows_gdi_64bits.ini +++ b/user_setups/win32/windows_gdi_64bits.ini @@ -20,8 +20,8 @@ build_flags = -D HASP_USE_SPIFFS=0 -D HASP_USE_LITTLEFS=0 -D HASP_USE_EEPROM=0 - -D HASP_USE_GPIO=1 - -D HASP_USE_CONFIG=1 ; Standalone application, as library + -D HASP_USE_GPIO=0 + -D HASP_USE_CONFIG=1 -D HASP_USE_DEBUG=1 -D HASP_USE_PNGDECODE=1 -D HASP_USE_BMPDECODE=1 diff --git a/user_setups/win32/windows_sdl_64bits.ini b/user_setups/win32/windows_sdl_64bits.ini index 0caaccca..a4a131e2 100644 --- a/user_setups/win32/windows_sdl_64bits.ini +++ b/user_setups/win32/windows_sdl_64bits.ini @@ -25,8 +25,8 @@ build_flags = -D HASP_USE_SPIFFS=0 -D HASP_USE_LITTLEFS=0 -D HASP_USE_EEPROM=0 - -D HASP_USE_GPIO=1 - -D HASP_USE_CONFIG=1 ; Standalone application, as library + -D HASP_USE_GPIO=0 + -D HASP_USE_CONFIG=1 -D HASP_USE_DEBUG=1 -D HASP_USE_PNGDECODE=1 -D HASP_USE_BMPDECODE=1