更新UI。增加时间和IP显示
This commit is contained in:
121
main/main.cpp
121
main/main.cpp
@@ -1,24 +1,84 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "wifi-connect.h"
|
||||
#include "esp_lvgl_port.h" // 包含 LVGL 端口头文件,提供 LVGL 相关的类型定义和函数声明
|
||||
#include "lvgl_st7789_use.h" // 使用EEZStudio提供的LVGL和ST7789驱动,便于后续扩展
|
||||
#include "ui.h" // 使用EEZStudio提供的ui组件,便于后续扩展
|
||||
#include "vars.h" // 包含全局变量定义和访问函数
|
||||
#include "esp_lvgl_port.h"
|
||||
#include "lvgl_st7789_use.h"
|
||||
#include "ui.h"
|
||||
#include "vars.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "sntp_time.h"
|
||||
#include "esp_mac.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_efuse.h"
|
||||
#include "esp_efuse_table.h"
|
||||
|
||||
#define TAG "MAIN"
|
||||
|
||||
/**
|
||||
* @brief UI 任务函数
|
||||
*
|
||||
* 负责定期驱动 UI 刷新,并实现屏幕自动轮播。
|
||||
*
|
||||
* @param arg 任务参数(未使用)
|
||||
* @brief 系统环境数据结构体
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
char time_str[32];
|
||||
float temp;
|
||||
float humidity;
|
||||
} env_data_t;
|
||||
|
||||
// 定义全局变量和锁
|
||||
static env_data_t s_env_data;
|
||||
static SemaphoreHandle_t s_env_data_lock = NULL;
|
||||
|
||||
// 等待 Wi-Fi 连接成功
|
||||
static bool wait_for_wifi_connected(TickType_t timeout_ticks)
|
||||
{
|
||||
const TickType_t start_ticks = xTaskGetTickCount();
|
||||
while ((xTaskGetTickCount() - start_ticks) < timeout_ticks)
|
||||
{
|
||||
if (wifi_connect_get_status() == WIFI_CONNECT_STATUS_CONNECTED)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(200));
|
||||
}
|
||||
return wifi_connect_get_status() == WIFI_CONNECT_STATUS_CONNECTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 更新系统状态到结构体,并同步到 UI
|
||||
*/
|
||||
static void env_data_update_system_info(void)
|
||||
{
|
||||
if (s_env_data_lock == NULL)
|
||||
return;
|
||||
|
||||
// 获取当前时间
|
||||
time_t now;
|
||||
struct tm timeinfo;
|
||||
time(&now);
|
||||
localtime_r(&now, &timeinfo);
|
||||
char time_buf[32];
|
||||
strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S", &timeinfo);
|
||||
|
||||
// 更新结构体 (线程安全)
|
||||
xSemaphoreTake(s_env_data_lock, portMAX_DELAY);
|
||||
strncpy(s_env_data.time_str, time_buf, sizeof(s_env_data.time_str));
|
||||
|
||||
xSemaphoreGive(s_env_data_lock);
|
||||
set_var_local_time(s_env_data.time_str);
|
||||
}
|
||||
|
||||
static void ui_task(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
env_data_update_system_info();
|
||||
|
||||
lvgl_port_lock(0);
|
||||
ui_tick();
|
||||
@@ -29,16 +89,43 @@ static void ui_task(void *arg)
|
||||
|
||||
extern "C" void app_main(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(wifi_connect_init()); // 初始化 Wi-Fi 配网模块
|
||||
printf("\n\n--- APP START ---\n\n");
|
||||
|
||||
start_lvgl_demo(); // 初始化 LVGL 和 LCD 显示
|
||||
// 初始化 UI 组件(需在 LVGL 锁内进行对象创建)
|
||||
// 创建结构体互斥锁
|
||||
s_env_data_lock = xSemaphoreCreateMutex();
|
||||
|
||||
// 初始化 Wi-Fi
|
||||
ESP_ERROR_CHECK(wifi_connect_init());
|
||||
|
||||
// 初始化屏幕和 LVGL
|
||||
start_lvgl_demo();
|
||||
|
||||
// 初始化 UI
|
||||
lvgl_port_lock(0);
|
||||
ui_init();
|
||||
lvgl_port_unlock();
|
||||
// 创建一个 FreeRTOS 任务来更新 UI 显示(字符串拼接需要更多栈空间)
|
||||
|
||||
// 设置初始状态变量 (常量)
|
||||
set_var_door_status("关闭");
|
||||
set_var_food_status("良好");
|
||||
set_var_hum_status("有人");
|
||||
set_var_hot_status("开");
|
||||
set_var_cool_status("关");
|
||||
set_var_fan_status("开");
|
||||
set_var_light_status("开");
|
||||
|
||||
// 创建 UI 刷新任务
|
||||
xTaskCreate(ui_task, "ui_task", 4096, NULL, 10, NULL);
|
||||
|
||||
|
||||
|
||||
}
|
||||
// 等待网络并对时
|
||||
if (wait_for_wifi_connected(pdMS_TO_TICKS(60000)))
|
||||
{
|
||||
printf("Wi-Fi connected, IP address: %s\n", wifi_connect_get_ip());
|
||||
set_var_system_ip(wifi_connect_get_ip());
|
||||
esp_err_t sntp_ret = sntp_timp_sync_time(12000);
|
||||
if (sntp_ret != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "SNTP sync failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user