【类 型】:fix

【原  因】:防止播放声音函数 频繁执行
【过  程】:记录时间戳 两次执行之间需要超过3秒
【影  响】:

# 类型 包含:
# feat:新功能(feature)
# fix:修补bug
# docs:文档(documentation)
# style: 格式(不影响代码运行的变动)
# refactor:重构(即不是新增功能,也不是修改bug的代码变动)
# test:增加测试
# chore:构建过程或辅助工具的变动
This commit is contained in:
air 2025-05-18 21:58:13 +08:00
parent 5fd6ca6eb9
commit 52c445f981
2 changed files with 24 additions and 15 deletions

View File

@ -1,6 +1,6 @@
#include "FoodDeliveryBase.h" #include "FoodDeliveryBase.h"
///MQTT和Mavlink业务逻辑控制 /// MQTT和Mavlink业务逻辑控制
/// @file FoodDeliveryBase.cpp /// @file FoodDeliveryBase.cpp
/** /**
* @description: * @description:
*/ */
@ -37,7 +37,7 @@ FoodCube::FoodCube(char *userSsid, char *userPassword, char *userMqttServer, int
case 2: case 2:
Serial2.begin(57600, SERIAL_8N1); Serial2.begin(57600, SERIAL_8N1);
// 设置接收缓冲区大小为1024字节 // 设置接收缓冲区大小为1024字节
Serial2.setRxBufferSize(2048); //飞控用 Serial2.setRxBufferSize(2048); // 飞控用
break; break;
} }
// 初始化声音模块串口 波特率 // 初始化声音模块串口 波特率
@ -273,6 +273,16 @@ void FoodCube::SetplayvolMax()
*/ */
void FoodCube::playText(String str, VoiceVolume vol) void FoodCube::playText(String str, VoiceVolume vol)
{ {
unsigned long currentTime = millis();
// 判断是否到了可以执行函数的时间
if (currentTime - lastRunTime > 3000)
{
lastRunTime = currentTime; // 更新上次执行时间
}
else
{
return; // 频繁为 达到3秒 不播放声音退出
}
// 拼接音量控制前缀,例如 "[v5]起飞" // 拼接音量控制前缀,例如 "[v5]起飞"
String vstr = "[v" + String((int)vol) + "]" + str; String vstr = "[v" + String((int)vol) + "]" + str;
@ -307,7 +317,7 @@ void FoodCube::playText(String str, VoiceVolume vol)
SWrite(command, sizeof(command), voiceSerial); SWrite(command, sizeof(command), voiceSerial);
// 延时等待模块处理,防止连续播放死机 // 延时等待模块处理,防止连续播放死机
//delay(300); // 可根据模块处理时间调节 // delay(300); // 可根据模块处理时间调节
} }
/** /**
@ -500,17 +510,15 @@ void FoodCube::mav_request_data()
* MAV_DATA_STREAM_ENUM_END=13, * MAV_DATA_STREAM_ENUM_END=13,
*/ */
// 根据从Pixhawk请求的所需信息进行设置 // 根据从Pixhawk请求的所需信息进行设置
//const int maxStreams = 1; // 遍历次数 (下面组合的长度) // const int maxStreams = 1; // 遍历次数 (下面组合的长度)
//const uint8_t MAVStreams[maxStreams] = {MAV_DATA_STREAM_ALL}; // 请求的数据流组合 放到一个对象 后面进行遍历 // const uint8_t MAVStreams[maxStreams] = {MAV_DATA_STREAM_ALL}; // 请求的数据流组合 放到一个对象 后面进行遍历
//const uint16_t MAVRates[maxStreams] = {0x01}; // 设定发送频率 分别对应上面数据流 ps:0X01 1赫兹 既每秒发送一次 // const uint16_t MAVRates[maxStreams] = {0x01}; // 设定发送频率 分别对应上面数据流 ps:0X01 1赫兹 既每秒发送一次
const int maxStreams = 3; const int maxStreams = 3;
const uint8_t MAVStreams[maxStreams] = { const uint8_t MAVStreams[maxStreams] = {
MAV_DATA_STREAM_EXTENDED_STATUS, MAV_DATA_STREAM_EXTENDED_STATUS,
MAV_DATA_STREAM_POSITION, MAV_DATA_STREAM_POSITION,
MAV_DATA_STREAM_EXTRA1 MAV_DATA_STREAM_EXTRA1};
}; const uint16_t MAVRates[maxStreams] = {1, 1, 1};
const uint16_t MAVRates[maxStreams] = {1,1,1};
for (int i = 0; i < maxStreams; i++) for (int i = 0; i < maxStreams; i++)
{ {

View File

@ -71,6 +71,7 @@ public:
V8 = 8, V8 = 8,
V9 = 9 V9 = 9
}; };
unsigned long lastRunTime = 0; // playText()上次运行时间戳
void playText(String str, VoiceVolume vol = V1); void playText(String str, VoiceVolume vol = V1);
void SetplayvolMax(); void SetplayvolMax();
uint8_t chekVoiceMcu(); uint8_t chekVoiceMcu();