#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) LV_FONT_DECLARE(lv_font_din_16); 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_obj_set_layout(obj, LV_LAYOUT_FLEX); lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_ROW); lv_battery_indicator_t* self = (lv_battery_indicator_t*)obj; LV_IMG_DECLARE(battery_icon); lv_obj_t* indicator_container = lv_obj_create(obj); lv_obj_set_style_bg_img_src(indicator_container, &battery_icon, 0); lv_obj_set_size(indicator_container, 26, 16); lv_obj_set_style_margin_right(indicator_container, 4, 0); lv_obj_set_style_pad_all(indicator_container, 2, 0); lv_obj_set_style_pad_left(indicator_container, 3, 0); lv_obj_set_style_pad_right(indicator_container, 6, 0); self->indicator = lv_obj_create(indicator_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(obj); lv_obj_set_style_bg_color(self->label, lv_color_hex(0x040507), 0); lv_obj_set_size(self->label, 24, LV_SIZE_CONTENT); lv_obj_set_style_text_font(self->label, &lv_font_din_16, 0); 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_text_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_bg_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 = LV_CLAMP(0, value, 100); lv_label_set_text_fmt(self->label, "%d%%", self->percentage); lv_obj_set_width(self->indicator, lv_pct(LV_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; }