feat: desktop connection status icon.

This commit is contained in:
Ivan Li 2023-05-03 00:05:04 +08:00
parent b7476428a2
commit 324a307fe3
3 changed files with 38 additions and 14 deletions

View File

@ -1,8 +1,4 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/
#pragma once
#include <stdio.h>
@ -34,10 +30,10 @@ static const char *GUI_TAG = "LVGL_GUI";
// EF 87 AB
#define APP_WIFI_GOOD_SYMBOL "\xEF\x87\xAB"
// EE 87 8C
#define APP_CONNECTED_SYMBOL "\xEE\x87\x8C"
// EE 87 8B
#define APP_DISCONNECTED_SYMBOL "\xEE\x87\x8B"
#define APP_CONNECTED_SYMBOL "\xEE\x87\x8B"
// EE 87 8C
#define APP_DISCONNECTED_SYMBOL "\xEE\x87\x8C"
// EE 8A 9E
#define APP_TIMER_SYMBOL "\xEE\x8A\x9E"
@ -144,13 +140,14 @@ static void gui_set_wifi_connecting() {
static void gui_set_wifi_connected() {
lv_anim_del(wifi_label, NULL);
vTaskDelay(300 / portTICK_PERIOD_MS);
lv_obj_clear_flag(wifi_label, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(wifi_label, APP_WIFI_GOOD_SYMBOL);
}
static void gui_set_wifi_disconnected() {
lv_anim_del(&wifi_label, NULL);
vTaskDelay(300 / portTICK_PERIOD_MS);
lv_obj_clear_flag(wifi_label, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(wifi_label, APP_WIFI_WEAK_SYMBOL);
}
@ -160,13 +157,15 @@ static void gui_set_server_connecting() {
}
static void gui_set_server_connected() {
lv_anim_del(&desktop_label, NULL);
lv_anim_del(desktop_label, NULL);
vTaskDelay(300 / portTICK_PERIOD_MS);
lv_obj_clear_flag(desktop_label, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(desktop_label, APP_CONNECTED_SYMBOL);
}
static void gui_set_server_disconnected() {
lv_anim_del(&desktop_label, NULL);
lv_anim_del(desktop_label, NULL);
vTaskDelay(300 / portTICK_PERIOD_MS);
lv_obj_clear_flag(desktop_label, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(desktop_label, APP_DISCONNECTED_SYMBOL);
}

View File

@ -52,7 +52,7 @@ void app_main(void) {
gui_set_wifi_disconnected();
}
udp_server_init();
// service_discovery_init();
service_discovery_init();
// vTaskDelay(pdMS_TO_TICKS(1000));
// mqtt_app_start();
// if (waiting_for_mqtt_connected()) {

View File

@ -10,6 +10,7 @@
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "gui.c"
#include "light.c"
#include "lwip/err.h"
#include "lwip/sockets.h"
@ -20,6 +21,9 @@
static const char *UDP_SERVER_TAG = "UDP_SERVER";
static bool desktop_connected = false;
static uint64_t last_desktop_ping_at = 0;
static void udp_server_task(void *pvParameters) {
char rx_buffer[1024];
char addr_str[128];
@ -84,6 +88,7 @@ static void udp_server_task(void *pvParameters) {
switch (rx_buffer[0]) {
case 1:
last_desktop_ping_at = esp_timer_get_time();
sendto(sock, rx_buffer, 1, 0, (struct sockaddr *)&source_addr,
sizeof(source_addr));
break;
@ -127,8 +132,28 @@ static void udp_server_task(void *pvParameters) {
vTaskDelete(NULL);
}
void udp_server_init(void) {
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
xTaskCreate(udp_server_task, "udp_server", 4096, (void *)AF_INET, 5, NULL);
static void change_desktop_connection_status(bool connected) {
desktop_connected = connected;
if (connected) {
gui_set_server_connected();
} else {
gui_set_server_disconnected();
}
}
static void desktop_online_check_task(void *pvParameters) {
while (1) {
if (esp_timer_get_time() - last_desktop_ping_at > 5000000) { // 2 seconds
change_desktop_connection_status(false);
} else {
change_desktop_connection_status(true);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void udp_server_init(void) {
xTaskCreate(udp_server_task, "udp_server", 4096, (void *)AF_INET, 5, NULL);
xTaskCreate(desktop_online_check_task, "desktop_online_check", 4096, NULL, 10,
NULL);
}