From 7376c02bbfa53b41fab1c49562917d678c24848f Mon Sep 17 00:00:00 2001 From: Alex Dolzhenkov Date: Sat, 29 Oct 2022 12:20:44 +1300 Subject: Add linear approximation and use it for improving battery percentage Add linear approximation class and use it to better model the non-linear discharge curve of the battery. Changed the minimum voltage level to 3.5V and the maximum to 4.18V. For reference the maximum observed voltage is 4.21V during charging. --- src/components/utility/LinearApproximation.h | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/components/utility/LinearApproximation.h (limited to 'src/components/utility/LinearApproximation.h') diff --git a/src/components/utility/LinearApproximation.h b/src/components/utility/LinearApproximation.h new file mode 100644 index 00000000..f7104ced --- /dev/null +++ b/src/components/utility/LinearApproximation.h @@ -0,0 +1,41 @@ +#pragma once + +#include +#include + +namespace Pinetime { + namespace Utility { + + // based on: https://github.com/SHristov92/LinearApproximation/blob/main/Linear.h + template class LinearApproximation { + using Point = struct { + Key key; + Value value; + }; + + public: + LinearApproximation(const std::array&& sorted_points) : points {sorted_points} { + } + + Value GetValue(Key key) const { + if (key <= points[0].key) { + return points[0].value; + } + + for (std::size_t i = 1; i < Size; i++) { + const auto& p = points[i]; + const auto& p_prev = points[i - 1]; + + if (key < p.key) { + return p_prev.value + (key - p_prev.key) * (p.value - p_prev.value) / (p.key - p_prev.key); + } + } + + return points[Size - 1].value; + } + + private: + std::array points; + }; + } +} -- cgit v1.2.3-70-g09d2