@@ -71,6 +71,7 @@ static adc_oneshot_unit_handle_t adc_handle = NULL;
7171float g_battery_voltage = 3.7f ;
7272int g_charging_status = 0 ;
7373int g_battery_percentage = 75 ;
74+ float g_usb_voltage = 0.0f ;
7475bool g_usb_connected = false;
7576
7677// 安全的LED操作函数
@@ -319,29 +320,76 @@ void test_adc_detection(led_strip_handle_t led_strip, adc_oneshot_unit_handle_t
319320 update_power_status (led_strip );
320321}
321322
323+ static int calculate_smooth_percentage (float voltage , bool is_charging )
324+ {
325+ // 定义查找表结构
326+ typedef struct { float v ; int p ; } point_t ;
327+
328+ // 充电曲线表 (电压从小到大)
329+ const point_t chrg_table [] = {
330+ {3.40f , 0 }, {3.61f , 25 }, {3.88f , 50 }, {4.12f , 75 }, {4.20f , 100 }
331+ };
332+ // 放电曲线表 (电压从小到大)
333+ const point_t dischrg_table [] = {
334+ {3.33f , 0 }, {3.55f , 25 }, {3.81f , 50 }, {4.07f , 75 }, {4.20f , 100 }
335+ };
336+
337+ const point_t * table = is_charging ? chrg_table : dischrg_table ;
338+ int count = 5 ;
339+
340+ if (voltage <= table [0 ].v ) return 0 ;
341+ if (voltage >= table [count - 1 ].v ) return 100 ;
342+
343+ // 线性插值
344+ for (int i = 0 ; i < count - 1 ; i ++ ) {
345+ if (voltage >= table [i ].v && voltage < table [i + 1 ].v ) {
346+ float range_v = table [i + 1 ].v - table [i ].v ;
347+ float range_p = table [i + 1 ].p - table [i ].p ;
348+ float offset = voltage - table [i ].v ;
349+ return (int )(table [i ].p + (offset / range_v ) * range_p );
350+ }
351+ }
352+ return 100 ;
353+ }
354+
322355void update_power_status (led_strip_handle_t led_strip )
323356{
324357 adc_result_t result = perform_adc_detection ();
325-
358+
326359 // 更新全局变量
327360 g_battery_voltage = result .battery_voltage ;
328- g_charging_status = (strcmp (result .charge_status , "Charging" ) == 0 ) ? 1
361+
362+ // 判定充电状态: 0=未充电, 1=充电中, 2=充满
363+ g_charging_status = (strcmp (result .charge_status , "Charging" ) == 0 ) ? 1
329364 : (strcmp (result .charge_status , "Fully Charged" ) == 0 ) ? 2
330- : 0 ;
365+ : 0 ;
366+ g_usb_voltage = result .vbus_voltage ;
367+
368+ g_usb_connected = result .usb_connected ;
369+
370+ // --- 优化后的电量计算逻辑开始 ---
331371 if (g_charging_status == 2 ) {
372+ // 硬件指示已充满,强制100%
332373 g_battery_percentage = 100 ;
333374 } else {
334- g_battery_percentage = (int )((g_battery_voltage - 3.0f ) / 1.2f * 100 );
375+ // 根据充电/放电状态选择不同的曲线
376+ bool is_charging_curve = (g_charging_status == 1 );
377+ g_battery_percentage = calculate_smooth_percentage (g_battery_voltage , is_charging_curve );
335378 }
336- g_usb_connected = result .usb_connected ;
379+ // --- 优化后的电量计算逻辑结束 ---
380+
381+ // 边界保护
337382 if (g_battery_percentage > 100 ) g_battery_percentage = 100 ;
338383 if (g_battery_percentage < 0 ) g_battery_percentage = 0 ;
339384
340385 // 更新LED状态
341386 if (led_strip != NULL ) {
342- // 打印检测结果
387+ // 打印检测结果,增加百分比显示
343388 ESP_LOGI (TAG , "=== ADC Detection Results ===" );
344- ESP_LOGI (TAG , "Battery Voltage: %.2fV %s" , result .battery_voltage , result .battery_low ? "(LOW!)" : "(OK)" );
389+ ESP_LOGI (TAG , "Battery: %.2fV [%d%%] %s" ,
390+ result .battery_voltage ,
391+ g_battery_percentage ,
392+ result .battery_low ? "(LOW!)" : "(OK)" );
345393 ESP_LOGI (TAG , "USB Voltage: %.2fV %s" , result .vbus_voltage ,
346394 result .usb_connected ? "(Connected)" : "(Disconnected)" );
347395 ESP_LOGI (TAG , "Charge Status: %s (%.2fV)" , result .charge_status , result .chrg_voltage );
0 commit comments