Skip to content

Commit be48f52

Browse files
authored
Merge pull request #168 from Panky-codes/fix-erratic-battery
Fix erratic battery
2 parents 6d85139 + b31c0e7 commit be48f52

5 files changed

Lines changed: 49 additions & 12 deletions

File tree

src/components/battery/BatteryController.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ void Battery::Update() {
3434

3535
// see https://forum.pine64.org/showthread.php?tid=8147
3636
voltage = (value * 2.0f) / (1024/3.0f);
37-
percentRemaining = ((voltage - 3.55f)*100.0f)*3.9f;
38-
percentRemaining = std::max(percentRemaining, 0.0f);
39-
percentRemaining = std::min(percentRemaining, 100.0f);
37+
int percentRemaining = ((voltage - 3.55f)*100.0f)*3.9f;
38+
percentRemaining = std::max(percentRemaining, 0);
39+
percentRemaining = std::min(percentRemaining, 100);
40+
41+
percentRemainingBuffer.insert(percentRemaining);
4042

4143
// NRF_LOG_INFO("BATTERY " NRF_LOG_FLOAT_MARKER " %% - " NRF_LOG_FLOAT_MARKER " v", NRF_LOG_FLOAT(percentRemaining), NRF_LOG_FLOAT(voltage));
4244
// NRF_LOG_INFO("POWER Charging : %d - Power : %d", isCharging, isPowerPresent);

src/components/battery/BatteryController.h

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,48 @@
11
#pragma once
22
#include <cstdint>
33
#include <drivers/include/nrfx_saadc.h>
4+
#include <array>
5+
#include <numeric>
46

57
namespace Pinetime {
68
namespace Controllers {
9+
/** A simple circular buffer that can be used to average
10+
out the sensor values. The total capacity of the CircBuffer
11+
is given as the template parameter N.
12+
*/
13+
template <int N>
14+
class CircBuffer {
15+
public:
16+
CircBuffer() : arr{}, sz{}, cap{N}, head{} {}
17+
/**
18+
insert member function overwrites the next data to the current
19+
HEAD and moves the HEAD to the newly inserted value.
20+
*/
21+
void insert(const int num) {
22+
head %= cap;
23+
arr[head++] = num;
24+
if (sz != cap) {
25+
sz++;
26+
}
27+
}
28+
29+
int GetAverage() const {
30+
int sum = std::accumulate(arr.begin(), arr.end(), 0);
31+
return (sum / sz);
32+
}
33+
34+
private:
35+
std::array<int, N> arr; /**< internal array used to store the values*/
36+
uint8_t sz; /**< The current size of the array.*/
37+
uint8_t cap; /**< Total capacity of the CircBuffer.*/
38+
uint8_t head; /**< The current head of the CircBuffer*/
39+
};
40+
741
class Battery {
842
public:
943
void Init();
1044
void Update();
11-
float PercentRemaining() const { return percentRemaining; }
45+
int PercentRemaining() const { return percentRemainingBuffer.GetAverage(); }
1246
float Voltage() const { return voltage; }
1347
bool IsCharging() const { return isCharging; }
1448
bool IsPowerPresent() const { return isPowerPresent; }
@@ -17,8 +51,9 @@ namespace Pinetime {
1751
static constexpr uint32_t chargingPin = 12;
1852
static constexpr uint32_t powerPresentPin = 19;
1953
static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7;
54+
static constexpr uint8_t percentRemainingSamples = 10;
2055
static void SaadcEventHandler(nrfx_saadc_evt_t const * p_event);
21-
float percentRemaining = 0.0f;
56+
CircBuffer<percentRemainingSamples> percentRemainingBuffer {};
2257
float voltage = 0.0f;
2358
bool isCharging = false;
2459
bool isPowerPresent = false;

src/displayapp/screens/BatteryIcon.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
using namespace Pinetime::Applications::Screens;
55

6-
const char* BatteryIcon::GetBatteryIcon(float batteryPercent) {
7-
if(batteryPercent > 90.0f) return Symbols::batteryFull;
8-
if(batteryPercent > 75.0f) return Symbols::batteryThreeQuarter;
9-
if(batteryPercent > 50.0f) return Symbols::batteryHalf;
10-
if(batteryPercent > 25.0f) return Symbols::batteryOneQuarter;
6+
const char* BatteryIcon::GetBatteryIcon(int batteryPercent) {
7+
if(batteryPercent > 90) return Symbols::batteryFull;
8+
if(batteryPercent > 75) return Symbols::batteryThreeQuarter;
9+
if(batteryPercent > 50) return Symbols::batteryHalf;
10+
if(batteryPercent > 25) return Symbols::batteryOneQuarter;
1111
return Symbols::batteryEmpty;
1212
}
1313

src/displayapp/screens/BatteryIcon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Pinetime {
66
class BatteryIcon {
77
public:
88
static const char* GetUnknownIcon();
9-
static const char* GetBatteryIcon(float batteryPercent);
9+
static const char* GetBatteryIcon(int batteryPercent);
1010
static const char* GetPlugIcon(bool isCharging);
1111
};
1212
}

src/displayapp/screens/Clock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ namespace Pinetime {
6464
Pinetime::Controllers::DateTime::Days currentDayOfWeek = Pinetime::Controllers::DateTime::Days::Unknown;
6565
uint8_t currentDay = 0;
6666

67-
DirtyValue<float> batteryPercentRemaining {0};
67+
DirtyValue<int> batteryPercentRemaining {0};
6868
DirtyValue<bool> bleState {false};
6969
DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime;
7070
DirtyValue<uint32_t> stepCount {0};

0 commit comments

Comments
 (0)