界面实现,然后称重还有问题,灯改为手动控制

This commit is contained in:
Wang Beihong
2026-04-18 02:03:01 +08:00
parent 990abc210c
commit 98ea274985
22 changed files with 39154 additions and 73 deletions

View File

@@ -2,13 +2,18 @@
#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 "screens.h" // 包含屏幕ID定义
#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 void ui_task(void *arg); // 声明UI更新任务函数
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)
{
@@ -21,7 +26,36 @@ extern "C" void app_main(void)
lvgl_port_unlock();
// 创建一个 FreeRTOS 任务来更新 UI 显示(字符串拼接需要更多栈空间)
xTaskCreate(ui_task, "ui_task", 4096, NULL, 10, NULL);
ws2812_start_task(); // 启动 WS2812 LED 灯控制任务,大小为 4096 字节,优先级为 5
// 初始化称重传感器硬件。
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);
@@ -47,4 +81,52 @@ static void ui_task(void *arg)
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));
}
}