};
-#define batteryservice_VOLTAGE_MAX (4.2f)
-#define batteryservice_VOLTAGE_MIN (3.3f)
-
+float battery_percent_map[][2]={
+ {4.136,100},
+ {4.086,95},
+ {4.031,90},
+ {3.977,85},
+ {3.936,80},
+ {3.883,75},
+ {3.836,70},
+ {3.804,65},
+ {3.771,60},
+ {3.74,55},
+ {3.713,50},
+ {3.693,45},
+ {3.672,40},
+ {3.654,35},
+ {3.639,30},
+ {3.62,25},
+ {3.596,20},
+ {3.571,15},
+ {3.534,10},
+ {3.487,5},
+ {3.38,0}
+};
+#define batteryservice_VOLTAGE_MAX (4.136f)
+#define batteryservice_VOLTAGE_MIN (3.38f)
static uint32_t g_adc_sample_times = 0;
static uint32_t g_adc_sample_discard = 0;
// osal_printk("battery ntc temp value1 %.4f value2 %.4f\r\n",ret,temp_value);
return temp_value;
}
+/**
+ * 输入电池电压值
+ * 根据 battery_percent_map 计算并返回 电池电量百分比
+ * 返回值 0~100
+*/
+static int battery_get_power_percentage(float voltage)
+{
+ int percent = 0;
+ int map_len = sizeof(battery_percent_map)/sizeof(battery_percent_map[0]);
+ float temp_value = 0.0f;
+ for (int i =0;i<map_len -1;i++)
+ {
+ //电压在前后两个之间
+ if (voltage <= battery_percent_map[i][0] && voltage > battery_percent_map[i+1][0] )
+ {
+ //电压差 前大后小
+ float r_div = ( battery_percent_map[i][0] - voltage)/(battery_percent_map[i][0] - battery_percent_map[i+1][0]);
+ //百分比 前大后小
+ temp_value = battery_percent_map[i][1] - (battery_percent_map[i][1] - battery_percent_map[i+1][1] ) * r_div ;
+
+ percent = (int)(temp_value + 0.5);
+
+ // printf("batterymap[%d]=%f,batterymap[%d]=%f, r_div = %f , temp_value = %f ,percent = %d\n",
+ // i,battery_percent_map[i][0],i+1,battery_percent_map[i+1][0],
+ // r_div,temp_value,percent
+ // );
+ break;
+ }
+ }
+
+ if (voltage <= batteryservice_VOLTAGE_MIN)
+ percent = 0;
+ if (voltage >= batteryservice_VOLTAGE_MAX)
+ percent = 100;
+
+ return percent;
+}
static bool adc_get_sample_result(float *voltage, uint8_t channel)
{
{
uint8_t percent = 0;
battery_info_t *current_battery_info = get_current_battery_info();
+
+ percent = battery_get_power_percentage(current_battery_info->battery_voltage);
*voltage = current_battery_info->battery_voltage;
- if (*voltage <= batteryservice_VOLTAGE_MIN)
- return 0;
- if (*voltage >= batteryservice_VOLTAGE_MAX)
- return 100;
- percent = (*voltage - batteryservice_VOLTAGE_MIN)*100/(batteryservice_VOLTAGE_MAX - batteryservice_VOLTAGE_MIN) ;
+
return percent;
}