Commit 4a910df1 authored by Stefan Haun's avatar Stefan Haun
Browse files

epic: calculate the battery capacity

parent f1818479
......@@ -62,6 +62,7 @@ typedef _Bool bool;
#define API_CHARGEIN_CURRENT 0x33
#define API_SYSTEM_VOLTAGE 0x34
#define API_THERMISTOR_VOLTAGE 0x35
#define API_BATTERY_CAPACITY 0x36
#define API_FILE_OPEN 0x40
#define API_FILE_CLOSE 0x41
......@@ -296,6 +297,13 @@ API(API_SYSTEM_VOLTAGE, int epic_read_system_voltage(float *result));
*/
API(API_THERMISTOR_VOLTAGE, int epic_read_thermistor_voltage(float *result));
/**
* Calculate the remaining battery capacity in percent.
*
* Uses a lookup table of 42 logarithmic voltage values to calculate
* the remaining capatity in percent.
*/
API(API_BATTERY_CAPACITY, int epic_read_battery_capacity(float *result));
/**
* UART/Serial Interface
......
......@@ -338,3 +338,46 @@ void vPmicTask(void *pvParameters)
}
}
}
/* Voltage values for 42 capcity values (logarithmic) */
const float battery_voltage_to_capacity[] = {
3.400, 3.505, 3.575, 3.628, 3.670, 3.705, 3.735, 3.761, 3.785,
3.806, 3.825, 3.843, 3.859, 3.874, 3.888, 3.901, 3.913, 3.925,
3.936, 3.947, 3.957, 3.966, 3.975, 3.984, 3.993, 4.001, 4.008,
4.016, 4.023, 4.030, 4.037, 4.044, 4.050, 4.056, 4.062, 4.068,
4.074, 4.079, 4.085, 4.090, 4.095, 4.100
};
/*
* Find the index for the given voltage
*
* TODO this can be done more efficient
*/
int _voltage_to_index(float voltage)
{
const int N = sizeof(battery_voltage_to_capacity) / sizeof(float);
for (int i = 0; i < N; i++) {
if (voltage < battery_voltage_to_capacity[i])
return i;
}
return N - 1;
}
int epic_read_battery_capacity(float *result)
{
float v;
int res = epic_read_battery_voltage(&v);
if (res < 0) {
return res;
}
int idx = _voltage_to_index(v);
const int N = sizeof(battery_voltage_to_capacity) / sizeof(float);
*result = idx * 100 / (N - 1);
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment