dashboard: throttle lvgl label updates
This commit is contained in:
@@ -24,14 +24,25 @@ static lv_obj_t *touch_count_label = nullptr;
|
||||
|
||||
static uint32_t touch_count = 0;
|
||||
static unsigned long last_wifi_attempt_ms = 0;
|
||||
static unsigned long last_wifi_label_update_ms = 0;
|
||||
static unsigned long last_status_poll_ms = 0;
|
||||
|
||||
static void set_label_text(lv_obj_t *label, const String &text)
|
||||
static String last_wifi_label_text;
|
||||
static String last_api_label_text;
|
||||
static String last_status_label_text;
|
||||
|
||||
static void set_label_text_if_changed(lv_obj_t *label, String &last_text, const String &text)
|
||||
{
|
||||
if (label == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (last_text == text) {
|
||||
return;
|
||||
}
|
||||
|
||||
last_text = text;
|
||||
|
||||
lvgl_port_lock(-1);
|
||||
lv_label_set_text(label, text.c_str());
|
||||
lvgl_port_unlock();
|
||||
@@ -70,7 +81,7 @@ static void connect_wifi_if_needed()
|
||||
Serial.print("Connecting to Cargo ESP AP: ");
|
||||
Serial.println(CARGO_WIFI_SSID);
|
||||
|
||||
set_label_text(wifi_label, "WiFi: connecting...");
|
||||
set_label_text_if_changed(wifi_label, last_wifi_label_text, "WiFi: connecting...");
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(CARGO_WIFI_SSID, CARGO_WIFI_PASSWORD);
|
||||
@@ -78,23 +89,29 @@ static void connect_wifi_if_needed()
|
||||
|
||||
static void update_wifi_label()
|
||||
{
|
||||
if (millis() - last_wifi_label_update_ms < 5000) {
|
||||
return;
|
||||
}
|
||||
|
||||
last_wifi_label_update_ms = millis();
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
String text = "WiFi: connected\nIP: ";
|
||||
text += WiFi.localIP().toString();
|
||||
text += "\nRSSI: ";
|
||||
text += String(WiFi.RSSI());
|
||||
text += " dBm";
|
||||
set_label_text(wifi_label, text);
|
||||
set_label_text_if_changed(wifi_label, last_wifi_label_text, text);
|
||||
} else {
|
||||
set_label_text(wifi_label, "WiFi: not connected\nSSID: " + String(CARGO_WIFI_SSID));
|
||||
set_label_text_if_changed(wifi_label, last_wifi_label_text, "WiFi: not connected\nSSID: " + String(CARGO_WIFI_SSID));
|
||||
}
|
||||
}
|
||||
|
||||
static void poll_status_api()
|
||||
{
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
set_label_text(api_label, "API: waiting for WiFi");
|
||||
set_label_text(status_label, "No API response yet.");
|
||||
set_label_text_if_changed(api_label, last_api_label_text, "API: waiting for WiFi");
|
||||
set_label_text_if_changed(status_label, last_status_label_text, "No API response yet.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -107,7 +124,7 @@ static void poll_status_api()
|
||||
Serial.print("GET ");
|
||||
Serial.println(CARGO_API_STATUS_URL);
|
||||
|
||||
set_label_text(api_label, "API: requesting /api/v1/status");
|
||||
set_label_text_if_changed(api_label, last_api_label_text, "API: requesting /api/v1/status");
|
||||
|
||||
HTTPClient http;
|
||||
http.begin(CARGO_API_STATUS_URL);
|
||||
@@ -123,21 +140,21 @@ static void poll_status_api()
|
||||
String api_text = "API: HTTP ";
|
||||
api_text += String(code);
|
||||
api_text += "\nGET /api/v1/status";
|
||||
set_label_text(api_label, api_text);
|
||||
set_label_text_if_changed(api_label, last_api_label_text, api_text);
|
||||
|
||||
if (body.length() > 900) {
|
||||
body = body.substring(0, 900);
|
||||
body += "\n...truncated...";
|
||||
}
|
||||
|
||||
set_label_text(status_label, body);
|
||||
set_label_text_if_changed(status_label, last_status_label_text, body);
|
||||
} else {
|
||||
String error = "API request failed: ";
|
||||
error += http.errorToString(code);
|
||||
|
||||
Serial.println(error);
|
||||
set_label_text(api_label, error);
|
||||
set_label_text(status_label, "No valid response from Cargo ESP.");
|
||||
set_label_text_if_changed(api_label, last_api_label_text, error);
|
||||
set_label_text_if_changed(status_label, last_status_label_text, "No valid response from Cargo ESP.");
|
||||
}
|
||||
|
||||
http.end();
|
||||
|
||||
Reference in New Issue
Block a user