#include #include "ql_type.h" #include "ql_data_call.h" #include "ql_rtos.h" #include "ql_application.h" #include #include "sockets.h" #include "ql_uart.h" #include "ql_func.h" #include "netdb.h" #include "ql_sim.h" #include "ql_dev.h" #include "ql_wtd.h" #include "ql_fs.h" #include "ql_fota.h" #include "ql_sim.h" #include "ql_ping.h" #include "ql_nw.h" #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" // 电量阈值定义 #define BAT_VOLTAGE_100 4200 #define BAT_VOLTAGE_90 4125 #define BAT_VOLTAGE_80 4050 #define BAT_VOLTAGE_70 3950 #define BAT_VOLTAGE_60 3850 #define BAT_VOLTAGE_50 3775 #define BAT_VOLTAGE_40 3700 #define BAT_VOLTAGE_30 3650 #define BAT_VOLTAGE_20 3600 #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 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; bat_level_t calc_battery_level(uint16_t voltage_mv) { if (voltage_mv >= BAT_VOLTAGE_90) return BAT_LEVEL_100; else if (voltage_mv >= BAT_VOLTAGE_80) return BAT_LEVEL_90; else if (voltage_mv >= BAT_VOLTAGE_70) return BAT_LEVEL_80; else if (voltage_mv >= BAT_VOLTAGE_60) return BAT_LEVEL_70; else if (voltage_mv >= BAT_VOLTAGE_50) return BAT_LEVEL_60; else if (voltage_mv >= BAT_VOLTAGE_40) return BAT_LEVEL_50; else if (voltage_mv >= BAT_VOLTAGE_30) return BAT_LEVEL_40; else if (voltage_mv >= BAT_VOLTAGE_20) return BAT_LEVEL_30; else if (voltage_mv >= BAT_VOLTAGE_10) return BAT_LEVEL_20; else if (voltage_mv >= BAT_VOLTAGE_MIN) return BAT_LEVEL_10; 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; } } /* 修改后的 battery_check:分阶段采样,采样窗口为6次 */ void battery_check(void) { static unsigned int call_count = 0; // 总调用计数 static unsigned char sample_idx = 0; // 当前已采集样本数 (0~5) static unsigned short sample_buf[6] = {0}; // 本地缓冲(也可用全局 vbat_sample,此处直接用局部) 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) { do_sample = 1; } } if (!do_sample) { return; // 本次不采样,直接返回 } /* ----- 执行一次ADC采样 ----- */ 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; sample_idx++; /* 当收集满6个样本时进行平均处理 */ if (sample_idx == 6) { unsigned short max = sample_buf[0], min = sample_buf[0]; unsigned short j = 0, vol_all = 0; for (j = 1; j < 6; j++) { if (max < sample_buf[j]) max = sample_buf[j]; if (min > sample_buf[j]) min = sample_buf[j]; } for (j = 0; j < 6; j++) { vol_all += sample_buf[j]; } 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); /* 计算电量等级并限制跳变 */ 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); 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; } sample_idx = 0; // 重置样本索引,准备下一轮采集 } /* 若未满6个,则本次只存储,不进行平均,直接返回 */ }