Skip to content

Commit 7efe2b7

Browse files
committed
Fix misconfigured ADC and remove now unnecessary filtering
1 parent 4f378e8 commit 7efe2b7

2 files changed

Lines changed: 4 additions & 48 deletions

File tree

src/components/battery/BatteryController.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ void Battery::Update() {
2626
return;
2727
}
2828
// Non blocking read
29-
samples = 0;
3029
isReading = true;
3130
SaadcInit();
3231

@@ -45,7 +44,7 @@ void Battery::SaadcInit() {
4544
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,
4645
.gain = NRF_SAADC_GAIN1_5,
4746
.reference = NRF_SAADC_REFERENCE_INTERNAL,
48-
.acq_time = NRF_SAADC_ACQTIME_3US,
47+
.acq_time = NRF_SAADC_ACQTIME_40US,
4948
.mode = NRF_SAADC_MODE_SINGLE_ENDED,
5049
.burst = NRF_SAADC_BURST_ENABLED,
5150
.pin_p = batteryVoltageAdcInput,
@@ -75,14 +74,7 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
7574
percentRemaining = std::max(percentRemaining, 0);
7675
percentRemaining = std::min(percentRemaining, 100);
7776

78-
percentRemainingBuffer.insert(percentRemaining);
79-
80-
samples++;
81-
if (samples > percentRemainingSamples) {
82-
nrfx_saadc_uninit();
83-
isReading = false;
84-
} else {
85-
nrfx_saadc_sample();
86-
}
77+
nrfx_saadc_uninit();
78+
isReading = false;
8779
}
8880
}

src/components/battery/BatteryController.h

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,6 @@
77
namespace 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 int num) {
23-
head %= cap;
24-
arr[head++] = num;
25-
if (sz != cap) {
26-
sz++;
27-
}
28-
}
29-
30-
int GetAverage() const {
31-
int sum = std::accumulate(arr.begin(), arr.end(), 0);
32-
return (sum / sz);
33-
}
34-
35-
private:
36-
std::array<int, 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,7 +15,7 @@ namespace Pinetime {
4715
void Update();
4816

4917
int PercentRemaining() const {
50-
return percentRemainingBuffer.GetAverage();
18+
return percentRemaining;
5119
}
5220

5321
uint16_t Voltage() const {
@@ -65,9 +33,6 @@ namespace Pinetime {
6533
static Battery* instance;
6634
nrf_saadc_value_t saadc_value;
6735

68-
static constexpr uint8_t percentRemainingSamples = 5;
69-
CircBuffer<percentRemainingSamples> percentRemainingBuffer {};
70-
7136
static constexpr uint32_t chargingPin = 12;
7237
static constexpr uint32_t powerPresentPin = 19;
7338
static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7;
@@ -83,7 +48,6 @@ namespace Pinetime {
8348
static void adcCallbackStatic(nrfx_saadc_evt_t const* event);
8449

8550
bool isReading = false;
86-
uint8_t samples = 0;
8751
};
8852
}
8953
}

0 commit comments

Comments
 (0)