Merge pull request #580 from Riksu9000/timer_battery_reading
Make battery reading periodic
This commit is contained in:
commit
f0329298ac
8 changed files with 38 additions and 31 deletions
|
@ -9,10 +9,7 @@ Battery* Battery::instance = nullptr;
|
||||||
|
|
||||||
Battery::Battery() {
|
Battery::Battery() {
|
||||||
instance = this;
|
instance = this;
|
||||||
}
|
nrf_gpio_cfg_input(chargingPin, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Disabled);
|
||||||
|
|
||||||
void Battery::Init() {
|
|
||||||
nrf_gpio_cfg_input(chargingPin, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Pullup);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Battery::Update() {
|
void Battery::Update() {
|
||||||
|
@ -75,5 +72,11 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
|
||||||
|
|
||||||
nrfx_saadc_uninit();
|
nrfx_saadc_uninit();
|
||||||
isReading = false;
|
isReading = false;
|
||||||
|
|
||||||
|
systemTask->PushMessage(System::Messages::BatteryMeasurementDone);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Battery::Register(Pinetime::System::SystemTask* systemTask) {
|
||||||
|
this->systemTask = systemTask;
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <drivers/include/nrfx_saadc.h>
|
#include <drivers/include/nrfx_saadc.h>
|
||||||
#include <array>
|
#include <systemtask/SystemTask.h>
|
||||||
#include <numeric>
|
|
||||||
|
|
||||||
namespace Pinetime {
|
namespace Pinetime {
|
||||||
namespace Controllers {
|
namespace Controllers {
|
||||||
|
@ -11,8 +10,8 @@ namespace Pinetime {
|
||||||
public:
|
public:
|
||||||
Battery();
|
Battery();
|
||||||
|
|
||||||
void Init();
|
|
||||||
void Update();
|
void Update();
|
||||||
|
void Register(System::SystemTask* systemTask);
|
||||||
|
|
||||||
uint8_t PercentRemaining() const {
|
uint8_t PercentRemaining() const {
|
||||||
return percentRemaining;
|
return percentRemaining;
|
||||||
|
@ -49,6 +48,8 @@ namespace Pinetime {
|
||||||
static void AdcCallbackStatic(nrfx_saadc_evt_t const* event);
|
static void AdcCallbackStatic(nrfx_saadc_evt_t const* event);
|
||||||
|
|
||||||
bool isReading = false;
|
bool isReading = false;
|
||||||
|
|
||||||
|
Pinetime::System::SystemTask* systemTask = nullptr;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -194,9 +194,6 @@ void DisplayApp::Refresh() {
|
||||||
// clockScreen.SetBleConnectionState(bleController.IsConnected() ? Screens::Clock::BleConnectionStates::Connected :
|
// clockScreen.SetBleConnectionState(bleController.IsConnected() ? Screens::Clock::BleConnectionStates::Connected :
|
||||||
// Screens::Clock::BleConnectionStates::NotConnected);
|
// Screens::Clock::BleConnectionStates::NotConnected);
|
||||||
break;
|
break;
|
||||||
case Messages::UpdateBatteryLevel:
|
|
||||||
batteryController.Update();
|
|
||||||
break;
|
|
||||||
case Messages::NewNotification:
|
case Messages::NewNotification:
|
||||||
LoadApp(Apps::NotificationsPreview, DisplayApp::FullRefreshDirections::Down);
|
LoadApp(Apps::NotificationsPreview, DisplayApp::FullRefreshDirections::Down);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -7,7 +7,6 @@ namespace Pinetime {
|
||||||
GoToRunning,
|
GoToRunning,
|
||||||
UpdateDateTime,
|
UpdateDateTime,
|
||||||
UpdateBleConnection,
|
UpdateBleConnection,
|
||||||
UpdateBatteryLevel,
|
|
||||||
TouchEvent,
|
TouchEvent,
|
||||||
ButtonPushed,
|
ButtonPushed,
|
||||||
NewNotification,
|
NewNotification,
|
||||||
|
|
|
@ -55,8 +55,6 @@ BatteryInfo::~BatteryInfo() {
|
||||||
|
|
||||||
void BatteryInfo::Refresh() {
|
void BatteryInfo::Refresh() {
|
||||||
|
|
||||||
batteryController.Update();
|
|
||||||
|
|
||||||
batteryPercent = batteryController.PercentRemaining();
|
batteryPercent = batteryController.PercentRemaining();
|
||||||
batteryVoltage = batteryController.Voltage();
|
batteryVoltage = batteryController.Voltage();
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,9 @@ namespace Pinetime {
|
||||||
EnableSleeping,
|
EnableSleeping,
|
||||||
DisableSleeping,
|
DisableSleeping,
|
||||||
OnNewDay,
|
OnNewDay,
|
||||||
OnChargingEvent
|
OnChargingEvent,
|
||||||
|
MeasureBatteryTimerExpired,
|
||||||
|
BatteryMeasurementDone,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,6 +47,11 @@ void IdleTimerCallback(TimerHandle_t xTimer) {
|
||||||
sysTask->OnIdle();
|
sysTask->OnIdle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MeasureBatteryTimerCallback(TimerHandle_t xTimer) {
|
||||||
|
auto* sysTask = static_cast<SystemTask*>(pvTimerGetTimerID(xTimer));
|
||||||
|
sysTask->PushMessage(Pinetime::System::Messages::MeasureBatteryTimerExpired);
|
||||||
|
}
|
||||||
|
|
||||||
SystemTask::SystemTask(Drivers::SpiMaster& spi,
|
SystemTask::SystemTask(Drivers::SpiMaster& spi,
|
||||||
Drivers::St7789& lcd,
|
Drivers::St7789& lcd,
|
||||||
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
|
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
|
||||||
|
@ -127,7 +132,8 @@ void SystemTask::Work() {
|
||||||
twiMaster.Init();
|
twiMaster.Init();
|
||||||
touchPanel.Init();
|
touchPanel.Init();
|
||||||
dateTimeController.Register(this);
|
dateTimeController.Register(this);
|
||||||
batteryController.Init();
|
batteryController.Register(this);
|
||||||
|
batteryController.Update();
|
||||||
motorController.Init();
|
motorController.Init();
|
||||||
motionSensor.SoftReset();
|
motionSensor.SoftReset();
|
||||||
timerController.Register(this);
|
timerController.Register(this);
|
||||||
|
@ -144,8 +150,6 @@ void SystemTask::Work() {
|
||||||
displayApp.Register(this);
|
displayApp.Register(this);
|
||||||
displayApp.Start();
|
displayApp.Start();
|
||||||
|
|
||||||
displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
|
|
||||||
|
|
||||||
heartRateSensor.Init();
|
heartRateSensor.Init();
|
||||||
heartRateSensor.Disable();
|
heartRateSensor.Disable();
|
||||||
heartRateApp.Start();
|
heartRateApp.Start();
|
||||||
|
@ -188,7 +192,9 @@ void SystemTask::Work() {
|
||||||
|
|
||||||
idleTimer = xTimerCreate("idleTimer", pdMS_TO_TICKS(2000), pdFALSE, this, IdleTimerCallback);
|
idleTimer = xTimerCreate("idleTimer", pdMS_TO_TICKS(2000), pdFALSE, this, IdleTimerCallback);
|
||||||
dimTimer = xTimerCreate("dimTimer", pdMS_TO_TICKS(settingsController.GetScreenTimeOut() - 2000), pdFALSE, this, DimTimerCallback);
|
dimTimer = xTimerCreate("dimTimer", pdMS_TO_TICKS(settingsController.GetScreenTimeOut() - 2000), pdFALSE, this, DimTimerCallback);
|
||||||
|
measureBatteryTimer = xTimerCreate("measureBattery", batteryMeasurementPeriod, pdTRUE, this, MeasureBatteryTimerCallback);
|
||||||
xTimerStart(dimTimer, 0);
|
xTimerStart(dimTimer, 0);
|
||||||
|
xTimerStart(measureBatteryTimer, portMAX_DELAY);
|
||||||
|
|
||||||
// Suppress endless loop diagnostic
|
// Suppress endless loop diagnostic
|
||||||
#pragma clang diagnostic push
|
#pragma clang diagnostic push
|
||||||
|
@ -198,11 +204,6 @@ void SystemTask::Work() {
|
||||||
|
|
||||||
uint8_t msg;
|
uint8_t msg;
|
||||||
if (xQueueReceive(systemTasksMsgQueue, &msg, 100)) {
|
if (xQueueReceive(systemTasksMsgQueue, &msg, 100)) {
|
||||||
|
|
||||||
batteryController.Update();
|
|
||||||
// the battery does not emit events when changing charge levels, so we piggyback
|
|
||||||
// on any system event to read and update the current values
|
|
||||||
|
|
||||||
Messages message = static_cast<Messages>(msg);
|
Messages message = static_cast<Messages>(msg);
|
||||||
switch (message) {
|
switch (message) {
|
||||||
case Messages::EnableSleeping:
|
case Messages::EnableSleeping:
|
||||||
|
@ -232,7 +233,6 @@ void SystemTask::Work() {
|
||||||
lcd.Wakeup();
|
lcd.Wakeup();
|
||||||
|
|
||||||
displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToRunning);
|
displayApp.PushMessage(Pinetime::Applications::Display::Messages::GoToRunning);
|
||||||
displayApp.PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
|
|
||||||
heartRateApp.PushMessage(Pinetime::Applications::HeartRateTask::Messages::WakeUp);
|
heartRateApp.PushMessage(Pinetime::Applications::HeartRateTask::Messages::WakeUp);
|
||||||
|
|
||||||
isSleeping = false;
|
isSleeping = false;
|
||||||
|
@ -327,8 +327,18 @@ void SystemTask::Work() {
|
||||||
stepCounterMustBeReset = true;
|
stepCounterMustBeReset = true;
|
||||||
break;
|
break;
|
||||||
case Messages::OnChargingEvent:
|
case Messages::OnChargingEvent:
|
||||||
|
batteryController.Update();
|
||||||
motorController.RunForDuration(15);
|
motorController.RunForDuration(15);
|
||||||
// Battery level is updated on every message - there's no need to do anything
|
break;
|
||||||
|
case Messages::MeasureBatteryTimerExpired:
|
||||||
|
sendBatteryNotification = true;
|
||||||
|
batteryController.Update();
|
||||||
|
break;
|
||||||
|
case Messages::BatteryMeasurementDone:
|
||||||
|
if (sendBatteryNotification) {
|
||||||
|
sendBatteryNotification = false;
|
||||||
|
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -347,11 +357,6 @@ void SystemTask::Work() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (xTaskGetTickCount() - batteryNotificationTick > batteryNotificationPeriod) {
|
|
||||||
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
|
|
||||||
batteryNotificationTick = xTaskGetTickCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
monitor.Process();
|
monitor.Process();
|
||||||
uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG);
|
uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG);
|
||||||
dateTimeController.UpdateTime(systick_counter);
|
dateTimeController.UpdateTime(systick_counter);
|
||||||
|
|
|
@ -136,13 +136,15 @@ namespace Pinetime {
|
||||||
uint8_t bleDiscoveryTimer = 0;
|
uint8_t bleDiscoveryTimer = 0;
|
||||||
TimerHandle_t dimTimer;
|
TimerHandle_t dimTimer;
|
||||||
TimerHandle_t idleTimer;
|
TimerHandle_t idleTimer;
|
||||||
|
TimerHandle_t measureBatteryTimer;
|
||||||
|
bool sendBatteryNotification = false;
|
||||||
bool doNotGoToSleep = false;
|
bool doNotGoToSleep = false;
|
||||||
|
|
||||||
void GoToRunning();
|
void GoToRunning();
|
||||||
void UpdateMotion();
|
void UpdateMotion();
|
||||||
bool stepCounterMustBeReset = false;
|
bool stepCounterMustBeReset = false;
|
||||||
static constexpr TickType_t batteryNotificationPeriod = 1000 * 60 * 10; // 1 tick ~= 1ms. 1ms * 60 * 10 = 10 minutes
|
static constexpr TickType_t batteryMeasurementPeriod = pdMS_TO_TICKS(10 * 60 * 1000);
|
||||||
TickType_t batteryNotificationTick = 0;
|
TickType_t lastBatteryNotificationTime = 0;
|
||||||
|
|
||||||
#if configUSE_TRACE_FACILITY == 1
|
#if configUSE_TRACE_FACILITY == 1
|
||||||
SystemMonitor<FreeRtosMonitor> monitor;
|
SystemMonitor<FreeRtosMonitor> monitor;
|
||||||
|
|
Loading…
Reference in a new issue