diff --git a/src/utility.c b/src/utility.c index 502fb357..93524fca 100644 --- a/src/utility.c +++ b/src/utility.c @@ -30,7 +30,7 @@ int spline_battery_level(const int p[], const int v[], const size_t size, uint16 // if not last if (i < size - 1 && voltage >= v[i + 1]) { - percent = p[i + 1] + (voltage - v[i + 1]) / (v[i] - v[i + 1]) * (p[i] - p[i + 1]); + percent = p[i + 1] + (float)(voltage - v[i + 1]) / (float)(v[i] - v[i + 1]) * (p[i] - p[i + 1]); break; } } diff --git a/src/utility.h b/src/utility.h index 1ad214cc..676e92b6 100644 --- a/src/utility.h +++ b/src/utility.h @@ -23,6 +23,14 @@ unsigned int round_to_multiples(unsigned int number, unsigned int multiple); /** * @brief This function calculates the estimate batttery level in percent using splines. * + * Example: + * @code{.c} + * static const int battery_estimate_percentages[] = { 100, 50, 30, 20, 5, 0 }; + * static const int battery_estimate_voltages[] = { 4175, 3817, 3766, 3730, 3664, 3310 }; + * static size_t battery_estimate_size = sizeof(battery_estimate_percentages)/sizeof(battery_estimate_percentages[0]); + * int level = spline_battery_level(battery_estimate_percentages, battery_estimate_voltages, battery_estimate_size, voltage_read); + * @endcode + * * @param p percentage values to be associated with voltage values * @param v voltage values associated with percentage values * @param size number of percentage and voltage associations