feat: 支持通过 UDP 接收网速数据并显示在界面上。

This commit is contained in:
Ivan Li 2023-05-15 21:06:09 +08:00
parent f1ab7aa361
commit ceaa1d343a
9 changed files with 2793 additions and 4 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@ build/
sdkconfig
sdkconfig.old
.vscode/
managed_components/
managed_components/
.DS_Store

Binary file not shown.

Binary file not shown.

View File

@ -18,7 +18,7 @@ idf_component_register(
"light.c"
"led_strip_encoder/led_strip_encoder.c"
"gui.c"
"net_gateway_monitor.c"
"app_icon_8.c"
INCLUDE_DIRS "."
)

View File

@ -35,7 +35,19 @@ static LV_ATTRIBUTE_LARGE_CONST const uint8_t glyph_bitmap[] = {
0xc0,
/* U+F6AB "" */
0x79, 0x8, 0x1, 0x80};
0x79, 0x8, 0x1, 0x80,
/* U+F354 "" */
0x38, 0x28, 0x28, 0x28, 0xee, 0x82, 0x44, 0x28, 0x10,
/* U+F357 "" */
0x8, 0x14, 0x22, 0x41, 0x77, 0x14, 0x14, 0x14, 0x1c,
/* U+F354 "" */
0x31, 0x45, 0x37, 0x85, 0x23, 0x0,
/* U+F357 "" */
0x31, 0x28, 0x7b, 0x30, 0xc3, 0x0};
/*---------------------
* GLYPH DESCRIPTION
@ -83,7 +95,21 @@ static const lv_font_fmt_txt_glyph_dsc_t glyph_dsc[] = {
.box_w = 7,
.box_h = 4,
.ofs_x = 2,
.ofs_y = 0}};
.ofs_y = 0},
{.bitmap_index = 48,
.adv_w = 96,
.box_w = 6,
.box_h = 7,
.ofs_x = 0,
.ofs_y = 0},
{.bitmap_index = 54,
.adv_w = 96,
.box_w = 6,
.box_h = 7,
.ofs_x = 0,
.ofs_y = -1}
};
/*---------------------
* CHARACTER MAPPING

2461
main/fonts/app_10.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -8,6 +8,7 @@
#include "esp_err.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "fonts/app_10.c"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "lvgl.h"
@ -37,6 +38,10 @@ static const char *GUI_TAG = "LVGL_GUI";
// EE 8A 9E
#define APP_TIMER_SYMBOL "\xEE\x8A\x9E"
// EF 8D 97
#define APP_UP_SYMBOL "\xEF\x8D\x97"
// EF 8D 94
#define APP_DOWN_SYMBOL "\xEF\x8D\x94"
#define GUI_PANEL_TIMEOUT_US 3000000 // 3s
@ -88,6 +93,16 @@ static bool panel_locked = false;
static lv_obj_t *scr;
typedef struct gui_network_speed_panel_s {
lv_obj_t *panel;
lv_obj_t *direct_upload_label;
lv_obj_t *direct_download_label;
lv_obj_t *proxy_upload_label;
lv_obj_t *proxy_download_label;
} gui_network_speed_panel_t;
static gui_network_speed_panel_t *gui_network_speed_panel = NULL;
static void example_lvgl_rounder(lv_disp_drv_t *disp_drv, lv_area_t *area) {
area->y1 = area->y1 & (~0x7);
area->y2 = area->y2 | 0x7;
@ -326,6 +341,142 @@ static void gui_change_volume_level(int32_t target_value) {
panel_locked = false;
}
static void gui_network_speed_human_readable(uint64_t *speed, char *str) {
if (*speed > 1024 * 1024 * 1024) {
float v = (float)*speed / 1024.0f / 1024.0f / 1024.0f;
if (v >= 1000) {
sprintf(str, "% 5.0f GB/s", v);
} else if (v >= 100) {
sprintf(str, "% 5.1f GB/s", v);
} else {
sprintf(str, "% 5.2f GB/s", v);
}
} else if (*speed > 1024 * 1024) {
float v = (float)*speed / 1024.0f / 1024.0f;
if (v >= 1000) {
sprintf(str, "% 5.0f MB/s", v);
} else if (v >= 100) {
sprintf(str, "% 5.1f MB/s", v);
} else {
sprintf(str, "% 5.2f MB/s", v);
}
} else if (*speed > 1024) {
float v = (float)*speed / 1024.0f;
if (v >= 1000) {
sprintf(str, "% 5.0f KB/s", v);
} else if (v >= 100) {
sprintf(str, "% 5.1f KB/s", v);
} else {
sprintf(str, "% 5.2f KB/s", v);
}
} else {
sprintf(str, "% 5d B/s", (uint8_t)*speed);
}
}
static void gui_update_network_label_style(lv_obj_t *label, uint64_t *speed) {
if (*speed > 1024 * 1024 * 50 / 8) { // 50 Mbps
lv_obj_set_style_bg_color(label, lv_color_black(), LV_PART_MAIN);
lv_obj_set_style_text_color(label, lv_color_white(), LV_PART_MAIN);
} else {
lv_obj_set_style_bg_color(label, lv_color_white(), LV_PART_MAIN);
lv_obj_set_style_text_color(label, lv_color_black(), LV_PART_MAIN);
}
}
static void gui_change_network_speed(uint64_t *direct_upload_speed,
uint64_t *direct_download_speed,
uint64_t *proxy_upload_speed,
uint64_t *proxy_download_speed) {
if (gui_network_speed_panel == NULL) {
return;
}
static char str[32];
strcpy(str, APP_UP_SYMBOL);
gui_network_speed_human_readable(direct_upload_speed, &str[3]);
lv_label_set_text(gui_network_speed_panel->direct_upload_label, str);
gui_network_speed_human_readable(proxy_upload_speed, &str[3]);
lv_label_set_text(gui_network_speed_panel->proxy_upload_label, str);
strcpy(str, APP_DOWN_SYMBOL);
gui_network_speed_human_readable(direct_download_speed, &str[3]);
lv_label_set_text(gui_network_speed_panel->direct_download_label, str);
gui_network_speed_human_readable(proxy_download_speed, &str[3]);
lv_label_set_text(gui_network_speed_panel->proxy_download_label, str);
gui_update_network_label_style(gui_network_speed_panel->direct_upload_label,
direct_upload_speed);
gui_update_network_label_style(gui_network_speed_panel->direct_download_label,
direct_download_speed);
gui_update_network_label_style(gui_network_speed_panel->proxy_upload_label,
proxy_upload_speed);
gui_update_network_label_style(gui_network_speed_panel->proxy_download_label,
proxy_download_speed);
}
static void gui_init_network_speed(lv_obj_t *scr) {
gui_network_speed_panel =
(gui_network_speed_panel_t *)malloc(sizeof(gui_network_speed_panel_t));
gui_network_speed_panel->panel = lv_obj_create(scr);
lv_obj_set_size(gui_network_speed_panel->panel, 128, 40);
lv_obj_align(gui_network_speed_panel->panel, LV_ALIGN_BOTTOM_MID, 0, 0);
lv_obj_set_style_bg_color(gui_network_speed_panel->panel, lv_color_white(),
LV_PART_MAIN);
lv_obj_set_style_border_color(gui_network_speed_panel->panel,
lv_color_black(), LV_PART_MAIN);
lv_obj_set_style_border_width(gui_network_speed_panel->panel, 1,
LV_PART_MAIN);
lv_obj_set_style_radius(gui_network_speed_panel->panel, 5, LV_PART_MAIN);
lv_obj_set_style_pad_hor(gui_network_speed_panel->panel, 1, LV_PART_MAIN);
lv_obj_set_style_pad_ver(gui_network_speed_panel->panel, 1, LV_PART_MAIN);
gui_network_speed_panel->direct_upload_label =
lv_label_create(gui_network_speed_panel->panel);
lv_obj_align(gui_network_speed_panel->direct_upload_label, LV_ALIGN_TOP_LEFT,
0, 0);
lv_obj_set_style_text_font(gui_network_speed_panel->direct_upload_label,
&app_10, LV_PART_MAIN);
lv_label_set_text(gui_network_speed_panel->direct_upload_label,
APP_UP_SYMBOL " 0.00 KB/s");
lv_obj_set_style_bg_opa(gui_network_speed_panel->direct_upload_label,
LV_OPA_COVER, LV_PART_MAIN);
gui_network_speed_panel->direct_download_label =
lv_label_create(gui_network_speed_panel->panel);
lv_obj_align(gui_network_speed_panel->direct_download_label,
LV_ALIGN_BOTTOM_LEFT, 0, 0);
lv_obj_set_style_text_font(gui_network_speed_panel->direct_download_label,
&app_10, LV_PART_MAIN);
lv_label_set_text(gui_network_speed_panel->direct_download_label,
APP_DOWN_SYMBOL " 0.00 KB/s");
lv_obj_set_style_bg_opa(gui_network_speed_panel->direct_download_label,
LV_OPA_COVER, LV_PART_MAIN);
gui_network_speed_panel->proxy_upload_label =
lv_label_create(gui_network_speed_panel->panel);
lv_obj_align(gui_network_speed_panel->proxy_upload_label, LV_ALIGN_TOP_RIGHT,
0, 0);
lv_obj_set_style_text_font(gui_network_speed_panel->proxy_upload_label,
&app_10, LV_PART_MAIN);
lv_label_set_text(gui_network_speed_panel->proxy_upload_label,
APP_UP_SYMBOL " 0.00 KB/s");
lv_obj_set_style_bg_opa(gui_network_speed_panel->proxy_upload_label,
LV_OPA_COVER, LV_PART_MAIN);
gui_network_speed_panel->proxy_download_label =
lv_label_create(gui_network_speed_panel->panel);
lv_obj_align(gui_network_speed_panel->proxy_download_label,
LV_ALIGN_BOTTOM_RIGHT, 0, 0);
lv_obj_set_style_text_font(gui_network_speed_panel->proxy_download_label,
&app_10, LV_PART_MAIN);
lv_label_set_text(gui_network_speed_panel->proxy_download_label,
APP_DOWN_SYMBOL " 0.00 KB/s");
lv_obj_set_style_bg_opa(gui_network_speed_panel->proxy_download_label,
LV_OPA_COVER, LV_PART_MAIN);
}
void gui_status_bar_create(lv_obj_t *scr) {
// wifi icon
wifi_label = lv_label_create(scr);
@ -370,6 +521,7 @@ void example_lvgl_demo_ui(lv_disp_t *disp) {
lv_obj_align(label, LV_ALIGN_BOTTOM_RIGHT, 0, 0);
gui_status_bar_create(scr);
gui_init_network_speed(scr);
}
static void gui_tick(void *pvParameters) {

View File

@ -14,6 +14,7 @@
#include "service_discovery.c"
// #include "temperature.c"
#include "desktop.c"
#include "net_gateway_monitor.c"
#include "ui_input.c"
#include "wifi.c"
@ -52,6 +53,7 @@ void app_main(void) {
gui_set_wifi_disconnected();
}
udp_server_init();
net_gateway_monitor_init();
service_discovery_init();
// vTaskDelay(pdMS_TO_TICKS(1000));
// mqtt_app_start();

147
main/net_gateway_monitor.c Normal file
View File

@ -0,0 +1,147 @@
#include <lwip/netdb.h>
#include <string.h>
#include <sys/param.h>
#include "esp_err.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "gui.c"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#define NET_GATEWAY_MONITOR_UDP_PORT 23043
#define NET_GATEWAY_MONITOR_SERVER_ADDR 0xC0A81F05 // 192.168.31.5
#define NET_GATEWAY_MONITOR_SERVER_PORT 17890
static const char *NET_GATEWAY_MONITOR_TAG = "NET_GATE_MONITOR";
static int net_gateway_monitor_socket = -1;
static void net_gateway_monitor_request_task(void *pvParameters) {
int sock = net_gateway_monitor_socket;
struct sockaddr_in server_addr;
server_addr.sin_addr.s_addr = htonl(NET_GATEWAY_MONITOR_SERVER_ADDR);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(NET_GATEWAY_MONITOR_SERVER_PORT);
while (sock == net_gateway_monitor_socket) {
int err = sendto(net_gateway_monitor_socket, "ping", 4, 0, &server_addr,
sizeof(server_addr));
if (err < 0) {
ESP_LOGE(NET_GATEWAY_MONITOR_TAG,
"Error occurred during sending: errno %d. sock: %d", errno,
net_gateway_monitor_socket);
}
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
}
static void net_gateway_monitor_run_task(void *pvParameters) {
char rx_buffer[1024];
char addr_str[128];
int addr_family = (int)pvParameters;
int ip_protocol = 0;
struct sockaddr_in dest_addr;
struct sockaddr_in server_addr;
while (1) {
dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(NET_GATEWAY_MONITOR_UDP_PORT);
ip_protocol = IPPROTO_IP;
server_addr.sin_addr.s_addr = htonl(NET_GATEWAY_MONITOR_SERVER_ADDR);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(NET_GATEWAY_MONITOR_SERVER_PORT);
net_gateway_monitor_socket = socket(addr_family, SOCK_DGRAM, ip_protocol);
if (net_gateway_monitor_socket < 0) {
ESP_LOGE(NET_GATEWAY_MONITOR_TAG,
"Unable to create socket: errno %d. sock: %d", errno,
net_gateway_monitor_socket);
break;
}
ESP_LOGI(NET_GATEWAY_MONITOR_TAG, "Socket created");
// Set timeout
struct timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
setsockopt(net_gateway_monitor_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout,
sizeof timeout);
int err = bind(net_gateway_monitor_socket, (struct sockaddr *)&dest_addr,
sizeof(dest_addr));
if (err < 0) {
ESP_LOGE(NET_GATEWAY_MONITOR_TAG,
"Socket unable to bind: errno %d. sock: %d", errno,
net_gateway_monitor_socket);
net_gateway_monitor_socket = -1;
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
// err = connect(net_gateway_monitor_socket, (struct sockaddr
// *)&server_addr,
// sizeof(server_addr));
if (err < 0) {
ESP_LOGE(NET_GATEWAY_MONITOR_TAG,
"Socket unable to connect: errno %d. sock: %d", errno,
net_gateway_monitor_socket);
net_gateway_monitor_socket = -1;
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
ESP_LOGI(NET_GATEWAY_MONITOR_TAG, "Socket bound, port %d",
NET_GATEWAY_MONITOR_UDP_PORT);
xTaskCreate(net_gateway_monitor_request_task, "net_gateway_monitor_request",
1024, NULL, 5, NULL);
while (1) {
uint8_t from_len = sizeof(server_addr);
// ESP_LOGI(NET_GATEWAY_MONITOR_TAG, "Waiting for data");
int len =
recv(net_gateway_monitor_socket, rx_buffer, sizeof(rx_buffer) - 1, 0);
// Error occurred during receiving
if (len < 0) {
if (errno == EAGAIN) {
continue;
}
ESP_LOGE(NET_GATEWAY_MONITOR_TAG, "recvfrom failed: errno %d. len: %d",
errno, len);
}
// Data received
else {
// Get the sender's ip address as string
inet_ntoa_r(server_addr.sin_addr, addr_str, sizeof(addr_str) - 1);
rx_buffer[len] = 0; // Null-terminate whatever we received and treat
// like a string...
gui_change_network_speed(
(uint64_t *)&rx_buffer[0], (uint64_t *)&rx_buffer[8],
(uint64_t *)&rx_buffer[16], (uint64_t *)&rx_buffer[24]);
}
if (net_gateway_monitor_socket != -1 && len < 0) {
ESP_LOGE(NET_GATEWAY_MONITOR_TAG,
"Shutting down socket and restarting...");
shutdown(net_gateway_monitor_socket, 0);
close(net_gateway_monitor_socket);
break;
}
}
}
vTaskDelete(NULL);
}
static void net_gateway_monitor_init() {
ESP_LOGI(NET_GATEWAY_MONITOR_TAG, "Start net gateway monitor");
xTaskCreate(net_gateway_monitor_run_task, "net_gateway_monitor_run", 4096,
(void *)AF_INET, 5, NULL);
}