2021-04-04 04:08:51 +02:00
|
|
|
#include "BatteryInfo.h"
|
|
|
|
#include "../DisplayApp.h"
|
|
|
|
#include "components/battery/BatteryController.h"
|
|
|
|
|
|
|
|
using namespace Pinetime::Applications::Screens;
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
static void lv_update_task(struct _lv_task_t* task) {
|
|
|
|
auto user_data = static_cast<BatteryInfo*>(task->user_data);
|
2021-04-04 04:08:51 +02:00
|
|
|
user_data->UpdateScreen();
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
static void lv_anim_task(struct _lv_task_t* task) {
|
|
|
|
auto user_data = static_cast<BatteryInfo*>(task->user_data);
|
2021-04-04 04:08:51 +02:00
|
|
|
user_data->UpdateAnim();
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
BatteryInfo::BatteryInfo(Pinetime::Applications::DisplayApp* app, Pinetime::Controllers::Battery& batteryController)
|
|
|
|
: Screen(app), batteryController {batteryController} {
|
2021-04-04 04:08:51 +02:00
|
|
|
|
|
|
|
batteryPercent = batteryController.PercentRemaining();
|
|
|
|
batteryVoltage = batteryController.Voltage();
|
|
|
|
|
|
|
|
charging_bar = lv_bar_create(lv_scr_act(), nullptr);
|
|
|
|
lv_obj_set_size(charging_bar, 200, 15);
|
|
|
|
lv_bar_set_range(charging_bar, 0, 100);
|
|
|
|
lv_obj_align(charging_bar, nullptr, LV_ALIGN_CENTER, 0, 10);
|
|
|
|
lv_bar_set_anim_time(charging_bar, 2000);
|
|
|
|
lv_obj_set_style_local_radius(charging_bar, LV_BAR_PART_BG, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
|
|
|
|
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_BG, LV_STATE_DEFAULT, lv_color_hex(0x222222));
|
|
|
|
lv_obj_set_style_local_bg_opa(charging_bar, LV_BAR_PART_BG, LV_STATE_DEFAULT, LV_OPA_100);
|
2021-04-18 19:28:14 +02:00
|
|
|
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, lv_color_hex(0xFF0000));
|
2021-04-04 04:08:51 +02:00
|
|
|
lv_bar_set_value(charging_bar, batteryPercent, LV_ANIM_OFF);
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
status = lv_label_create(lv_scr_act(), nullptr);
|
|
|
|
lv_label_set_text_static(status, "Reading Battery status");
|
2021-04-04 04:08:51 +02:00
|
|
|
lv_label_set_align(status, LV_LABEL_ALIGN_CENTER);
|
|
|
|
lv_obj_align(status, charging_bar, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
|
2021-04-18 19:28:14 +02:00
|
|
|
|
2021-04-04 04:08:51 +02:00
|
|
|
percent = lv_label_create(lv_scr_act(), nullptr);
|
|
|
|
lv_obj_set_style_local_text_font(percent, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76);
|
2021-07-11 16:55:06 +02:00
|
|
|
lv_label_set_text_fmt(percent, "%02i%%", batteryPercent);
|
2021-04-04 04:08:51 +02:00
|
|
|
lv_label_set_align(percent, LV_LABEL_ALIGN_LEFT);
|
|
|
|
lv_obj_align(percent, nullptr, LV_ALIGN_CENTER, 0, -60);
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
voltage = lv_label_create(lv_scr_act(), nullptr);
|
2021-04-04 04:08:51 +02:00
|
|
|
lv_obj_set_style_local_text_color(voltage, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xC6A600));
|
2021-07-02 17:30:32 +02:00
|
|
|
lv_label_set_text_fmt(voltage, "%1i.%02i volts", batteryVoltage / 1000, batteryVoltage % 1000 / 10);
|
2021-04-04 04:08:51 +02:00
|
|
|
lv_label_set_align(voltage, LV_LABEL_ALIGN_CENTER);
|
|
|
|
lv_obj_align(voltage, nullptr, LV_ALIGN_CENTER, 0, 95);
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
lv_obj_t* backgroundLabel = lv_label_create(lv_scr_act(), nullptr);
|
2021-04-04 04:08:51 +02:00
|
|
|
lv_label_set_long_mode(backgroundLabel, LV_LABEL_LONG_CROP);
|
|
|
|
lv_obj_set_size(backgroundLabel, 240, 240);
|
|
|
|
lv_obj_set_pos(backgroundLabel, 0, 0);
|
|
|
|
lv_label_set_text_static(backgroundLabel, "");
|
|
|
|
|
|
|
|
taskUpdate = lv_task_create(lv_update_task, 500000, LV_TASK_PRIO_LOW, this);
|
|
|
|
taskAnim = lv_task_create(lv_anim_task, 1000, LV_TASK_PRIO_LOW, this);
|
|
|
|
UpdateScreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
BatteryInfo::~BatteryInfo() {
|
|
|
|
lv_task_del(taskUpdate);
|
|
|
|
lv_task_del(taskAnim);
|
|
|
|
lv_obj_clean(lv_scr_act());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BatteryInfo::UpdateAnim() {
|
|
|
|
batteryPercent = batteryController.PercentRemaining();
|
|
|
|
|
2021-07-11 16:55:06 +02:00
|
|
|
if (batteryController.IsCharging() and batteryPercent < 100) {
|
|
|
|
animation += 1;
|
|
|
|
if (animation >= 100) {
|
|
|
|
animation = 0;
|
2021-04-04 04:08:51 +02:00
|
|
|
}
|
|
|
|
|
2021-07-11 16:55:06 +02:00
|
|
|
} else {
|
|
|
|
if (animation > batteryPercent) {
|
|
|
|
animation--;
|
|
|
|
}
|
|
|
|
if (animation < batteryPercent) {
|
|
|
|
animation++;
|
|
|
|
}
|
2021-04-04 04:08:51 +02:00
|
|
|
}
|
2021-07-11 16:55:06 +02:00
|
|
|
|
|
|
|
lv_bar_set_value(charging_bar, animation, LV_ANIM_OFF);
|
2021-04-04 04:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BatteryInfo::UpdateScreen() {
|
|
|
|
|
|
|
|
batteryController.Update();
|
|
|
|
|
|
|
|
batteryPercent = batteryController.PercentRemaining();
|
|
|
|
batteryVoltage = batteryController.Voltage();
|
|
|
|
|
2021-07-11 16:55:06 +02:00
|
|
|
if (batteryController.IsCharging() and batteryPercent < 100) {
|
|
|
|
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_RED);
|
|
|
|
lv_label_set_text_static(status, "Battery charging");
|
|
|
|
} 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, "Battery is fully charged");
|
|
|
|
} else if (batteryPercent < 10) {
|
|
|
|
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 is low");
|
2021-04-04 04:08:51 +02:00
|
|
|
} else {
|
2021-07-11 16:55:06 +02:00
|
|
|
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_GREEN);
|
|
|
|
lv_label_set_text_static(status, "Battery discharging");
|
2021-04-04 04:08:51 +02:00
|
|
|
}
|
|
|
|
|
2021-07-11 16:55:06 +02:00
|
|
|
lv_label_set_text_fmt(percent, "%02i%%", batteryPercent);
|
|
|
|
|
2021-04-04 04:08:51 +02:00
|
|
|
lv_obj_align(status, charging_bar, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
|
2021-07-02 17:30:32 +02:00
|
|
|
lv_label_set_text_fmt(voltage, "%1i.%02i volts", batteryVoltage / 1000, batteryVoltage % 1000 / 10);
|
2021-04-04 04:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BatteryInfo::Refresh() {
|
2021-04-18 19:28:14 +02:00
|
|
|
|
2021-04-04 04:08:51 +02:00
|
|
|
return running;
|
|
|
|
}
|