1
0
Fork 0
InfiniTime/src/displayapp/screens/BatteryIcon.cpp
Riku Isokoski 08b4cfbb50 Add low battery indicator to StatusIcons, digital and analog watchfaces
Define deepOrange color in InfiniTimeTheme
2023-03-02 13:38:31 +02:00

53 lines
1.8 KiB
C++

#include "displayapp/screens/BatteryIcon.h"
#include <cstdint>
#include "displayapp/screens/Symbols.h"
#include "displayapp/icons/battery/batteryicon.c"
#include "displayapp/InfiniTimeTheme.h"
using namespace Pinetime::Applications::Screens;
BatteryIcon::BatteryIcon(bool colorOnLowBattery) : colorOnLowBattery {colorOnLowBattery} {};
void BatteryIcon::Create(lv_obj_t* parent) {
batteryImg = lv_img_create(parent, nullptr);
lv_img_set_src(batteryImg, &batteryicon);
lv_obj_set_style_local_image_recolor(batteryImg, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
batteryJuice = lv_obj_create(batteryImg, nullptr);
lv_obj_set_width(batteryJuice, 8);
lv_obj_align(batteryJuice, nullptr, LV_ALIGN_IN_BOTTOM_RIGHT, -2, -2);
lv_obj_set_style_local_radius(batteryJuice, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 0);
}
lv_obj_t* BatteryIcon::GetObject() {
return batteryImg;
}
void BatteryIcon::SetBatteryPercentage(uint8_t percentage) {
lv_obj_set_height(batteryJuice, percentage * 14 / 100);
lv_obj_realign(batteryJuice);
if (colorOnLowBattery) {
static constexpr int lowBatteryThreshold = 15;
static constexpr int criticalBatteryThreshold = 5;
if (percentage > lowBatteryThreshold) {
SetColor(LV_COLOR_WHITE);
} else if (percentage > criticalBatteryThreshold) {
SetColor(LV_COLOR_ORANGE);
} else {
SetColor(Colors::deepOrange);
}
}
}
void BatteryIcon::SetColor(lv_color_t color) {
lv_obj_set_style_local_image_recolor(batteryImg, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, color);
lv_obj_set_style_local_image_recolor_opa(batteryImg, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);
lv_obj_set_style_local_bg_color(batteryJuice, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, color);
}
const char* BatteryIcon::GetPlugIcon(bool isCharging) {
if (isCharging)
return Symbols::plug;
else
return "";
}