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.
This commit is contained in:
Alex Dolzhenkov
2022-10-29 12:20:44 +13:00
committed by JF
parent a67f401b30
commit 7376c02bbf
3 changed files with 51 additions and 36 deletions
@@ -0,0 +1,41 @@
#pragma once
#include <cstddef>
#include <array>
namespace Pinetime {
namespace Utility {
// based on: https://github.com/SHristov92/LinearApproximation/blob/main/Linear.h
template <typename Key, typename Value, std::size_t Size> class LinearApproximation {
using Point = struct {
Key key;
Value value;
};
public:
LinearApproximation(const std::array<Point, Size>&& 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<Point, Size> points;
};
}
}