1、refactor battery indicator
This commit is contained in:
1 parent
0becd90755
commit
9686a7ff68
5 files changed
+160
-37
No files matched your search
@@ -1,35 +1,22 @@
|
||||
#include "bike.h"
|
||||
#include "bike.h"
|
||||
#include "widgets/battery_indicator.h"
|
||||
|
||||
static lv_obj_t* container = NULL;
|
||||
|
||||
static lv_theme_t* theme_original;
|
||||
static const uint32_t bg_color = 0x040507;
|
||||
static int32_t battery_percent = 15;
|
||||
|
||||
static lv_obj_t* battery_indicator = NULL;
|
||||
static lv_obj_t* battery_label = NULL;
|
||||
|
||||
static void update_battery_indicator() {
|
||||
lv_label_set_text_fmt(battery_label, "%d%%", battery_percent);
|
||||
lv_obj_set_width(battery_indicator, lv_pct(max(battery_percent, 10)));
|
||||
if (battery_percent >= 60) {
|
||||
lv_obj_set_style_bg_color(battery_indicator, lv_color_hex(0x00ff00), 0);
|
||||
}
|
||||
else if (battery_percent >= 20) {
|
||||
lv_obj_set_style_bg_color(battery_indicator, lv_color_hex(0xffff00), 0);
|
||||
}
|
||||
else {
|
||||
lv_obj_set_style_bg_color(battery_indicator, lv_color_hex(0xff0000), 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void battery_timer_cb(lv_timer_t* timer) {
|
||||
static void battery_timer_cb(lv_timer_t* timer) {
|
||||
int8_t battery_percent = lv_battery_indicator_get_percentage(battery_indicator);
|
||||
battery_percent -= 1;
|
||||
if (battery_percent < 0)
|
||||
{
|
||||
battery_percent = 100;
|
||||
}
|
||||
update_battery_indicator();
|
||||
lv_battery_indicator_set_percentage(battery_indicator, (uint8_t)battery_percent);
|
||||
|
||||
}
|
||||
|
||||
@@ -59,29 +46,30 @@ static void create_header() {
|
||||
lv_obj_set_style_bg_color(gap_bar, lv_color_hex(bg_color), 0);
|
||||
|
||||
//battery
|
||||
//icon
|
||||
LV_IMG_DECLARE(battery);
|
||||
lv_obj_t* battery_container = lv_obj_create(header);
|
||||
lv_obj_set_style_bg_img_src(battery_container, &battery, 0);
|
||||
lv_obj_set_size(battery_container, 26, 16);
|
||||
lv_obj_set_style_margin_right(battery_container, 4, 0);
|
||||
//icon
|
||||
battery_indicator = lv_battery_indicator_create(header);
|
||||
//LV_IMG_DECLARE(battery_icon);
|
||||
//lv_obj_t* battery_container = lv_obj_create(header);
|
||||
//lv_obj_set_style_bg_img_src(battery_container, &battery_icon, 0);
|
||||
//lv_obj_set_size(battery_container, 26, 16);
|
||||
//lv_obj_set_style_margin_right(battery_container, 4, 0);
|
||||
|
||||
lv_obj_set_style_pad_all(battery_container, 2, 0);
|
||||
lv_obj_set_style_pad_left(battery_container, 3, 0);
|
||||
lv_obj_set_style_pad_right(battery_container, 6, 0);
|
||||
//lv_obj_set_style_pad_all(battery_container, 2, 0);
|
||||
//lv_obj_set_style_pad_left(battery_container, 3, 0);
|
||||
//lv_obj_set_style_pad_right(battery_container, 6, 0);
|
||||
|
||||
battery_indicator = lv_obj_create(battery_container);
|
||||
lv_obj_set_size(battery_indicator, lv_pct(battery_percent), lv_pct(90));
|
||||
lv_obj_set_style_radius(battery_indicator, 2, 0);
|
||||
lv_obj_set_align(battery_indicator, LV_ALIGN_LEFT_MID);
|
||||
//battery_indicator = lv_obj_create(battery_container);
|
||||
//lv_obj_set_size(battery_indicator, lv_pct(battery_percent), lv_pct(90));
|
||||
//lv_obj_set_style_radius(battery_indicator, 2, 0);
|
||||
//lv_obj_set_align(battery_indicator, LV_ALIGN_LEFT_MID);
|
||||
|
||||
|
||||
battery_label = lv_label_create(header);
|
||||
lv_obj_set_style_bg_color(battery_label, lv_color_hex(bg_color), 0);
|
||||
lv_obj_set_size(battery_label, 40, LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_text_color(battery_label, lv_color_hex(0xffffff), 0);
|
||||
//battery_label = lv_label_create(header);
|
||||
//lv_obj_set_style_bg_color(battery_label, lv_color_hex(bg_color), 0);
|
||||
//lv_obj_set_size(battery_label, 40, LV_SIZE_CONTENT);
|
||||
//lv_obj_set_style_text_color(battery_label, lv_color_hex(0xffffff), 0);
|
||||
|
||||
update_battery_indicator();
|
||||
//update_battery_indicator();
|
||||
|
||||
lv_obj_t* timer = lv_timer_create(battery_timer_cb, 3 * 1000, NULL);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user