132 lines
3.0 KiB
C++
132 lines
3.0 KiB
C++
#include <stdio.h>
|
|
#include <time.h>
|
|
#include <string.h>
|
|
|
|
#include "wifi-connect.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 系统环境数据结构体
|
|
*/
|
|
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();
|
|
lvgl_port_unlock();
|
|
vTaskDelay(pdMS_TO_TICKS(20));
|
|
}
|
|
}
|
|
|
|
extern "C" void app_main(void)
|
|
{
|
|
printf("\n\n--- APP START ---\n\n");
|
|
|
|
// 创建结构体互斥锁
|
|
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();
|
|
|
|
// 设置初始状态变量 (常量)
|
|
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");
|
|
}
|
|
}
|
|
}
|