添加 SU-03T 语音命令解析与分发功能,更新主流程以支持语音命令处理

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
Wang Beihong
2026-04-23 21:58:56 +08:00
parent 80de6eb366
commit 5916428fe4
4 changed files with 250 additions and 20 deletions

View File

@@ -38,6 +38,7 @@
#include "fire_sensor.h"
#include "hx711.hpp"
#include "su-03t.h"
#include "su-03t_voice.h"
#include "agri_env.h"
#include "cJSON.h"
@@ -406,6 +407,18 @@ static void sntp_task(void *arg)
vTaskDelete(NULL);
}
static void su03t_reply_double_value(uint8_t reply_msgno, double value, const char *label)
{
esp_err_t ret = su03t_voice_send_double_reply(reply_msgno, value);
if (ret != ESP_OK)
{
ESP_LOGW(TAG, "SU03T TX %s failed: %s", label, esp_err_to_name(ret));
return;
}
ESP_LOGI(TAG, "SU03T TX %s msgno=0x%02X value=%.3f", label, reply_msgno, value);
}
/* 函数: su03t_rx_callback
* 作用: 执行模块内与函数名对应的业务逻辑。
* 重点: 关注输入合法性、返回码与并发安全。
@@ -418,26 +431,79 @@ static void su03t_rx_callback(const su03t_frame_t *frame, void *user_ctx)
return;
}
char hex_buf[256];
size_t pos = 0;
for (size_t i = 0; i < frame->params_len && pos + 4 < sizeof(hex_buf); ++i)
const su03t_voice_command_t *cmd = su03t_voice_command_from_msgno(frame->msgno);
if (cmd != NULL)
{
int n = snprintf(&hex_buf[pos], sizeof(hex_buf) - pos, "%02X ", frame->params[i]);
if (n <= 0)
{
break;
}
pos += (size_t)n;
}
if (pos == 0)
{
snprintf(hex_buf, sizeof(hex_buf), "(no params)");
}
ESP_LOGI(TAG, "SU03T RX msgno=0x%02X cmd=%s len=%u",
frame->msgno,
cmd->key,
(unsigned)frame->params_len);
ESP_LOGI(TAG, "SU03T RX msgno=0x%02X len=%u params=%s",
frame->msgno,
(unsigned)frame->params_len,
hex_buf);
double reply_value = 0.0;
uint8_t reply_msgno = 0;
const char *reply_label = NULL;
bool need_reply = false;
if (s_env_data_lock)
{
xSemaphoreTake(s_env_data_lock, portMAX_DELAY);
switch (cmd->id)
{
case SU03T_VOICE_CMD_ASK_TEMP:
reply_msgno = 0x01;
reply_value = (double)s_env_data.temp;
reply_label = "temp";
need_reply = true;
break;
case SU03T_VOICE_CMD_ASK_HUMI:
reply_msgno = 0x02;
reply_value = (double)s_env_data.humidity;
reply_label = "humidity";
need_reply = true;
break;
case SU03T_VOICE_CMD_ASK_LUX:
reply_msgno = 0x03;
reply_value = (double)s_env_data.lux;
reply_label = "lux";
need_reply = true;
break;
case SU03T_VOICE_CMD_ASK_MQ2:
reply_msgno = 0x04;
reply_value = (double)s_env_data.gas_percent;
reply_label = "air_quality";
need_reply = true;
break;
case SU03T_VOICE_CMD_ASK_CO2:
reply_msgno = 0x05;
reply_value = (double)s_env_data.co2;
reply_label = "co2";
need_reply = true;
break;
case SU03T_VOICE_CMD_ASK_WEIGHT:
reply_msgno = 0x06;
reply_value = (double)s_env_data.ice_weight;
reply_label = "weight";
need_reply = true;
break;
default:
break;
}
xSemaphoreGive(s_env_data_lock);
}
if (need_reply)
{
su03t_reply_double_value(reply_msgno, reply_value, reply_label);
}
su03t_voice_handle_frame(frame);
}
else
{
ESP_LOGI(TAG, "SU03T RX msgno=0x%02X cmd=unknown len=%u",
frame->msgno,
(unsigned)frame->params_len);
}
if (s_env_data_lock)
{