133 lines
4.1 KiB
C++
Executable File
133 lines
4.1 KiB
C++
Executable File
#include "freertos/FreeRTOS.h"
|
||
#include "freertos/task.h"
|
||
#include "wifi-connect.h"
|
||
#include "ws2812.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 "screens.h" // 包含屏幕ID定义
|
||
#include "sgm8031_use.h" // 引入SGM58031 ADC 硬件包装
|
||
#include "scale_controller.h" // 接入独立称重控制逻辑模块
|
||
#include "esp_log.h"
|
||
|
||
static const char *MAIN_TAG = "main";
|
||
static void scale_task(void *arg); // 声明称重读取任务函数
|
||
static void ui_task(void *arg); // 声明UI更新任务函数
|
||
|
||
extern "C" void app_main(void)
|
||
{
|
||
wifi_connect_init(); // 初始化 Wi-Fi 配网模块
|
||
|
||
start_lvgl_demo(); // 初始化 LVGL 和 LCD 显示
|
||
// 初始化 UI 组件(需在 LVGL 锁内进行对象创建)
|
||
lvgl_port_lock(0);
|
||
ui_init();
|
||
lvgl_port_unlock();
|
||
// 创建一个 FreeRTOS 任务来更新 UI 显示(字符串拼接需要更多栈空间)
|
||
xTaskCreate(ui_task, "ui_task", 4096, NULL, 10, NULL);
|
||
|
||
// 初始化称重传感器硬件。
|
||
if (sgm8031_use_init(0, 4, 5) == ESP_OK)
|
||
{
|
||
// 根据 20Kg 量程初步估算系数 (20000g / 844 ADC)
|
||
scale_config_t cfg = {.calibration_factor = 23.69f, .tare_offset = 0};
|
||
scale_controller_init(&cfg);
|
||
|
||
printf("Hardware init OK, starting scale task...\n");
|
||
|
||
// 开启一个后台任务专门读取重量数据
|
||
xTaskCreate(scale_task, "scale_task", 4096, NULL, 5, NULL);
|
||
}
|
||
else
|
||
{
|
||
printf("SGM58031 hardware init Failed!\n");
|
||
}
|
||
|
||
// 初始化 WS2812 LED 控制器并设置初始颜色为白色
|
||
if (ws2812_init() == ESP_OK)
|
||
{
|
||
ws2812_set_preset_color(WS2812_COLOR_WHITE);
|
||
|
||
vTaskDelay(pdMS_TO_TICKS(2000)); // 等待 2 秒后切换到绿色,表示系统已准备就绪
|
||
ws2812_off(); // 直接熄灭 LED,表示系统已准备就绪
|
||
}
|
||
else
|
||
{
|
||
printf("ws2812 init failed\n");
|
||
}
|
||
for (;;)
|
||
{
|
||
vTaskDelay(100000);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief UI 任务函数
|
||
*
|
||
* 负责定期驱动 UI 刷新,并实现屏幕自动轮播。
|
||
*
|
||
* @param arg 任务参数(未使用)
|
||
*/
|
||
static void ui_task(void *arg)
|
||
{
|
||
(void)arg;
|
||
|
||
for (;;)
|
||
{
|
||
|
||
lvgl_port_lock(0);
|
||
ui_tick();
|
||
lvgl_port_unlock();
|
||
vTaskDelay(pdMS_TO_TICKS(20));
|
||
}
|
||
}
|
||
/**
|
||
* @brief 称重传感器数据采集任务
|
||
*/
|
||
static void scale_task(void *arg)
|
||
{
|
||
(void)arg;
|
||
scale_data_t data;
|
||
int32_t tare_sum = 0;
|
||
const int tare_samples = 15;
|
||
const int discard_samples = 5;
|
||
|
||
// --- 在任务开始初进行异步去皮 ---
|
||
printf("【任务】正在去皮,请保持秤面空载...\n");
|
||
for (int i = 0; i < (tare_samples + discard_samples); i++)
|
||
{
|
||
if (sgm8031_use_read_data(&data) == ESP_OK)
|
||
{
|
||
if (i >= discard_samples)
|
||
{
|
||
tare_sum += (int32_t)data.weight;
|
||
}
|
||
}
|
||
vTaskDelay(pdMS_TO_TICKS(60));
|
||
}
|
||
scale_controller_set_tare(tare_sum / tare_samples);
|
||
printf("【任务】去皮完成!开始正常读取数据...\n");
|
||
|
||
for (;;)
|
||
{
|
||
if (sgm8031_use_read_data(&data) == ESP_OK)
|
||
{
|
||
// 通过 Controller 处理算法层面的数据
|
||
float final_weight = scale_controller_raw_to_weight((int16_t)data.weight);
|
||
|
||
// 限制为1位小数,避免UI刷新过快和末位跳动
|
||
final_weight = (float)((int)(final_weight * 10 + 0.5f)) / 10.0f;
|
||
|
||
printf("【称重数据】原始ADC: %6d, 最终重量: %8.2f g\n", (int)data.weight, final_weight);
|
||
set_var_weigt_ui(final_weight); // 将读取到的重量数据同步给 UI
|
||
}
|
||
else
|
||
{
|
||
ESP_LOGW(MAIN_TAG, "读取传感器失败");
|
||
}
|
||
|
||
vTaskDelay(pdMS_TO_TICKS(500));
|
||
}
|
||
}
|