1、refactor battery indicator
This commit is contained in:
1 parent
0becd90755
commit
9686a7ff68
5 files changed
+160
-37
No files matched your search
@@ -0,0 +1,109 @@
|
||||
|
||||
#include "lvgl.h"
|
||||
#include "battery_indicator.h"
|
||||
#include "battery_indicator_private.h"
|
||||
#include "src/core/lv_obj_class_private.h"
|
||||
|
||||
#define MY_CLASS (&lv_battery_indicator_class)
|
||||
|
||||
static void lv_battery_indicator_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj);
|
||||
static void lv_battery_indicator_event(const lv_obj_class_t* class_p, lv_event_t* e);
|
||||
|
||||
|
||||
const lv_obj_class_t lv_battery_indicator_class = {
|
||||
.constructor_cb = lv_battery_indicator_constructor,
|
||||
.event_cb = lv_battery_indicator_event,
|
||||
.width_def = LV_SIZE_CONTENT,
|
||||
.height_def = LV_SIZE_CONTENT,
|
||||
.instance_size = sizeof(lv_battery_indicator_t),
|
||||
.base_class = &lv_obj_class,
|
||||
.name = "lv_battery_indicator",
|
||||
LV_PROPERTY_CLASS_FIELDS(battery_indicator, BATTERY_INDICATOR)
|
||||
};
|
||||
|
||||
lv_obj_t* lv_battery_indicator_create(lv_obj_t* parent) {
|
||||
LV_LOG_INFO("begin");
|
||||
lv_obj_t* obj = lv_obj_class_create_obj(MY_CLASS, parent);
|
||||
lv_obj_class_init_obj(obj);
|
||||
|
||||
lv_battery_indicator_t* self = (lv_battery_indicator_t*)obj;
|
||||
|
||||
|
||||
LV_IMG_DECLARE(battery_icon);
|
||||
lv_obj_t* battery_container = obj;
|
||||
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);
|
||||
|
||||
self->indicator = lv_obj_create(battery_container);
|
||||
lv_obj_set_size(self->indicator, lv_pct(self->percentage), lv_pct(90));
|
||||
lv_obj_set_style_radius(self->indicator, 2, 0);
|
||||
lv_obj_set_align(self->indicator, LV_ALIGN_LEFT_MID);
|
||||
|
||||
|
||||
self->label = lv_label_create(parent);
|
||||
lv_obj_set_style_bg_color(self->label, lv_color_hex(0x040507), 0);
|
||||
lv_obj_set_size(self->label, 40, LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_text_color(self->label, lv_color_hex(0xffffff), 0);
|
||||
|
||||
lv_battery_indicator_set_percentage(obj, lv_battery_indicator_get_percentage(obj));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
void lv_battery_indicator_set_text_color_hex(lv_obj_t* obj, uint32_t c) {
|
||||
lv_battery_indicator_t* self = (lv_battery_indicator_t*)obj;
|
||||
lv_obj_set_style_bg_color(self->label, lv_color_hex(c), 0);
|
||||
}
|
||||
|
||||
void lv_battery_indicator_set_text_bg_color_hex(lv_obj_t* obj, uint32_t c) {
|
||||
lv_battery_indicator_t* self = (lv_battery_indicator_t*)obj;
|
||||
lv_obj_set_style_text_color(self->label, lv_color_hex(c), 0);
|
||||
}
|
||||
|
||||
void lv_battery_indicator_set_percentage(lv_obj_t* obj, uint8_t value) {
|
||||
lv_battery_indicator_t* self = (lv_battery_indicator_t*)obj;
|
||||
self->percentage = value;
|
||||
lv_label_set_text_fmt(self->label, "%d%%", self->percentage);
|
||||
lv_obj_set_width(self->indicator, lv_pct(max(self->percentage, 10)));
|
||||
if (self->percentage >= 60) {
|
||||
lv_obj_set_style_bg_color(self->indicator, lv_color_hex(0x00ff00), 0);
|
||||
}
|
||||
else if (self->percentage >= 20) {
|
||||
lv_obj_set_style_bg_color(self->indicator, lv_color_hex(0xffff00), 0);
|
||||
}
|
||||
else {
|
||||
lv_obj_set_style_bg_color(self->indicator, lv_color_hex(0xff0000), 0);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t lv_battery_indicator_get_percentage(lv_obj_t* obj) {
|
||||
lv_battery_indicator_t* self = (lv_battery_indicator_t*)obj;
|
||||
return self->percentage;
|
||||
}
|
||||
|
||||
static void lv_battery_indicator_constructor(const lv_obj_class_t* class_p, lv_obj_t* obj) {
|
||||
LV_UNUSED(class_p);
|
||||
LV_TRACE_OBJ_CREATE("begin");
|
||||
|
||||
lv_battery_indicator_t* indicator = (lv_battery_indicator_t*)obj;
|
||||
|
||||
indicator->percentage = 0;
|
||||
|
||||
lv_obj_remove_flag(obj, LV_OBJ_FLAG_CLICKABLE);
|
||||
|
||||
LV_TRACE_OBJ_CREATE("finished");
|
||||
|
||||
}
|
||||
static void lv_battery_indicator_event(const lv_obj_class_t* class_p, lv_event_t* e) {
|
||||
LV_UNUSED(class_p);
|
||||
|
||||
lv_result_t res;
|
||||
|
||||
res = lv_obj_event_base(MY_CLASS, e);
|
||||
if (res != LV_RESULT_OK) return;
|
||||
}
|
||||
Reference in new issue
Block a user