From 1ab4d0c16f5aa24f29758d37aa9950568d4f9528 Mon Sep 17 00:00:00 2001 From: jpk Date: Mon, 15 Aug 2022 16:47:53 +0200 Subject: [PATCH 1/3] Move calculation of battery level for discharging battery to utility function --- src/utility.c | 19 +++++++++++++++++++ src/utility.h | 13 +++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/utility.c b/src/utility.c index 9a6b6511..e9449472 100644 --- a/src/utility.c +++ b/src/utility.c @@ -1,6 +1,25 @@ +#include +#include + #include "utility.h" int map(int x, int in_min, int in_max, int out_min, int out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } + +float poly_battery_level(const double terms[], const size_t numterms, uint16_t voltage) +{ + double t = 1; + double percent = 0; + for (int i = 0; i < numterms; i++) { + percent += terms[i] * t; + t *= voltage; + } + + if (percent > 100) + percent = 100; + if (percent < 0) + percent = 0; + return percent; +} diff --git a/src/utility.h b/src/utility.h index bd6e1538..45778ecd 100644 --- a/src/utility.h +++ b/src/utility.h @@ -8,3 +8,16 @@ * @return the mapped value */ int map(int x, int in_min, int in_max, int out_min, int out_max); + +/** + * @brief This function calculates the estimate batttery level in percent. + * + * To find the terms representing the polynominal discarge curve of the + * battery an solver like https://arachnoid.com/polysolve/ can be used. + * + * @param array polynominal terms for the battery discharge curve + * @param number of terms + * @param voltage readings + * @return battery level in percent + */ +float poly_battery_level(const double terms[], const size_t numterms, uint16_t voltage); From 1acc1806084c372d4ffaddf916c402ec133b8355 Mon Sep 17 00:00:00 2001 From: jpk Date: Mon, 15 Aug 2022 16:48:42 +0200 Subject: [PATCH 2/3] Refactored battery calculation to use the 'utility function' --- src/devices/logitech_gpro.c | 60 +++++++++++-------------------------- 1 file changed, 17 insertions(+), 43 deletions(-) diff --git a/src/devices/logitech_gpro.c b/src/devices/logitech_gpro.c index d5a4db2a..be24b73b 100644 --- a/src/devices/logitech_gpro.c +++ b/src/devices/logitech_gpro.c @@ -1,11 +1,13 @@ -#include "../device.h" -#include "../utility.h" -#include "logitech.h" - #include #include #include +#include + +#include "../device.h" +#include "../utility.h" +#include "logitech.h" + static struct device device_gpro; @@ -19,6 +21,16 @@ static const uint16_t PRODUCT_IDS[] = { ID_LOGITECH_PRO_X_1, }; +static const double battery_estimate_terms[] = { + -1.7790085824253613e+006, + 2.3692153307344888e+003, + -1.2580905041506840e+000, + 3.3295201187388395e-004, + -4.3913981733597497e-008, + 2.3093121045539907e-012 +}; +static const size_t num_terms = 6; + static int gpro_send_sidetone(hid_device* device_handle, uint8_t num); static int gpro_request_battery(hid_device* device_handle); static int gpro_send_inactive_time(hid_device* device_handle, uint8_t num); @@ -52,44 +64,6 @@ static int gpro_send_sidetone(hid_device* device_handle, uint8_t num) return hid_write(device_handle, sidetone_data, sizeof(sidetone_data) / sizeof(sidetone_data[0])); } -/** - * @brief This function calculates the estimate batttery level in percent. - * - * Battery stats: - * - Voltage capacity: 4200 (100%) - * - Charging voltage: 4700 - * - Power off voltage: 3300 ( 0%) - * - * @param voltage readings - * @return battery level in percent - */ -static float estimate_battery_level(uint16_t voltage) -{ - double terms[] = { - -1.7790085824253613e+006, - 2.3692153307344888e+003, - -1.2580905041506840e+000, - 3.3295201187388395e-004, - -4.3913981733597497e-008, - 2.3093121045539907e-012 - }; - - size_t csz = sizeof terms / sizeof *terms; - - double t = 1; - double percent = 0; - for (int i = 0; i < csz; i++) { - percent += terms[i] * t; - t *= voltage; - } - - if (percent > 100) - percent = 100; - if (percent < 0) - percent = 0; - return percent; -} - static int gpro_request_battery(hid_device* device_handle) { /* @@ -121,7 +95,7 @@ static int gpro_request_battery(hid_device* device_handle) return HSC_ERROR; const uint16_t voltage = (buf[4] << 8) | buf[5]; - return (int)(roundf(estimate_battery_level(voltage))); + return (int)(roundf(poly_battery_level(battery_estimate_terms, num_terms, voltage))); } static int gpro_send_inactive_time(hid_device* device_handle, uint8_t num) From dd9764394a6fec7f62532bad33150c9baeb6ef1b Mon Sep 17 00:00:00 2001 From: jpk Date: Mon, 15 Aug 2022 17:13:12 +0200 Subject: [PATCH 3/3] Applied formatting styles --- src/devices/logitech_gpro.c | 3 +-- src/utility.c | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/devices/logitech_gpro.c b/src/devices/logitech_gpro.c index be24b73b..b3852f43 100644 --- a/src/devices/logitech_gpro.c +++ b/src/devices/logitech_gpro.c @@ -1,14 +1,13 @@ #include #include -#include #include +#include #include "../device.h" #include "../utility.h" #include "logitech.h" - static struct device device_gpro; #define ID_LOGITECH_PRO 0x0aa7 diff --git a/src/utility.c b/src/utility.c index e9449472..d8a8bab7 100644 --- a/src/utility.c +++ b/src/utility.c @@ -1,5 +1,5 @@ -#include #include +#include #include "utility.h"