按官方线性标准显示电量
This commit is contained in:
parent
fcd762c436
commit
e528569644
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user