125 lines
3.1 KiB
C++
125 lines
3.1 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 "relay_ctrl.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"
|
||
|
||
typedef struct
|
||
{
|
||
char time_str[32];
|
||
char mac_str[20];
|
||
char uid_str[20];
|
||
float temp;
|
||
float humidity;
|
||
} env_data_t;
|
||
|
||
static env_data_t s_env_data;
|
||
static SemaphoreHandle_t s_env_data_lock = NULL;
|
||
|
||
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;
|
||
}
|
||
|
||
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(30));
|
||
}
|
||
}
|
||
|
||
extern "C" void app_main(void)
|
||
{
|
||
vTaskDelay(pdMS_TO_TICKS(100));
|
||
ESP_LOGI(TAG, "--- APP STARTING ---");
|
||
|
||
s_env_data_lock = xSemaphoreCreateMutex();
|
||
|
||
// 2. 初始化 Wi-Fi
|
||
ESP_ERROR_CHECK(wifi_connect_init());
|
||
|
||
// 3. 初始化显示屏和 LVGL
|
||
start_lvgl_demo();
|
||
|
||
// 4. 初始化 UI
|
||
lvgl_port_lock(100 / portTICK_PERIOD_MS);
|
||
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("开");
|
||
|
||
xTaskCreate(ui_task, "ui_task", 8192, NULL, 5, NULL);
|
||
|
||
if (wait_for_wifi_connected(pdMS_TO_TICKS(15000)))
|
||
{
|
||
ESP_LOGI(TAG, "IP: %s", wifi_connect_get_ip());
|
||
set_var_system_ip(wifi_connect_get_ip());
|
||
}
|
||
|
||
// 1. 初始化继电器 (避开 Octal PSRAM/Flash 所占用的引脚)
|
||
// ESP32-S3 N16R8 使用 8线 PSRAM,占用 GPIO 33-37
|
||
static const relay_config_t relay_cfg[RELAY_CTRL_ID_MAX] = {
|
||
{.pin = GPIO_NUM_38, .active_high = true}, // 示例改为常用 GPIO
|
||
{.pin = GPIO_NUM_39, .active_high = false},
|
||
{.pin = GPIO_NUM_40, .active_high = false},
|
||
};
|
||
ESP_ERROR_CHECK(relay_ctrl_init(relay_cfg));
|
||
}
|