Merge branch 'display'

This commit is contained in:
2026-03-28 09:13:12 +08:00
11 changed files with 2202 additions and 73 deletions

View File

@@ -0,0 +1,175 @@
#include <errno.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/sys/byteorder.h>
#include <app_event_manager.h>
#define MODULE ble_time_sync
#include <caf/events/module_state_event.h>
#include "time_manager.h"
#include "time_sync_event.h"
#include "time_sync_protocol.h"
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
#define BT_UUID_TIME_SYNC_SERVICE_VAL \
BT_UUID_128_ENCODE(0x0b7f5000, 0x38d2, 0x4f62, 0x8f6f, 0x36c4fd73a110)
#define BT_UUID_TIME_SYNC_WRITE_CHAR_VAL \
BT_UUID_128_ENCODE(0x0b7f5001, 0x38d2, 0x4f62, 0x8f6f, 0x36c4fd73a110)
#define BT_UUID_TIME_SYNC_SERVICE \
BT_UUID_DECLARE_128(BT_UUID_TIME_SYNC_SERVICE_VAL)
#define BT_UUID_TIME_SYNC_WRITE_CHAR \
BT_UUID_DECLARE_128(BT_UUID_TIME_SYNC_WRITE_CHAR_VAL)
struct ble_time_sync_ctx {
bool ble_stack_ready;
bool time_manager_ready;
bool module_ready;
};
static struct ble_time_sync_ctx ble_time_sync;
/* 统一检查协议版本和长度,避免在回调里分散出现偏移判断。 */
static bool ble_time_sync_payload_is_valid(const uint8_t *buf, uint16_t len)
{
if (!buf || (len != TIME_SYNC_PROTOCOL_PAYLOAD_SIZE)) {
return false;
}
if (buf[TIME_SYNC_PROTOCOL_OFFSET_VERSION] != TIME_SYNC_PROTOCOL_VERSION) {
return false;
}
if ((buf[TIME_SYNC_PROTOCOL_OFFSET_FLAGS] &
TIME_SYNC_PROTOCOL_FLAG_TIMEZONE_VALID) == 0U) {
return false;
}
return true;
}
/*
* 把私有 GATT payload 解码为统一的 time_sync_update
* - BLE 只负责协议适配;
* - 传输无关的时间语义都转成公共结构体后再交给事件层。
*/
static void ble_time_sync_decode_payload(const uint8_t *buf,
struct time_sync_update *update)
{
update->utc_ms = sys_get_le64(&buf[TIME_SYNC_PROTOCOL_OFFSET_UTC_MS]);
update->timezone_min =
(int16_t)sys_get_le16(&buf[TIME_SYNC_PROTOCOL_OFFSET_TIMEZONE]);
update->accuracy_ms =
sys_get_le32(&buf[TIME_SYNC_PROTOCOL_OFFSET_ACCURACY_MS]);
update->source = TIME_SYNC_SOURCE_BLE;
}
/*
* GATT 写回调必须尽量短:
* - 不支持 offset/prepare write避免被拆成长写事务
* - 校验和解码完成后直接提交统一事件,不在这里做耗时存储。
*/
static ssize_t write_time_sync(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
const void *buf,
uint16_t len,
uint16_t offset,
uint8_t flags)
{
struct time_sync_update update;
ARG_UNUSED(conn);
ARG_UNUSED(attr);
if (!ble_time_sync.module_ready || !time_manager_is_ready()) {
return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
}
if (offset != 0U) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
if ((flags & BT_GATT_WRITE_FLAG_PREPARE) != 0U) {
return BT_GATT_ERR(BT_ATT_ERR_ATTRIBUTE_NOT_LONG);
}
if (!ble_time_sync_payload_is_valid(buf, len)) {
return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED);
}
ble_time_sync_decode_payload(buf, &update);
time_sync_event_submit(&update);
LOG_INF("Accepted BLE time sync utc_ms=%llu tz=%d acc=%u",
(unsigned long long)update.utc_ms,
update.timezone_min,
update.accuracy_ms);
return len;
}
BT_GATT_SERVICE_DEFINE(ble_time_sync_svc,
BT_GATT_PRIMARY_SERVICE(BT_UUID_TIME_SYNC_SERVICE),
BT_GATT_CHARACTERISTIC(BT_UUID_TIME_SYNC_WRITE_CHAR,
BT_GATT_CHRC_WRITE |
BT_GATT_CHRC_WRITE_WITHOUT_RESP,
BT_GATT_PERM_WRITE_ENCRYPT,
NULL,
write_time_sync,
NULL),
);
/*
* 只有 BLE 栈和 time_manager 都 ready 后,才把模块状态标记为 READY
* - 虽然静态 GATT service 会跟随蓝牙栈注册;
* - 但真正是否接受写入,仍由 module_ready 再做一层保护。
*/
static void ble_time_sync_update_ready_state(void)
{
bool should_be_ready = ble_time_sync.ble_stack_ready &&
ble_time_sync.time_manager_ready;
if (should_be_ready == ble_time_sync.module_ready) {
return;
}
ble_time_sync.module_ready = should_be_ready;
module_set_state(should_be_ready ? MODULE_STATE_READY : MODULE_STATE_STANDBY);
LOG_INF("BLE time sync %s", should_be_ready ? "ready" : "standby");
}
/* 模块依赖只来自 ble_state 和 time_manager两者 READY 顺序不做假设。 */
static bool handle_module_state_event(const struct module_state_event *event)
{
if (check_state(event, MODULE_ID(ble_state), MODULE_STATE_READY)) {
ble_time_sync.ble_stack_ready = true;
ble_time_sync_update_ready_state();
return false;
}
if (check_state(event, MODULE_ID(time_manager), MODULE_STATE_READY)) {
ble_time_sync.time_manager_ready = true;
ble_time_sync_update_ready_state();
return false;
}
return false;
}
static bool app_event_handler(const struct app_event_header *aeh)
{
if (is_module_state_event(aeh)) {
return handle_module_state_event(cast_module_state_event(aeh));
}
__ASSERT_NO_MSG(false);
return false;
}
APP_EVENT_LISTENER(MODULE, app_event_handler);
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);

