Skip to content

Commit fd52ca8

Browse files
committed
Detect full charge and improve watchface display
1 parent 6f9f0e8 commit fd52ca8

9 files changed

Lines changed: 75 additions & 26 deletions

File tree

src/components/battery/BatteryController.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ void Battery::Update() {
1919
isCharging = !nrf_gpio_pin_read(chargingPin);
2020
isPowerPresent = !nrf_gpio_pin_read(powerPresentPin);
2121

22+
if (isPowerPresent && !isCharging) {
23+
isFull = true;
24+
} else if (!isPowerPresent) {
25+
isFull = false;
26+
}
27+
2228
if (isReading) {
2329
return;
2430
}
@@ -65,12 +71,12 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
6571
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024
6672
voltage = p_event->data.done.p_buffer[0] * (8 * 600) / 1024;
6773

68-
if (voltage > battery_max) {
74+
if (isFull) {
6975
percentRemaining = 100;
7076
} else if (voltage < battery_min) {
7177
percentRemaining = 0;
7278
} else {
73-
percentRemaining = (voltage - battery_min) * 100 / (battery_max - battery_min);
79+
percentRemaining = std::min((voltage - battery_min) * 100 / (battery_max - battery_min), 99);
7480
}
7581

7682
nrfx_saadc_uninit();

src/components/battery/BatteryController.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ namespace Pinetime {
2323
}
2424

2525
bool IsCharging() const {
26-
return isCharging;
26+
// isCharging will go up and down when fully charged
27+
// isFull makes sure this returns false while fully charged.
28+
return isCharging && !isFull;
2729
}
2830

2931
bool IsPowerPresent() const {
3032
return isPowerPresent;
3133
}
3234

35+
bool IsFull() const {
36+
return isFull;
37+
}
38+
3339
private:
3440
static Battery* instance;
3541
nrf_saadc_value_t saadc_value;
@@ -40,6 +46,7 @@ namespace Pinetime {
4046
uint16_t voltage = 0;
4147
uint8_t percentRemaining = 0;
4248

49+
bool isFull = false;
4350
bool isCharging = false;
4451
bool isPowerPresent = false;
4552

src/displayapp/screens/BatteryInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ void BatteryInfo::Refresh() {
6060
batteryPercent = batteryController.PercentRemaining();
6161
batteryVoltage = batteryController.Voltage();
6262

63-
if (batteryController.IsCharging() and batteryPercent < 100) {
63+
if (batteryController.IsCharging()) {
6464
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_RED);
6565
lv_label_set_text_static(status, "Charging");
66-
} else if (batteryPercent == 100) {
66+
} else if (batteryController.IsFull()) {
6767
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_BLUE);
6868
lv_label_set_text_static(status, "Fully charged");
6969
} else if (batteryPercent < 10) {

src/displayapp/screens/PineTimeStyle.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ PineTimeStyle::PineTimeStyle(DisplayApp* app,
100100
lv_obj_set_style_local_text_color(batteryIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x000000));
101101
lv_label_set_text(batteryIcon, Symbols::batteryFull);
102102
lv_obj_align(batteryIcon, sidebar, LV_ALIGN_IN_TOP_MID, 0, 2);
103-
104-
batteryPlug = lv_label_create(lv_scr_act(), nullptr);
105-
lv_obj_set_style_local_text_color(batteryPlug, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x000000));
106-
lv_obj_align(batteryPlug, sidebar, LV_ALIGN_IN_TOP_MID, 0, 2);
103+
lv_obj_set_auto_realign(batteryIcon, true);
107104

108105
bleIcon = lv_label_create(lv_scr_act(), nullptr);
109106
lv_obj_set_style_local_text_color(bleIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x000000));
@@ -205,18 +202,24 @@ PineTimeStyle::~PineTimeStyle() {
205202
lv_obj_clean(lv_scr_act());
206203
}
207204

205+
void PineTimeStyle::SetBatteryIcon() {
206+
auto batteryPercent = batteryPercentRemaining.Get();
207+
lv_label_set_text(batteryIcon, BatteryIcon::GetBatteryIcon(batteryPercent));
208+
}
209+
208210
void PineTimeStyle::Refresh() {
209-
batteryPercentRemaining = batteryController.PercentRemaining();
210-
if (batteryPercentRemaining.IsUpdated()) {
211-
auto batteryPercent = batteryPercentRemaining.Get();
212-
if (batteryController.IsCharging()) {
213-
auto isCharging = batteryController.IsCharging() || batteryController.IsPowerPresent();
214-
lv_label_set_text(batteryPlug, BatteryIcon::GetPlugIcon(isCharging));
215-
lv_obj_realign(batteryPlug);
216-
lv_label_set_text(batteryIcon, "");
211+
isCharging = batteryController.IsCharging();
212+
if (isCharging.IsUpdated()) {
213+
if (isCharging.Get()) {
214+
lv_label_set_text(batteryIcon, Symbols::plug);
217215
} else {
218-
lv_label_set_text(batteryIcon, BatteryIcon::GetBatteryIcon(batteryPercent));
219-
lv_label_set_text(batteryPlug, "");
216+
SetBatteryIcon();
217+
}
218+
}
219+
if (!isCharging.Get()) {
220+
batteryPercentRemaining = batteryController.PercentRemaining();
221+
if (batteryPercentRemaining.IsUpdated()) {
222+
SetBatteryIcon();
220223
}
221224
}
222225

src/displayapp/screens/PineTimeStyle.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace Pinetime {
4141
uint8_t currentDay = 0;
4242

4343
DirtyValue<uint8_t> batteryPercentRemaining {};
44+
DirtyValue<bool> isCharging {};
4445
DirtyValue<bool> bleState {};
4546
DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime {};
4647
DirtyValue<bool> motionSensorOk {};
@@ -58,7 +59,6 @@ namespace Pinetime {
5859
lv_obj_t* backgroundLabel;
5960
lv_obj_t* batteryIcon;
6061
lv_obj_t* bleIcon;
61-
lv_obj_t* batteryPlug;
6262
lv_obj_t* calendarOuter;
6363
lv_obj_t* calendarInner;
6464
lv_obj_t* calendarBar1;
@@ -76,6 +76,8 @@ namespace Pinetime {
7676
Controllers::Settings& settingsController;
7777
Controllers::MotionController& motionController;
7878

79+
void SetBatteryIcon();
80+
7981
lv_task_t* taskRefresh;
8082
};
8183
}

src/displayapp/screens/WatchFaceAnalog.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,31 @@ void WatchFaceAnalog::UpdateClock() {
176176
}
177177
}
178178

179+
void WatchFaceAnalog::SetBatteryIcon() {
180+
auto batteryPercent = batteryPercentRemaining.Get();
181+
if (batteryPercent == 100) {
182+
lv_obj_set_style_local_text_color(batteryIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
183+
} else {
184+
lv_obj_set_style_local_text_color(batteryIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
185+
}
186+
lv_label_set_text(batteryIcon, BatteryIcon::GetBatteryIcon(batteryPercent));
187+
}
188+
179189
void WatchFaceAnalog::Refresh() {
180-
batteryPercentRemaining = batteryController.PercentRemaining();
181-
if (batteryPercentRemaining.IsUpdated()) {
182-
auto batteryPercent = batteryPercentRemaining.Get();
183-
lv_label_set_text(batteryIcon, BatteryIcon::GetBatteryIcon(batteryPercent));
190+
isCharging = batteryController.IsCharging();
191+
if (isCharging.IsUpdated()) {
192+
if (isCharging.Get()) {
193+
lv_obj_set_style_local_text_color(batteryIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
194+
lv_label_set_text(batteryIcon, Symbols::plug);
195+
} else {
196+
SetBatteryIcon();
197+
}
198+
}
199+
if (!isCharging.Get()) {
200+
batteryPercentRemaining = batteryController.PercentRemaining();
201+
if (batteryPercentRemaining.IsUpdated()) {
202+
SetBatteryIcon();
203+
}
184204
}
185205

186206
notificationState = notificationManager.AreNewNotificationsAvailable();

src/displayapp/screens/WatchFaceAnalog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ namespace Pinetime {
4949
uint8_t currentDay = 0;
5050

5151
DirtyValue<uint8_t> batteryPercentRemaining {0};
52+
DirtyValue<bool> isCharging {};
5253
DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime;
5354
DirtyValue<bool> notificationState {false};
5455

@@ -81,6 +82,7 @@ namespace Pinetime {
8182
Controllers::Settings& settingsController;
8283

8384
void UpdateClock();
85+
void SetBatteryIcon();
8486

8587
lv_task_t* taskRefresh;
8688
};

src/displayapp/screens/WatchFaceDigital.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,20 @@ WatchFaceDigital::~WatchFaceDigital() {
102102
}
103103

104104
void WatchFaceDigital::Refresh() {
105+
isCharging = batteryController.IsCharging();
106+
if (isCharging.IsUpdated()) {
107+
lv_label_set_text(batteryPlug, BatteryIcon::GetPlugIcon(isCharging.Get()));
108+
}
109+
105110
batteryPercentRemaining = batteryController.PercentRemaining();
106111
if (batteryPercentRemaining.IsUpdated()) {
107112
auto batteryPercent = batteryPercentRemaining.Get();
113+
if (batteryPercent == 100) {
114+
lv_obj_set_style_local_text_color(batteryIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
115+
} else {
116+
lv_obj_set_style_local_text_color(batteryIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
117+
}
108118
lv_label_set_text(batteryIcon, BatteryIcon::GetBatteryIcon(batteryPercent));
109-
auto isCharging = batteryController.IsCharging() or batteryController.IsPowerPresent();
110-
lv_label_set_text(batteryPlug, BatteryIcon::GetPlugIcon(isCharging));
111119
}
112120

113121
bleState = bleController.IsConnected();

src/displayapp/screens/WatchFaceDigital.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace Pinetime {
4444
uint8_t currentDay = 0;
4545

4646
DirtyValue<uint8_t> batteryPercentRemaining {};
47+
DirtyValue<bool> isCharging {};
4748
DirtyValue<bool> bleState {};
4849
DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime {};
4950
DirtyValue<bool> motionSensorOk {};

0 commit comments

Comments
 (0)