fix(ui): step the battery percentage by 5 so it cannot cliff

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
yuquanjun 2026-09-27 18:15:03 +08:00
parent 3220602f89
commit 58e8b3dbc7
1 changed files with 23 additions and 1 deletions

View File

@ -358,6 +358,7 @@ static void app_adc_volume_process(void)
static uint16_t s_vbat_raw = 0;
static uint8_t s_vbat_valid = 0;
static uint8_t s_charge_ui_allowed = 0;
static uint8_t s_pct_shown_valid = 0;
/* 高于 6.8V 才允许点亮关机充电页;未采样视为不允许 */
uint8_t app_adc_charge_ui_allowed(void)
@ -396,6 +397,7 @@ void app_adc_init(void)
s_vbat_raw = 0;
s_vbat_valid = 0;
s_charge_ui_allowed = 0;
s_pct_shown_valid = 0;
}
/* 采样:读 ADC + 滑动平均 */
@ -409,6 +411,25 @@ static void app_adc_battery_do_sample(void)
bat_index = (bat_index + 1) % BAT_SAMPLE_COUNT;
}
/* 显示电量:四舍五入到 5%,之后每次最多升/降 5,避免一下掉一截。关机仍看原始电压。 */
static uint8_t bat_display_percent(uint8_t raw_pct)
{
uint8_t q = (uint8_t)((((uint16_t)raw_pct + 2u) / 5u) * 5u);
if (q > 100u)
q = 100u;
if (!s_pct_shown_valid)
{
s_pct_shown_valid = 1u;
return q;
}
if ((uint16_t)q + 5u < percentage)
return (uint8_t)(percentage - 5u);
if (q > (uint8_t)(percentage + 5u))
return (uint8_t)(percentage + 5u);
return q;
}
/* 上报:映射成百分比,变化才发消息;低于等于 6.8V 触发低电量关机(插着 Type-C 也执行) */
static void app_adc_battery_report(void)
{
@ -437,6 +458,7 @@ static void app_adc_battery_report(void)
MainTask_Sendmsg(MSG_ID_LOW_BAT_TIP, pct, filtered, 0);
}
pct = bat_display_percent(pct);
pct_changed = (pct != percentage) ? 1u : 0u;
allow_changed = (allow != s_charge_ui_allowed) ? 1u : 0u;
s_charge_ui_allowed = allow;
@ -445,7 +467,7 @@ static void app_adc_battery_report(void)
if (usb_charging_state && !powon)
{
/* 跨过 6.8V 也要通知,否则百分比不变时充电页不会亮或熄 */
/* 跨过 6.8V 时通知界面:高于则一直亮,否则 10s 后关屏 */
if (pct_changed || allow_changed)
MainTask_Sendmsg(MSG_ID_POWOFF_CHARG, percentage, usb_charging_state, 0);
}