From e5285696440381b5ba79b9965567f603325e04fa Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 24 Jun 2026 10:01:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89=E5=AE=98=E6=96=B9=E7=BA=BF=E6=80=A7?= =?UTF-8?q?=E6=A0=87=E5=87=86=E6=98=BE=E7=A4=BA=E7=94=B5=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/battery_monitor.c | 52 ++++++------------------------------------ 1 file changed, 7 insertions(+), 45 deletions(-) diff --git a/main/battery_monitor.c b/main/battery_monitor.c index 5988979..20d1467 100644 --- a/main/battery_monitor.c +++ b/main/battery_monitor.c @@ -18,6 +18,8 @@ static const char *TAG = "battery"; #define BATTERY_ADC_CHANNEL ADC_CHANNEL_3 #define BATTERY_ADC_ATTEN ADC_ATTEN_DB_12 #define BATTERY_DIVIDER 3 +#define BATTERY_EMPTY_MV 2500 +#define BATTERY_FULL_MV 4200 static adc_oneshot_unit_handle_t s_adc_handle; static adc_cali_handle_t s_cali_handle; @@ -34,31 +36,6 @@ typedef struct { int samples; } battery_reading_t; -typedef struct { - int millivolts; - int percent; -} battery_curve_point_t; - -// ETA6098 stops at end-of-charge and restarts roughly 160 mV below 4.2 V, -// so the maintenance band should still read as a full battery. -static const battery_curve_point_t s_battery_curve[] = { - { 4200, 100 }, - { 4080, 100 }, - { 4050, 98 }, - { 4020, 95 }, - { 3980, 92 }, - { 3950, 88 }, - { 3910, 82 }, - { 3870, 75 }, - { 3830, 65 }, - { 3790, 52 }, - { 3750, 35 }, - { 3710, 20 }, - { 3650, 10 }, - { 3570, 5 }, - { 3300, 0 }, -}; - static int clamp_percent(int value) { if (value < 0) { @@ -72,30 +49,15 @@ static int clamp_percent(int value) static int battery_percent_from_mv(int mv) { - if (mv <= 0) { + if (mv <= BATTERY_EMPTY_MV) { return 0; } - - const int count = sizeof(s_battery_curve) / sizeof(s_battery_curve[0]); - if (mv >= s_battery_curve[0].millivolts) { - return s_battery_curve[0].percent; + if (mv >= BATTERY_FULL_MV) { + return 100; } - for (int i = 1; i < count; i++) { - const battery_curve_point_t high = s_battery_curve[i - 1]; - const battery_curve_point_t low = s_battery_curve[i]; - if (mv >= low.millivolts) { - int mv_range = high.millivolts - low.millivolts; - int percent_range = high.percent - low.percent; - int percent = low.percent; - if (mv_range > 0) { - percent += ((mv - low.millivolts) * percent_range + mv_range / 2) / mv_range; - } - return clamp_percent(percent); - } - } - - return s_battery_curve[count - 1].percent; + const int range_mv = BATTERY_FULL_MV - BATTERY_EMPTY_MV; + return clamp_percent(((mv - BATTERY_EMPTY_MV) * 100 + range_mv / 2) / range_mv); } static esp_err_t battery_adc_init(void)