Skip to content

Commit 23bde0d

Browse files
committed
Make battery reading periodic. Add events. Disable pullup
1 parent 6430773 commit 23bde0d

8 files changed

Lines changed: 38 additions & 32 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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ BatteryInfo::~BatteryInfo() {
5959
}
6060

6161
void BatteryInfo::UpdateScreen() {
62-
63-
batteryController.Update();
64-
6562
batteryPercent = batteryController.PercentRemaining();
6663
batteryVoltage = batteryController.Voltage();
6764

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,
@@ -126,7 +131,8 @@ void SystemTask::Work() {
126131
twiMaster.Init();
127132
touchPanel.Init();
128133
dateTimeController.Register(this);
129-
batteryController.Init();
134+
batteryController.Register(this);
135+
batteryController.Update();
130136
motorController.Init();
131137
motionSensor.SoftReset();
132138
timerController.Register(this);
@@ -143,8 +149,6 @@ void SystemTask::Work() {
143149
displayApp.Register(this);
144150
displayApp.Start();
145151

146-
displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
147-
148152
heartRateSensor.Init();
149153
heartRateSensor.Disable();
150154
heartRateApp.Start();
@@ -187,7 +191,9 @@ void SystemTask::Work() {
187191

188192
idleTimer = xTimerCreate("idleTimer", pdMS_TO_TICKS(2000), pdFALSE, this, IdleTimerCallback);
189193
dimTimer = xTimerCreate("dimTimer", pdMS_TO_TICKS(settingsController.GetScreenTimeOut() - 2000), pdFALSE, this, DimTimerCallback);
194+
measureBatteryTimer = xTimerCreate("measureBattery", batteryMeasurementPeriod, pdTRUE, this, MeasureBatteryTimerCallback);
190195
xTimerStart(dimTimer, 0);
196+
xTimerStart(measureBatteryTimer, portMAX_DELAY);
191197

192198
// Suppress endless loop diagnostic
193199
#pragma clang diagnostic push
@@ -197,11 +203,6 @@ void SystemTask::Work() {
197203

198204
uint8_t msg;
199205
if (xQueueReceive(systemTasksMsgQueue, &msg, 100)) {
200-
201-
batteryController.Update();
202-
// the battery does not emit events when changing charge levels, so we piggyback
203-
// on any system event to read and update the current values
204-
205206
Messages message = static_cast<Messages>(msg);
206207
switch (message) {
207208
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;
@@ -326,8 +326,18 @@ void SystemTask::Work() {
326326
stepCounterMustBeReset = true;
327327
break;
328328
case Messages::OnChargingEvent:
329+
batteryController.Update();
329330
motorController.SetDuration(15);
330-
// Battery level is updated on every message - there's no need to do anything
331+
break;
332+
case Messages::MeasureBatteryTimerExpired:
333+
sendBatteryNotification = true;
334+
batteryController.Update();
335+
break;
336+
case Messages::BatteryMeasurementDone:
337+
if (sendBatteryNotification) {
338+
sendBatteryNotification = false;
339+
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
340+
}
331341
break;
332342

333343
default:
@@ -346,11 +356,6 @@ void SystemTask::Work() {
346356
}
347357
}
348358

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

src/systemtask/SystemTask.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,15 @@ namespace Pinetime {
131131
uint8_t bleDiscoveryTimer = 0;
132132
TimerHandle_t dimTimer;
133133
TimerHandle_t idleTimer;
134+
TimerHandle_t measureBatteryTimer;
135+
bool sendBatteryNotification = false;
134136
bool doNotGoToSleep = false;
135137

136138
void GoToRunning();
137139
void UpdateMotion();
138140
bool stepCounterMustBeReset = false;
139-
static constexpr TickType_t batteryNotificationPeriod = 1000 * 60 * 10; // 1 tick ~= 1ms. 1ms * 60 * 10 = 10 minutes
140-
TickType_t batteryNotificationTick = 0;
141+
static constexpr TickType_t batteryMeasurementPeriod = pdMS_TO_TICKS(10 * 60 * 1000);
142+
TickType_t lastBatteryNotificationTime = 0;
141143

142144
#if configUSE_TRACE_FACILITY == 1
143145
SystemMonitor<FreeRtosMonitor> monitor;

0 commit comments

Comments
 (0)