Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/components/battery/BatteryController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,20 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
newPercent = std::min(aprox.GetValue(voltage), isCharging ? uint8_t {99} : uint8_t {100});
}

if (isPowerPresent) {
batteryLowNotified = false;
}

if ((isPowerPresent && newPercent > percentRemaining) || (!isPowerPresent && newPercent < percentRemaining) || firstMeasurement) {
firstMeasurement = false;
percentRemaining = newPercent;
systemTask->PushMessage(System::Messages::BatteryPercentageUpdated);

// warn about low battery when not charging and below threshold
if (BatteryIsLow() && !isPowerPresent && !batteryLowNotified) {
systemTask->PushMessage(System::Messages::LowBattery);
batteryLowNotified = true;
}
}

nrfx_saadc_uninit();
Expand Down
6 changes: 6 additions & 0 deletions src/components/battery/BatteryController.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ namespace Pinetime {
uint8_t PercentRemaining() const {
return percentRemaining;
}
bool BatteryIsLow() const {
return percentRemaining <= lowBatteryThreshold;
}

uint16_t Voltage() const {
return voltage;
Expand All @@ -39,6 +42,7 @@ namespace Pinetime {
static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7;
uint16_t voltage = 0;
uint8_t percentRemaining = 0;
bool batteryLowNotified = false;

bool isFull = false;
bool isCharging = false;
Expand All @@ -50,6 +54,8 @@ namespace Pinetime {
void SaadcEventHandler(nrfx_saadc_evt_t const* p_event);
static void AdcCallbackStatic(nrfx_saadc_evt_t const* event);

static constexpr uint8_t lowBatteryThreshold {15};

bool isReading = false;

Pinetime::System::SystemTask* systemTask = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/displayapp/screens/BatteryInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void BatteryInfo::Refresh() {
} else if (batteryPercent == 100) {
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_BLUE);
lv_label_set_text_static(status, "Fully charged");
} else if (batteryPercent < 10) {
} else if (batteryController.BatteryIsLow()) {
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
lv_label_set_text_static(status, "Battery low");
} else {
Expand Down
1 change: 1 addition & 0 deletions src/systemtask/Messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace Pinetime {
StopRinging,
MeasureBatteryTimerExpired,
BatteryPercentageUpdated,
LowBattery,
StartFileTransfer,
StopFileTransfer,
BleRadioEnableToggle
Expand Down
12 changes: 12 additions & 0 deletions src/systemtask/SystemTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "BootErrors.h"

#include <memory>
#include <algorithm>
#include <cstring>

using namespace Pinetime::System;

Expand Down Expand Up @@ -424,6 +426,16 @@ void SystemTask::Work() {
case Messages::BatteryPercentageUpdated:
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
break;
case Messages::LowBattery: {
Pinetime::Controllers::NotificationManager::Notification notif;
constexpr char message[] = "Low Battery\0Charge your watch to prevent data loss.\0";
constexpr size_t messageSize = std::min(sizeof(message), Pinetime::Controllers::NotificationManager::MaximumMessageSize());
std::memcpy(notif.message.data(), message, messageSize);
notif.size = messageSize;
notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert;
notificationManager.Push(std::move(notif));
PushMessage(Messages::OnNewNotification);
} break;
case Messages::OnPairing:
if (state == SystemTaskState::Sleeping) {
GoToRunning();
Expand Down