77namespace Pinetime {
88 namespace Controllers {
99
10- /* * A simple circular buffer that can be used to average
11- out the sensor values. The total capacity of the CircBuffer
12- is given as the template parameter N.
13- */
14- template <int N> class CircBuffer {
15- public:
16- CircBuffer () : arr {}, sz {}, cap {N}, head {} {
17- }
18- /* *
19- insert member function overwrites the next data to the current
20- HEAD and moves the HEAD to the newly inserted value.
21- */
22- void Insert (const uint8_t num) {
23- head %= cap;
24- arr[head++] = num;
25- if (sz != cap) {
26- sz++;
27- }
28- }
29-
30- uint8_t GetAverage () const {
31- int sum = std::accumulate (arr.begin (), arr.end (), 0 );
32- return static_cast <uint8_t >(sum / sz);
33- }
34-
35- private:
36- std::array<uint8_t , N> arr; /* *< internal array used to store the values*/
37- uint8_t sz; /* *< The current size of the array.*/
38- uint8_t cap; /* *< Total capacity of the CircBuffer.*/
39- uint8_t head; /* *< The current head of the CircBuffer*/
40- };
41-
4210 class Battery {
4311 public:
4412 Battery ();
@@ -47,10 +15,7 @@ namespace Pinetime {
4715 void Update ();
4816
4917 uint8_t PercentRemaining () const {
50- auto avg = percentRemainingBuffer.GetAverage ();
51- avg = std::min (avg, static_cast <uint8_t >(100 ));
52- avg = std::max (avg, static_cast <uint8_t >(0 ));
53- return avg;
18+ return percentRemaining;
5419 }
5520
5621 uint16_t Voltage () const {
@@ -69,14 +34,11 @@ namespace Pinetime {
6934 static Battery* instance;
7035 nrf_saadc_value_t saadc_value;
7136
72- static constexpr uint8_t percentRemainingSamples = 5 ;
73- CircBuffer<percentRemainingSamples> percentRemainingBuffer {};
74-
7537 static constexpr uint32_t chargingPin = 12 ;
7638 static constexpr uint32_t powerPresentPin = 19 ;
7739 static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7;
7840 uint16_t voltage = 0 ;
79- int percentRemaining = - 1 ;
41+ uint8_t percentRemaining = 0 ;
8042
8143 bool isCharging = false ;
8244 bool isPowerPresent = false ;
@@ -87,7 +49,6 @@ namespace Pinetime {
8749 static void AdcCallbackStatic (nrfx_saadc_evt_t const * event);
8850
8951 bool isReading = false ;
90- uint8_t samples = 0 ;
9152 };
9253 }
9354}
0 commit comments