feat(display): 添加完整的LVGL显示界面和电源管理功能
- 集成自定义字体ui_font_keyboard_small_18和ui_font_keyboard_time_48 - 配置LVGL编译选项,启用flex布局和压缩字体支持 - 实现完整的显示UI界面,包括日期时间、电池状态、连接状态等组件 - 添加显示模块的电源管理功能,支持自动休眠和唤醒 - 实现与电池状态、键盘LED、模式切换等事件的交互响应 - 添加1分钟空闲超时自动熄屏功能 - 使用自定义精简字体替换默认蒙特塞拉特字体
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/drivers/display.h>
|
||||
@@ -9,32 +13,100 @@
|
||||
#include <lvgl_zephyr.h>
|
||||
|
||||
#define MODULE display
|
||||
#include <caf/events/button_event.h>
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include "battery_status_event.h"
|
||||
#include "keyboard_led_event.h"
|
||||
#include "mode_event.h"
|
||||
#include "time_manager.h"
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define DISPLAY_UPDATE_PERIOD_MS 1000
|
||||
#define DISPLAY_IDLE_TIMEOUT_MIN 1
|
||||
#define DISPLAY_BACKLIGHT_BRIGHTNESS 100
|
||||
#define DISPLAY_DEMO_BASE_YEAR 2026
|
||||
#define DISPLAY_DEMO_BASE_MONTH 3
|
||||
#define DISPLAY_DEMO_BASE_DAY 27
|
||||
#define DISPLAY_DEMO_BASE_HOUR 14
|
||||
#define DISPLAY_DEMO_BASE_MIN 28
|
||||
#define DISPLAY_DEMO_BASE_SEC 36
|
||||
#define DISPLAY_SYMBOL_PLUG "\xEF\x87\xA6" /* U+F1E6, custom plug glyph in ui_font_keyboard_small_18 */
|
||||
|
||||
struct display_ctx {
|
||||
LV_FONT_DECLARE(ui_font_keyboard_small_18);
|
||||
LV_FONT_DECLARE(ui_font_keyboard_time_48);
|
||||
|
||||
enum display_status_id
|
||||
{
|
||||
DISPLAY_STATUS_USB = 0,
|
||||
DISPLAY_STATUS_BLE,
|
||||
DISPLAY_STATUS_NUMLOCK,
|
||||
DISPLAY_STATUS_CAPSLOCK,
|
||||
DISPLAY_STATUS_COUNT,
|
||||
};
|
||||
|
||||
enum display_pm_state
|
||||
{
|
||||
DISPLAY_PM_STATE_ACTIVE = 0,
|
||||
DISPLAY_PM_STATE_OFF,
|
||||
};
|
||||
|
||||
struct display_ui_state
|
||||
{
|
||||
lv_color_t theme_color;
|
||||
lv_color_t inactive_border_color;
|
||||
uint8_t battery_level;
|
||||
mode_type_t mode;
|
||||
uint8_t led_mask;
|
||||
uint8_t battery_flags;
|
||||
bool status_enabled[DISPLAY_STATUS_COUNT];
|
||||
lv_obj_t *status_badges[DISPLAY_STATUS_COUNT];
|
||||
lv_obj_t *status_labels[DISPLAY_STATUS_COUNT];
|
||||
lv_obj_t *battery_icon;
|
||||
lv_obj_t *battery_label;
|
||||
lv_obj_t *battery_state_label;
|
||||
lv_obj_t *date_label;
|
||||
lv_obj_t *time_label;
|
||||
};
|
||||
|
||||
struct display_ctx
|
||||
{
|
||||
const struct device *dev;
|
||||
struct display_capabilities caps;
|
||||
struct k_work_delayable update_work;
|
||||
lv_obj_t *title_label;
|
||||
lv_obj_t *count_label;
|
||||
struct k_work_delayable idle_work;
|
||||
struct display_ui_state ui;
|
||||
uint32_t tick_count;
|
||||
bool ui_ready;
|
||||
enum display_pm_state pm_state;
|
||||
bool initialized;
|
||||
};
|
||||
|
||||
static struct display_ctx disp = {
|
||||
.dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display)),
|
||||
.ui.theme_color = LV_COLOR_MAKE(0x4C, 0xC9, 0xF0),
|
||||
.ui.inactive_border_color = LV_COLOR_MAKE(0xA0, 0xA7, 0xB4),
|
||||
.ui.battery_level = 15U,
|
||||
.ui.battery_flags = 0U,
|
||||
.ui.mode = MODE_TYPE_USB,
|
||||
.ui.status_enabled = {true, true, false, true},
|
||||
.pm_state = DISPLAY_PM_STATE_OFF,
|
||||
};
|
||||
|
||||
static const struct led_dt_spec display_backlight =
|
||||
LED_DT_SPEC_GET(DT_NODELABEL(backlight));
|
||||
|
||||
static const char *const g_status_texts[DISPLAY_STATUS_COUNT] = {
|
||||
LV_SYMBOL_USB,
|
||||
LV_SYMBOL_BLUETOOTH,
|
||||
"1",
|
||||
"A",
|
||||
};
|
||||
|
||||
static void display_refresh_all_locked(void);
|
||||
|
||||
static void display_schedule_update(k_timeout_t delay)
|
||||
{
|
||||
#ifdef CONFIG_LV_Z_RUN_LVGL_ON_WORKQUEUE
|
||||
@@ -44,136 +116,581 @@ static void display_schedule_update(k_timeout_t delay)
|
||||
#endif
|
||||
}
|
||||
|
||||
static int display_backlight_init(void)
|
||||
static void display_schedule_idle_timeout(k_timeout_t delay)
|
||||
{
|
||||
k_work_reschedule(&disp.idle_work, delay);
|
||||
}
|
||||
|
||||
/* 背光初始化独立处理,避免 UI 创建逻辑里混入硬件使能细节。 */
|
||||
static int display_backlight_set(uint8_t brightness)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!led_is_ready_dt(&display_backlight)) {
|
||||
if (!led_is_ready_dt(&display_backlight))
|
||||
{
|
||||
LOG_WRN("Display backlight device not ready");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* 背光亮度交给 pwm-leds 驱动管理,这样后面如果要做调光、呼吸灯或亮度档位,
|
||||
* 都可以直接沿用 Zephyr 的 LED/PWM 接口,而不需要再单独碰 PWM 寄存器。
|
||||
*/
|
||||
err = led_set_brightness_dt(&display_backlight, DISPLAY_BACKLIGHT_BRIGHTNESS);
|
||||
if (err) {
|
||||
LOG_ERR("Failed to set backlight brightness: %d", err);
|
||||
err = led_set_brightness_dt(&display_backlight, brightness);
|
||||
if (err)
|
||||
{
|
||||
LOG_ERR("Failed to set backlight brightness(%u): %d", brightness, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void display_create_ui_locked(void)
|
||||
static bool display_is_active(void)
|
||||
{
|
||||
lv_obj_t *screen = lv_screen_active();
|
||||
|
||||
/*
|
||||
* 先显式设置背景和文字颜色,避免把“有画面但颜色刚好看不见”误判为
|
||||
* “LVGL 没有刷新”。这里使用高对比度配色,便于快速验证渲染链路。
|
||||
*/
|
||||
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, LV_PART_MAIN);
|
||||
lv_obj_set_style_bg_color(screen, lv_color_hex(0x102A43), LV_PART_MAIN);
|
||||
lv_obj_set_style_text_color(screen, lv_color_hex(0xF0F4F8), LV_PART_MAIN);
|
||||
lv_obj_clean(screen);
|
||||
|
||||
disp.title_label = lv_label_create(screen);
|
||||
lv_label_set_text(disp.title_label, "Zephyr LVGL running");
|
||||
lv_obj_set_style_text_color(disp.title_label, lv_color_hex(0xF0F4F8), LV_PART_MAIN);
|
||||
lv_obj_align(disp.title_label, LV_ALIGN_CENTER, 0, -16);
|
||||
|
||||
disp.count_label = lv_label_create(screen);
|
||||
lv_label_set_text(disp.count_label, "tick 0");
|
||||
lv_obj_set_style_text_color(disp.count_label, lv_color_hex(0xFFD166), LV_PART_MAIN);
|
||||
lv_obj_align(disp.count_label, LV_ALIGN_CENTER, 0, 16);
|
||||
|
||||
disp.ui_ready = true;
|
||||
return disp.pm_state == DISPLAY_PM_STATE_ACTIVE;
|
||||
}
|
||||
|
||||
static void display_update_work_fn(struct k_work *work)
|
||||
/* 只负责保活屏幕空闲计时,不隐式点亮屏幕。 */
|
||||
static void display_kick_idle_timer(void)
|
||||
{
|
||||
char count_str[24];
|
||||
lv_color_t bg_color;
|
||||
if (!disp.initialized || !display_is_active())
|
||||
return;
|
||||
|
||||
ARG_UNUSED(work);
|
||||
display_schedule_idle_timeout(K_MINUTES(DISPLAY_IDLE_TIMEOUT_MIN));
|
||||
}
|
||||
|
||||
if (!disp.initialized) {
|
||||
/* 熄屏时同时关闭刷新和背光,并将模块状态切到 OFF。 */
|
||||
static void display_sleep(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!disp.initialized || !display_is_active())
|
||||
return;
|
||||
|
||||
(void)k_work_cancel_delayable(&disp.update_work);
|
||||
(void)k_work_cancel_delayable(&disp.idle_work);
|
||||
|
||||
err = display_blanking_on(disp.dev);
|
||||
if (err)
|
||||
LOG_WRN("Display blanking on failed: %d", err);
|
||||
|
||||
(void)display_backlight_set(0U);
|
||||
disp.pm_state = DISPLAY_PM_STATE_OFF;
|
||||
module_set_state(MODULE_STATE_OFF);
|
||||
}
|
||||
|
||||
/* 唤醒屏幕后立刻刷新 UI,并重新启动定时刷新和空闲超时。 */
|
||||
static void display_wake(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!disp.initialized)
|
||||
return;
|
||||
|
||||
if (display_is_active()) {
|
||||
display_kick_idle_timer();
|
||||
return;
|
||||
}
|
||||
|
||||
lvgl_lock();
|
||||
err = display_blanking_off(disp.dev);
|
||||
if (err)
|
||||
LOG_WRN("Display blanking off failed: %d", err);
|
||||
|
||||
if (!disp.ui_ready) {
|
||||
display_create_ui_locked();
|
||||
(void)display_backlight_set(DISPLAY_BACKLIGHT_BRIGHTNESS);
|
||||
|
||||
lvgl_lock();
|
||||
display_refresh_all_locked();
|
||||
lvgl_unlock();
|
||||
|
||||
disp.pm_state = DISPLAY_PM_STATE_ACTIVE;
|
||||
display_schedule_update(K_NO_WAIT);
|
||||
display_kick_idle_timer();
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
|
||||
static void display_idle_timeout_fn(struct k_work *work)
|
||||
{
|
||||
ARG_UNUSED(work);
|
||||
|
||||
display_sleep();
|
||||
}
|
||||
|
||||
/* 电量颜色与 PC 原型保持一致,顶部状态区能快速表达健康度。 */
|
||||
static lv_color_t display_get_battery_color(uint8_t battery_level)
|
||||
{
|
||||
if (battery_level > 70U)
|
||||
return lv_color_hex(0x8BD450);
|
||||
|
||||
if (battery_level >= 20U)
|
||||
return lv_color_hex(0xF4D35E);
|
||||
|
||||
return lv_color_hex(0xE63946);
|
||||
}
|
||||
|
||||
/* 电池图标由精简图标字体提供,不再依赖 LVGL 内建字体资源。 */
|
||||
static const char *display_get_battery_symbol(uint8_t battery_level)
|
||||
{
|
||||
if (battery_level > 85U)
|
||||
return LV_SYMBOL_BATTERY_FULL;
|
||||
|
||||
if (battery_level > 60U)
|
||||
return LV_SYMBOL_BATTERY_3;
|
||||
|
||||
if (battery_level > 35U)
|
||||
return LV_SYMBOL_BATTERY_2;
|
||||
|
||||
if (battery_level >= 20U)
|
||||
return LV_SYMBOL_BATTERY_1;
|
||||
|
||||
return LV_SYMBOL_BATTERY_EMPTY;
|
||||
}
|
||||
|
||||
/* 模式事件只需要驱动 USB/BLE 两个 badge,2.4G 模式两者都灭。 */
|
||||
static void display_update_mode_state(mode_type_t mode)
|
||||
{
|
||||
disp.ui.mode = mode;
|
||||
disp.ui.status_enabled[DISPLAY_STATUS_USB] = (mode == MODE_TYPE_USB);
|
||||
disp.ui.status_enabled[DISPLAY_STATUS_BLE] = (mode == MODE_TYPE_BLE);
|
||||
}
|
||||
|
||||
/* 最新原型只显示 NumLock 和 CapsLock,不再展示 ScrollLock。 */
|
||||
static void display_update_keyboard_led_state(uint8_t led_mask)
|
||||
{
|
||||
disp.ui.led_mask = led_mask;
|
||||
disp.ui.status_enabled[DISPLAY_STATUS_NUMLOCK] =
|
||||
(led_mask & KEYBOARD_LED_MASK_NUM_LOCK) != 0U;
|
||||
disp.ui.status_enabled[DISPLAY_STATUS_CAPSLOCK] =
|
||||
(led_mask & KEYBOARD_LED_MASK_CAPS_LOCK) != 0U;
|
||||
}
|
||||
|
||||
/* 底部状态条的亮灭与边框颜色联动更新,保持原型机视觉语言。 */
|
||||
static void display_refresh_status_bar_locked(void)
|
||||
{
|
||||
for (uint32_t i = 0; i < DISPLAY_STATUS_COUNT; i++)
|
||||
{
|
||||
lv_obj_t *badge = disp.ui.status_badges[i];
|
||||
lv_obj_t *label = disp.ui.status_labels[i];
|
||||
bool active = disp.ui.status_enabled[i];
|
||||
|
||||
if (!badge || !label)
|
||||
continue;
|
||||
|
||||
lv_obj_set_style_border_width(badge, 4, 0);
|
||||
lv_obj_set_style_border_color(badge,
|
||||
active ? disp.ui.theme_color : disp.ui.inactive_border_color,
|
||||
0);
|
||||
lv_obj_set_style_bg_color(badge,
|
||||
active ? lv_color_hex(0x1D2735) : lv_color_hex(0x161A20),
|
||||
0);
|
||||
lv_obj_set_style_text_color(label,
|
||||
active ? lv_color_white() : lv_color_hex(0x7C8798),
|
||||
0);
|
||||
}
|
||||
}
|
||||
|
||||
/* 电池图标、百分比和状态图标分开更新,便于独立配色。 */
|
||||
static void display_refresh_battery_locked(void)
|
||||
{
|
||||
char battery_text[8];
|
||||
lv_color_t battery_color;
|
||||
const char *state_symbol = "";
|
||||
lv_color_t state_color = lv_color_white();
|
||||
|
||||
if (!disp.ui.battery_icon || !disp.ui.battery_label || !disp.ui.battery_state_label)
|
||||
return;
|
||||
|
||||
battery_color = display_get_battery_color(disp.ui.battery_level);
|
||||
snprintk(battery_text, sizeof(battery_text), "%u%%", disp.ui.battery_level);
|
||||
|
||||
if ((disp.ui.battery_flags & BATTERY_STATUS_FLAG_FULL) != 0U)
|
||||
{
|
||||
state_symbol = DISPLAY_SYMBOL_PLUG;
|
||||
state_color = lv_color_hex(0x4C9EF5);
|
||||
}
|
||||
else if ((disp.ui.battery_flags & BATTERY_STATUS_FLAG_CHARGING) != 0U)
|
||||
{
|
||||
state_symbol = LV_SYMBOL_CHARGE;
|
||||
state_color = lv_color_hex(0xF4D35E);
|
||||
}
|
||||
|
||||
bg_color = ((disp.tick_count & 0x01u) == 0U) ? lv_color_hex(0x102A43) :
|
||||
lv_color_hex(0x1F6F8B);
|
||||
lv_obj_set_style_bg_color(lv_screen_active(), bg_color, LV_PART_MAIN);
|
||||
lv_label_set_text(disp.ui.battery_icon,
|
||||
display_get_battery_symbol(disp.ui.battery_level));
|
||||
lv_obj_set_style_text_color(disp.ui.battery_icon, battery_color, 0);
|
||||
lv_label_set_text(disp.ui.battery_label, battery_text);
|
||||
lv_label_set_text(disp.ui.battery_state_label, state_symbol);
|
||||
lv_obj_set_style_text_color(disp.ui.battery_state_label, state_color, 0);
|
||||
}
|
||||
|
||||
snprintk(count_str, sizeof(count_str), "tick %u", disp.tick_count++);
|
||||
lv_label_set_text(disp.count_label, count_str);
|
||||
lv_obj_invalidate(lv_screen_active());
|
||||
/*
|
||||
* 时间优先显示 time_manager 的真实快照。
|
||||
* 如果当前尚未同步,则退回到固定基准上的 demo 时间,保证 UI 结构始终可见。
|
||||
*/
|
||||
static void display_refresh_datetime_locked(void)
|
||||
{
|
||||
struct time_manager_snapshot snapshot;
|
||||
char date_text[16];
|
||||
char time_text[16];
|
||||
int err = time_manager_get_snapshot(&snapshot);
|
||||
|
||||
if (!disp.ui.date_label || !disp.ui.time_label)
|
||||
return;
|
||||
|
||||
if (!err)
|
||||
{
|
||||
time_t local_seconds;
|
||||
struct tm tm_buf;
|
||||
struct tm *tm_info;
|
||||
|
||||
local_seconds = (time_t)(snapshot.utc_ms / 1000ULL) +
|
||||
(time_t)((int32_t)snapshot.timezone_min * 60);
|
||||
tm_info = gmtime_r(&local_seconds, &tm_buf);
|
||||
|
||||
if (tm_info)
|
||||
{
|
||||
unsigned int year = (unsigned int)(tm_info->tm_year + 1900);
|
||||
unsigned int month = (unsigned int)(tm_info->tm_mon + 1);
|
||||
unsigned int day = (unsigned int)tm_info->tm_mday;
|
||||
unsigned int hour = (unsigned int)tm_info->tm_hour;
|
||||
unsigned int minute = (unsigned int)tm_info->tm_min;
|
||||
unsigned int second = (unsigned int)tm_info->tm_sec;
|
||||
|
||||
snprintk(date_text, sizeof(date_text), "%04u/%02u/%02u",
|
||||
year, month, day);
|
||||
snprintk(time_text, sizeof(time_text), "%02u:%02u:%02u",
|
||||
hour, minute, second);
|
||||
lv_label_set_text(disp.ui.date_label, date_text);
|
||||
lv_label_set_text(disp.ui.time_label, time_text);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
uint32_t seconds = disp.tick_count;
|
||||
uint32_t hour = (DISPLAY_DEMO_BASE_HOUR + (seconds / 3600U)) % 24U;
|
||||
uint32_t minute = (DISPLAY_DEMO_BASE_MIN + ((seconds / 60U) % 60U)) % 60U;
|
||||
uint32_t second = (DISPLAY_DEMO_BASE_SEC + (seconds % 60U)) % 60U;
|
||||
|
||||
snprintk(date_text, sizeof(date_text), "%04d/%02d/%02d",
|
||||
DISPLAY_DEMO_BASE_YEAR,
|
||||
DISPLAY_DEMO_BASE_MONTH,
|
||||
DISPLAY_DEMO_BASE_DAY);
|
||||
snprintk(time_text, sizeof(time_text), "%02u:%02u:%02u",
|
||||
hour, minute, second);
|
||||
lv_label_set_text(disp.ui.date_label, date_text);
|
||||
lv_label_set_text(disp.ui.time_label, time_text);
|
||||
}
|
||||
}
|
||||
|
||||
/* 一次性把缓存状态刷到 UI,避免控件创建与状态恢复互相耦合。 */
|
||||
static void display_refresh_all_locked(void)
|
||||
{
|
||||
display_refresh_status_bar_locked();
|
||||
display_refresh_battery_locked();
|
||||
display_refresh_datetime_locked();
|
||||
}
|
||||
|
||||
/* 状态 badge 保持原型尺寸和圆角,确保在 320x172 面板上视觉一致。 */
|
||||
static void display_create_status_chip_locked(lv_obj_t *parent, enum display_status_id id)
|
||||
{
|
||||
lv_obj_t *badge = lv_obj_create(parent);
|
||||
lv_obj_t *label;
|
||||
|
||||
lv_obj_remove_style_all(badge);
|
||||
lv_obj_set_size(badge, 50, 32);
|
||||
lv_obj_set_style_radius(badge, 10, 0);
|
||||
lv_obj_set_style_bg_opa(badge, LV_OPA_COVER, 0);
|
||||
lv_obj_set_style_pad_all(badge, 0, 0);
|
||||
|
||||
label = lv_label_create(badge);
|
||||
lv_label_set_text(label, g_status_texts[id]);
|
||||
lv_obj_set_width(label, LV_PCT(100));
|
||||
lv_obj_set_style_text_font(label, &ui_font_keyboard_small_18, 0);
|
||||
lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
|
||||
lv_obj_center(label);
|
||||
|
||||
disp.ui.status_badges[id] = badge;
|
||||
disp.ui.status_labels[id] = label;
|
||||
}
|
||||
|
||||
/* UI 直接内联到 display_module,保留原型布局而不引入额外 ui 抽象层。 */
|
||||
static void display_create_ui_locked(void)
|
||||
{
|
||||
lv_obj_t *screen = lv_screen_active();
|
||||
lv_obj_t *content;
|
||||
lv_obj_t *top_row;
|
||||
lv_obj_t *battery_wrap;
|
||||
lv_obj_t *middle_row;
|
||||
lv_obj_t *bottom_row;
|
||||
|
||||
lv_obj_clean(screen);
|
||||
lv_obj_set_style_bg_color(screen, lv_color_hex(0x0F1115), 0);
|
||||
lv_obj_set_style_bg_grad_color(screen, lv_color_hex(0x1A1F29), 0);
|
||||
lv_obj_set_style_bg_grad_dir(screen, LV_GRAD_DIR_VER, 0);
|
||||
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
|
||||
lv_obj_set_style_text_color(screen, lv_color_white(), 0);
|
||||
lv_obj_set_style_pad_all(screen, 0, 0);
|
||||
lv_obj_set_scrollbar_mode(screen, LV_SCROLLBAR_MODE_OFF);
|
||||
|
||||
content = lv_obj_create(screen);
|
||||
lv_obj_remove_style_all(content);
|
||||
lv_obj_set_size(content, LV_PCT(100), LV_PCT(100));
|
||||
lv_obj_set_style_bg_color(content, lv_color_hex(0x0F1115), 0);
|
||||
lv_obj_set_style_bg_opa(content, LV_OPA_TRANSP, 0);
|
||||
lv_obj_set_style_pad_left(content, 14, 0);
|
||||
lv_obj_set_style_pad_right(content, 14, 0);
|
||||
lv_obj_set_style_pad_top(content, 8, 0);
|
||||
lv_obj_set_style_pad_bottom(content, 8, 0);
|
||||
lv_obj_set_layout(content, LV_LAYOUT_FLEX);
|
||||
lv_obj_set_flex_flow(content, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_flex_align(content,
|
||||
LV_FLEX_ALIGN_START,
|
||||
LV_FLEX_ALIGN_CENTER,
|
||||
LV_FLEX_ALIGN_CENTER);
|
||||
|
||||
top_row = lv_obj_create(content);
|
||||
lv_obj_remove_style_all(top_row);
|
||||
lv_obj_set_width(top_row, LV_PCT(100));
|
||||
lv_obj_set_flex_grow(top_row, 1);
|
||||
lv_obj_set_style_bg_color(top_row, lv_color_hex(0x0F1115), 0);
|
||||
lv_obj_set_style_bg_opa(top_row, LV_OPA_TRANSP, 0);
|
||||
lv_obj_set_layout(top_row, LV_LAYOUT_FLEX);
|
||||
lv_obj_set_flex_flow(top_row, LV_FLEX_FLOW_ROW);
|
||||
lv_obj_set_flex_align(top_row,
|
||||
LV_FLEX_ALIGN_SPACE_BETWEEN,
|
||||
LV_FLEX_ALIGN_CENTER,
|
||||
LV_FLEX_ALIGN_CENTER);
|
||||
|
||||
disp.ui.date_label = lv_label_create(top_row);
|
||||
lv_obj_set_style_text_font(disp.ui.date_label, &ui_font_keyboard_small_18, 0);
|
||||
lv_obj_set_style_text_color(disp.ui.date_label, lv_color_hex(0xD8DEE9), 0);
|
||||
|
||||
battery_wrap = lv_obj_create(top_row);
|
||||
lv_obj_remove_style_all(battery_wrap);
|
||||
lv_obj_set_width(battery_wrap, LV_SIZE_CONTENT);
|
||||
lv_obj_set_layout(battery_wrap, LV_LAYOUT_FLEX);
|
||||
lv_obj_set_flex_flow(battery_wrap, LV_FLEX_FLOW_ROW);
|
||||
lv_obj_set_flex_align(battery_wrap,
|
||||
LV_FLEX_ALIGN_CENTER,
|
||||
LV_FLEX_ALIGN_CENTER,
|
||||
LV_FLEX_ALIGN_CENTER);
|
||||
lv_obj_set_style_pad_column(battery_wrap, 4, 0);
|
||||
|
||||
disp.ui.battery_icon = lv_label_create(battery_wrap);
|
||||
lv_obj_set_style_text_font(disp.ui.battery_icon, &ui_font_keyboard_small_18, 0);
|
||||
|
||||
disp.ui.battery_label = lv_label_create(battery_wrap);
|
||||
lv_obj_set_style_text_font(disp.ui.battery_label, &ui_font_keyboard_small_18, 0);
|
||||
lv_obj_set_style_text_color(disp.ui.battery_label, lv_color_hex(0xD8DEE9), 0);
|
||||
|
||||
disp.ui.battery_state_label = lv_label_create(battery_wrap);
|
||||
lv_obj_set_style_text_font(disp.ui.battery_state_label, &ui_font_keyboard_small_18, 0);
|
||||
|
||||
middle_row = lv_obj_create(content);
|
||||
lv_obj_remove_style_all(middle_row);
|
||||
lv_obj_set_width(middle_row, LV_PCT(100));
|
||||
lv_obj_set_flex_grow(middle_row, 2);
|
||||
lv_obj_set_style_bg_color(middle_row, lv_color_hex(0x0F1115), 0);
|
||||
lv_obj_set_style_bg_opa(middle_row, LV_OPA_TRANSP, 0);
|
||||
|
||||
disp.ui.time_label = lv_label_create(middle_row);
|
||||
lv_obj_set_style_text_font(disp.ui.time_label, &ui_font_keyboard_time_48, 0);
|
||||
lv_obj_set_style_text_color(disp.ui.time_label, lv_color_white(), 0);
|
||||
lv_obj_center(disp.ui.time_label);
|
||||
|
||||
bottom_row = lv_obj_create(content);
|
||||
lv_obj_remove_style_all(bottom_row);
|
||||
lv_obj_set_width(bottom_row, LV_PCT(100));
|
||||
lv_obj_set_flex_grow(bottom_row, 1);
|
||||
lv_obj_set_style_bg_color(bottom_row, lv_color_hex(0x0F1115), 0);
|
||||
lv_obj_set_style_bg_opa(bottom_row, LV_OPA_TRANSP, 0);
|
||||
lv_obj_set_layout(bottom_row, LV_LAYOUT_FLEX);
|
||||
lv_obj_set_flex_flow(bottom_row, LV_FLEX_FLOW_ROW);
|
||||
lv_obj_set_flex_align(bottom_row,
|
||||
LV_FLEX_ALIGN_CENTER,
|
||||
LV_FLEX_ALIGN_CENTER,
|
||||
LV_FLEX_ALIGN_CENTER);
|
||||
lv_obj_set_style_pad_column(bottom_row, 6, 0);
|
||||
|
||||
for (uint32_t i = 0; i < DISPLAY_STATUS_COUNT; i++)
|
||||
display_create_status_chip_locked(bottom_row, (enum display_status_id)i);
|
||||
|
||||
display_refresh_all_locked();
|
||||
}
|
||||
|
||||
/* 周期刷新只负责时间区域;状态图标改为事件驱动,避免无谓重绘。 */
|
||||
static void display_update_work_fn(struct k_work *work)
|
||||
{
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!disp.initialized)
|
||||
return;
|
||||
|
||||
if (!display_is_active())
|
||||
return;
|
||||
|
||||
disp.tick_count++;
|
||||
|
||||
lvgl_lock();
|
||||
display_refresh_datetime_locked();
|
||||
lvgl_unlock();
|
||||
|
||||
display_schedule_update(K_MSEC(DISPLAY_UPDATE_PERIOD_MS));
|
||||
}
|
||||
|
||||
static int display_demo_init(void)
|
||||
/* 显示初始化完成后,后续 UI 更新全部通过事件和定时刷新驱动。 */
|
||||
static int display_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!device_is_ready(disp.dev)) {
|
||||
if (!device_is_ready(disp.dev))
|
||||
{
|
||||
LOG_ERR("Display device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
display_get_capabilities(disp.dev, &disp.caps);
|
||||
LOG_INF("Display caps: %ux%u fmt=%d", disp.caps.x_resolution, disp.caps.y_resolution,
|
||||
disp.caps.current_pixel_format);
|
||||
LOG_INF("Display caps: %ux%u fmt=%d",
|
||||
disp.caps.x_resolution,
|
||||
disp.caps.y_resolution,
|
||||
disp.caps.current_pixel_format);
|
||||
|
||||
k_work_init_delayable(&disp.update_work, display_update_work_fn);
|
||||
k_work_init_delayable(&disp.idle_work, display_idle_timeout_fn);
|
||||
disp.tick_count = 0U;
|
||||
disp.ui_ready = false;
|
||||
|
||||
err = display_blanking_off(disp.dev);
|
||||
if (err) {
|
||||
if (err)
|
||||
{
|
||||
LOG_ERR("Display blanking off failed: %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = display_backlight_init();
|
||||
if (err) {
|
||||
err = display_backlight_set(DISPLAY_BACKLIGHT_BRIGHTNESS);
|
||||
if (err)
|
||||
return err;
|
||||
}
|
||||
|
||||
lvgl_lock();
|
||||
display_create_ui_locked();
|
||||
lvgl_unlock();
|
||||
|
||||
disp.initialized = true;
|
||||
disp.pm_state = DISPLAY_PM_STATE_ACTIVE;
|
||||
display_schedule_update(K_NO_WAIT);
|
||||
LOG_INF("LVGL display demo initialized");
|
||||
display_kick_idle_timer();
|
||||
LOG_INF("Display UI initialized");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 电池事件只缓存最新 SOC,UI 若已就绪则立即刷新顶部电池区域。 */
|
||||
static bool handle_battery_status_event(const struct battery_status_event *event)
|
||||
{
|
||||
disp.ui.battery_level = battery_status_event_get_soc(event);
|
||||
disp.ui.battery_flags = battery_status_event_get_flags(event);
|
||||
|
||||
if (!disp.initialized)
|
||||
return false;
|
||||
|
||||
if (!display_is_active())
|
||||
return false;
|
||||
|
||||
lvgl_lock();
|
||||
display_refresh_battery_locked();
|
||||
lvgl_unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 模式事件只影响 USB/BLE 两个 badge 的亮灭。 */
|
||||
static bool handle_mode_event(const struct mode_event *event)
|
||||
{
|
||||
display_update_mode_state(event->mode_type);
|
||||
|
||||
if (!disp.initialized)
|
||||
return false;
|
||||
|
||||
if (!display_is_active())
|
||||
return false;
|
||||
|
||||
lvgl_lock();
|
||||
display_refresh_status_bar_locked();
|
||||
lvgl_unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
/* NumLock/CapsLock/ScrollLock 变化后,底部三个状态 badge 立即更新。 */
|
||||
static bool handle_keyboard_led_event(const struct keyboard_led_event *event)
|
||||
{
|
||||
display_update_keyboard_led_state(keyboard_led_event_get_mask(event));
|
||||
|
||||
if (!disp.initialized)
|
||||
return false;
|
||||
|
||||
if (!display_is_active())
|
||||
return false;
|
||||
|
||||
lvgl_lock();
|
||||
display_refresh_status_bar_locked();
|
||||
lvgl_unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 任意按钮事件都可点亮屏幕并重置 1 分钟空闲计时。 */
|
||||
static bool handle_button_event(const struct button_event *event)
|
||||
{
|
||||
ARG_UNUSED(event);
|
||||
|
||||
display_wake();
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_power_down_event(void)
|
||||
{
|
||||
display_sleep();
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_wake_up_event(void)
|
||||
{
|
||||
display_wake();
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_module_state_event(const struct module_state_event *event)
|
||||
{
|
||||
if (!check_state(event, MODULE_ID(main), MODULE_STATE_READY))
|
||||
return false;
|
||||
|
||||
if (!display_init())
|
||||
{
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
else
|
||||
{
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
if (is_module_state_event(aeh))
|
||||
return handle_module_state_event(cast_module_state_event(aeh));
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err = display_demo_init();
|
||||
if (is_battery_status_event(aeh))
|
||||
return handle_battery_status_event(cast_battery_status_event(aeh));
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
if (is_mode_event(aeh))
|
||||
return handle_mode_event(cast_mode_event(aeh));
|
||||
|
||||
return false;
|
||||
}
|
||||
if (is_keyboard_led_event(aeh))
|
||||
return handle_keyboard_led_event(cast_keyboard_led_event(aeh));
|
||||
|
||||
if (is_button_event(aeh))
|
||||
return handle_button_event(cast_button_event(aeh));
|
||||
|
||||
if (is_power_down_event(aeh))
|
||||
return handle_power_down_event();
|
||||
|
||||
if (is_wake_up_event(aeh))
|
||||
return handle_wake_up_event();
|
||||
|
||||
__ASSERT_NO_MSG(false);
|
||||
return false;
|
||||
@@ -181,3 +698,9 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, battery_status_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, mode_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, keyboard_led_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, button_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
Reference in New Issue
Block a user