Author SHA1 Message Date
chenjintao d24c8e186a feat: add local random timeout prompts 2026-09-21 10:48:15 +08:00
6 changed files with 246 additions and 130 deletions
@@ -5,6 +5,7 @@
#include "ql_iic.h"
#include "ql_gpio.h"
#include "app_inc.h"
#include "audio_prompt.h"
#include "ql_fs.h"
#define DEFAULT_VOLUME 6
@@ -14,7 +15,6 @@
u8 g_cur_volume = 0;
u8 g_pre_volume = 0;
extern const char *mp3_file[];
extern int g_file_valid;
@@ -19,11 +19,9 @@
#include "ql_atcmd.h"
#include "app_inc.h"
#include "ql_uart.h"
#include "ql_application.h"
#include "ql_log.h"
#include "ql_adc.h"
#include "ql_power.h"
#include "hal_api.h"
#include "ql_application.h"
#include "ql_log.h"
#include "hal_api.h"
// 电量阈值定义
#define BAT_VOLTAGE_100 4200
@@ -38,24 +36,25 @@
#define BAT_VOLTAGE_10 3550
#define BAT_VOLTAGE_MIN 3500
#define BAT_LOW_WARN_MV 3550
#define BAT_LOW_PROBE_EXIT_MV 3600
#define BAT_SHUTDOWN_MV 3500
#define BAT_VALID_MIN_MV 2500
#define BAT_VALID_MAX_MV 5000
#define BAT_SHUTDOWN_CONFIRM_COUNT 3
#define BAT_NORMAL_SAMPLE_CALLS 6
// ========== 新增:10分钟电压历史记录用于判断充放电状态 ==========
#define VOLTAGE_HISTORY_SIZE 60 // 10分钟 = 60个10秒采样点
#define VOLTAGE_RISE_THRESHOLD 15 // 10分钟内上升超过15mV判定为充电
#define VOLTAGE_DROP_THRESHOLD 15 // 10分钟内下降超过15mV判定为放电
#define VOLTAGE_STABLE_RANGE 10 // 变化在±10mV内判定为稳定
unsigned short g_vbat_vol = 0;
unsigned short vbat_sample[6] = {0}; // 修改:样本数改为6
unsigned char battery_first_update_flag = 0;
static bat_level_t g_bat_level_last = BAT_LEVEL_100;
static unsigned char g_bat_level_valid = 0;
static unsigned char g_low_voltage_count = 0;
static unsigned char g_low_probe_active = 0;
static unsigned char g_low_power_warned = 0;
static unsigned short g_vbat_vol_last = 0;
static bat_level_t g_bat_level_last = BAT_LEVEL_100;
// ========== 新增:电压历史记录环形缓冲区 ==========
static unsigned short g_voltage_history[VOLTAGE_HISTORY_SIZE] = {0};
static unsigned char g_history_index = 0;
static unsigned char g_history_count = 0; // 已记录的有效点数
static int g_charging_state = 0; // 当前判定的充电状态:1-充电, 0-未充电
static unsigned char g_charging_stable_cnt = 0; // 状态稳定计数
bat_level_t calc_battery_level(uint16_t voltage_mv)
{
@@ -72,64 +71,101 @@ bat_level_t calc_battery_level(uint16_t voltage_mv)
else return BAT_LEVEL_0;
}
bat_level_t battery_get_level(void)
{
return g_bat_level_valid ? g_bat_level_last : calc_battery_level(g_vbat_vol);
}
static bat_level_t limit_level_jump(bat_level_t raw_level)
{
bat_level_t result = g_bat_level_last;
if (!g_bat_level_valid) {
g_bat_level_valid = 1;
g_bat_level_last = raw_level;
printf("[BAT] first level initialized: %d\r\n", raw_level);
return raw_level;
}
if (raw_level > g_bat_level_last) {
result = (raw_level >= g_bat_level_last + BAT_LEVEL_10)
? (bat_level_t)(g_bat_level_last + BAT_LEVEL_10)
: raw_level;
} else if (raw_level < g_bat_level_last) {
result = (raw_level <= g_bat_level_last - BAT_LEVEL_10)
? (bat_level_t)(g_bat_level_last - BAT_LEVEL_10)
: raw_level;
}
printf("[BAT] level update: last=%d raw=%d result=%d\r\n",
g_bat_level_last, raw_level, result);
g_bat_level_last = result;
return result;
}
static void battery_check_critical_voltage(unsigned short voltage_mv)
{
if (voltage_mv < BAT_LOW_WARN_MV) {
g_low_probe_active = 1;
} else if (voltage_mv >= BAT_LOW_PROBE_EXIT_MV) {
g_low_probe_active = 0;
}
if (voltage_mv < BAT_SHUTDOWN_MV) {
if (g_low_voltage_count < BAT_SHUTDOWN_CONFIRM_COUNT) {
g_low_voltage_count++;
}
printf("[BAT] critical voltage:%d, confirm:%d/%d\r\n",
voltage_mv, g_low_voltage_count, BAT_SHUTDOWN_CONFIRM_COUNT);
if (g_low_voltage_count >= BAT_SHUTDOWN_CONFIRM_COUNT) {
printf("[BAT] critical voltage confirmed, poweroff now\r\n");
app_play_tips_file(15);
ql_rtos_task_sleep_s(5);
ql_power_down(1);
}
} else {
g_low_voltage_count = 0;
}
}
/**
* @brief 基于10分钟电压历史判断充放电状态
* @return 1-充电中, 0-未充电/放电
*/
static int detect_charging_by_trend(void)
{
if (g_history_count < VOLTAGE_HISTORY_SIZE) {
printf("[BAT] history not full (%d/%d), keep state:%d\r\n",
g_history_count, VOLTAGE_HISTORY_SIZE, g_charging_state);
return g_charging_state;
}
unsigned char old_index = (g_history_index + 1) % VOLTAGE_HISTORY_SIZE;
unsigned short vbat_now = g_voltage_history[(g_history_index + VOLTAGE_HISTORY_SIZE - 1) % VOLTAGE_HISTORY_SIZE];
unsigned short vbat_10min_ago = g_voltage_history[old_index];
short delta = (short)vbat_now - (short)vbat_10min_ago;
printf("[BAT] trend: now=%d, 10min_ago=%d, delta=%d\r\n",
vbat_now, vbat_10min_ago, delta);
int new_state = g_charging_state;
if (delta > VOLTAGE_RISE_THRESHOLD) {
new_state = 1;
printf("[BAT] trend: CHARGING detected (rise %dmV)\r\n", delta);
}
else if (delta < -VOLTAGE_DROP_THRESHOLD) {
new_state = 0;
printf("[BAT] trend: DISCHARGING detected (drop %dmV)\r\n", -delta);
}
else {
printf("[BAT] trend: STABLE (delta %dmV), keep state:%d\r\n", delta, g_charging_state);
}
if (new_state == g_charging_state) {
g_charging_stable_cnt = 0;
} else {
g_charging_stable_cnt++;
if (g_charging_stable_cnt < 3) {
printf("[BAT] state change pending: %d -> %d (%d/3)\r\n",
g_charging_state, new_state, g_charging_stable_cnt);
return g_charging_state;
}
g_charging_stable_cnt = 0;
printf("[BAT] state changed: %d -> %d (confirmed)\r\n", g_charging_state, new_state);
}
return new_state;
}
static void update_voltage_history(unsigned short voltage)
{
g_voltage_history[g_history_index] = voltage;
g_history_index = (g_history_index + 1) % VOLTAGE_HISTORY_SIZE;
if (g_history_count < VOLTAGE_HISTORY_SIZE) {
g_history_count++;
}
}
static int is_charging(void)
{
// 如有硬件检测引脚,可优先使用
return detect_charging_by_trend();
}
static bat_level_t limit_level_jump(bat_level_t raw_level, bat_level_t last_level, int charging)
{
bat_level_t result = last_level;
if (charging) {
if (raw_level > last_level) {
if (raw_level - last_level == 1) {
result = raw_level;
printf("[BAT] level up (charging): %d -> %d\r\n", last_level, raw_level);
} else if (raw_level - last_level > 1) {
result = (bat_level_t)(last_level + 1);
printf("[BAT] level up limited (charging): %d -> %d (raw:%d)\r\n",
last_level, result, raw_level);
}
}
} else {
if (raw_level < last_level) {
if (last_level - raw_level == 1) {
result = raw_level;
printf("[BAT] level down (discharging): %d -> %d\r\n", last_level, raw_level);
} else if (last_level - raw_level > 1) {
result = (bat_level_t)(last_level - 1);
printf("[BAT] level down limited (discharging): %d -> %d (raw:%d)\r\n",
last_level, result, raw_level);
}
}
}
return result;
}
/* 修改后的 battery_check:分阶段采样,采样窗口为6次 */
void battery_check(void)
@@ -138,17 +174,15 @@ void battery_check(void)
static unsigned char sample_idx = 0; // 当前已采集样本数 (0~5)
static unsigned short sample_buf[6] = {0}; // 本地缓冲(也可用全局 vbat_sample,此处直接用局部)
call_count++;
call_count++;
/* 判断本次是否执行ADC采样 */
unsigned char do_sample = 0;
if (call_count <= 24) { // 开机2分钟内 (24次*5s=120s)
do_sample = 1;
} else if (g_low_probe_active) {
do_sample = 1; // 低电风险时每5秒快速确认
} else {
// 2分钟后,每6次调用采样一次 (6*5s=30秒)
if (call_count % BAT_NORMAL_SAMPLE_CALLS == 0) {
if (call_count <= 24) { // 开机2分钟内 (24次*5s=120s)
do_sample = 1;
} else {
// 2分钟后,每36次调用采样一次 (36*5s=180s=3分钟)
if ((call_count - 25) % 36 == 0) {
do_sample = 1;
}
}
@@ -161,21 +195,9 @@ void battery_check(void)
u16 vbat_vol = 0;
ql_adc_onff(1);
ql_rtos_task_sleep_ms(5);
ql_adc_onff(0);
vbat_vol = ql_get_battery_vol();
printf("[BAT][SAMPLE] voltage=%d, low_probe=%d\r\n",
vbat_vol, g_low_probe_active);
if (vbat_vol < BAT_VALID_MIN_MV || vbat_vol > BAT_VALID_MAX_MV) {
printf("[BAT] invalid voltage sample ignored:%d\r\n", vbat_vol);
g_low_voltage_count = 0;
return;
}
battery_check_critical_voltage(vbat_vol);
sample_buf[sample_idx] = vbat_vol;
ql_adc_onff(0);
vbat_vol = ql_get_battery_vol();
sample_buf[sample_idx] = vbat_vol;
sample_idx++;
/* 当收集满6个样本时进行平均处理 */
@@ -193,29 +215,47 @@ void battery_check(void)
vol_all = vol_all - min - max;
vbat_vol = vol_all / 4; // 6个样本去掉最大最小,剩余4个平均
g_vbat_vol = vbat_vol;
printf("vbat_vol:%d\r\n", g_vbat_vol);
/* 更新电压历史(用于充电趋势判断) */
update_voltage_history(vbat_vol);
/* 计算电量等级并限制跳变 */
bat_level_t raw_level = calc_battery_level(g_vbat_vol);
bat_level_t level = limit_level_jump(raw_level);
printf("[BAT] raw_level:%d, limited_level:%d\r\n",
raw_level, level);
/* 判断充电状态 */
int charging = is_charging();
g_charging_state = charging;
/* 非充电状态下,电压不允许上升 */
if (!charging) {
if (g_vbat_vol_last > 0 && vbat_vol > g_vbat_vol_last) {
printf("[BAT] voltage rise blocked: %d -> %d (not charging)\r\n",
g_vbat_vol_last, vbat_vol);
vbat_vol = g_vbat_vol_last;
}
}
g_vbat_vol = vbat_vol;
g_vbat_vol_last = vbat_vol;
printf("vbat_vol:%d, charging:%d\r\n", g_vbat_vol, charging);
/* 计算电量等级并限制跳变 */
bat_level_t raw_level = calc_battery_level(g_vbat_vol);
bat_level_t level = limit_level_jump(raw_level, g_bat_level_last, charging);
g_bat_level_last = level;
printf("[BAT] raw_level:%d, limited_level:%d, charging:%d\r\n",
raw_level, level, charging);
if (battery_first_update_flag == 0) {
battery_first_update_flag = 1;
}
/* 极低电关机由原始电压独立处理,普通低电提示只播放一次 */
if (level <= BAT_LEVEL_10) {
if (!g_low_power_warned) {
g_low_power_warned = 1;
app_play_tips_file(14);
}
} else if (level >= BAT_LEVEL_20) {
g_low_power_warned = 0;
}
/* 低电量处理 */
if (level == BAT_LEVEL_0) {
printf("poweroff now\r\n");
app_play_tips_file(15);
ql_rtos_task_sleep_s(5);
ql_power_down(1);
} else if (level <= BAT_LEVEL_10) {
app_play_tips_file(14);
}
sample_idx = 0; // 重置样本索引,准备下一轮采集
}
@@ -14,7 +14,9 @@
#include "sockets.h"
#include "layer3.h"
#include "ql_uart.h"
const char *mp3_file[] ={
#include "audio_prompt.h"
const char *mp3_file[AUDIO_PROMPT_FILE_COUNT] ={
"test.mp3",// 0
"network_connect_zh.mp3",//1 系统启动中,正在等待网络连接
"network_check.mp3",//2 网络不好 请检查网络
@@ -31,9 +33,16 @@ const char *mp3_file[] ={
"device_wake_up_zh.mp3",//13 设备已唤醒
"low_power_zh.mp3",//14 电量低
"low_power_off_zh.mp3",//15 电量严重不足,马上关机
"volume_up_zh.mp3",//16
"volume_down_zh.mp3",//17
"volume_up_zh.mp3",//16
"volume_down_zh.mp3",//17
"ai_active_zh.mp3",//18
/* 19~24:对话响应超时随机提示音,文件名与资源服务器保持一致 */
"tts_error01.mp3",//19
"tts_error02.mp3",//20
"tts_error03.mp3",//21
"tts_error04.mp3",//22
"tts_error05.mp3",//23
"tts_error06.mp3",//24
};
//http://banban.api.talkingq.com:8080/assets/roles/banban/tts_error.mp3
@@ -183,7 +192,8 @@ void app_file_check_download(void)
char filename_buff[64]={0x00};
int ret = 0, cnt = 0;
for(index=1;index<19;index++){
/* 遍历完整资源表,新增提示音不存在时会在联网后自动补下载 */
for(index=1;index<AUDIO_PROMPT_FILE_COUNT;index++){
if(index==4||index==5||index==6||index==7||index==8){
continue;
@@ -208,7 +218,7 @@ void app_file_check_del(void)
int index = 1;
char filename_buff[64]={0x00};
for(index=1;index<19;index++){
for(index=1;index<AUDIO_PROMPT_FILE_COUNT;index++){
if(index==4||index==5||index==6||index==7||index==8){
continue;
@@ -0,0 +1,19 @@
#ifndef _AUDIO_PROMPT_H_
#define _AUDIO_PROMPT_H_
/*
* 本地提示音索引约定。
* 现有索引保持不变,新增的 6 条对话响应超时音频从 19 开始追加,
* 避免影响项目中已经使用的其他提示音索引。
*/
#define AUDIO_PROMPT_TTS_ERROR_INDEX 9
#define AUDIO_PROMPT_TTS_ERROR_FIRST_INDEX 19
#define AUDIO_PROMPT_TTS_ERROR_LAST_INDEX 24
#define AUDIO_PROMPT_FILE_COUNT 25
extern const char *mp3_file[AUDIO_PROMPT_FILE_COUNT];
int app_play_tips_file(int index);
int app_play_random_tts_error(void);
#endif
@@ -72,8 +72,7 @@ int g_nfc_bind_status = 0;
extern u8 g_cur_volume;
extern u8 g_pre_volume;
extern unsigned short g_vbat_vol;
extern bat_level_t battery_get_level(void);
extern unsigned short g_vbat_vol;
extern sleep_config_t g_sleep_cfg;
extern uint8_t g_sleep_config_valid;
@@ -369,7 +368,7 @@ int parse_event_response(const char *json_str, response_t *resp)
}
else {
// ===== 原有逻辑: NFC事件 - 动态查找 url_x 字段 =====
for (int i = 1; i <= 10; i++) {
for (int i = 1; i <= 5; i++) {
char key[16];
snprintf(key, sizeof(key), "url_%d", i);
cJSON *url = cJSON_GetObjectItem(params, key);
@@ -1478,7 +1477,7 @@ static void app_mqtt_task_event_thread(void *arvg)
app_heartbeat_time_record = hal_os_up_time_ms();
// 采集设备信息
info.power = battery_get_level(); // 使用与设备内部一致的最终电量档位
info.power = calc_battery_level(g_vbat_vol); // 10,20,30...100
info.voice = g_cur_volume; // 40,60,80,100
@@ -25,6 +25,7 @@
#include "ql_uart.h"
#include "hal_api.h"
#include "fvad.h"
#include "audio_prompt.h"
@@ -779,12 +780,16 @@ void sendRecordingAudioPacket(AudioPacket *packet) {
}
#endif
extern const char *mp3_file[];
int app_play_tips_file(int index)
{
int file_valid_check = 0;
/* 防止提示音索引越界访问文件名数组 */
if(index < 0 || index >= AUDIO_PROMPT_FILE_COUNT){
printf("invalid prompt index:%d\n", index);
return -3;
}
printf("xiaoice_play_index:%d\n", index);
//正在录音过程中不允许播放
@@ -820,6 +825,48 @@ int app_play_tips_file(int index)
return 0;
}
/*
* 随机播放一条对话响应超时提示音。
* 只把 LittleFS 中实际存在的文件加入候选列表,新增音频尚未下载完成时,
* 仍可回退播放原来的 tts_error.mp3,不会随机到不存在的文件。
*/
int app_play_random_tts_error(void)
{
static const int timeout_prompt_indices[] = {
AUDIO_PROMPT_TTS_ERROR_INDEX,
AUDIO_PROMPT_TTS_ERROR_FIRST_INDEX,
AUDIO_PROMPT_TTS_ERROR_FIRST_INDEX + 1,
AUDIO_PROMPT_TTS_ERROR_FIRST_INDEX + 2,
AUDIO_PROMPT_TTS_ERROR_FIRST_INDEX + 3,
AUDIO_PROMPT_TTS_ERROR_FIRST_INDEX + 4,
AUDIO_PROMPT_TTS_ERROR_LAST_INDEX
};
int valid_indices[sizeof(timeout_prompt_indices) / sizeof(timeout_prompt_indices[0])];
int valid_count = 0;
int selected_index;
int i;
char filename_buff[64] = {0x00};
for(i = 0; i < (int)(sizeof(timeout_prompt_indices) / sizeof(timeout_prompt_indices[0])); i++){
int prompt_index = timeout_prompt_indices[i];
sprintf(filename_buff, "U:/%s", mp3_file[prompt_index]);
if(ql_access(filename_buff, 0) == 0){ //检查filename_buff指向的文件是否存在
valid_indices[valid_count++] = prompt_index;
}
}
if(valid_count == 0){
printf("no local tts error prompt file\n");
return -2;
}
selected_index = valid_indices[hal_os_get_rand() % valid_count];
printf("random tts_error prompt index:%d, file:%s\n",
selected_index, mp3_file[selected_index]);
return app_play_tips_file(selected_index);
}
void app_player_thread(void *arvg)
{
ice_work_item *player_item=NULL;
@@ -1023,7 +1070,8 @@ static void check_connect_thread_entry(void *parameter)
printf("tts_error_set_speech1\n");
vad_set_speech_timeout_flag(1);
app_play_tips_file(9);
/* 对话响应超时后,从本地已有的 7 条提示音中随机播放一条 */
app_play_random_tts_error();
//talkingq_ws_send_text("REQUEST_PROMPT_SOUND:tts_error");
//改为播放本地音频 听不清 请再说一遍
}