1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#include <cstdint>
#include <cmath>
#include "displayapp/screens/BatteryIcon.h"
#include "displayapp/screens/Symbols.h"
#include "displayapp/icons/battery/batteryicon.c"
#include "displayapp/InfiniTimeTheme.h"
#include <lvgl/src/lv_misc/lv_color.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 "";
}
lv_color_t BatteryIcon::ColorFromPercentage(int batteryPercent) {
// HSV color model has red at 0° and green at 120°.
// We lock saturation and brightness at 100% and traverse the cylinder
// between red and green, thus avoiding the darker RGB on medium battery
// charges and giving us a much nicer color range.
const float normalizedPercent = batteryPercent / 100.0f;
// Follow -e^{-4x} + 1 curve: begins going yellow around 25%, yellow around 15%, red around 5%
const uint8_t hue = (-std::exp(-4.0f * normalizedPercent) + 1.0f) * 120.0f;
return lv_color_hsv_to_rgb(hue, 100, 100);
}
|