Skip to content

Commit fa6c291

Browse files
authored
Merge pull request #638 from Riksu9000/detect_full_charge
Detect full charge and improve watchface battery display
2 parents 3442085 + 980ac17 commit fa6c291

9 files changed

Lines changed: 71 additions & 25 deletions

File tree

src/components/battery/BatteryController.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ void Battery::Update() {
1717
isCharging = !nrf_gpio_pin_read(PinMap::Charging);
1818
isPowerPresent = !nrf_gpio_pin_read(PinMap::PowerPresent);
1919

20+
if (isPowerPresent && !isCharging) {
21+
isFull = true;
22+
} else if (!isPowerPresent) {
23+
isFull = false;
24+
}
25+
2026
if (isReading) {
2127
return;
2228
}
@@ -63,12 +69,12 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
6369
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024
6470
voltage = p_event->data.done.p_buffer[0] * (8 * 600) / 1024;
6571

66-
if (voltage > battery_max) {
72+
if (isFull) {
6773
percentRemaining = 100;
6874
} else if (voltage < battery_min) {
6975
percentRemaining = 0;
7076
} else {
71-
percentRemaining = (voltage - battery_min) * 100 / (battery_max - battery_min);
77+
percentRemaining = std::min((voltage - battery_min) * 100 / (battery_max - battery_min), isCharging ? 99 : 100);
7278
}
7379

7480
nrfx_saadc_uninit();

src/components/battery/BatteryController.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ namespace Pinetime {
2222
}
2323

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

2830
bool IsPowerPresent() const {
@@ -37,6 +39,7 @@ namespace Pinetime {
3739
uint16_t voltage = 0;
3840
uint8_t percentRemaining = 0;
3941

42+
bool isFull = false;
4043
bool isCharging = false;
4144
bool isPowerPresent = false;
4245

src/displayapp/screens/BatteryInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void BatteryInfo::Refresh() {
5858
batteryPercent = batteryController.PercentRemaining();
5959
batteryVoltage = batteryController.Voltage();
6060

61-
if (batteryController.IsCharging() and batteryPercent < 100) {
61+
if (batteryController.IsCharging()) {
6262
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_RED);
6363
lv_label_set_text_static(status, "Charging");
6464
} else if (batteryPercent == 100) {

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: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ WatchFaceAnalog::WatchFaceAnalog(Pinetime::Applications::DisplayApp* app,
6868
batteryIcon = lv_label_create(lv_scr_act(), nullptr);
6969
lv_label_set_text(batteryIcon, Symbols::batteryHalf);
7070
lv_obj_align(batteryIcon, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
71+
lv_obj_set_auto_realign(batteryIcon, true);
7172

7273
notificationIcon = lv_label_create(lv_scr_act(), NULL);
7374
lv_obj_set_style_local_text_color(notificationIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x00FF00));
@@ -176,11 +177,31 @@ void WatchFaceAnalog::UpdateClock() {
176177
}
177178
}
178179

180+
void WatchFaceAnalog::SetBatteryIcon() {
181+
auto batteryPercent = batteryPercentRemaining.Get();
182+
if (batteryPercent == 100) {
183+
lv_obj_set_style_local_text_color(batteryIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
184+
} else {
185+
lv_obj_set_style_local_text_color(batteryIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
186+
}
187+
lv_label_set_text(batteryIcon, BatteryIcon::GetBatteryIcon(batteryPercent));
188+
}
189+
179190
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));
191+
isCharging = batteryController.IsCharging();
192+
if (isCharging.IsUpdated()) {
193+
if (isCharging.Get()) {
194+
lv_obj_set_style_local_text_color(batteryIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
195+
lv_label_set_text(batteryIcon, Symbols::plug);
196+
} else {
197+
SetBatteryIcon();
198+
}
199+
}
200+
if (!isCharging.Get()) {
201+
batteryPercentRemaining = batteryController.PercentRemaining();
202+
if (batteryPercentRemaining.IsUpdated()) {
203+
SetBatteryIcon();
204+
}
184205
}
185206

186207
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+
powerPresent = batteryController.IsPowerPresent();
106+
if (powerPresent.IsUpdated()) {
107+
lv_label_set_text(batteryPlug, BatteryIcon::GetPlugIcon(powerPresent.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> powerPresent {};
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)