Skip to content

Commit f032929

Browse files
authored
Merge pull request #580 from Riksu9000/timer_battery_reading
Make battery reading periodic
2 parents 977321c + 21d37d1 commit f032929

8 files changed

Lines changed: 38 additions & 31 deletions

File tree

src/components/battery/BatteryController.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ Battery* Battery::instance = nullptr;
99

1010
Battery::Battery() {
1111
instance = this;
12-
}
13-
14-
void Battery::Init() {
15-
nrf_gpio_cfg_input(chargingPin, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Pullup);
12+
nrf_gpio_cfg_input(chargingPin, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Disabled);
1613
}
1714

1815
void Battery::Update() {
@@ -75,5 +72,11 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
7572

7673
nrfx_saadc_uninit();
7774
isReading = false;
75+
76+
systemTask->PushMessage(System::Messages::BatteryMeasurementDone);
7877
}
7978
}
79+
80+
void Battery::Register(Pinetime::System::SystemTask* systemTask) {
81+
this->systemTask = systemTask;
82+
}

src/components/battery/BatteryController.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#pragma once
22
#include <cstdint>
33
#include <drivers/include/nrfx_saadc.h>
4-
#include <array>
5-
#include <numeric>
4+
#include <systemtask/SystemTask.h>
65

76
namespace Pinetime {
87
namespace Controllers {
@@ -11,8 +10,8 @@ namespace Pinetime {
1110
public:
1211
Battery();
1312

14-
void Init();
1513
void Update();
14+
void Register(System::SystemTask* systemTask);
1615

1716
uint8_t PercentRemaining() const {
1817
return percentRemaining;
@@ -49,6 +48,8 @@ namespace Pinetime {
4948
static void AdcCallbackStatic(nrfx_saadc_evt_t const* event);
5049

5150
bool isReading = false;
51+
52+
Pinetime::System::SystemTask* systemTask = nullptr;
5253
};
5354
}
5455
}

src/displayapp/DisplayApp.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,6 @@ void DisplayApp::Refresh() {
194194
// clockScreen.SetBleConnectionState(bleController.IsConnected() ? Screens::Clock::BleConnectionStates::Connected :
195195
// Screens::Clock::BleConnectionStates::NotConnected);
196196
break;
197-
case Messages::UpdateBatteryLevel:
198-
batteryController.Update();
199-
break;
200197
case Messages::NewNotification:
201198
LoadApp(Apps::NotificationsPreview, DisplayApp::FullRefreshDirections::Down);
202199
break;

src/displayapp/Messages.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace Pinetime {
77
GoToRunning,
88
UpdateDateTime,
99
UpdateBleConnection,
10-
UpdateBatteryLevel,
1110
TouchEvent,
1211
ButtonPushed,
1312
NewNotification,

src/displayapp/screens/BatteryInfo.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ BatteryInfo::~BatteryInfo() {
5555

5656
void BatteryInfo::Refresh() {
5757

58-
batteryController.Update();
59-
6058
batteryPercent = batteryController.PercentRemaining();
6159
batteryVoltage = batteryController.Voltage();
6260

src/systemtask/Messages.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ namespace Pinetime {
2020
EnableSleeping,
2121
DisableSleeping,
2222
OnNewDay,
23-
OnChargingEvent
23+
OnChargingEvent,
24+
MeasureBatteryTimerExpired,
25+
BatteryMeasurementDone,
2426
};
2527
}
2628
}

src/systemtask/SystemTask.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ void IdleTimerCallback(TimerHandle_t xTimer) {
4747
sysTask->OnIdle();
4848
}
4949

50+
void MeasureBatteryTimerCallback(TimerHandle_t xTimer) {
51+
auto* sysTask = static_cast<SystemTask*>(pvTimerGetTimerID(xTimer));
52+
sysTask->PushMessage(Pinetime::System::Messages::MeasureBatteryTimerExpired);
53+
}
54+
5055
SystemTask::SystemTask(Drivers::SpiMaster& spi,
5156
Drivers::St7789& lcd,
5257
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
@@ -127,7 +132,8 @@ void SystemTask::Work() {
127132
twiMaster.Init();
128133
touchPanel.Init();
129134
dateTimeController.Register(this);
130-
batteryController.Init();
135+
batteryController.Register(this);
136+
batteryController.Update();
131137
motorController.Init();
132138
motionSensor.SoftReset();
133139
timerController.Register(this);
@@ -144,8 +150,6 @@ void SystemTask::Work() {
144150
displayApp.Register(this);
145151
displayApp.Start();
146152

147-
displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
148-
149153
heartRateSensor.Init();
150154
heartRateSensor.Disable();
151155
heartRateApp.Start();
@@ -188,7 +192,9 @@ void SystemTask::Work() {
188192

189193
idleTimer = xTimerCreate("idleTimer", pdMS_TO_TICKS(2000), pdFALSE, this, IdleTimerCallback);
190194
dimTimer = xTimerCreate("dimTimer", pdMS_TO_TICKS(settingsController.GetScreenTimeOut() - 2000), pdFALSE, this, DimTimerCallback);
195+
measureBatteryTimer = xTimerCreate("measureBattery", batteryMeasurementPeriod, pdTRUE, this, MeasureBatteryTimerCallback);
191196
xTimerStart(dimTimer, 0);
197+
xTimerStart(measureBatteryTimer, portMAX_DELAY);
192198

193199
// Suppress endless loop diagnostic
194200
#pragma clang diagnostic push
@@ -198,11 +204,6 @@ void SystemTask::Work() {
198204

199205
uint8_t msg;
200206
if (xQueueReceive(systemTasksMsgQueue, &msg, 100)) {
201-
202-
batteryController.Update();
203-
// the battery does not emit events when changing charge levels, so we piggyback
204-
// on any system event to read and update the current values
205-
206207
Messages message = static_cast<Messages>(msg);
207208
switch (message) {
208209
case Messages::EnableSleeping:
@@ -232,7 +233,6 @@ void SystemTask::Work() {
232233
lcd.Wakeup();
233234

234235
displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToRunning);
235-
displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
236236
heartRateApp.PushMessage(Pinetime::Applications::HeartRateTask::Messages::WakeUp);
237237

238238
isSleeping = false;
@@ -327,8 +327,18 @@ void SystemTask::Work() {
327327
stepCounterMustBeReset = true;
328328
break;
329329
case Messages::OnChargingEvent:
330+
batteryController.Update();
330331
motorController.RunForDuration(15);
331-
// Battery level is updated on every message - there's no need to do anything
332+
break;
333+
case Messages::MeasureBatteryTimerExpired:
334+
sendBatteryNotification = true;
335+
batteryController.Update();
336+
break;
337+
case Messages::BatteryMeasurementDone:
338+
if (sendBatteryNotification) {
339+
sendBatteryNotification = false;
340+
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
341+
}
332342
break;
333343

334344
default:
@@ -347,11 +357,6 @@ void SystemTask::Work() {
347357
}
348358
}
349359

350-
if (xTaskGetTickCount() - batteryNotificationTick > batteryNotificationPeriod) {
351-
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
352-
batteryNotificationTick = xTaskGetTickCount();
353-
}
354-
355360
monitor.Process();
356361
uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG);
357362
dateTimeController.UpdateTime(systick_counter);

src/systemtask/SystemTask.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,15 @@ namespace Pinetime {
136136
uint8_t bleDiscoveryTimer = 0;
137137
TimerHandle_t dimTimer;
138138
TimerHandle_t idleTimer;
139+
TimerHandle_t measureBatteryTimer;
140+
bool sendBatteryNotification = false;
139141
bool doNotGoToSleep = false;
140142

141143
void GoToRunning();
142144
void UpdateMotion();
143145
bool stepCounterMustBeReset = false;
144-
static constexpr TickType_t batteryNotificationPeriod = 1000 * 60 * 10; // 1 tick ~= 1ms. 1ms * 60 * 10 = 10 minutes
145-
TickType_t batteryNotificationTick = 0;
146+
static constexpr TickType_t batteryMeasurementPeriod = pdMS_TO_TICKS(10 * 60 * 1000);
147+
TickType_t lastBatteryNotificationTime = 0;
146148

147149
#if configUSE_TRACE_FACILITY == 1
148150
SystemMonitor<FreeRtosMonitor> monitor;

0 commit comments

Comments
 (0)