Files
bike/bike.c
T
2026-05-17 13:33:27 +08:00

188 lines
6.3 KiB
C

#include "bike.h"
#include "widgets/battery_indicator.h"
#include "widgets/speedmeter.h"
#include "widgets/info_card.h"
static lv_obj_t* container = NULL;
static lv_theme_t* theme_original;
static const uint32_t bg_color = 0x040507;
static lv_obj_t* battery_indicator = NULL;
static lv_obj_t* speed_meter = NULL;
LV_FONT_DECLARE(lv_font_din_16);
static void battery_timer_cb(lv_timer_t* timer) {
int8_t battery_percent = lv_battery_indicator_get_percentage(battery_indicator);
battery_percent -= 1;
static int accelerating = 0;
if (speed_meter != NULL) {
if (accelerating) {
lv_speedmeter_set_speed(speed_meter, lv_speedmeter_get_speed(speed_meter) + 1.5f);
if (lv_speedmeter_get_speed(speed_meter) >= 90.0f) {
accelerating = 0;
}
}
else {
lv_speedmeter_set_speed(speed_meter, lv_speedmeter_get_speed(speed_meter) - 1.0f);
if (lv_speedmeter_get_speed(speed_meter) <= 0.0f) {
accelerating = 1;
}
}
}
if (battery_percent < 0)
{
battery_percent = 100;
}
lv_battery_indicator_set_percentage(battery_indicator, (uint8_t)battery_percent);
}
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);
lv_obj_set_style_text_font(time_bar, &lv_font_din_16, 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
battery_indicator = lv_battery_indicator_create(header);
lv_obj_t* timer = lv_timer_create(battery_timer_cb, 3 * 1000, NULL);
}
void bike_init(lv_obj_t* parent) {
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);
lv_obj_set_scrollbar_mode(container, LV_SCROLLBAR_MODE_OFF);
create_header();
//内容
lv_obj_t* content = lv_obj_create(container);
lv_obj_set_size(content, lv_pct(100), LV_SIZE_CONTENT);
lv_obj_set_layout(content, LV_LAYOUT_FLEX);
lv_obj_set_flex_flow(content, LV_FLEX_FLOW_ROW);
lv_obj_set_style_pad_all(content, 2, 0);
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* speedmeter_container = lv_obj_create(content);
lv_obj_set_size(speedmeter_container, lv_pct(50), lv_pct(100));
lv_obj_set_style_bg_color(speedmeter_container, lv_color_hex(bg_color), 0);
speed_meter = lv_speedmeter_create(speedmeter_container, 90, "km/h");
//dashboard
lv_obj_t* dashboard = lv_obj_create(content);
lv_obj_set_size(dashboard, lv_pct(50), lv_pct(100));
lv_obj_set_style_bg_color(dashboard, lv_color_hex(bg_color), 0);
lv_obj_set_layout(dashboard, LV_LAYOUT_FLEX);
lv_obj_set_flex_flow(dashboard, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(
dashboard,
LV_FLEX_ALIGN_SPACE_BETWEEN,
LV_FLEX_ALIGN_CENTER,
LV_FLEX_ALIGN_CENTER
);
LV_IMG_DECLARE(stopwatch_icon);
lv_obj_t* time_info = lv_info_card_create(dashboard, &stopwatch_icon);
lv_info_card_set_title(time_info, "Duration");
lv_info_card_set_content(time_info, "01:23:35");
lv_info_card_set_footer(time_info, "hh:mm:ss");
LV_IMG_DECLARE(speed_icon);
lv_obj_t* speed_info = lv_info_card_create(dashboard, &speed_icon);
lv_info_card_set_title(speed_info, "Max Speed");
lv_info_card_set_content(speed_info, "20.5");
lv_info_card_set_footer(speed_info, "km/h");
LV_IMG_DECLARE(road_icon);
lv_obj_t* distance_info = lv_info_card_create(dashboard, &road_icon);
lv_info_card_set_title(distance_info, "Distance");
lv_info_card_set_content(distance_info, "120.8");
lv_info_card_set_footer(distance_info, "km");
//底部
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);
lv_obj_set_layout(footer, LV_LAYOUT_FLEX);
lv_obj_set_flex_flow(footer, LV_FLEX_FLOW_ROW);
lv_obj_set_style_pad_all(footer, 2, 0);
lv_obj_set_flex_align(footer,
LV_FLEX_ALIGN_START,
LV_FLEX_ALIGN_CENTER,
LV_FLEX_ALIGN_CENTER);
LV_IMG_DECLARE(warning_icon);
lv_obj_t* warning_image = lv_image_create(footer);
lv_image_set_src(warning_image, &warning_icon);
lv_obj_t* warning_label = lv_label_create(footer);
lv_obj_set_style_text_color(warning_label, lv_color_hex(0xB8BDC7), 0);
lv_obj_set_style_text_font(warning_label, &lv_font_din_16, 0);
lv_label_set_text(warning_label, "Drive Safely");
}
void bike_deinit() {
if (container) {
lv_obj_delete(container);
container = NULL;
}
}