Files
bike/bike.c
T
2026-04-27 20:59:06 +08:00

133 lines
4.5 KiB
C

#include "bike.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) {
battery_percent -= 1;
if (battery_percent < 0)
{
battery_percent = 100;
}
update_battery_indicator();
}
static void create_header() {
//header
lv_obj_t* header = lv_obj_create(container);
lv_obj_set_size(header, lv_pct(100), 30);
lv_obj_set_style_bg_color(header, lv_color_hex(bg_color), 0);
lv_obj_set_style_border_side(header, LV_BORDER_SIDE_BOTTOM, 0);
lv_obj_set_flex_align(header, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_set_layout(header, LV_LAYOUT_FLEX);
lv_obj_set_flex_flow(header, LV_FLEX_FLOW_ROW);
lv_obj_set_style_pad_all(header, 4, 0);
lv_obj_set_style_border_color(header, lv_color_hex(0x191B1D), 0);
lv_obj_set_style_border_width(header, 1, 0);
//time
lv_obj_t* time_bar = lv_label_create(header);
lv_obj_set_style_bg_color(time_bar, lv_color_hex(bg_color), 0);
lv_label_set_text(time_bar, "12:35");
lv_obj_set_style_text_color(time_bar, lv_color_hex(0xffffff), 0);
//gap
lv_obj_t* gap_bar = lv_obj_create(header);
lv_obj_set_size(gap_bar, LV_SIZE_CONTENT, lv_pct(100));
lv_obj_set_flex_grow(gap_bar, 1);
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);
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_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();
lv_obj_t* timer = lv_timer_create(battery_timer_cb, 3 * 1000, NULL);
}
void bike_init(lv_obj_t* parent) {
//assert(container == NULL);
lv_display_t* display = lv_display_get_default();
theme_original = lv_display_get_theme(display);
lv_theme_t* theme = lv_theme_simple_init(display);
lv_display_set_theme(display, theme);
container = lv_obj_create(parent);
lv_obj_set_size(container, lv_pct(100), lv_pct(100));
lv_obj_align(container, LV_ALIGN_CENTER, 0, 0);
lv_obj_set_flex_flow(container, LV_FLEX_FLOW_COLUMN);
lv_obj_set_layout(container, LV_LAYOUT_FLEX);
lv_obj_set_style_bg_color(container, lv_color_hex(bg_color), 0);
create_header();
//内容
lv_obj_t* content = lv_obj_create(container);
lv_obj_set_size(content, lv_pct(100), LV_SIZE_CONTENT);
lv_obj_set_style_bg_color(content, lv_color_hex(bg_color), 0);
lv_obj_set_style_margin_top(content, 5, 0);
lv_obj_set_flex_grow(content, 1);
//底部
lv_obj_t* footer = lv_obj_create(container);
lv_obj_set_size(footer, lv_pct(100), 30);
lv_obj_set_style_bg_color(footer, lv_color_hex(bg_color), 0);
lv_obj_set_style_margin_top(footer, 5, 0);
lv_obj_set_style_border_side(footer, LV_BORDER_SIDE_FULL, 0);
lv_obj_set_style_border_color(footer, lv_color_hex(0x191B1D), 0);
lv_obj_set_style_border_width(footer, 1, 0);
}
void bike_deinit() {
if (container) {
lv_obj_delete(container);
container = NULL;
}
}