From 7a7b390fc9ab725f0094978e3196efea8df6b50e Mon Sep 17 00:00:00 2001 From: KlausMu Date: Sat, 9 Nov 2024 07:49:42 +0100 Subject: [PATCH] add WiFi status to GUI threadsafe --- .../src/applicationInternal/gui/guiBase.cpp | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/Platformio/src/applicationInternal/gui/guiBase.cpp b/Platformio/src/applicationInternal/gui/guiBase.cpp index 4cc33347..4295fd1e 100644 --- a/Platformio/src/applicationInternal/gui/guiBase.cpp +++ b/Platformio/src/applicationInternal/gui/guiBase.cpp @@ -1,3 +1,4 @@ +#include #include #include "applicationInternal/hardware/hardwarePresenter.h" #include "applicationInternal/memoryUsage.h" @@ -27,6 +28,12 @@ lv_style_t panel_style; lv_style_t style_red_border; #endif +// to add text threadsafe +std::mutex mutex_guiBase; +bool newWifiLabelStatusAvailable = false; +bool newWiFiLabelStatus; +void flushWiFiConnectedStatus(); + void guis_doTabCreationOnStartup(); void guis_doTabCreationAfterSliding(int newTabID); @@ -300,6 +307,12 @@ void gui_loop(void) { // } lv_timer_handler(); + + // flush texts that might have been added from callbacks from other threads + // has to be done in a thread safe way in the main thread + #if (ENABLE_WIFI_AND_MQTT ==1) + flushWiFiConnectedStatus(); + #endif } // ------------------------------------------------------------------------------------------------------------ @@ -351,10 +364,23 @@ void setActiveTab(uint32_t index, lv_anim_enable_t anim_en, bool send_tab_change } } -void showWiFiConnected(bool connected) { - if (connected) { - if (WifiLabel != NULL) {lv_label_set_text(WifiLabel, LV_SYMBOL_WIFI);} - } else { - if (WifiLabel != NULL) {lv_label_set_text(WifiLabel, "");} +void flushWiFiConnectedStatus() { + // this is to flush text which was added by another thread via "void showWiFiConnected(bool connected)" + // the function "flushWiFiConnectedStatus" is called from the main thread + std::lock_guard lck(mutex_guiBase); + if (newWifiLabelStatusAvailable) { + if (newWiFiLabelStatus) { + if (WifiLabel != NULL) {lv_label_set_text(WifiLabel, LV_SYMBOL_WIFI);} + } else { + if (WifiLabel != NULL) {lv_label_set_text(WifiLabel, "");} + } + newWifiLabelStatusAvailable = false; } } + +void showWiFiConnected(bool connected) { + // this callback is called from another thread from mqtt_hal_esp32.cpp + std::lock_guard lck(mutex_guiBase); + newWifiLabelStatusAvailable = true; + newWiFiLabelStatus = connected; +}