View File

@@ -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 两个 badge2.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;
}
/* 电池事件只缓存最新 SOCUI 若已就绪则立即刷新顶部电池区域。 */
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);

View File

@@ -0,0 +1,407 @@
#include <errno.h>
#include <string.h>
#include <zephyr/kernel.h>
#include <zephyr/settings/settings.h>
#include <zephyr/spinlock.h>
#include <app_event_manager.h>
#define MODULE time_manager
#include <caf/events/module_state_event.h>
#include <caf/events/power_event.h>
#include "time_manager.h"
#include "time_sync_event.h"
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
#define TIME_MANAGER_SAVE_DELAY_MS 1000
#define TIME_MANAGER_STORAGE_KEY "state"
/*
* 持久化数据只记录“最近一次成功校时”的元数据:
* - 它可以帮助我们知道设备曾经被校时过;
* - 但由于当前没有独立 RTC重启后不能把这份数据当作“当前仍准确”的时间。
*/
struct time_manager_storage_data {
uint64_t utc_ms;
int16_t timezone_min;
uint32_t accuracy_ms;
uint8_t source;
uint8_t valid_marker;
};
struct time_manager_ctx {
struct k_spinlock lock;
struct k_work_delayable save_work;
struct time_manager_storage_data persisted;
uint64_t base_utc_ms;
int64_t base_uptime_ms;
int16_t timezone_min;
uint32_t accuracy_ms;
enum time_sync_source source;
bool ready;
bool synchronized;
bool has_persisted_time;
bool storage_dirty;
bool storage_loaded;
};
static struct time_manager_ctx time_ctx;
/* 统一判断来源是否合法,避免把损坏配置或“未设置来源”写进时间状态。 */
static bool time_manager_source_is_valid(enum time_sync_source source)
{
switch (source) {
case TIME_SYNC_SOURCE_BLE:
case TIME_SYNC_SOURCE_USB:
case TIME_SYNC_SOURCE_MANUAL:
return true;
default:
return false;
}
}
/* 对时区做保守校验,避免异常包把显示层带到离谱偏移。 */
static bool time_manager_timezone_is_valid(int16_t timezone_min)
{
return (timezone_min >= -(24 * 60)) && (timezone_min <= (24 * 60));
}
/*
* 保存工作在系统工作队列里执行:
* - 这样 GATT 写回调和事件派发路径都只做内存更新;
* - 真正可能触发 flash 擦写的 settings_save_one 被挪到异步上下文。
*/
static int time_manager_store_state(const struct time_manager_storage_data *storage)
{
char key[] = MODULE_NAME "/" TIME_MANAGER_STORAGE_KEY;
int err = settings_save_one(key, storage, sizeof(*storage));
if (err) {
LOG_ERR("Failed to save time state err=%d", err);
return err;
}
LOG_INF("Stored time state src=%u utc_ms=%llu",
storage->source,
(unsigned long long)storage->utc_ms);
return 0;
}
/*
* 从运行态复制一份稳定快照,再在锁外执行 flash 写入:
* - 锁内只做小块 memcpy避免长时间持锁
* - 即使保存失败,也不回滚内存时间状态,避免影响当前功能。
*/
static void time_manager_save_work_fn(struct k_work *work)
{
struct time_manager_storage_data storage;
bool should_store;
k_spinlock_key_t key;
ARG_UNUSED(work);
key = k_spin_lock(&time_ctx.lock);
should_store = time_ctx.storage_dirty && time_ctx.has_persisted_time;
storage = time_ctx.persisted;
time_ctx.storage_dirty = false;
k_spin_unlock(&time_ctx.lock, key);
if (!should_store) {
return;
}
(void)time_manager_store_state(&storage);
}
/*
* 把一次同步结果写入运行态:
* - base_utc_ms + base_uptime_ms 组成当前时间基准;
* - persisted 只保存“最近一次同步结果”,后续异步落盘。
*/
static void time_manager_apply_update(const struct time_sync_update *update)
{
k_spinlock_key_t key = k_spin_lock(&time_ctx.lock);
time_ctx.base_utc_ms = update->utc_ms;
time_ctx.base_uptime_ms = k_uptime_get();
time_ctx.timezone_min = update->timezone_min;
time_ctx.accuracy_ms = update->accuracy_ms;
time_ctx.source = update->source;
time_ctx.synchronized = true;
time_ctx.has_persisted_time = true;
time_ctx.persisted.utc_ms = update->utc_ms;
time_ctx.persisted.timezone_min = update->timezone_min;
time_ctx.persisted.accuracy_ms = update->accuracy_ms;
time_ctx.persisted.source = (uint8_t)update->source;
time_ctx.persisted.valid_marker = 1U;
time_ctx.storage_dirty = true;
k_spin_unlock(&time_ctx.lock, key);
/*
* 保存延迟 1 秒:
* - 避免未来 BLE/USB 连续校时时反复触发 flash 写入;
* - 也避免在同步入口上下文里直接阻塞等待存储完成。
*/
k_work_reschedule(&time_ctx.save_work, K_MSEC(TIME_MANAGER_SAVE_DELAY_MS));
LOG_INF("Time synchronized src=%u tz=%d utc_ms=%llu acc=%u",
update->source,
update->timezone_min,
(unsigned long long)update->utc_ms,
update->accuracy_ms);
}
/*
* settings 子系统回调只负责恢复原始持久化结构:
* - 不在这里决定“当前时间是否有效”;
* - 运行态初始化放到 settings_loader ready 之后统一完成。
*/
static int settings_set(const char *key,
size_t len_rd,
settings_read_cb read_cb,
void *cb_arg)
{
ssize_t rc;
if (strcmp(key, TIME_MANAGER_STORAGE_KEY) != 0) {
return 0;
}
if (len_rd != sizeof(time_ctx.persisted)) {
LOG_WRN("Time state size mismatch got=%u expect=%u",
(uint32_t)len_rd,
sizeof(time_ctx.persisted));
time_ctx.storage_loaded = false;
return 0;
}
rc = read_cb(cb_arg, &time_ctx.persisted, sizeof(time_ctx.persisted));
time_ctx.storage_loaded = (rc == sizeof(time_ctx.persisted));
if (!time_ctx.storage_loaded) {
LOG_WRN("Time state read failed rc=%d", (int)rc);
}
return 0;
}
SETTINGS_STATIC_HANDLER_DEFINE(time_manager,
MODULE_NAME,
NULL,
settings_set,
NULL,
NULL);
/*
* settings 恢复完成后初始化运行态:
* - 有持久化历史仅表示“以前同步过”,不能直接把时间标记成有效;
* - 当前 boot 仍然需要新的同步事件来建立可信时间基准。
*/
static void time_manager_init_after_settings_loaded(void)
{
k_spinlock_key_t key = k_spin_lock(&time_ctx.lock);
time_ctx.ready = true;
time_ctx.synchronized = false;
time_ctx.source = TIME_SYNC_SOURCE_NONE;
time_ctx.base_utc_ms = 0U;
time_ctx.base_uptime_ms = 0;
time_ctx.timezone_min = 0;
time_ctx.accuracy_ms = 0U;
time_ctx.has_persisted_time = time_ctx.storage_loaded &&
(time_ctx.persisted.valid_marker == 1U) &&
time_manager_source_is_valid(
(enum time_sync_source)time_ctx.persisted.source) &&
time_manager_timezone_is_valid(
time_ctx.persisted.timezone_min);
time_ctx.storage_dirty = false;
k_spin_unlock(&time_ctx.lock, key);
if (time_ctx.has_persisted_time) {
LOG_INF("Loaded persisted time metadata tz=%d src=%u utc_ms=%llu",
time_ctx.persisted.timezone_min,
time_ctx.persisted.source,
(unsigned long long)time_ctx.persisted.utc_ms);
} else {
LOG_INF("No valid persisted time metadata");
}
module_set_state(MODULE_STATE_READY);
}
/*
* 校时事件入口只做快速校验和内存更新:
* - 数据格式错误直接丢弃;
* - flash 持久化交给延迟工作处理。
*/
static bool handle_time_sync_event(const struct time_sync_event *event)
{
const struct time_sync_update *update = time_sync_event_get_update(event);
if (!time_ctx.ready) {
LOG_WRN("Drop time sync before manager ready");
return false;
}
if (!time_manager_source_is_valid(update->source)) {
LOG_WRN("Drop time sync invalid source=%u", update->source);
return false;
}
if (!time_manager_timezone_is_valid(update->timezone_min)) {
LOG_WRN("Drop time sync invalid timezone=%d", update->timezone_min);
return false;
}
if (update->utc_ms == 0U) {
LOG_WRN("Drop time sync utc_ms=0");
return false;
}
time_manager_apply_update(update);
return false;
}
/*
* 掉电前尽量把最后一次同步结果落盘:
* - 如果没有脏数据,立即返回;
* - 如果有待保存数据,则同步执行一次保存,降低突然掉电时的丢失概率。
*/
static bool handle_power_down_event(void)
{
struct time_manager_storage_data storage;
bool should_store;
k_spinlock_key_t key;
if (!time_manager_is_ready()) {
return false;
}
(void)k_work_cancel_delayable(&time_ctx.save_work);
key = k_spin_lock(&time_ctx.lock);
should_store = time_ctx.storage_dirty && time_ctx.has_persisted_time;
storage = time_ctx.persisted;
time_ctx.storage_dirty = false;
k_spin_unlock(&time_ctx.lock, key);
if (!should_store) {
return false;
}
(void)time_manager_store_state(&storage);
return false;
}
/* 仅在 settings_loader 完成后宣布 READY保证外部读取到的是稳定状态。 */
static bool handle_module_state_event(const struct module_state_event *event)
{
if (!check_state(event, MODULE_ID(settings_loader), MODULE_STATE_READY)) {
return false;
}
if (time_ctx.ready) {
return false;
}
k_work_init_delayable(&time_ctx.save_work, time_manager_save_work_fn);
time_manager_init_after_settings_loaded();
return false;
}
bool time_manager_is_ready(void)
{
k_spinlock_key_t key = k_spin_lock(&time_ctx.lock);
bool ready = time_ctx.ready;
k_spin_unlock(&time_ctx.lock, key);
return ready;
}
/*
* 获取快照时只复制一次基准,再在锁外计算当前 UTC
* - 这样不会让调用者在锁里做时间换算;
* - 也避免 64 位字段在 32 位 MCU 上被撕裂读取。
*/
int time_manager_get_snapshot(struct time_manager_snapshot *snapshot)
{
uint64_t base_utc_ms;
int64_t base_uptime_ms;
int16_t timezone_min;
uint32_t accuracy_ms;
enum time_sync_source source;
bool ready;
bool synchronized;
bool has_persisted_time;
k_spinlock_key_t key;
int64_t elapsed_ms;
if (!snapshot) {
return -EINVAL;
}
key = k_spin_lock(&time_ctx.lock);
ready = time_ctx.ready;
synchronized = time_ctx.synchronized;
has_persisted_time = time_ctx.has_persisted_time;
base_utc_ms = time_ctx.base_utc_ms;
base_uptime_ms = time_ctx.base_uptime_ms;
timezone_min = time_ctx.timezone_min;
accuracy_ms = time_ctx.accuracy_ms;
source = time_ctx.source;
k_spin_unlock(&time_ctx.lock, key);
snapshot->ready = ready;
snapshot->synchronized = synchronized;
snapshot->has_persisted_time = has_persisted_time;
snapshot->timezone_min = timezone_min;
snapshot->accuracy_ms = accuracy_ms;
snapshot->source = source;
snapshot->utc_ms = 0U;
if (!ready) {
return -EAGAIN;
}
if (!synchronized) {
return -ENODATA;
}
elapsed_ms = k_uptime_get() - base_uptime_ms;
if (elapsed_ms < 0) {
elapsed_ms = 0;
}
snapshot->utc_ms = base_utc_ms + (uint64_t)elapsed_ms;
return 0;
}
static bool app_event_handler(const struct app_event_header *aeh)
{
if (is_module_state_event(aeh)) {
return handle_module_state_event(cast_module_state_event(aeh));
}
if (is_time_sync_event(aeh)) {
return handle_time_sync_event(cast_time_sync_event(aeh));
}
if (is_power_down_event(aeh)) {
return handle_power_down_event();
}
__ASSERT_NO_MSG(false);
return false;
}
APP_EVENT_LISTENER(MODULE, app_event_handler);
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
APP_EVENT_SUBSCRIBE(MODULE, time_sync_event);
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);