Skip to content

Commit

Permalink
add WiFi status to GUI threadsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausMu committed Nov 9, 2024
1 parent 823381b commit 7a7b390
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions Platformio/src/applicationInternal/gui/guiBase.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <mutex>
#include <lvgl.h>
#include "applicationInternal/hardware/hardwarePresenter.h"
#include "applicationInternal/memoryUsage.h"
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
}

// ------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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<std::mutex> 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<std::mutex> lck(mutex_guiBase);
newWifiLabelStatusAvailable = true;
newWiFiLabelStatus = connected;
}

0 comments on commit 7a7b390

Please sign in to comment.