Compare commits

..

6 Commits

Author SHA1 Message Date
908e7a0a4d feat: 添加Atguigu键盘板支持和IP5306电源管理驱动
添加了两个新的键盘板支持:
- atguigu_keyboard_dongle (基于nrf52833)
- atguigu_mini_keyboard (基于nrf52840)

同时添加了完整的IP5306电源管理芯片驱动,包括:
- 支持充电状态检测
- 提供软硬件保活脉冲功能
- 硬件后端利用nRF的RTC+GPIOTE+GPPI实现低功耗保活
- 软件后端作为备用方案
2026-04-10 08:18:46 +08:00
2356cb4fe8 refactor(display): 重构显示模块UI逻辑分离到独立组件
- 将UI相关的代码从display_module.c中提取到新的display_ui.c文件
- 创建display_ui.h头文件定义UI模型和接口函数
- 在CMakeLists.txt中添加UI目录包含路径和源文件引用
- 修改display_module.c中的UI相关数据结构和函数调用
- 将UI创建和刷新逻辑替换为对新UI模块的调用
- 优化了时间日期文本的更新机制,提高性能
- 移除了原有的内部UI实现代码,保持模块职责清晰
2026-04-02 17:05:16 +08:00
b5433f0403 feat(power): 更新电源管理模块从IP5305到IP5306
- 在CMakeLists.txt中更新ZEPHYR_EXTRA_MODULES路径从ip5305到ip5306
- 在文档中更新未纳入索引的项目项,将IP5305相关引用替换为IP5306
- 在prj.conf中将CONFIG_IP5305配置更改为CONFIG_IP5306
- 在battery_module.c中更新头文件包含、设备获取和函数调用从ip5305到ip5306
- 更新日志输出中的设备名称以匹配新的IP5306芯片
2026-04-01 17:08:04 +08:00
881b36274c feat(qdec_module): 添加电源管理功能支持
- 引入pm/device.h和sys/atomic.h头文件以支持电源管理和原子操作
- 添加active原子变量来跟踪模块激活状态
- 实现qdec_module_suspend()和qdec_module_resume()函数处理挂起和唤醒
- 集成power_event事件订阅,在电源下电时挂起模块,在唤醒时恢复模块
- 修改工作调度逻辑,确保仅在活动状态下执行旋转编码器数据处理
- 重构初始化逻辑,使用qdec_reset_state()函数重置内部状态
2026-04-01 16:18:52 +08:00
e369567998 docs: 添加蓝牙SIG官方规格中BLE Profile汇总文档
新增bluetooth_sig_ble_profiles_summary.md文档,包含:
- 基于GATT的传统BLE Profile
- 基于LE Audio的音频类Profile
- 基于Bluetooth Mesh/NLC的Profile
- 共59个Profile的详细分类说明
- 各Profile的官方条目链接和应用场景描述
- 为new_kbd项目提供键盘相关Profile参考
2026-04-01 15:06:54 +08:00
c0a6e45911 feat(bt): 添加蓝牙设备信息服务的即插即用配置
为蓝牙设备信息服务添加VID和PID配置,支持即插即用功能,
包括供应商ID(0x1209)、产品ID(0x0001)和VID源配置。
2026-04-01 11:52:10 +08:00
37 changed files with 2779 additions and 413 deletions

View File

@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.20.0) cmake_minimum_required(VERSION 3.20.0)
if(EXISTS "E:/extra/modules/ip5305") if(EXISTS "E:/extra/modules/ip5306")
list(APPEND ZEPHYR_EXTRA_MODULES "E:/extra/modules/ip5305") list(APPEND ZEPHYR_EXTRA_MODULES "E:/extra/modules/ip5306")
endif() endif()
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
@@ -10,6 +10,7 @@ project(new_kbd)
zephyr_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc) zephyr_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)
zephyr_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/events) zephyr_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/events)
zephyr_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/ui)
zephyr_compile_definitions( zephyr_compile_definitions(
LV_LVGL_H_INCLUDE_SIMPLE=1 LV_LVGL_H_INCLUDE_SIMPLE=1
@@ -56,6 +57,7 @@ target_sources(app PRIVATE
src/modules/time_manager_module.c src/modules/time_manager_module.c
src/modules/usb_hid_module.c src/modules/usb_hid_module.c
src/modules/ble_hid_module.c src/modules/ble_hid_module.c
src/ui/display_ui.c
src/ui/fonts/ui_font_keyboard_small_18.c src/ui/fonts/ui_font_keyboard_small_18.c
src/ui/fonts/ui_font_keyboard_time_48.c src/ui/fonts/ui_font_keyboard_time_48.c
) )

View File

@@ -0,0 +1,2 @@
config BOARD_ATGUIGU_KEYBOARD_DONGLE
select SOC_NRF52833_QDAA

View File

@@ -0,0 +1,2 @@
&pinctrl {
};

View File

@@ -0,0 +1,42 @@
/dts-v1/;
#include <nordic/nrf52833_qdaa.dtsi>
#include "atguigu_keyboard_dongle-pinctrl.dtsi"
/ {
model = "Keyboard Dongle";
compatible = "atguigu,atguigu-keyboard-dongle";
chosen {
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,code-partition = &slot0_partition;
};
};
&flash0 {
partitions {
compatible = "fixed-partitions";
#address-cells = <1>;
#size-cells = <1>;
boot_partition: partition@0 {
label = "mcuboot";
reg = <0x00000000 DT_SIZE_K(48)>;
};
slot0_partition: partition@c000 {
label = "image-0";
reg = <0x0000c000 DT_SIZE_K(220)>;
};
slot1_partition: partition@43000 {
label = "image-1";
reg = <0x00043000 DT_SIZE_K(220)>;
};
storage_partition: partition@7a000 {
label = "storage";
reg = <0x0007a000 DT_SIZE_K(24)>;
};
};
};

View File

@@ -0,0 +1,10 @@
identifier: atguigu_keyboard_dongle/nrf52833
name: Keyboard Dongle
vendor: atguigu
type: mcu
arch: arm
ram: 128
flash: 512
toolchain:
- zephyr
supported: []

View File

@@ -0,0 +1,2 @@
CONFIG_ARM_MPU=y
CONFIG_HW_STACK_PROTECTION=y

View File

@@ -0,0 +1,9 @@
set(OPENOCD_NRF5_SUBFAMILY "nrf52")
board_runner_args(jlink "--device=nRF52833_xxAA" "--speed=4000")
board_runner_args(pyocd "--target=nrf52833" "--frequency=4000000")
include(${ZEPHYR_BASE}/boards/common/nrfutil.board.cmake)
include(${ZEPHYR_BASE}/boards/common/nrfjprog.board.cmake)
include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake)
include(${ZEPHYR_BASE}/boards/common/pyocd.board.cmake)
include(${ZEPHYR_BASE}/boards/common/openocd-nrf5.board.cmake)

View File

@@ -0,0 +1,5 @@
board:
name: atguigu_keyboard_dongle
vendor: atguigu
socs:
- name: nrf52833

View File

@@ -0,0 +1,2 @@
# Suppress "unique_unit_address_if_enabled" to handle some overlaps
list(APPEND EXTRA_DTC_FLAGS "-Wno-unique_unit_address_if_enabled")

View File

@@ -0,0 +1,2 @@
config BOARD_ATGUIGU_MINI_KEYBOARD
select SOC_NRF52840_QFAA

View File

@@ -0,0 +1,73 @@
&pinctrl {
qdec_default: qdec_default {
group1 {
psels = <NRF_PSEL(QDEC_A, 0, 10)>,
<NRF_PSEL(QDEC_B, 1, 6)>;
bias-pull-up;
};
};
qdec_sleep: qdec_sleep {
group1 {
psels = <NRF_PSEL(QDEC_A, 0, 10)>,
<NRF_PSEL(QDEC_B, 1, 6)>;
low-power-enable;
};
};
led_spi_default: led_spi_default {
group1 {
psels = <NRF_PSEL(SPIM_MOSI, 0, 20)>;
};
};
led_spi_sleep: led_spi_sleep {
group1 {
psels = <NRF_PSEL(SPIM_MOSI, 0, 20)>;
low-power-enable;
};
};
lcd_spi_default: lcd_spi_default {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 1, 13)>,
<NRF_PSEL(SPIM_MOSI, 0, 28)>;
};
};
lcd_spi_sleep: lcd_spi_sleep {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 1, 13)>,
<NRF_PSEL(SPIM_MOSI, 0, 28)>;
low-power-enable;
};
};
i2c1_default: i2c1_default {
group1 {
psels = <NRF_PSEL(TWIM_SDA, 1, 0)>,
<NRF_PSEL(TWIM_SCL, 0, 24)>;
bias-pull-up;
};
};
i2c1_sleep: i2c1_sleep {
group1 {
psels = <NRF_PSEL(TWIM_SDA, 1, 0)>,
<NRF_PSEL(TWIM_SCL, 0, 24)>;
low-power-enable;
};
};
pwm0_default: pwm0_default {
group1 {
psels = <NRF_PSEL(PWM_OUT0, 1, 11)>;
};
};
pwm0_sleep: pwm0_sleep {
group1 {
psels = <NRF_PSEL(PWM_OUT0, 1, 11)>;
low-power-enable;
};
};
};

View File

@@ -0,0 +1,240 @@
/dts-v1/;
#include <nordic/nrf52840_qiaa.dtsi>
#include "atguigu_mini_keyboard-pinctrl.dtsi"
#include <zephyr/dt-bindings/pinctrl/nrf-pinctrl.h>
#include <zephyr/dt-bindings/led/led.h>
/ {
model = "Mini Keyboard, 17 keys";
compatible = "atguigu,atguigu-mini-keyboard";
chosen {
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,code-partition = &slot0_partition;
zephyr,display = &st7789v3;
};
aliases {
backlight = &backlight;
};
mode_sense: mode-sense {
compatible = "voltage-divider";
io-channels = <&adc 5>;
output-ohms = <1>;
full-ohms = <1>;
};
battery_sense: battery-sense {
compatible = "voltage-divider";
io-channels = <&adc 7>;
output-ohms = <100000>;
full-ohms = <200000>;
power-gpios = <&gpio0 9 GPIO_ACTIVE_HIGH>;
power-on-sample-delay-us = <100>;
};
pwm_leds: pwm_leds {
compatible = "pwm-leds";
status = "okay";
backlight: pwm_led_0 {
pwms = <&pwm0 0 PWM_MSEC(10) PWM_POLARITY_INVERTED>;
};
};
led_0: led_0 {
compatible = "gpio-leds";
status = "okay";
chan0 {
gpios = <&gpio0 17 GPIO_ACTIVE_LOW>;
};
};
led_1: led_1 {
compatible = "gpio-leds";
status = "okay";
chan0 {
gpios = <&gpio1 2 GPIO_ACTIVE_LOW>;
};
};
mipi_dbi: mipi_dbi {
compatible = "zephyr,mipi-dbi-spi";
status = "okay";
spi-dev = <&spi3>;
dc-gpios = <&gpio0 3 GPIO_ACTIVE_HIGH>;
reset-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>;
write-only;
#address-cells = <1>;
#size-cells = <0>;
st7789v3: st7789v@0 {
compatible = "sitronix,st7789v";
status = "okay";
reg = <0>;
mipi-max-frequency = <32000000>;
width = <320>;
height = <172>;
x-offset = <0>;
y-offset = <34>;
vcom = <0x2b>;
gctrl = <0x35>;
vrhs = <0x11>;
vdvs = <0x20>;
mdac = <0xA0>;
lcm = <0x2c>;
colmod = <0x55>;
gamma = <0x01>;
porch-param = [ 0c 0c 00 33 33 ];
cmd2en-param = [ 5a 69 02 01 ];
pwctrl1-param = [ a4 a1 ];
pvgam-param = [ d0 00 02 07 0a 28 32 44 42 06 0e 12 14 17 ];
nvgam-param = [ d0 00 02 07 0a 28 31 54 47 0e 1c 17 1b 1e ];
ram-param = [ 00 f0 ];
rgb-param = [ c0 02 14 ];
mipi-mode = "MIPI_DBI_MODE_SPI_4WIRE";
};
};
};
&spi2 {
status = "okay";
pinctrl-0 = <&led_spi_default>;
pinctrl-1 = <&led_spi_sleep>;
pinctrl-names = "default", "sleep";
cs-gpios = <&gpio0 21 GPIO_ACTIVE_LOW>;
led_strip: ws2812@0 {
compatible = "worldsemi,ws2812-spi";
supply-gpios = <&gpio0 13 GPIO_ACTIVE_HIGH>;
reg = <0>;
spi-max-frequency = <8000000>;
chain-length = <17>;
color-mapping = <LED_COLOR_ID_GREEN LED_COLOR_ID_RED LED_COLOR_ID_BLUE>;
spi-one-frame = <0xFC>;
spi-zero-frame = <0xC0>;
};
};
&i2c1 {
status = "okay";
pinctrl-0 = <&i2c1_default>;
pinctrl-1 = <&i2c1_sleep>;
pinctrl-names = "default", "sleep";
clock-frequency = <400000>;
ip5306: pmic@75 {
status = "okay";
compatible = "injoinic,ip5306";
reg = <0x75>;
keepalive-gpios = <&gpio0 22 GPIO_ACTIVE_LOW>;
keepalive-interval-ms = <10000>;
keepalive-offload;
};
};
/* 编码器 */
&qdec {
status = "okay";
/* 引用上面定义的标签 */
pinctrl-0 = <&qdec_default>;
pinctrl-1 = <&qdec_sleep>;
/* 指定 pinctrl-0 为默认pinctrl-1 为睡眠 */
pinctrl-names = "default", "sleep";
/* 别忘了 QDEC 必须的两个属性 */
steps = <40>;
led-pre = <0>;
};
&flash0 {
partitions {
compatible = "fixed-partitions";
#address-cells = <1>;
#size-cells = <1>;
boot_partition: partition@0 {
label = "mcuboot";
reg = <0x00000000 DT_SIZE_K(48)>;
};
slot0_partition: partition@c000 {
label = "image-0";
reg = <0x0000c000 DT_SIZE_K(472)>;
};
slot1_partition: partition@82000 {
label = "image-1";
reg = <0x00082000 DT_SIZE_K(472)>;
};
storage_partition: partition@f8000 {
label = "storage";
reg = <0x000f8000 DT_SIZE_K(32)>;
};
};
};
&spi3 {
status = "okay";
pinctrl-0 = <&lcd_spi_default>;
pinctrl-1 = <&lcd_spi_sleep>;
pinctrl-names = "default", "sleep";
cs-gpios = <&gpio0 2 GPIO_ACTIVE_LOW>;
};
&adc {
status = "okay";
#address-cells = <1>;
#size-cells = <0>;
channel@5 {
reg = <5>;
zephyr,gain = "ADC_GAIN_1_6";
zephyr,reference = "ADC_REF_INTERNAL";
zephyr,acquisition-time = <ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 20)>;
zephyr,input-positive = <NRF_SAADC_AIN5>;
zephyr,resolution = <12>;
};
channel@7 {
reg = <7>;
zephyr,gain = "ADC_GAIN_1_6";
zephyr,reference = "ADC_REF_INTERNAL";
zephyr,acquisition-time = <ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 20)>;
zephyr,input-positive = <NRF_SAADC_AIN7>;
zephyr,resolution = <12>;
};
};
&pwm0 {
status = "okay";
pinctrl-0 = <&pwm0_default>;
pinctrl-1 = <&pwm0_sleep>;
pinctrl-names = "default", "sleep";
};
&gpio0 {
status = "okay";
};
&gpio1 {
status = "okay";
};
&gpiote {
status = "okay";
};
&usbd {
status = "okay";
};
&uicr {
nfct-pins-as-gpios;
gpio-as-nreset;
};

View File

@@ -0,0 +1,10 @@
identifier: atguigu_mini_keyboard/nrf52840
name: Mini Keyboard, 17 keys
vendor: atguigu
type: mcu
arch: arm
ram: 256
flash: 1024
toolchain:
- zephyr
supported: []

View File

@@ -0,0 +1,2 @@
CONFIG_ARM_MPU=y
CONFIG_HW_STACK_PROTECTION=y

View File

@@ -0,0 +1,9 @@
set(OPENOCD_NRF5_SUBFAMILY "nrf52")
board_runner_args(jlink "--device=nRF52840_xxAA" "--speed=4000")
board_runner_args(pyocd "--target=nrf52840" "--frequency=4000000")
include(${ZEPHYR_BASE}/boards/common/nrfutil.board.cmake)
include(${ZEPHYR_BASE}/boards/common/nrfjprog.board.cmake)
include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake)
include(${ZEPHYR_BASE}/boards/common/pyocd.board.cmake)
include(${ZEPHYR_BASE}/boards/common/openocd-nrf5.board.cmake)

View File

@@ -0,0 +1,5 @@
board:
name: atguigu_mini_keyboard
vendor: atguigu
socs:
- name: nrf52840

View File

@@ -0,0 +1,2 @@
# Suppress "unique_unit_address_if_enabled" to handle some overlaps
list(APPEND EXTRA_DTC_FLAGS "-Wno-unique_unit_address_if_enabled")

View File

@@ -0,0 +1,314 @@
# Bluetooth SIG 规格页中的 BLE Profile 汇总
更新时间2026-04-01Asia/Hong_Kong
## 1. 说明与筛选口径
本文基于 Bluetooth SIG 官方规格索引页 `https://www.bluetooth.com/specifications/specs/` 当前处于 `Adopted` 状态的条目进行筛选,只保留运行在 Bluetooth Low Energy 体系上的 Profile包括
- 基于 GATT 的传统 BLE Profile
- 基于 LE Audio 的音频类 Profile
- 基于 Bluetooth Mesh / NLC 的 Profile
本文中的“详细说明”不是官方规范原文摘录,也不是对规范条款的逐句转述,而是我基于 BLE/GATT、LE Audio、Mesh/NLC 的通用工程记忆做的高层总结。用于快速建立全局认知和做架构预判是合适的;如果要做互操作或拿来做 BQB/QDID 级别实现,仍应回到对应规范逐条核对。
本次共纳入 **59** 个 Profile。
## 2. 总表
| Profile | 最新版本 | 类别 | 官方条目 |
| --- | --- | --- | --- |
| Alert Notification Profile | 1.0 | 核心/GATT | https://www.bluetooth.com/specifications/specs/alert-notification-profile-1-0/ |
| Ambient Light Sensor NLC Profile | 1.0.1 | Mesh/NLC | https://www.bluetooth.com/specifications/specs/ambient-light-sensor-nlc-profile-1-0-1/ |
| Asset Tracking Profile | 1.0 | 商业/工业/定位 | https://www.bluetooth.com/specifications/specs/asset-tracking-profile-1-0/ |
| Authorization Control Profile | 1.0 | 商业/工业/定位 | https://www.bluetooth.com/specifications/specs/authorization-control-profile-1-0/ |
| Automation IO Profile | 1.0 | 核心/GATT | https://www.bluetooth.com/specifications/specs/automation-io-profile-1-0/ |
| Basic Audio Profile | 1.0.2 | LE Audio | https://www.bluetooth.com/specifications/specs/basic-audio-profile-1-0-2/ |
| Basic Lightness Controller NLC Profile | 1.0.1 | Mesh/NLC | https://www.bluetooth.com/specifications/specs/basic-lightness-controller-nlc-profile-1-0-1/ |
| Basic Scene Selector NLC Profile | 1.0.1 | Mesh/NLC | https://www.bluetooth.com/specifications/specs/basic-scene-selector-nlc-profile-1-0-1/ |
| Binary Sensor Profile | 1.0 | 商业/工业/定位 | https://www.bluetooth.com/specifications/specs/binary-sensor-profile-1-0/ |
| Blood Pressure Profile | 1.1.1 | 健康/医疗 | https://www.bluetooth.com/specifications/specs/blood-pressure-profile-1-1-1/ |
| Calendar Tasks and Notes Profile | 1.0.1 | 核心/GATT | https://www.bluetooth.com/specifications/specs/calendar-tasks-and-notes-profile-1-0-1/ |
| Call Control Profile | 1.0 | LE Audio | https://www.bluetooth.com/specifications/specs/call-control-profile-1-0/ |
| Common Audio Profile | 1.0.1 | LE Audio | https://www.bluetooth.com/specifications/specs/common-audio-profile-1-0-1/ |
| Continuous Glucose Monitoring Profile | 1.0.2 | 健康/医疗 | https://www.bluetooth.com/specifications/specs/continuous-glucose-monitoring-profile-1-0-2/ |
| Cookware Profile | 1.0 | 商业/工业/定位 | https://www.bluetooth.com/specifications/specs/cookware-profile-1-0/ |
| Coordinated Set Identification Profile | 1.1 | LE Audio | https://www.bluetooth.com/specifications/specs/coordinated-set-identification-profile-1-1/ |
| Cycling Power Profile | 1.1.1 | 运动/定位 | https://www.bluetooth.com/specifications/specs/cycling-power-profile-1-1-1/ |
| Cycling Speed and Cadence Profile | 1.0.1 | 运动/定位 | https://www.bluetooth.com/specifications/specs/cycling-speed-and-cadence-profile/ |
| Device Time Profile | 1.0 | 核心/GATT | https://www.bluetooth.com/specifications/specs/device-time-profile-1-0/ |
| Dimming Control NLC Profile | 1.0.1 | Mesh/NLC | https://www.bluetooth.com/specifications/specs/dimming-control-nlc-profile-1-0-1/ |
| Electronic Shelf Label Profile | 1.0.1 | 商业/工业/定位 | https://www.bluetooth.com/specifications/specs/electronic-shelf-label-profile-1-0-1/ |
| Emergency Profile | 1.0 | 商业/工业/定位 | https://www.bluetooth.com/specifications/specs/emergency-profile-1-0/ |
| Energy Monitor NLC Profile | 1.0.1 | Mesh/NLC | https://www.bluetooth.com/specifications/specs/energy-monitor-nlc-profile1-0-1/ |
| Environmental Sensing Profile | 1.0.1 | 核心/GATT | https://www.bluetooth.com/specifications/specs/environmental-sensing-profile-1-0-1/ |
| Find Me Profile | 1.0 | 核心/GATT | https://www.bluetooth.com/specifications/specs/find-me-profile-1-0/ |
| Fitness Machine Profile | 1.0.1 | 运动/定位 | https://www.bluetooth.com/specifications/specs/fitness-machine-profile-1-0-1/ |
| Gaming Audio Profile | 1.0.1 | LE Audio | https://www.bluetooth.com/specifications/specs/gaming-audio-profile-4/ |
| Generic Health Sensor Profile | 1.0 | 健康/医疗 | https://www.bluetooth.com/specifications/specs/generic-health-sensor-profile/ |
| Global Navigation Satellite System Profile | 1.0 | 运动/定位 | https://www.bluetooth.com/specifications/specs/global-navigation-satellite-system-profile-1-0/ |
| Glucose Profile | 1.0.1 | 健康/医疗 | https://www.bluetooth.com/specifications/specs/glucose-profile-1-0-1/ |
| Health Thermometer Profile | 1.0 | 健康/医疗 | https://www.bluetooth.com/specifications/specs/health-thermometer-profile-1-0/ |
| Hearing Access Profile | 1.0.1 | LE Audio | https://www.bluetooth.com/specifications/specs/hearing-access-profile-1-0-1/ |
| Heart Rate Profile | 1.0 | 健康/医疗 | https://www.bluetooth.com/specifications/specs/heart-rate-profile-1-0/ |
| HID Over GATT Profile | 1.1 | 核心/GATT | https://www.bluetooth.com/specifications/specs/hid-over-gatt-profile/ |
| HVAC Integration NLC Profile | 1.0 | Mesh/NLC | https://www.bluetooth.com/specifications/specs/hvac-integration-nlc-profile/ |
| Industrial Measurement Device Profile | 1.0 | 商业/工业/定位 | https://www.bluetooth.com/specifications/specs/industrial-measurement-device-profile-1-0/ |
| Insulin Delivery Profile | 1.0.2 | 健康/医疗 | https://www.bluetooth.com/specifications/specs/insulin-delivery-profile-1-0-2/ |
| Internet Protocol Support Profile | 1.0 | 核心/GATT | https://www.bluetooth.com/specifications/specs/internet-protocol-support-profile-1-0/ |
| Location and Navigation Profile | 1.0.1 | 运动/定位 | https://www.bluetooth.com/specifications/specs/location-and-navigation-profile-1-0-1/ |
| Media Control Profile | 1.0 | LE Audio | https://www.bluetooth.com/specifications/specs/media-control-profile/ |
| Mesh Configuration Database Profile | 1.0.1 | Mesh/NLC | https://www.bluetooth.com/specifications/specs/mesh-configuration-database-profile-1-0-1/ |
| Mesh Profile | 1.0.1 | Mesh/NLC | https://www.bluetooth.com/specifications/specs/mesh-profile-1-0-1/ |
| Microphone Control Profile | 1.0 | LE Audio | https://www.bluetooth.com/specifications/specs/microphone-control-profile-1-0/ |
| Object Transfer Profile | 1.0 | 核心/GATT | https://www.bluetooth.com/specifications/specs/object-transfer-profile-1-0/ |
| Occupancy Sensor NLC Profile | 1.0.1 | Mesh/NLC | https://www.bluetooth.com/specifications/specs/occupancy-sensor-nlc-profile-1-0-1/ |
| Phone Alert Status Profile | 1.0 | 核心/GATT | https://www.bluetooth.com/specifications/specs/phone-alert-status-profile-1-0/ |
| Physical Activity Monitor Profile | 1.0 | 健康/医疗 | https://www.bluetooth.com/specifications/specs/physical-activity-monitor-profile-1-0/ |
| Proximity Profile | 1.0.1 | 核心/GATT | https://www.bluetooth.com/specifications/specs/proximity-profile-1-0-1/ |
| Public Broadcast Profile | 1.0.2 | LE Audio | https://www.bluetooth.com/specifications/specs/public-broadcast-profile-1-0-2/ |
| Pulse Oximeter Profile | 1.0.1 | 健康/医疗 | https://www.bluetooth.com/specifications/specs/pulse-oximeter-profile-1-0-1/ |
| Ranging Profile | 1.0 | 商业/工业/定位 | https://www.bluetooth.com/specifications/specs/ranging-profile-1-0/ |
| Reconnection Configuration Profile | 1.0.1 | LE Audio | https://www.bluetooth.com/specifications/specs/reconnection-configuration-profile-1-0-1/ |
| Running Speed and Cadence Profile | 1.0.1 | 运动/定位 | https://www.bluetooth.com/specifications/specs/running-speed-and-cadence-profile-1-0-1/ |
| Scan Parameters Profile | 1.0 | 核心/GATT | https://www.bluetooth.com/specifications/specs/scan-parameters-profile-1-0/ |
| Telephony and Media Audio Profile | 1.0.1 | LE Audio | https://www.bluetooth.com/specifications/specs/telephony-and-media-audio-profile-1-0-1/ |
| Time Profile | 1.0 | 核心/GATT | https://www.bluetooth.com/specifications/specs/time-profile-1-0/ |
| Voice Assistant Profile | 1.0 | LE Audio | https://www.bluetooth.com/specifications/specs/voice-assistant-profile-1-0/ |
| Volume Control Profile | 1.0 | LE Audio | https://www.bluetooth.com/specifications/specs/volume-control-profile-1-0/ |
| Weight Scale Profile | 1.0.1 | 健康/医疗 | https://www.bluetooth.com/specifications/specs/weight-scale-profile-1-0-1/ |
## 3. 逐项详细说明
### 3.1 核心 / GATT 类
#### Alert Notification Profile
这是一个把“手机或主设备上的提醒事件”标准化转给外设的 Profile常见目标是手表、腕带、桌面提醒器之类的低功耗接收端。它关注的是类别化通知、已读状态、未读计数、立即提醒这类轻量信息而不是完整消息正文同步。工程上它适合做“提醒镜像”不适合做完整 IM 客户端。
#### Automation IO Profile
这个 Profile 面向简单工业控制和楼宇场景,核心思想是把离散输入、离散输出、模拟量等基础 I/O 点位做成统一抽象。它适合开关量、继电器、按钮、状态量等低复杂度控制接口,不强调复杂控制逻辑本身。工程上常被当作 BLE 版“轻量远程 I/O 面板”。
#### Calendar Tasks and Notes Profile
它的定位是让外设以标准方式访问日历、待办和笔记这类个人信息对象。和“通知”类 Profile 相比,它更偏对象化数据同步,强调项目集合、元数据、内容条目和访问流程。适用场景通常是可穿戴设备、车载设备或桌面配件做轻量 PIM 同步。
#### Device Time Profile
这个 Profile 的重点是设备侧时间基准管理适合让一个时间主设备给多个从设备做校时。它通常关心设备当前时间、时区、DST 或更新时间来源等信息。工程上它比通用的 Time Profile 更偏“设备维护”视角。
#### Environmental Sensing Profile
这是 BLE 里很常见的环境类 Profile覆盖温度、湿度、气压、露点、风速、空气质量等大量环境传感字段。它的价值在于给环境监测节点定义统一组织方式减少私有 GATT 设计。做传感器平台时,它通常是优先考虑的标准基线。
#### Find Me Profile
这是最早一批 BLE Profile 之一,场景非常直接:主设备触发外设发声、闪灯或震动,从而“找回”钥匙扣、标签或其他小设备。它的交互非常简单,通常没有复杂数据通道。工程上它偏“单向触发”,不是精确定位方案。
#### HID Over GATT Profile
HOGP 是 BLE 键盘、鼠标、遥控器、触控设备最核心的标准 Profile 之一。它把传统 HID 语义映射到 GATT让低功耗输入设备能和主机系统直接互通并支持报告模式、协议模式、Boot 兼容路径等。对键盘项目来说,它通常就是“标准输入外设”的第一选择。
#### Internet Protocol Support Profile
这个 Profile 的意义在于让 BLE 不只是传属性值,还能承载 IPv6/6LoWPAN 之类的上层网络协议。它面向的是更偏物联网基础设施的场景,而不是简单外设控制。工程上只有在你明确需要把设备纳入 IP 网络模型时才值得采用。
#### Object Transfer Profile
OTP 面向“对象”而不是“单个特征值”,适合在 BLE 上搬运图片、固件片段、记录文件、联系人卡片等具备元数据和可寻址性的内容。它通常依赖对象目录、对象元信息、读写偏移、创建删除等对象管理能力。和自定义长特征值相比,它更规范,但实现复杂度也更高。
#### Phone Alert Status Profile
PASP 主要用于把手机侧“来电铃声、振动、静音、提醒状态”这类手机告警状态同步给外设。它和 Alert Notification Profile 关系很近,但更强调电话和提醒设备状态而不是通知明细本身。典型设备是手环、腕表和桌面提醒器。
#### Proximity Profile
Proximity Profile 更关注链路“靠近/远离”带来的行为,比如距离太远时报警、靠近后自动静音或恢复。它通常依赖发射功率与链路损耗这类简单近远估计,不等同于现代高精度测距。工程上可以把它理解为 Find Me 的“距离关联版”。
#### Scan Parameters Profile
这个 Profile 主要用来让扫描方和被扫描方就扫描参数做一定程度的协同,目标是兼顾发现时延和功耗。它是很早期 BLE 生态为手机与外设配合而定义的补充机制。现代系统里它未必总是显性出现,但在兼容旧实现时仍值得知道。
#### Time Profile
Time Profile 是 BLE 时间同步体系的通用基线,关注当前时间、精度、参考时间来源以及时间更新。它适合需要基本时钟同步但又不想自定义时间服务的设备。和 Device Time Profile 相比,它更像“通用时间语义”的上层封装。
### 3.2 健康 / 医疗类
#### Blood Pressure Profile
这个 Profile 面向电子血压计,核心数据通常是收缩压、舒张压、平均动脉压、测量时间戳和脉搏等。它的价值在于让手机健康应用、家庭医疗网关和血压计之间具备一致的数据模型。工程上应特别注意单位、时序和一条记录的完整性。
#### Continuous Glucose Monitoring Profile
CGM Profile 面向连续血糖监测设备,强调连续采样、趋势、会话状态和告警管理。它比传统 Glucose Profile 更适合长时间持续数据流和设备状态追踪。医疗属性很强,实现时通常还要同时考虑记录缓存、断线补传和告警可靠性。
#### Generic Health Sensor Profile
从命名和生态位置看,它是为了给大量新型健康传感器提供一个更泛化的标准外壳,避免每类新传感器都重新造一套 Profile。它适合指标多样但又不值得为单一指标单独立 Profile 的健康设备。工程上可把它看作医疗/健康类的“可扩展通用框架”。
#### Glucose Profile
Glucose Profile 更偏间歇式测量设备,例如指尖采血血糖仪。它通常围绕测量记录、上下文信息、时间戳和历史数据访问来组织。和 CGM 相比,它不强调连续流,而更强调离散测量记录管理。
#### Health Thermometer Profile
这个 Profile 用于体温计或温度类健康设备,重点是体温测量值、测量部位、时间戳等。它在家庭健康设备中很常见,互操作也比较成熟。工程实现通常不复杂,但要处理好单位、一次性测量与持续测量模式的区分。
#### Heart Rate Profile
这是最常见的 BLE 健康/运动 Profile 之一,面向心率带、手环、运动设备。它核心传的是实时心率值,并可包含能量消耗或 RR-Interval 等附加信息。因为支持广、实现轻,很多平台把它当成 BLE 互操作示范案例。
#### Insulin Delivery Profile
这个 Profile 面向胰岛素泵或给药相关设备,重点是输注状态、历史记录、治疗参数和控制安全性。它比一般传感器 Profile 更强调状态机和风险控制语义。工程上要把“数据展示”和“可导致治疗动作的控制”严格分层。
#### Physical Activity Monitor Profile
它覆盖日常活动跟踪场景,如步数、活动时长、活动等级、消耗估算等。相比 Heart Rate 这类单一生理信号,它更偏行为统计与日常健康监测。适合手环、健康贴片、轻量可穿戴。
#### Pulse Oximeter Profile
这个 Profile 主要用于脉搏血氧设备,核心指标是 SpO2、脉率以及相关上下文。它在家用健康、睡眠监测和康复设备中很常见。工程上要区分瞬时显示值和可归档记录值避免 UI 和记录逻辑混淆。
#### Weight Scale Profile
Weight Scale Profile 面向体重秤和身体成分相关设备,基础数据是体重,也可带 BMI、身高、时间戳等信息。它的价值在于手机健康应用能直接理解标准数据而不需要厂商私有解析。对消费级产品来说这个 Profile 的生态成熟度很高。
### 3.3 运动 / 定位类
#### Cycling Power Profile
面向功率计、自行车台、训练设备,核心是功率值以及踏频、扭矩、左右平衡、曲线段等扩展信息。它属于运动传感里实现要求较高的一类,因为用户往往关心高刷新率和训练准确性。做骑行训练生态时,这个 Profile 很关键。
#### Cycling Speed and Cadence Profile
这个 Profile 用于车轮速度和踏频传感器,数据模型比功率计更轻。它非常适合电池供电的低功耗传感器做长续航设计。常见场景是码表、运动表、骑行 App 与车轮/踏频传感器互联。
#### Fitness Machine Profile
FTMP 面向跑步机、动感单车、划船机、椭圆机等健身器材,既包含器材上报,也允许控制类交互。它的意义是把“训练设备”从单纯传感器提升到可配置、可联动的训练终端。工程上需要特别留意控制权限和状态一致性。
#### Global Navigation Satellite System Profile
这个 Profile 面向 GNSS 接收设备,把位置、速度、卫星状态或导航相关信息以标准方式暴露给对端。它适合外置卫星定位模块、运动设备或定位记录器。工程上常见用途是把 GNSS 功能从主机中解耦到独立 BLE 外设。
#### Location and Navigation Profile
这个 Profile 是通用位置/导航数据封装,通常涵盖位置、速度、航向、海拔和导航点信息。它比单纯 GNSS 更高层,能容纳更多导航语义。适用于户外设备、船舶设备、骑行/跑步导航附件。
#### Running Speed and Cadence Profile
RSC Profile 面向跑步脚环、鞋夹或可穿戴传感器,关注跑速、步频、步长、跑/走状态等。它和骑行速度/踏频 Profile 类似,属于低功耗、低带宽但很实用的运动类标准。对跑步生态兼容性来说,它是基础件。
### 3.4 LE Audio 类
#### Basic Audio Profile
BAP 是 LE Audio 体系里的基础音频传输 Profile围绕单播/广播音频流、流配置和基本音频能力组织。可以把它理解为 LE Audio 的“音频承载基石”。很多更上层的音频 Profile 都会依赖它提供的流控制和能力表达。
#### Call Control Profile
CCP 用来表达通话相关控制语义,比如接听、挂断、保持、来电状态变化等。它的目的不是定义音频编解码,而是把“通话控制面”标准化。对耳机、免提设备、车载设备来说,它是电话业务的关键拼图。
#### Common Audio Profile
CAP 更像 LE Audio 各类设备协同行为的公共约束层,定义一组通用角色和组合方式,避免不同音频 Profile 各自形成孤岛。它通常与 CSIP、BAP 等配合使用。工程上可把它看成 LE Audio 设备行为一致性的“总装配规则”。
#### Coordinated Set Identification Profile
CSIP 用来把多个独立设备组织成一个协调集合,典型例子就是左右耳耳机、双扬声器或多设备音频系统。它解决的问题是“多个物理设备如何在逻辑上被识别为一组”。没有它,多设备配对、切换和同步体验会明显变差。
#### Gaming Audio Profile
从命名和 LE Audio 位置看,它面向游戏场景的低时延、双向音频和设备控制协同。它通常比通用媒体播放更强调时延预算、语音聊天和交互响应。工程上适合游戏耳机、掌机配件、无线麦克风方案。
#### Hearing Access Profile
HAP 面向助听器、听辅设备及其控制终端,重点是接入、控制、状态读取以及个性化听力功能配合。它是 LE Audio 在医疗辅助听力方向的重要接口层。实现时要特别关注可靠性、低时延和多设备协同。
#### Media Control Profile
MCP 把媒体播放控制抽象成标准接口,例如播放、暂停、下一曲、上一曲、快进、元数据查看等。它解决的是控制面统一,不直接替代音频流承载本身。对耳机、遥控器、车载控制面板都很有价值。
#### Microphone Control Profile
这个 Profile 聚焦麦克风相关控制,最典型的是静音/取消静音以及麦克风状态同步。它常用于耳机、会议设备、采集配件和语音终端。虽然看起来简单,但在多端协同和 UI 同步上很有必要。
#### Public Broadcast Profile
PBP 是 Auracast/Public Broadcast 体系中的关键 Profile解决的是公开广播音频如何被发现、识别和接入。它偏广播音频的“可发现性与可消费性”约束而不只是裸流传输。适合商场、机场、影院、公共导览等大范围音频分发场景。
#### Reconnection Configuration Profile
从命名和音频生态位置看,这个 Profile 用来描述设备断开后如何重连、优先重连谁、何时自动恢复等重连策略。它的目的通常是改善多主机、多耳塞、多场景切换时的用户体验。工程上它属于“体验型 Profile”对 TWS 这类设备尤其重要。
#### Telephony and Media Audio Profile
TMAP 是 LE Audio 设备在电话与媒体两大主场景中的角色组合规范。它更多定义“设备是什么、在这个场景里应该怎么协同”,而不是单一控制命令。可以把它理解为 LE Audio 终端类型与用例映射。
#### Voice Assistant Profile
从生态定位看VAP 面向语音助手接入场景,让耳机或外设能标准化启动助手会话、处理语音交互路径和相关状态。它通常和麦克风控制、音频流控制、设备角色协同使用。适用场景是耳机呼出助手、可穿戴语音入口、车载语音终端。
#### Volume Control Profile
VCP 负责音量控制与状态同步,包括绝对音量、音量步进、静音状态等。它的价值是让多类 LE Audio 终端对音量行为有一致表达。工程上如果你不希望每个设备都做私有音量协议,它是很自然的选择。
### 3.5 商业 / 工业 / 定位类
#### Asset Tracking Profile
这个 Profile 面向资产标签与定位基础设施,典型场景是仓储、零售、医院、物流等环境中的物品追踪。它通常会和广播、周期广播、方向查找或其他定位能力协作,而不是只靠一个静态 GATT 表。工程上它强调“标签、定位器、后台系统”三者的协同。
#### Authorization Control Profile
从命名看,它用于标准化“授权对象”和“访问控制状态”的 BLE 交互,适合门锁、闸机、设备启停授权、数字钥匙等场景。它的重点更像权限校验和授权流程,而不是通用传感。实现时安全边界、凭证生命周期和离线行为都应优先设计。
#### Binary Sensor Profile
这是一个很实用但语义非常克制的 Profile用来表达开/关、在/不在、触发/未触发、占用/未占用这类二值状态。它适合门磁、按钮、限位、干接点、简单安防触发器等设备。它的优势是简单、低功耗、互操作成本低。
#### Cookware Profile
从命名和家电生态位置看,它面向智能锅具、烹饪容器或配套温控设备。核心信息大概率围绕锅具温度、烹饪阶段、加热状态、定时和食材完成度提示。它适合做厨房配件的标准化接口,但实际产品里常仍会叠加厂商私有逻辑。
#### Electronic Shelf Label Profile
ESL Profile 是零售领域非常明确的商业场景标准,用于价签设备和控制基础设施之间的低功耗、批量、可靠更新。它强调群组调度、同步刷新、超低功耗待机和门店级大规模部署能力。对商超场景来说,这是 BLE 向“基础设施协议”扩展的代表作。
#### Emergency Profile
从名称判断,它服务于紧急告警、求助、事件上报和联动响应这类场景,例如老人监护、工业告警、紧急按钮设备。它通常不只是传一个布尔值,更强调事件等级、状态和响应流程。工程实现时要把“告警一定要送达”的可靠性目标单独考虑。
#### Industrial Measurement Device Profile
这个 Profile 面向工业测量设备,例如压力表、流量计、过程测量节点、便携仪表等。它的目标是让工业现场数据采集具备统一结构,而不是每家仪表都自定义私有 GATT。对工业场景来说它比消费电子 Profile 更强调量测语义和系统集成。
#### Ranging Profile
Ranging Profile 是 BLE 进入高精度距离测量后的关键 Profile核心目的就是把测距流程、角色和结果表达标准化。它通常适用于数字钥匙、物体接近判断、室内精细定位和安全接入场景。和早期 Proximity Profile 相比,它更接近“精确距离感知”而不是粗略远近估计。
### 3.6 Mesh / NLC 类
#### Mesh Configuration Database Profile
这个 Profile 更偏管理面,关注 Mesh 网络的配置数据库如何表达、交换和持久化。它不是普通终端用户直接感知的功能,而是配置工具、网关、后台系统之间保持网络一致性的基础。工程上它有助于把 Mesh 网络从“临时配网”提升为“可运维系统”。
#### Mesh Profile
Mesh Profile 是 Bluetooth Mesh 的核心定义了节点、消息中继、发布订阅、低功耗节点、Friend、代理等网络行为。虽然它不是传统意义上的单设备 GATT Profile但它的承载底座仍是 Bluetooth LE 广播和相关承载机制,所以应纳入 BLE 体系讨论。对照明、楼宇和大规模传感网络来说,它是总根。
#### Ambient Light Sensor NLC Profile
这个 NLC Profile 面向网络化照明控制里的环境光传感器节点。它把环境照度读数纳入 NLC 语义,使照明系统能做日光补偿、节能调光和场景联动。适合办公室、商业照明和智能楼宇。
#### Basic Lightness Controller NLC Profile
它对应最基础的亮度控制器角色,强调“以标准化方式控制灯光亮度”。和底层 Mesh Model 相比NLC Profile 给的是更高层、面向成品设备的约束组合。工程上它降低了不同厂商控制面板和灯具的对接成本。
#### Basic Scene Selector NLC Profile
这个 Profile 用来标准化场景选择器,例如面板上的“会议模式、演示模式、离开模式”按钮。它适合把复杂灯光/楼宇状态打包成少量场景入口。对用户交互而言,它比直接调模型参数友好得多。
#### Dimming Control NLC Profile
这个 Profile 更强调连续调光过程和控制行为,比单纯的“设定亮度值”更贴近真实调光器交互。它适合旋钮面板、滑条、墙控设备等。工程上通常和灯光控制器、亮度控制逻辑配合使用。
#### Energy Monitor NLC Profile
它把能耗监测设备纳入 NLC 语义,可用于照明回路或楼宇设备的电能数据采集。场景包括能耗可视化、节能优化和维护分析。它的价值在于让照明系统不只“能控”,还能“可度量”。
#### HVAC Integration NLC Profile
这个 Profile 体现了 NLC 从照明向楼宇环境系统扩展的趋势,用于暖通空调相关设备和照明/楼宇控制系统互联。它适合温控器、风机盘管接口、环境联动面板等设备。工程上它更强调跨子系统协同,而不只是灯控本身。
#### Occupancy Sensor NLC Profile
这个 Profile 面向人体存在/占用检测节点,是楼宇自动化里非常核心的传感输入。它常用于自动开灯、延时关灯、空调节能、会议室占用判断。和 Binary Sensor 相比,它位于 NLC 生态内部,强调与照明/楼宇场景联动。
## 4. 未纳入的网页条目
Bluetooth SIG 规格页里还存在不少名称中带有 `Profile` 的条目,但它们不属于 BLE 体系,或者主要是 BR/EDR / Classic Bluetooth 方向,因此本次没有纳入,例如:
- 3D Synchronization Profile
- A/V Remote Control Profile
- Advanced Audio Distribution Profile
- Basic Imaging Profile
- Basic Printing Profile
- BR/EDR Connection Handover Profile
- Device Identification Profile
- Dial-Up Networking Profile
- File Transfer Profile
- Generic A/V Distribution Profile
- Generic Object Exchange Profile
- Generic PIM Profile
- Hands-Free Profile
- Hardcopy Cable Replacement Profile
- Headset Profile
- Health Device Profile
- Human Interface Device Profile
- Message Access Profile
- Multi Profile Specification
- Object Push Profile
- Personal Area Networking Profile
- Phone Book Access Profile
- Serial Port Profile
- SIM Access Profile
- Synchronization Profile
- Video Distribution Profile
说明:其中个别条目可能在现代产品里和 LE 设备共存,但其规范本体并不是“基于 BLE 的 Profile”所以这里按“协议归属”而不是“产品是否可能双模”来排除。
## 5. 对 `new_kbd` 项目的直接参考价值
如果你的项目是键盘/输入设备,最直接相关的是:
- `HID Over GATT Profile`BLE 键盘主标准
- `Battery Service``Device Information Service``Bonding/Security` 等虽然不在本文 Profile 汇总内,但会和 HOGP 一起构成实际产品骨架
- 如果后续要做查找键盘、靠近告警,可参考 `Find Me Profile` / `Proximity Profile`
- 如果要做文件、配置对象同步,可看 `Object Transfer Profile` 的对象化思路,但一般键盘产品没必要直接上完整 OTP
如果要继续,我可以在下一步再补一份“**和 BLE 键盘最相关的 Profile / Service / Characteristic 清单**”,直接映射到你当前 `new_kbd` 工程的实现关注点。

View File

@@ -0,0 +1,926 @@
# NCS v3.2.3 集成的 GATT Service 汇总
更新时间2026-04-01Asia/Hong_Kong
## 1. 统计口径
本文面向 `C:\ncs\v3.2.3` 代码树中**应用可直接集成**的 GATT Service。
筛选规则:
-`zephyr/include/zephyr/bluetooth/services/``nrf/include/bluetooth/services/` 中公开头文件为主
-`zephyr/subsys/bluetooth/services/``nrf/subsys/bluetooth/services/` 中实际注册 GATT 服务的实现为准
- 优先整理**服务端**能力,也就是“本机作为 Peripheral/Server 暴露 GATT Service”时的用法
- `*_client``hogp``gattp` 这类主要用于 Central 侧发现/访问远端服务的客户端辅助库,不作为本文主表主体
按这个口径,当前 SDK 中可直接拿来做本地 GATT Service 的模块主要分为两类:
- Zephyr 自带标准服务
- NCS / Nordic 扩展服务
## 2. 先看结论
### 2.1 Zephyr 自带标准服务
| Service | Kconfig | 头文件 | 典型用途 |
| --- | --- | --- | --- |
| Alert Notification Service | `CONFIG_BT_ANS` | `<zephyr/bluetooth/services/ans.h>` | 对手表/手环输出通知类别与未读数 |
| Battery Service | `CONFIG_BT_BAS` | `<zephyr/bluetooth/services/bas.h>` | 暴露电量、电池状态 |
| Current Time Service | `CONFIG_BT_CTS` | `<zephyr/bluetooth/services/cts.h>` | 给客户端提供当前时间/校时 |
| Device Information Service | `CONFIG_BT_DIS` | `<zephyr/bluetooth/services/dis.h>` | 暴露厂商、型号、版本、PnP ID 等静态信息 |
| Heart Rate Service | `CONFIG_BT_HRS` | `<zephyr/bluetooth/services/hrs.h>` | 心率设备 |
| Immediate Alert Service | `CONFIG_BT_IAS` | `<zephyr/bluetooth/services/ias.h>` | 查找设备、远程触发蜂鸣/闪灯 |
| Nordic UART ServiceZephyr 版) | `CONFIG_BT_ZEPHYR_NUS` | `<zephyr/bluetooth/services/nus.h>` | 简单双向串口透传 |
| Object Transfer Service | `CONFIG_BT_OTS` | `<zephyr/bluetooth/services/ots.h>` | 面向对象的数据传输 |
| Tx Power Service | `CONFIG_BT_TPS` | 无独立公共 API 头 | 暴露当前发射功率 |
### 2.2 NCS / Nordic 扩展服务
| Service | Kconfig | 头文件 | 典型用途 |
| --- | --- | --- | --- |
| Bond Management Service | `CONFIG_BT_BMS` | `<bluetooth/services/bms.h>` | 让对端触发删 bond |
| Continuous Glucose Monitoring Service | `CONFIG_BT_CGMS` | `<bluetooth/services/cgms.h>` | 连续血糖监测 |
| Direction and Distance Finding Service | `CONFIG_BT_DDFS` | `<bluetooth/services/ddfs.h>` | 方位/距离测量结果输出 |
| Fast Pair Provider Service | `CONFIG_BT_FAST_PAIR` + `CONFIG_BT_FAST_PAIR_GATT_SERVICE` | `<bluetooth/services/fast_pair/fast_pair.h>` | Google Fast Pair |
| Human Interface Device Service | `CONFIG_BT_HIDS` | `<bluetooth/services/hids.h>` | BLE 键盘、鼠标、输入设备 |
| Latency Service | `CONFIG_BT_LATENCY` | `<bluetooth/services/latency.h>` | 延迟测试/回环 |
| LED Button Service | `CONFIG_BT_LBS` | `<bluetooth/services/lbs.h>` | 示例级 LED/Button 交互 |
| Memfault Diagnostic Service | `CONFIG_BT_MDS` | `<bluetooth/services/mds.h>` | Memfault 诊断数据导出 |
| Nordic Status Message Service | `CONFIG_BT_NSMS` | `<bluetooth/services/nsms.h>` | 暴露一段可读状态文本 |
| Nordic UART ServiceNordic 旧版) | `CONFIG_BT_NUS` | `<bluetooth/services/nus.h>` | NCS 样例常用串口透传 |
| Ranging Service | `CONFIG_BT_RAS` + `CONFIG_BT_RAS_RRSP` | `<bluetooth/services/ras.h>` | Channel Sounding / Ranging 服务端 |
| Running Speed and Cadence Service | `CONFIG_BT_RSCS` | `<bluetooth/services/rscs.h>` | 跑步速度步频 |
| Throughput Service | `CONFIG_BT_THROUGHPUT` | `<bluetooth/services/throughput.h>` | 吞吐测试 |
| Wi-Fi Provisioning Service | `CONFIG_BT_WIFI_PROV` | `<bluetooth/services/wifi_provisioning.h>` | 通过 BLE 给设备配 Wi-Fi |
## 3. 使用方式总规律
大多数服务都遵循类似流程:
1.`prj.conf` 里打开对应 `CONFIG_BT_*`
2. 包含头文件
3. 若服务需要运行时初始化,则在 `bt_enable()` 前后调用对应 `init/register`
4. 连接建立后,通过通知/读写回调/API 更新服务数据
5. 在广告里决定是否放服务 UUID但这不是注册服务的必要条件
有三类例外要先记住:
- **纯 Kconfig 静态服务**:例如 `DIS``TPS`,启用配置后就自动挂到 GATT DB上层几乎没有运行时 API
- **宏定义式实例服务**:例如 `NSMS``Zephyr NUS` 多实例、`HIDS`,需要先用宏定义实例,再初始化或更新
- **复杂协议服务**:例如 `OTS``RAS``Fast Pair`,除了开 Kconfig还要按它自己的状态机/回调/缓冲机制接入
## 4. 逐项说明
## 4.1 Zephyr 标准服务
### 4.1.1 Alert Notification Service, `ANS`
源码位置:
- `zephyr/subsys/bluetooth/services/ans.c`
- `zephyr/include/zephyr/bluetooth/services/ans.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_ANS=y
```
怎么用:
- 先在 Kconfig 里决定支持哪些提醒类别,例如 `CONFIG_BT_ANS_NALRT_CAT_EMAIL=y`
- 运行时如果还想动态设置支持位图,可以调用 `bt_ans_set_new_alert_support_category()``bt_ans_set_unread_support_category()`
- 有新提醒时调用 `bt_ans_notify_new_alert(conn, category, num_new, text)`
- 未读数变化时调用 `bt_ans_set_unread_count(conn, category, unread)`
适合场景:
- 手环、手表、桌面提醒器
- 你只想同步“有提醒/未读数/提醒类别”,而不是完整消息正文
注意点:
- 头文件明确写了 `bt_ans_notify_new_alert()` / `bt_ans_set_unread_count()` 会拿互斥锁,**不要从 BT RX 线程或 System Workqueue 里直接调用**
- 这个服务偏“提醒镜像”,不适合做完整消息中心
### 4.1.2 Battery Service, `BAS`
源码位置:
- `zephyr/subsys/bluetooth/services/bas/`
- `zephyr/include/zephyr/bluetooth/services/bas.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_BAS=y
```
如果要扩展电池状态:
```conf
CONFIG_BT_BAS_BLS=y
CONFIG_BT_BAS_BCS=y
```
怎么用:
- 最基本只需要周期性调用 `bt_bas_set_battery_level(level)`,把 0..100 的电量同步给客户端
- 读取当前缓存值可用 `bt_bas_get_battery_level()`
- 如果启用了扩展状态特征,可继续设置:
- `bt_bas_bls_set_battery_present()`
- `bt_bas_bls_set_battery_charge_state()`
- `bt_bas_bls_set_battery_charge_level()`
- `bt_bas_bls_set_service_required()`
- `bt_bas_bls_set_battery_fault()`
适合场景:
- 键盘、鼠标、耳机、手表、遥控器等所有电池供电外设
对你当前项目的价值:
- 这是 `new_kbd` 最应该启用的标准服务之一
- 你已经有 `ble_battery_module.c`,它天然适合映射到 BAS
### 4.1.3 Current Time Service, `CTS`
源码位置:
- `zephyr/subsys/bluetooth/services/cts.c`
- `zephyr/include/zephyr/bluetooth/services/cts.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_CTS=y
```
可选工具 API
```conf
CONFIG_BT_CTS_HELPER_API=y
```
怎么用:
- 定义 `struct bt_cts_cb`
- 至少实现 `fill_current_cts_time()`,因为服务在被读或发通知时要靠它拿当前时间
- 如果允许对端写时间,再实现 `cts_time_write()`
- 如果关心客户端是否订阅时间更新,实现 `notification_changed()`
- 初始化时调用 `bt_cts_init(&cb)`
- 时间变化后调用 `bt_cts_send_notification(reason)`
最小使用思路:
1. 系统自己维护 RTC / Unix 时间
2. CTS 被读时把当前时间填进 `struct bt_cts_time_format`
3. 如果时间是由手机写入的,就在 `cts_time_write()` 里反写到系统时钟
### 4.1.4 Device Information Service, `DIS`
源码位置:
- `zephyr/subsys/bluetooth/services/dis.c`
- `zephyr/subsys/bluetooth/services/Kconfig.dis`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_DIS=y
CONFIG_BT_DIS_MANUF_NAME=y
CONFIG_BT_DIS_MANUF_NAME_STR="Your Company"
CONFIG_BT_DIS_MODEL_NUMBER=y
CONFIG_BT_DIS_MODEL_NUMBER_STR="KBD-01"
CONFIG_BT_DIS_PNP=y
```
怎么用:
- `DIS` 基本是**纯配置型服务**
- 你在 `prj.conf` 里打开需要的特征值并填字符串/ID服务会静态注册
- 常用字段包括:
- 厂商名
- 型号
- 序列号
- FW/HW/SW 版本
- PnP ID
- 医疗设备 UDI
适合场景:
- 几乎所有 BLE 外设都建议启用
对键盘项目的建议:
- 至少填 `Manufacturer Name``Model Number``FW Revision`
- 如果你将来打算做 HID 认证/系统识别,`PnP ID` 也建议补齐
### 4.1.5 Heart Rate Service, `HRS`
源码位置:
- `zephyr/subsys/bluetooth/services/hrs.c`
- `zephyr/include/zephyr/bluetooth/services/hrs.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_HRS=y
```
怎么用:
- 若要处理控制点请求,定义 `struct bt_hrs_cb` 并注册 `bt_hrs_cb_register(&cb)`
- 上报新心率时调用 `bt_hrs_notify(heartrate)`
- 如果支持 Energy Expended 重置,则在 `ctrl_point_write()` 里处理 `BT_HRS_CONTROL_POINT_RESET_ENERGY_EXPANDED_REQ`
适合场景:
- 心率带、健康设备、运动设备
### 4.1.6 Immediate Alert Service, `IAS`
源码位置:
- `zephyr/subsys/bluetooth/services/ias/ias.c`
- `zephyr/include/zephyr/bluetooth/services/ias.h`
- 参考样例:`zephyr/samples/bluetooth/peripheral/src/main.c`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_IAS=y
```
怎么用:
-`BT_IAS_CB_DEFINE(name)` 定义回调对象
- 在回调里实现:
- `no_alert()`
- `mild_alert()`
- `high_alert()`
- 远端写 Alert Level 时,服务会自动回调你
- 如果本机已经在报警,且你想主动停掉当前告警,可调用 `bt_ias_local_alert_stop()`
典型逻辑:
- `mild_alert()` 里短鸣、低频闪烁
- `high_alert()` 里持续蜂鸣或高亮闪烁
- `no_alert()` 里停蜂鸣、灭灯
### 4.1.7 Nordic UART Service, `Zephyr 版 NUS`
源码位置:
- `zephyr/subsys/bluetooth/services/nus/`
- `zephyr/include/zephyr/bluetooth/services/nus.h`
- 参考样例:`zephyr/samples/bluetooth/peripheral_nus`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_ZEPHYR_NUS=y
```
可选:
```conf
CONFIG_BT_ZEPHYR_NUS_DEFAULT_INSTANCE=y
CONFIG_BT_ZEPHYR_NUS_AUTO_START_BLUETOOTH=y
```
怎么用:
- 最简单:使用默认实例
- 定义 `struct bt_nus_cb`
- 注册 `bt_nus_cb_register(&cb, ctx)`
- 对端写 RX 特征值时,你的 `received()` 回调会收到数据
- 要发数据给对端时,调用 `bt_nus_send(conn, data, len)`
高级用法:
- 这个版本支持多实例
- 你可以先用 `BT_NUS_INST_DEFINE(name)` 定义多个 NUS 端点
- 然后对每个实例调用 `bt_nus_inst_cb_register()` / `bt_nus_inst_send()`
建议:
- 新代码如果想做“可扩展串口通道”Zephyr 版 NUS 比 Nordic 旧版更现代
### 4.1.8 Object Transfer Service, `OTS`
源码位置:
- `zephyr/subsys/bluetooth/services/ots/`
- `zephyr/include/zephyr/bluetooth/services/ots.h`
- 参考样例:`zephyr/samples/bluetooth/peripheral_ots`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_OTS=y
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
CONFIG_BT_GATT_DYNAMIC_DB=y
CONFIG_BT_SMP=y
```
按需求补充:
```conf
CONFIG_BT_OTS_DIR_LIST_OBJ=y
CONFIG_BT_OTS_OACP_WRITE_SUPPORT=y
CONFIG_BT_OTS_OACP_PATCH_SUPPORT=y
CONFIG_BT_OTS_OACP_CREATE_SUPPORT=y
CONFIG_BT_OTS_OACP_DELETE_SUPPORT=y
```
怎么用:
1. 先拿一个实例:`bt_ots_free_instance_get()`
2.`struct bt_ots_init_param`,配置支持的 OACP/OLCP Feature 与回调
3. 调用 `bt_ots_init(ots, &ots_init)`
4. 准备对象元数据与对象数据,再用 `bt_ots_obj_add(ots, &param)` 加入对象池
5. 客户端随后可通过 OTS 读/写/选择对象
什么时候值得用:
- 需要在 BLE 上传输“对象”,例如文件、图片、日志、记录块、配置块
- 需要比单个 characteristic 更规范的元数据和对象管理
什么时候不值得用:
- 只是传几字节配置或简单串口透传时,`NUS` 或自定义服务更轻
### 4.1.9 Tx Power Service, `TPS`
源码位置:
- `zephyr/subsys/bluetooth/services/tps.c`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_TPS=y
```
怎么用:
- 这是典型的**无应用层 API**服务
- 打开 `CONFIG_BT_TPS` 后,服务会静态注册
- 客户端读取 `TX Power Level` 特征值时,底层会通过 `bt_conn_le_get_tx_power_level()` 取当前发射功率并返回
适合场景:
- 定位、调试、需要标准化暴露 TX Power 的设备
## 4.2 NCS / Nordic 扩展服务
### 4.2.1 Bond Management Service, `BMS`
源码位置:
- `nrf/subsys/bluetooth/services/bms.c`
- `nrf/include/bluetooth/services/bms.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_BMS=y
```
怎么用:
- 定义 `struct bt_bms_init_params`
- 在里面配置支持哪些删 bond 操作,以及是否需要授权码
- 如果某些删除操作要授权,实现 `struct bt_bms_cb.authorize`
- 调用 `bt_bms_init(&init_params)`
适合场景:
- 设备端希望让手机或维护工具发起“删除本机 bond”
- 做售后恢复、重新配对、共享设备切换用户
对你当前项目的价值:
- 你的键盘已经在做 bond 管理,这个服务值得评估
- 但很多键盘不会直接对外开放 BMS而是自己做按键清配对逻辑
### 4.2.2 Continuous Glucose Monitoring Service, `CGMS`
源码位置:
- `nrf/subsys/bluetooth/services/cgms/`
- `nrf/include/bluetooth/services/cgms.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_CGMS=y
```
怎么用:
-`struct bt_cgms_init_param`
- 传感器类型
- 采样位置
- session 运行时长
- 初始通信间隔
- 回调
- 调用 `bt_cgms_init(&init)`
- 每有一条新血糖测量值,就调用 `bt_cgms_measurement_add(measurement)`
适合场景:
- 连续血糖监测设备
### 4.2.3 Direction and Distance Finding Service, `DDFS`
源码位置:
- `nrf/subsys/bluetooth/services/ddfs.c`
- `nrf/include/bluetooth/services/ddfs.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_DDFS=y
```
怎么用:
-`struct bt_ddfs_init_params`
- 初始 features
- `struct bt_ddfs_cb` 回调
- 调用 `bt_ddfs_init(&init)`
- 应用拿到测距/方位结果后,用以下 API 通知客户端:
- `bt_ddfs_distance_measurement_notify()`
- `bt_ddfs_azimuth_measurement_notify()`
- `bt_ddfs_elevation_measurement_notify()`
回调的意义:
- `dm_ranging_mode_set()`:对端改测距模式时通知应用
- `dm_config_read()`:对端读取配置时由应用填充配置
- `*_notification_config_changed()`:通知开关变化
适合场景:
- 方向查找、距离估计、定位试验平台
### 4.2.4 Fast Pair Provider Service, `FPS`
源码位置:
- `nrf/subsys/bluetooth/services/fast_pair/fp_gatt_service.c`
- `nrf/include/bluetooth/services/fast_pair/fast_pair.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_FAST_PAIR=y
CONFIG_BT_FAST_PAIR_GATT_SERVICE=y
```
怎么用:
- 这不是通用 GATT 服务,而是 Google Fast Pair 生态专用服务
- 正常接入方式不是自己操作 characteristic而是走高层 API
- 典型流程:
1. `bt_enable()`
2. `settings_load()`
3. 注册必要回调
4. 调用 `bt_fast_pair_enable()`
- 运行时常用 API
- `bt_fast_pair_set_pairing_mode()`
- `bt_fast_pair_battery_set()`
- `bt_fast_pair_info_cb_register()`
适合场景:
- 要做经过 Fast Pair 生态认证的耳机、输入设备、Tag 类设备
不适合场景:
- 普通 BLE 外设
- 只想做自定义配对体验
### 4.2.5 Human Interface Device Service, `HIDS`
源码位置:
- `nrf/subsys/bluetooth/services/hids.c`
- `nrf/include/bluetooth/services/hids.h`
- 参考样例:
- `nrf/samples/bluetooth/peripheral_hids_keyboard`
- `nrf/samples/bluetooth/peripheral_hids_mouse`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_HIDS=y
CONFIG_BT_HIDS_MAX_CLIENT_COUNT=2
CONFIG_BT_HIDS_DEFAULT_PERM_RW_ENCRYPT=y
```
怎么用:
1.`BT_HIDS_DEF(hids_obj, ...)` 定义一个 HID 服务实例
2. 准备 `struct bt_hids_init_param`
- HID 信息
- 输入/输出/特征报告组
- Report Map
- 协议模式回调
- Control Point 回调
- Boot Keyboard/Mouse 回调
- `is_kb` / `is_mouse`
3. 调用 `bt_hids_init(&hids_obj, &init_param)`
4. 连接回调里调用:
- `bt_hids_connected(&hids_obj, conn)`
- `bt_hids_disconnected(&hids_obj, conn)`
5. 发报告时调用:
- 通用输入报告:`bt_hids_inp_rep_send()`
- Boot Keyboard`bt_hids_boot_kb_inp_rep_send()`
- Boot Mouse`bt_hids_boot_mouse_inp_rep_send()`
对键盘项目的意义:
- 如果你走标准 HID over GATT 键盘路线,这是最关键的服务实现
- 比起自定义 NUSHIDS 才是操作系统原生识别键盘的正路
### 4.2.6 Latency Service
源码位置:
- `nrf/subsys/bluetooth/services/latency.c`
- `nrf/include/bluetooth/services/latency.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_LATENCY=y
```
怎么用:
- 定义一个 `struct bt_latency latency`
- 可选定义 `struct bt_latency_cb`
- 调用 `bt_latency_init(&latency, &cb)`
- 对端对 Latency Characteristic 发写请求时,会回调 `latency_request()`
适合场景:
- 链路时延测量
- 实验室验证,不是典型产品服务
### 4.2.7 LED Button Service, `LBS`
源码位置:
- `nrf/subsys/bluetooth/services/lbs.c`
- `nrf/include/bluetooth/services/lbs.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_LBS=y
```
怎么用:
- 定义 `struct bt_lbs_cb`
- `led_cb`:远端写 LED 特征值时回调
- `button_cb`:远端读 Button 特征值时回调
- 初始化:`bt_lbs_init(&callbacks)`
- 按键状态变化时调用 `bt_lbs_send_button_state(button_state)`
适合场景:
- 教学、验证链路、最小双向 GATT 交互 demo
不建议:
- 直接当成量产产品协议
### 4.2.8 Memfault Diagnostic Service, `MDS`
源码位置:
- `nrf/subsys/bluetooth/services/mds.c`
- `nrf/include/bluetooth/services/mds.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_MDS=y
```
怎么用:
- 这个服务主要为 Memfault 诊断导出服务
- 启用后服务静态注册
- 如果想控制谁能访问诊断数据,在 `bt_enable()` 前调用 `bt_mds_cb_register(&cb)`
- 关键回调是 `access_enable(conn)`,用来决定连接方是否有权限访问 MDS 数据
适合场景:
- 你已经集成 Memfault并希望通过 BLE 导出诊断信息
### 4.2.9 Nordic Status Message Service, `NSMS`
源码位置:
- `nrf/subsys/bluetooth/services/nsms.c`
- `nrf/include/bluetooth/services/nsms.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_NSMS=y
```
怎么用:
- 这个服务不是 `init()` 风格,而是**宏定义实例**
-`BT_NSMS_DEF(nsms_obj, "Status", BT_NSMS_SECURITY_LEVEL_ENCRYPT, "idle", 64)` 定义一个服务实例
- 之后运行时调用 `bt_nsms_set_status(&nsms_obj, "connected")` 更新状态文本
特点:
- 适合快速暴露一段人类可读状态字符串
- 支持安全级别配置
- 支持多实例
适合场景:
- 开发调试状态
- 设备工作模式简报
### 4.2.10 Nordic UART Service, `Nordic 旧版 NUS`
源码位置:
- `nrf/subsys/bluetooth/services/nus.c`
- `nrf/include/bluetooth/services/nus.h`
- 参考样例:`nrf/samples/bluetooth/peripheral_uart`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_NUS=y
```
可选:
```conf
CONFIG_BT_NUS_AUTHEN=y
```
怎么用:
- 定义 `struct bt_nus_cb`
- 调用 `bt_nus_init(&callbacks)`
-`received()` 回调里处理远端写进来的数据
- 调用 `bt_nus_send(conn, data, len)` 给远端发通知
- `bt_nus_get_mtu(conn)` 可拿到当前可用有效负载大小
和 Zephyr 版 NUS 的区别:
- Nordic 旧版更接近历史 NCS 示例代码
- API 更简单,通常单实例
- 如果项目已经基于 `peripheral_uart` 一类示例写的,通常继续沿用这个版本更省事
注意:
- **不要同时把 Zephyr NUS 和 Nordic 旧版 NUS 当成同一个业务通道一起开**,它们 UUID 相同,容易造成概念和实现混乱
### 4.2.11 Ranging Service, `RAS`
源码位置:
- `nrf/subsys/bluetooth/services/ras/rrsp/ras_rrsp.c`
- `nrf/include/bluetooth/services/ras.h`
启用方法:
如果本机要作为**服务端/响应端**暴露 Ranging Service
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_RAS=y
CONFIG_BT_RAS_RRSP=y
```
怎么用:
- 服务端 RRSP 角色启用后会注册标准 Ranging Service
- 每个连接通常要先调用 `bt_ras_rrsp_alloc(conn)` 关联上下文
- 通过 `bt_ras_rd_buffer_cb_register()` 监听 ranging data buffer 生命周期
- 当有新的测距数据可提供时,使用数据缓冲 API
- `bt_ras_rd_buffer_ready_check()`
- `bt_ras_rd_buffer_claim()`
- `bt_ras_rd_buffer_release()`
- `bt_ras_rd_buffer_bytes_pull()`
- 连接释放时调用 `bt_ras_rrsp_free(conn)`
补充说明:
- 同一个头文件里还提供 `RREQ` API那是**客户端/请求端**使用的,不是本地服务端注册逻辑
适合场景:
- BLE Channel Sounding / Ranging 实验与产品验证
### 4.2.12 Running Speed and Cadence Service, `RSCS`
源码位置:
- `nrf/subsys/bluetooth/services/rscs.c`
- `nrf/include/bluetooth/services/rscs.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_RSCS=y
```
怎么用:
-`struct bt_rscs_init_params`
- `features`
- 支持的位置列表
- 当前位置
- 事件处理函数
- 控制点回调
- 调用 `bt_rscs_init(&init)`
- 生成新测量值时调用 `bt_rscs_measurement_send(conn, &measurement)`
控制点回调负责:
- `set_cumulative()`
- `calibration()`
- `update_loc()`
适合场景:
- 跑步传感器、脚环、步频速度设备
### 4.2.13 Throughput Service
源码位置:
- `nrf/subsys/bluetooth/services/throughput.c`
- `nrf/include/bluetooth/services/throughput.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_THROUGHPUT=y
```
怎么用:
- 定义 `struct bt_throughput throughput`
- 定义 `struct bt_throughput_cb`
- 调用 `bt_throughput_init(&throughput, &cb)`
如果本机作为服务端:
- 服务会被注册为一个测试用 Throughput Service
- 对端读/写该特征值时,你会在回调里拿到吞吐统计信息
如果本机作为客户端测试远端吞吐:
-`bt_gatt_dm` 发现远端 Throughput Service
- 调用 `bt_throughput_handles_assign(dm, &throughput)`
- 然后用 `bt_throughput_read()` / `bt_throughput_write()` 发测试流量
适合场景:
- 链路性能压测
- MTU / DLE / PHY 参数实验
### 4.2.14 Wi-Fi Provisioning Service
源码位置:
- `nrf/subsys/bluetooth/services/wifi_prov/wifi_prov_ble.c`
- `nrf/include/bluetooth/services/wifi_provisioning.h`
- 配套核心库:`nrf/include/net/wifi_prov_core/wifi_prov_core.h`
启用方法:
```conf
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_WIFI_PROV=y
```
怎么用:
- 这个 BLE GATT 服务本身是 Wi-Fi Provisioning Core 的传输承载层
- 你真正要初始化的是 `wifi_prov_core`
- 典型流程:
1. 调用 `wifi_prov_init()`
2. 广告里带上 Provisioning Service UUID
3. 手机/配置工具向 Control Point 写 provisioning 请求
4. 核心库通过 `wifi_prov_send_rsp()` / `wifi_prov_send_result()` 把结果经 BLE 返回
适合场景:
- 设备本身有 Wi-Fi但没有屏幕/键盘,需要靠手机配网
## 5. 哪些是客户端辅助库,不是本地服务端
下面这些模块也在 `services` 目录里,但它们主要是**Central 侧访问远端服务**用的,不是“本机注册一个 GATT Service”
- `ams_client`
- `ancs_client`
- `bas_client`
- `cts_client`
- `dfu_smp`
- `gattp`
- `hogp`
- `hrs_client`
- `ias` 的 client API 部分
- `latency_client`
- `nus_client`
- `ots` 的 client API 部分
- `ras``RREQ` API 部分
如果你需要,我可以下一步单独再整理一份“**这些 client 库分别怎么发现远端服务、怎么订阅、怎么读写**”。
## 6. 对 `new_kbd` 项目的直接建议
如果目标是 BLE 键盘,优先级基本如下:
### 第一优先级
- `HIDS`:标准键盘主服务
- `BAS`:电量上报
- `DIS`:厂商/型号/版本
### 第二优先级
- `BMS`:如果你想让主机侧触发清配对
- `IAS` / `Proximity` 类思路:如果要做“找键盘”
- `TPS`:若你想暴露标准发射功率
### 通常不建议直接用于键盘主链路
- `NUS`:适合调试、配置通道,不适合作为系统键盘输入主协议
- `OTS`:太重
- `LBS`:示例用途
- `Throughput` / `Latency`:测试用途
## 7. 参考代码路径
你后面如果要继续落到工程实现,最值得先看的参考代码是:
- `C:\ncs\v3.2.3\nrf\samples\bluetooth\peripheral_hids_keyboard`
- `C:\ncs\v3.2.3\nrf\samples\bluetooth\peripheral_uart`
- `C:\ncs\v3.2.3\zephyr\samples\bluetooth\peripheral_nus`
- `C:\ncs\v3.2.3\zephyr\samples\bluetooth\peripheral_ots`
如果你要,我下一步可以继续给你补一份“**把这些 Service 映射到 `new_kbd` 当前代码结构的落地建议**”,直接对应你现在的 `ble_bond_module.c``ble_battery_module.c` 和后续 HIDS 模块拆分方式。

View File

@@ -90,7 +90,7 @@
## 未纳入本索引的项目项 ## 未纳入本索引的项目项
- `zephyr/drivers/power/ip5305.h``CONFIG_IP5305`:当前项目使用的是自定义驱动/自定义绑定Zephyr 官方 latest 站点未核到对应官方页面,因此未纳入 - `zephyr/drivers/power/ip5306.h``CONFIG_IP5306`:当前项目使用的是自定义驱动/自定义绑定Zephyr 官方 latest 站点未核到对应官方页面,因此未纳入
- `pm_static.yml` 对应的 Partition Manager 规划:属于 Nordic NCS 范畴,不属于 Zephyr 官方 docs 站点范围Nordic 官方索引见 [nordic_ncs_官方知识索引.md](./nordic_ncs_官方知识索引.md) - `pm_static.yml` 对应的 Partition Manager 规划:属于 Nordic NCS 范畴,不属于 Zephyr 官方 docs 站点范围Nordic 官方索引见 [nordic_ncs_官方知识索引.md](./nordic_ncs_官方知识索引.md)
- `CAF``App Event Manager``settings_loader``ble_state``ble_common_event``power_manager``buttons_def.h` 等:属于 Nordic NCS/CAF 文档范围,不属于本 Zephyr 官方索引Nordic 官方索引见 [nordic_ncs_官方知识索引.md](./nordic_ncs_官方知识索引.md) - `CAF``App Event Manager``settings_loader``ble_state``ble_common_event``power_manager``buttons_def.h` 等:属于 Nordic NCS/CAF 文档范围,不属于本 Zephyr 官方索引Nordic 官方索引见 [nordic_ncs_官方知识索引.md](./nordic_ncs_官方知识索引.md)
- 项目私有协议与实现,如时间同步私有 GATT 服务、主机 HID 命令协议、显示主题持久化逻辑等:只索引其依赖的 Zephyr 通用能力,不索引项目私有设计本身 - 项目私有协议与实现,如时间同步私有 GATT 服务、主机 HID 命令协议、显示主题持久化逻辑等:只索引其依赖的 Zephyr 通用能力,不索引项目私有设计本身

View File

@@ -0,0 +1,5 @@
zephyr_library()
zephyr_library_sources_ifdef(CONFIG_IP5306 drivers/power/ip5306.c)
zephyr_library_sources_ifdef(CONFIG_IP5306 drivers/power/ip5306_keepalive_sw.c)
zephyr_library_sources_ifdef(CONFIG_IP5306_KEEPALIVE_HW_NRF drivers/power/ip5306_keepalive_nrf.c)
zephyr_include_directories(include)

5
modules/ip5306/Kconfig Normal file
View File

@@ -0,0 +1,5 @@
menu "IP5306 Module"
rsource "drivers/power/Kconfig"
endmenu

View File

@@ -0,0 +1,17 @@
config IP5306
bool "Injoinic IP5306 PMIC driver"
depends on I2C && GPIO
default n
help
Enable IP5306 PMIC driver over I2C.
config IP5306_KEEPALIVE_HW_NRF
bool "Enable nRF keepalive HW offload backend"
depends on IP5306 && SOC_FAMILY_NORDIC_NRF
select NRFX_GPIOTE
select NRFX_GPPI
select NRFX_RTC2
default y
help
Enable low-power keepalive offload backend using RTC2 + GPIOTE + GPPI.
Runtime selection is controlled by devicetree property keepalive-offload.

View File

@@ -0,0 +1,152 @@
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <zephyr/device.h>
#include <zephyr/drivers/power/ip5306.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/util.h>
#include "ip5306_priv.h"
#if IS_ENABLED(CONFIG_IP5306_KEEPALIVE_HW_NRF)
#include <soc_nrf_common.h>
#define IP5306_CFG_KEEPALIVE_PSEL_INIT(inst) \
.keepalive_psel = NRF_DT_GPIOS_TO_PSEL_OR(DT_DRV_INST(inst), keepalive_gpios, 0),
#else
#define IP5306_CFG_KEEPALIVE_PSEL_INIT(inst)
#endif
LOG_MODULE_REGISTER(ip5306, LOG_LEVEL_INF);
#define DT_DRV_COMPAT injoinic_ip5306
#define IP5306_REG_READ0 0x70
#define IP5306_REG_READ1 0x71
#define IP5306_STATUS_BIT BIT(3)
static int ip5306_read_reg(const struct device *dev, uint8_t reg, uint8_t *val)
{
const struct ip5306_config *cfg = dev->config;
return i2c_reg_read_byte_dt(&cfg->i2c, reg, val);
}
static int ip5306_get_status_bit(const struct device *dev, uint8_t reg, bool *flag)
{
uint8_t value = 0U;
int ret;
if (flag == NULL) {
return -EINVAL;
}
ret = ip5306_read_reg(dev, reg, &value);
if (ret != 0) {
return ret;
}
*flag = ((value & IP5306_STATUS_BIT) != 0U);
return 0;
}
static int ip5306_api_is_charging(const struct device *dev, bool *charging)
{
return ip5306_get_status_bit(dev, IP5306_REG_READ0, charging);
}
static int ip5306_api_is_charge_full(const struct device *dev, bool *full)
{
return ip5306_get_status_bit(dev, IP5306_REG_READ1, full);
}
static void ip5306_keepalive_start(const struct device *dev)
{
struct ip5306_data *data = dev->data;
const struct ip5306_config *cfg = dev->config;
if (!cfg->has_keepalive_gpio || (data->keepalive_interval_ms == 0U)) {
data->backend = IP5306_KEEPALIVE_BACKEND_NONE;
return;
}
/*
* 选择策略:
* 1) DTS 显式请求硬件 offload 时,先尝试硬件后端;
* 2) 若硬件依赖不可用或资源被占用,则告警后回退软件后端。
*/
if (cfg->keepalive_offload) {
#if IS_ENABLED(CONFIG_IP5306_KEEPALIVE_HW_NRF)
int ret = ip5306_keepalive_hw_nrf_start(dev);
if (ret == 0) {
LOG_INF("Keepalive backend=HW(nRF), pulse=%ums interval=%ums",
data->keepalive_pulse_ms, data->keepalive_interval_ms);
return;
}
LOG_WRN("HW keepalive unavailable (%d), fallback to SW backend", ret);
#else
LOG_WRN("HW keepalive requested but HW backend is not built, fallback to SW backend");
#endif
}
ip5306_keepalive_sw_start(dev);
LOG_INF("Keepalive backend=SW, pulse=%ums interval=%ums",
data->keepalive_pulse_ms, data->keepalive_interval_ms);
}
static int ip5306_init(const struct device *dev)
{
const struct ip5306_config *cfg = dev->config;
struct ip5306_data *data = dev->data;
if (!i2c_is_ready_dt(&cfg->i2c)) {
return -ENODEV;
}
if (cfg->has_keepalive_gpio) {
if (!gpio_is_ready_dt(&cfg->keepalive_gpio)) {
return -ENODEV;
}
if (gpio_pin_configure_dt(&cfg->keepalive_gpio, GPIO_OUTPUT_INACTIVE) != 0) {
return -EIO;
}
}
data->keepalive_high = false;
data->dev = dev;
data->backend = IP5306_KEEPALIVE_BACKEND_NONE;
data->keepalive_pulse_ms = (cfg->keepalive_pulse_ms != 0U) ?
cfg->keepalive_pulse_ms : IP5306_KEEPALIVE_DEFAULT_PULSE_MS;
data->keepalive_interval_ms = (cfg->keepalive_interval_ms != 0U) ?
cfg->keepalive_interval_ms : IP5306_KEEPALIVE_DEFAULT_INTERVAL_MS;
ip5306_keepalive_start(dev);
return 0;
}
static const struct ip5306_driver_api ip5306_api = {
.is_charging = ip5306_api_is_charging,
.is_charge_full = ip5306_api_is_charge_full,
};
#define IP5306_DEFINE(inst) \
static struct ip5306_data ip5306_data_##inst; \
static const struct ip5306_config ip5306_cfg_##inst = { \
.i2c = I2C_DT_SPEC_INST_GET(inst), \
.keepalive_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, keepalive_gpios, {0}), \
IP5306_CFG_KEEPALIVE_PSEL_INIT(inst) \
.keepalive_pulse_ms = DT_INST_PROP(inst, keepalive_pulse_ms), \
.keepalive_interval_ms = DT_INST_PROP(inst, keepalive_interval_ms), \
.has_keepalive_gpio = DT_INST_NODE_HAS_PROP(inst, keepalive_gpios), \
.keepalive_offload = DT_INST_PROP_OR(inst, keepalive_offload, false), \
}; \
DEVICE_DT_INST_DEFINE(inst, ip5306_init, NULL, &ip5306_data_##inst, \
&ip5306_cfg_##inst, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
&ip5306_api);
DT_INST_FOREACH_STATUS_OKAY(IP5306_DEFINE)

View File

@@ -0,0 +1,198 @@
#include <errno.h>
#include <hal/nrf_rtc.h>
#include <nrfx_gpiote.h>
#include <nrfx_rtc.h>
#include <helpers/nrfx_gppi.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/util.h>
#include "ip5306_priv.h"
LOG_MODULE_DECLARE(ip5306, LOG_LEVEL_INF);
#define IP5306_KEEPALIVE_RTC_FREQUENCY_HZ 32768U
#define IP5306_KEEPALIVE_RTC_CC_HIGH 0U
#define IP5306_KEEPALIVE_RTC_CC_LOW 1U
#define IP5306_KEEPALIVE_RTC_CC_PERIOD 2U
#define IP5306_KEEPALIVE_MIN_PERIOD_TICKS 3U
/* nRF52 单核场景默认使用 GPIOTE0 + RTC2。 */
static nrfx_gpiote_t ip5306_keepalive_gpiote = NRFX_GPIOTE_INSTANCE(NRF_GPIOTE_INST_GET(0));
static const nrfx_rtc_t ip5306_keepalive_rtc = NRFX_RTC_INSTANCE(2);
static bool ip5306_keepalive_hw_claimed;
static void ip5306_keepalive_rtc_handler(nrfx_rtc_int_type_t int_type)
{
/*
* 这里故意不处理任何中断逻辑:
* 本模块的保活脉冲由 RTC event -> GPPI/PPI -> GPIOTE task 的硬件链路完成,
* 不依赖线程或 ISR 参与,从而减少 CPU 唤醒次数、降低功耗。
*/
ARG_UNUSED(int_type);
}
static uint32_t ip5306_keepalive_ms_to_rtc_ticks(uint32_t time_ms)
{
/* 四舍五入到最近 tick降低长时间运行时累计漂移。 */
uint64_t ticks = ((uint64_t)time_ms * IP5306_KEEPALIVE_RTC_FREQUENCY_HZ + 500U) / 1000U;
return (uint32_t)MAX(ticks, 1U);
}
int ip5306_keepalive_hw_nrf_start(const struct device *dev)
{
struct ip5306_data *data = dev->data;
const struct ip5306_config *cfg = dev->config;
const bool active_high = ((cfg->keepalive_gpio.dt_flags & GPIO_ACTIVE_LOW) == 0U);
const uint32_t period_ticks = ip5306_keepalive_ms_to_rtc_ticks(data->keepalive_interval_ms);
uint32_t high_ticks = ip5306_keepalive_ms_to_rtc_ticks(data->keepalive_pulse_ms);
int ret;
if (ip5306_keepalive_hw_claimed) {
return -EBUSY;
}
if (period_ticks < IP5306_KEEPALIVE_MIN_PERIOD_TICKS) {
return -EINVAL;
}
if (high_ticks >= (period_ticks - 1U)) {
high_ticks = period_ticks - 1U;
}
if (!nrfx_gpiote_init_check(&ip5306_keepalive_gpiote)) {
ret = nrfx_gpiote_init(&ip5306_keepalive_gpiote, NRFX_GPIOTE_DEFAULT_CONFIG_IRQ_PRIORITY);
if ((ret != 0) && (ret != -EALREADY)) {
return ret;
}
}
ret = nrfx_gpiote_channel_alloc(&ip5306_keepalive_gpiote, &data->keepalive_gpiote_channel);
if (ret != 0) {
/* GPIOTE 通道不足通常意味着被其他驱动占用。 */
LOG_ERR("Failed to allocate GPIOTE channel");
return ret;
}
const nrfx_gpiote_output_config_t output_config = {
.drive = NRF_GPIO_PIN_S0S1,
.input_connect = NRF_GPIO_PIN_INPUT_DISCONNECT,
.pull = NRF_GPIO_PIN_NOPULL,
};
const nrfx_gpiote_task_config_t task_config = {
.task_ch = data->keepalive_gpiote_channel,
.polarity = NRF_GPIOTE_POLARITY_TOGGLE,
.init_val = active_high ? NRF_GPIOTE_INITIAL_VALUE_LOW : NRF_GPIOTE_INITIAL_VALUE_HIGH,
};
ret = nrfx_gpiote_output_configure(&ip5306_keepalive_gpiote, cfg->keepalive_psel,
&output_config, &task_config);
if (ret != 0) {
goto err_free_channel;
}
nrfx_gpiote_out_task_enable(&ip5306_keepalive_gpiote, cfg->keepalive_psel);
nrfx_gpiote_out_task_force(&ip5306_keepalive_gpiote, cfg->keepalive_psel, active_high ? 0U : 1U);
/*
* RTC2 初始化前先检查是否已被外部占用:
* 若占用则直接返回 -EBUSY让上层走软件保活回退。
*/
if (nrfx_rtc_init_check(&ip5306_keepalive_rtc)) {
ret = -EBUSY;
goto err_disable_task;
}
const nrfx_rtc_config_t rtc_cfg = NRFX_RTC_DEFAULT_CONFIG;
ret = nrfx_rtc_init(&ip5306_keepalive_rtc, &rtc_cfg, ip5306_keepalive_rtc_handler);
if ((ret != 0) && (ret != -EALREADY)) {
goto err_disable_task;
}
nrfx_rtc_disable(&ip5306_keepalive_rtc);
nrfx_rtc_counter_clear(&ip5306_keepalive_rtc);
/*
* RTC 角色:时间基准,按 compare 点产生事件。
* - CC0 到点 -> 产生 COMPARE0 event脉冲上升沿触发点
* - CC1 到点 -> 产生 COMPARE1 event脉冲下降沿触发点
* - CC2 到点 -> 触发周期边界,配合 SHORT 实现自动循环
*/
ret = nrfx_rtc_cc_set(&ip5306_keepalive_rtc, IP5306_KEEPALIVE_RTC_CC_HIGH, period_ticks - high_ticks - 10U, false);
if (ret != 0) {
goto err_disable_task;
}
ret = nrfx_rtc_cc_set(&ip5306_keepalive_rtc, IP5306_KEEPALIVE_RTC_CC_LOW, period_ticks - 10U, false);
if (ret != 0) {
goto err_disable_task;
}
ret = nrfx_rtc_cc_set(&ip5306_keepalive_rtc, IP5306_KEEPALIVE_RTC_CC_PERIOD, period_ticks, false);
if (ret != 0) {
goto err_disable_task;
}
const uint32_t set_task = active_high ?
nrfx_gpiote_set_task_address_get(&ip5306_keepalive_gpiote, cfg->keepalive_psel) :
nrfx_gpiote_clr_task_address_get(&ip5306_keepalive_gpiote, cfg->keepalive_psel);
const uint32_t clr_task = active_high ?
nrfx_gpiote_clr_task_address_get(&ip5306_keepalive_gpiote, cfg->keepalive_psel) :
nrfx_gpiote_set_task_address_get(&ip5306_keepalive_gpiote, cfg->keepalive_psel);
const uint32_t high_event = nrfx_rtc_event_address_get(&ip5306_keepalive_rtc, NRF_RTC_EVENT_COMPARE_0);
const uint32_t low_event = nrfx_rtc_event_address_get(&ip5306_keepalive_rtc, NRF_RTC_EVENT_COMPARE_1);
const uint32_t period_event = nrfx_rtc_event_address_get(&ip5306_keepalive_rtc, NRF_RTC_EVENT_COMPARE_2);
const uint32_t clear_task = nrfx_rtc_task_address_get(&ip5306_keepalive_rtc, NRF_RTC_TASK_CLEAR);
/*
* GPPI/PPI 角色:事件路由器。
* 将 RTC compare event 直接连接到 GPIOTE task不经过线程/ISR。
*/
ret = nrfx_gppi_conn_alloc(high_event, set_task, &data->keepalive_set_handle);
if (ret != 0) {
goto err_disable_task;
}
ret = nrfx_gppi_conn_alloc(low_event, clr_task, &data->keepalive_clr_handle);
if (ret != 0) {
nrfx_gppi_conn_free(high_event, set_task, data->keepalive_set_handle);
goto err_disable_task;
}
/*
* 某些 SoC/SDK 组合下 RTC SHORT 宏不可用,因此这里用第三条 GPPI 链路实现“周期自动清零”。
* 这样仍保持纯硬件闭环COMPARE2 event -> RTC CLEAR task不需要 CPU/ISR 参与。
*/
ret = nrfx_gppi_conn_alloc(period_event, clear_task, &data->keepalive_period_handle);
if (ret != 0) {
nrfx_gppi_conn_free(low_event, clr_task, data->keepalive_clr_handle);
nrfx_gppi_conn_free(high_event, set_task, data->keepalive_set_handle);
goto err_disable_task;
}
/*
* GPIOTE 角色GPIO 执行器。
* 收到 task 后立即对目标 pin 执行 SET/CLR输出保活脉冲波形。
*/
nrfx_gppi_conn_enable(data->keepalive_set_handle);
nrfx_gppi_conn_enable(data->keepalive_clr_handle);
nrfx_gppi_conn_enable(data->keepalive_period_handle);
nrfx_rtc_enable(&ip5306_keepalive_rtc);
ip5306_keepalive_hw_claimed = true;
data->backend = IP5306_KEEPALIVE_BACKEND_HW_NRF;
data->dev = dev;
return 0;
err_disable_task:
nrfx_gpiote_out_task_disable(&ip5306_keepalive_gpiote, cfg->keepalive_psel);
err_free_channel:
(void)nrfx_gpiote_channel_free(&ip5306_keepalive_gpiote, data->keepalive_gpiote_channel);
return ret;
}

View File

@@ -0,0 +1,54 @@
#include <zephyr/logging/log.h>
#include <zephyr/sys/util.h>
#include "ip5306_priv.h"
LOG_MODULE_DECLARE(ip5306, LOG_LEVEL_INF);
void ip5306_keepalive_sw_work_handler(struct k_work *work)
{
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
struct ip5306_data *data = CONTAINER_OF(dwork, struct ip5306_data, keepalive_work);
const struct ip5306_config *cfg = data->dev->config;
int ret;
if ((data->backend != IP5306_KEEPALIVE_BACKEND_SW) || (data->keepalive_interval_ms == 0U))
{
return;
}
if (!data->keepalive_high)
{
ret = gpio_pin_set_dt(&cfg->keepalive_gpio, GPIO_OUTPUT_ACTIVE);
if (ret != 0)
{
LOG_ERR("SW keepalive set high failed: %d", ret);
(void)k_work_schedule(&data->keepalive_work, K_MSEC(data->keepalive_interval_ms));
return;
}
data->keepalive_high = true;
(void)k_work_schedule(&data->keepalive_work, K_MSEC(data->keepalive_pulse_ms));
}
else
{
ret = gpio_pin_set_dt(&cfg->keepalive_gpio, GPIO_OUTPUT_INACTIVE);
if (ret != 0)
{
LOG_ERR("SW keepalive set low failed: %d", ret);
}
data->keepalive_high = false;
(void)k_work_schedule(&data->keepalive_work, K_MSEC(data->keepalive_interval_ms));
}
}
int ip5306_keepalive_sw_start(const struct device *dev)
{
struct ip5306_data *data = dev->data;
data->backend = IP5306_KEEPALIVE_BACKEND_SW;
data->dev = dev;
k_work_init_delayable(&data->keepalive_work, ip5306_keepalive_sw_work_handler);
return (int)k_work_schedule(&data->keepalive_work, K_MSEC(data->keepalive_interval_ms));
}

View File

@@ -0,0 +1,58 @@
#ifndef IP5306_KEEPALIVE_PRIV_H_
#define IP5306_KEEPALIVE_PRIV_H_
#include <stdbool.h>
#include <stdint.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/util.h>
#if IS_ENABLED(CONFIG_IP5306_KEEPALIVE_HW_NRF)
#include <helpers/nrfx_gppi.h>
#endif
#define IP5306_KEEPALIVE_DEFAULT_PULSE_MS 100U
#define IP5306_KEEPALIVE_DEFAULT_INTERVAL_MS 20000U
enum ip5306_keepalive_backend {
IP5306_KEEPALIVE_BACKEND_NONE,
IP5306_KEEPALIVE_BACKEND_SW,
IP5306_KEEPALIVE_BACKEND_HW_NRF,
};
struct ip5306_config {
struct i2c_dt_spec i2c;
struct gpio_dt_spec keepalive_gpio;
#if IS_ENABLED(CONFIG_IP5306_KEEPALIVE_HW_NRF)
uint32_t keepalive_psel;
#endif
uint32_t keepalive_pulse_ms;
uint32_t keepalive_interval_ms;
bool has_keepalive_gpio;
bool keepalive_offload;
};
struct ip5306_data {
struct k_work_delayable keepalive_work;
bool keepalive_high;
uint32_t keepalive_pulse_ms;
uint32_t keepalive_interval_ms;
enum ip5306_keepalive_backend backend;
const struct device *dev;
#if IS_ENABLED(CONFIG_IP5306_KEEPALIVE_HW_NRF)
uint8_t keepalive_gpiote_channel;
nrfx_gppi_handle_t keepalive_set_handle;
nrfx_gppi_handle_t keepalive_clr_handle;
nrfx_gppi_handle_t keepalive_period_handle;
#endif
};
int ip5306_keepalive_sw_start(const struct device *dev);
int ip5306_keepalive_hw_nrf_start(const struct device *dev);
#endif /* IP5306_KEEPALIVE_PRIV_H_ */

View File

@@ -0,0 +1,33 @@
title: Injoinic IP5306 PMIC
description: |
Injoinic IP5306 power management IC accessed over I2C.
This binding exposes two status bits and an optional KEY pin keepalive
pulse output.
compatible: "injoinic,ip5306"
include: i2c-device.yaml
properties:
keepalive-gpios:
type: phandle-array
description: |
GPIO connected to KEY pin. The driver sends periodic pulses on this pin
to keep boost output alive.
keepalive-pulse-ms:
type: int
default: 100
description: Pulse width in milliseconds for each keepalive pulse.
keepalive-interval-ms:
type: int
default: 20000
description: Interval in milliseconds between keepalive pulses.
keepalive-offload:
type: boolean
description: |
Prefer hardware keepalive offload backend when available on this SoC.
If not set, driver always uses software keepalive backend.

View File

@@ -0,0 +1 @@
injoinic Injoinic

View File

@@ -0,0 +1,48 @@
#ifndef ZEPHYR_INCLUDE_DRIVERS_POWER_IP5306_H_
#define ZEPHYR_INCLUDE_DRIVERS_POWER_IP5306_H_
#include <stdbool.h>
#include <errno.h>
#include <zephyr/device.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef int (*ip5306_get_flag_t)(const struct device *dev, bool *flag);
struct ip5306_driver_api {
ip5306_get_flag_t is_charging;
ip5306_get_flag_t is_charge_full;
};
static inline int ip5306_is_charging(const struct device *dev, bool *charging)
{
const struct ip5306_driver_api *api =
(const struct ip5306_driver_api *)dev->api;
if (api == NULL || api->is_charging == NULL) {
return -ENOSYS;
}
return api->is_charging(dev, charging);
}
static inline int ip5306_is_charge_full(const struct device *dev, bool *full)
{
const struct ip5306_driver_api *api =
(const struct ip5306_driver_api *)dev->api;
if (api == NULL || api->is_charge_full == NULL) {
return -ENOSYS;
}
return api->is_charge_full(dev, full);
}
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_INCLUDE_DRIVERS_POWER_IP5306_H_ */

View File

@@ -0,0 +1,5 @@
build:
cmake: .
kconfig: Kconfig
settings:
dts_root: .

View File

@@ -62,6 +62,9 @@ CONFIG_BT_HIDS_FEATURE_REP_MAX=0
CONFIG_BT_BAS=y CONFIG_BT_BAS=y
CONFIG_BT_DIS=y CONFIG_BT_DIS=y
CONFIG_BT_DIS_PNP=y CONFIG_BT_DIS_PNP=y
CONFIG_BT_DIS_PNP_VID_SRC=2
CONFIG_BT_DIS_PNP_VID=0x1209
CONFIG_BT_DIS_PNP_PID=0x0001
CONFIG_USB_DEVICE_STACK_NEXT=y CONFIG_USB_DEVICE_STACK_NEXT=y
CONFIG_USBD_HID_SUPPORT=y CONFIG_USBD_HID_SUPPORT=y
@@ -89,7 +92,7 @@ CONFIG_CAF_BUTTONS_DEBOUNCE_INTERVAL=10
CONFIG_ADC=y CONFIG_ADC=y
CONFIG_I2C=y CONFIG_I2C=y
CONFIG_IP5305=y CONFIG_IP5306=y
CONFIG_SENSOR=y CONFIG_SENSOR=y
CONFIG_DISPLAY=y CONFIG_DISPLAY=y
CONFIG_MIPI_DBI=y CONFIG_MIPI_DBI=y

View File

@@ -2,7 +2,7 @@
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
#include <zephyr/device.h> #include <zephyr/device.h>
#include <zephyr/drivers/power/ip5305.h> #include <zephyr/drivers/power/ip5306.h>
#include <zephyr/drivers/sensor.h> #include <zephyr/drivers/sensor.h>
#include <zephyr/pm/device.h> #include <zephyr/pm/device.h>
#include <zephyr/sys/atomic.h> #include <zephyr/sys/atomic.h>
@@ -32,7 +32,7 @@ LOG_MODULE_REGISTER(MODULE);
#define BATTERY_EMPTY_MV 3300 #define BATTERY_EMPTY_MV 3300
#define BATTERY_FULL_MV 4100 #define BATTERY_FULL_MV 4100
static const struct device *const ip5305_dev = DEVICE_DT_GET(DT_NODELABEL(ip5305)); static const struct device *const ip5306_dev = DEVICE_DT_GET(DT_NODELABEL(ip5306));
static const struct device *const battery_sensor_dev = DEVICE_DT_GET(BATTERY_SENSE_NODE); static const struct device *const battery_sensor_dev = DEVICE_DT_GET(BATTERY_SENSE_NODE);
/* /*
@@ -164,19 +164,19 @@ static int board_power_monitor_read_voltage_mv(int32_t *voltage_mv)
return 0; return 0;
} }
/* 从 IP5305 读取一次充电态与满电态。 */ /* 从 IP5306 读取一次充电态与满电态。 */
static int board_power_monitor_read_charge_state(bool *charging, bool *full) static int board_power_monitor_read_charge_state(bool *charging, bool *full)
{ {
int err = ip5305_is_charging(ip5305_dev, charging); int err = ip5306_is_charging(ip5306_dev, charging);
if (err) { if (err) {
LOG_WRN("ip5305_is_charging failed (err=%d)", err); LOG_WRN("ip5306_is_charging failed (err=%d)", err);
return err; return err;
} }
err = ip5305_is_charge_full(ip5305_dev, full); err = ip5306_is_charge_full(ip5306_dev, full);
if (err) { if (err) {
LOG_WRN("ip5305_is_charge_full failed (err=%d)", err); LOG_WRN("ip5306_is_charge_full failed (err=%d)", err);
return err; return err;
} }
@@ -305,8 +305,8 @@ out_reschedule:
/* 初始化 board power monitor consumer并拉起首轮采样。 */ /* 初始化 board power monitor consumer并拉起首轮采样。 */
static int battery_module_init(void) static int battery_module_init(void)
{ {
if (!device_is_ready(ip5305_dev)) { if (!device_is_ready(ip5306_dev)) {
LOG_ERR("IP5305 device not ready"); LOG_ERR("IP5306 device not ready");
return -ENODEV; return -ENODEV;
} }

View File

@@ -1,6 +1,6 @@
#include <errno.h> #include <errno.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <time.h> #include <time.h>
#include <zephyr/device.h> #include <zephyr/device.h>
@@ -21,6 +21,7 @@
#include "battery_status_event.h" #include "battery_status_event.h"
#include "display_theme_event.h" #include "display_theme_event.h"
#include "display_ui.h"
#include "keyboard_led_event.h" #include "keyboard_led_event.h"
#include "mode_event.h" #include "mode_event.h"
#include "time_manager.h" #include "time_manager.h"
@@ -39,19 +40,6 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
#define DISPLAY_DEMO_BASE_HOUR 14 #define DISPLAY_DEMO_BASE_HOUR 14
#define DISPLAY_DEMO_BASE_MIN 28 #define DISPLAY_DEMO_BASE_MIN 28
#define DISPLAY_DEMO_BASE_SEC 36 #define DISPLAY_DEMO_BASE_SEC 36
#define DISPLAY_SYMBOL_PLUG "\xEF\x87\xA6" /* U+F1E6, custom plug glyph in ui_font_keyboard_small_18 */
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 enum display_pm_state
{ {
@@ -59,39 +47,6 @@ enum display_pm_state
DISPLAY_PM_STATE_OFF, 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;
struct k_work_delayable idle_work;
struct k_work_delayable theme_save_work;
struct display_ui_state ui;
uint32_t tick_count;
enum display_pm_state pm_state;
bool initialized;
bool theme_storage_dirty;
bool theme_storage_loaded;
};
struct display_theme_storage { struct display_theme_storage {
uint8_t red; uint8_t red;
uint8_t green; uint8_t green;
@@ -99,6 +54,23 @@ struct display_theme_storage {
uint8_t valid_marker; uint8_t valid_marker;
}; };
struct display_ctx
{
const struct device *dev;
struct display_capabilities caps;
struct k_work_delayable update_work;
struct k_work_delayable idle_work;
struct k_work_delayable theme_save_work;
struct display_ui_model ui;
uint32_t tick_count;
enum display_pm_state pm_state;
bool initialized;
bool theme_storage_dirty;
bool theme_storage_loaded;
char date_text[16];
char time_text[16];
};
static struct display_ctx disp = { static struct display_ctx disp = {
.dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display)), .dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display)),
.ui.theme_color = LV_COLOR_MAKE(0x4C, 0xC9, 0xF0), .ui.theme_color = LV_COLOR_MAKE(0x4C, 0xC9, 0xF0),
@@ -106,7 +78,7 @@ static struct display_ctx disp = {
.ui.battery_level = 15U, .ui.battery_level = 15U,
.ui.battery_flags = 0U, .ui.battery_flags = 0U,
.ui.mode = MODE_TYPE_USB, .ui.mode = MODE_TYPE_USB,
.ui.status_enabled = {true, true, false, true}, .ui.led_mask = 0U,
.pm_state = DISPLAY_PM_STATE_OFF, .pm_state = DISPLAY_PM_STATE_OFF,
}; };
@@ -115,15 +87,6 @@ static struct display_theme_storage display_theme_storage;
static const struct led_dt_spec display_backlight = static const struct led_dt_spec display_backlight =
LED_DT_SPEC_GET(DT_NODELABEL(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 int display_theme_store(const struct display_theme_storage *storage) static int display_theme_store(const struct display_theme_storage *storage)
{ {
char key[] = MODULE_NAME "/" DISPLAY_THEME_STORAGE_KEY; char key[] = MODULE_NAME "/" DISPLAY_THEME_STORAGE_KEY;
@@ -140,9 +103,9 @@ static int display_theme_store(const struct display_theme_storage *storage)
} }
static void display_theme_set_rgb(uint8_t red, static void display_theme_set_rgb(uint8_t red,
uint8_t green, uint8_t green,
uint8_t blue, uint8_t blue,
bool persist) bool persist)
{ {
disp.ui.theme_color = lv_color_make(red, green, blue); disp.ui.theme_color = lv_color_make(red, green, blue);
@@ -226,7 +189,6 @@ static void display_schedule_idle_timeout(k_timeout_t delay)
k_work_reschedule(&disp.idle_work, delay); k_work_reschedule(&disp.idle_work, delay);
} }
/* 背光初始化独立处理,避免 UI 创建逻辑里混入硬件使能细节。 */
static int display_backlight_set(uint8_t brightness) static int display_backlight_set(uint8_t brightness)
{ {
int err; int err;
@@ -252,7 +214,59 @@ static bool display_is_active(void)
return disp.pm_state == DISPLAY_PM_STATE_ACTIVE; return disp.pm_state == DISPLAY_PM_STATE_ACTIVE;
} }
/* 只负责保活屏幕空闲计时,不隐式点亮屏幕。 */ static void display_update_datetime_text(void)
{
struct time_manager_snapshot snapshot;
int err = time_manager_get_snapshot(&snapshot);
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(disp.date_text, sizeof(disp.date_text), "%04u/%02u/%02u",
year, month, day);
snprintk(disp.time_text, sizeof(disp.time_text), "%02u:%02u:%02u",
hour, minute, second);
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(disp.date_text, sizeof(disp.date_text), "%04d/%02d/%02d",
DISPLAY_DEMO_BASE_YEAR,
DISPLAY_DEMO_BASE_MONTH,
DISPLAY_DEMO_BASE_DAY);
snprintk(disp.time_text, sizeof(disp.time_text), "%02u:%02u:%02u",
hour, minute, second);
}
}
static void display_refresh_all_locked(void)
{
display_update_datetime_text();
display_ui_refresh_all(&disp.ui, disp.date_text, disp.time_text);
}
static void display_kick_idle_timer(void) static void display_kick_idle_timer(void)
{ {
if (!disp.initialized || !display_is_active()) if (!disp.initialized || !display_is_active())
@@ -261,7 +275,6 @@ static void display_kick_idle_timer(void)
display_schedule_idle_timeout(K_MINUTES(DISPLAY_IDLE_TIMEOUT_MIN)); display_schedule_idle_timeout(K_MINUTES(DISPLAY_IDLE_TIMEOUT_MIN));
} }
/* 熄屏时同时关闭刷新和背光,并将模块状态切到 OFF。 */
static void display_sleep(void) static void display_sleep(void)
{ {
int err; int err;
@@ -287,7 +300,6 @@ static void display_sleep(void)
module_set_state(MODULE_STATE_OFF); module_set_state(MODULE_STATE_OFF);
} }
/* 唤醒屏幕后立刻刷新 UI并重新启动定时刷新和空闲超时。 */
static void display_wake(void) static void display_wake(void)
{ {
int err; int err;
@@ -323,309 +335,6 @@ static void display_idle_timeout_fn(struct k_work *work)
display_sleep(); 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);
}
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);
}
/*
* 时间优先显示 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) static void display_update_work_fn(struct k_work *work)
{ {
ARG_UNUSED(work); ARG_UNUSED(work);
@@ -637,15 +346,15 @@ static void display_update_work_fn(struct k_work *work)
return; return;
disp.tick_count++; disp.tick_count++;
display_update_datetime_text();
lvgl_lock(); lvgl_lock();
display_refresh_datetime_locked(); display_ui_refresh_datetime(disp.date_text, disp.time_text);
lvgl_unlock(); lvgl_unlock();
display_schedule_update(K_MSEC(DISPLAY_UPDATE_PERIOD_MS)); display_schedule_update(K_MSEC(DISPLAY_UPDATE_PERIOD_MS));
} }
/* 显示初始化完成后,后续 UI 更新全部通过事件和定时刷新驱动。 */
static int display_init(void) static int display_init(void)
{ {
int err; int err;
@@ -658,15 +367,16 @@ static int display_init(void)
display_get_capabilities(disp.dev, &disp.caps); display_get_capabilities(disp.dev, &disp.caps);
LOG_INF("Display caps: %ux%u fmt=%d", LOG_INF("Display caps: %ux%u fmt=%d",
disp.caps.x_resolution, disp.caps.x_resolution,
disp.caps.y_resolution, disp.caps.y_resolution,
disp.caps.current_pixel_format); disp.caps.current_pixel_format);
k_work_init_delayable(&disp.update_work, display_update_work_fn); k_work_init_delayable(&disp.update_work, display_update_work_fn);
k_work_init_delayable(&disp.idle_work, display_idle_timeout_fn); k_work_init_delayable(&disp.idle_work, display_idle_timeout_fn);
k_work_init_delayable(&disp.theme_save_work, display_theme_save_work_fn); k_work_init_delayable(&disp.theme_save_work, display_theme_save_work_fn);
disp.tick_count = 0U; disp.tick_count = 0U;
display_theme_apply_loaded_storage(); display_theme_apply_loaded_storage();
display_update_datetime_text();
err = display_blanking_off(disp.dev); err = display_blanking_off(disp.dev);
if (err) if (err)
@@ -680,7 +390,7 @@ static int display_init(void)
return err; return err;
lvgl_lock(); lvgl_lock();
display_create_ui_locked(); display_ui_init(&disp.ui, disp.date_text, disp.time_text);
lvgl_unlock(); lvgl_unlock();
disp.initialized = true; disp.initialized = true;
@@ -692,54 +402,45 @@ static int display_init(void)
return 0; return 0;
} }
/* 电池事件只缓存最新 SOCUI 若已就绪则立即刷新顶部电池区域。 */
static bool handle_battery_status_event(const struct battery_status_event *event) 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_level = battery_status_event_get_soc(event);
disp.ui.battery_flags = battery_status_event_get_flags(event); disp.ui.battery_flags = battery_status_event_get_flags(event);
if (!disp.initialized) if (!disp.initialized || !display_is_active()) {
return false;
if (!display_is_active())
return false; return false;
}
lvgl_lock(); lvgl_lock();
display_refresh_battery_locked(); display_ui_refresh_battery(&disp.ui);
lvgl_unlock(); lvgl_unlock();
return false; return false;
} }
/* 模式事件只影响 USB/BLE 两个 badge 的亮灭。 */
static bool handle_mode_event(const struct mode_event *event) static bool handle_mode_event(const struct mode_event *event)
{ {
display_update_mode_state(event->mode_type); disp.ui.mode = event->mode_type;
if (!disp.initialized) if (!disp.initialized || !display_is_active()) {
return false;
if (!display_is_active())
return false; return false;
}
lvgl_lock(); lvgl_lock();
display_refresh_status_bar_locked(); display_ui_refresh_status_bar(&disp.ui);
lvgl_unlock(); lvgl_unlock();
return false; return false;
} }
/* NumLock/CapsLock/ScrollLock 变化后,底部三个状态 badge 立即更新。 */
static bool handle_keyboard_led_event(const struct keyboard_led_event *event) static bool handle_keyboard_led_event(const struct keyboard_led_event *event)
{ {
display_update_keyboard_led_state(keyboard_led_event_get_mask(event)); disp.ui.led_mask = keyboard_led_event_get_mask(event);
if (!disp.initialized) if (!disp.initialized || !display_is_active()) {
return false;
if (!display_is_active())
return false; return false;
}
lvgl_lock(); lvgl_lock();
display_refresh_status_bar_locked(); display_ui_refresh_status_bar(&disp.ui);
lvgl_unlock(); lvgl_unlock();
return false; return false;
} }
@@ -753,12 +454,11 @@ static bool handle_display_theme_event(const struct display_theme_event *event)
} }
lvgl_lock(); lvgl_lock();
display_refresh_status_bar_locked(); display_ui_refresh_status_bar(&disp.ui);
lvgl_unlock(); lvgl_unlock();
return false; return false;
} }
/* 任意按钮事件都可点亮屏幕并重置 1 分钟空闲计时。 */
static bool handle_button_event(const struct button_event *event) static bool handle_button_event(const struct button_event *event)
{ {
ARG_UNUSED(event); ARG_UNUSED(event);
@@ -786,7 +486,7 @@ static bool handle_module_state_event(const struct module_state_event *event)
if (disp.initialized && display_is_active()) { if (disp.initialized && display_is_active()) {
lvgl_lock(); lvgl_lock();
display_refresh_status_bar_locked(); display_ui_refresh_status_bar(&disp.ui);
lvgl_unlock(); lvgl_unlock();
} }

View File

@@ -3,8 +3,11 @@
#include <zephyr/device.h> #include <zephyr/device.h>
#include <zephyr/drivers/sensor.h> #include <zephyr/drivers/sensor.h>
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
#include <zephyr/pm/device.h>
#include <zephyr/sys/atomic.h>
#include <app_event_manager.h> #include <app_event_manager.h>
#include <caf/events/power_event.h>
#define MODULE qdec #define MODULE qdec
#include <caf/events/module_state_event.h> #include <caf/events/module_state_event.h>
@@ -24,6 +27,7 @@ struct qdec_ctx {
const struct device *dev; const struct device *dev;
struct sensor_trigger trigger; struct sensor_trigger trigger;
struct k_work_delayable emit_work; struct k_work_delayable emit_work;
atomic_t active;
int32_t acc_deg; int32_t acc_deg;
bool emit_scheduled; bool emit_scheduled;
}; };
@@ -36,8 +40,31 @@ static struct qdec_ctx qdec = {
}, },
}; };
static void qdec_reset_state(void)
{
qdec.acc_deg = 0;
qdec.emit_scheduled = false;
}
static int qdec_device_set_enabled(bool enable)
{
int err = pm_device_action_run(qdec.dev,
enable ? PM_DEVICE_ACTION_RESUME :
PM_DEVICE_ACTION_SUSPEND);
if ((err == 0) || (err == -EALREADY)) {
return 0;
}
return err;
}
static void schedule_emit_work(void) static void schedule_emit_work(void)
{ {
if (!atomic_get(&qdec.active)) {
return;
}
if (qdec.emit_scheduled) { if (qdec.emit_scheduled) {
return; return;
} }
@@ -52,6 +79,11 @@ static void qdec_emit_work_handler(struct k_work *work)
ARG_UNUSED(work); ARG_UNUSED(work);
if (!atomic_get(&qdec.active)) {
qdec.emit_scheduled = false;
return;
}
qdec.emit_scheduled = false; qdec.emit_scheduled = false;
if ((qdec.acc_deg < QDEC_DEG_PER_STEP_EVENT) && if ((qdec.acc_deg < QDEC_DEG_PER_STEP_EVENT) &&
@@ -83,6 +115,10 @@ static void qdec_data_handler(const struct device *dev,
ARG_UNUSED(trigger); ARG_UNUSED(trigger);
if (!atomic_get(&qdec.active)) {
return;
}
err = sensor_sample_fetch_chan(dev, SENSOR_CHAN_ROTATION); err = sensor_sample_fetch_chan(dev, SENSOR_CHAN_ROTATION);
if (err) { if (err) {
LOG_ERR("QDEC sample fetch failed: %d", err); LOG_ERR("QDEC sample fetch failed: %d", err);
@@ -113,8 +149,8 @@ static int qdec_init(void)
return -ENODEV; return -ENODEV;
} }
qdec.acc_deg = 0; qdec_reset_state();
qdec.emit_scheduled = false; atomic_set(&qdec.active, false);
k_work_init_delayable(&qdec.emit_work, qdec_emit_work_handler); k_work_init_delayable(&qdec.emit_work, qdec_emit_work_handler);
err = sensor_trigger_set(qdec.dev, &qdec.trigger, qdec_data_handler); err = sensor_trigger_set(qdec.dev, &qdec.trigger, qdec_data_handler);
@@ -123,12 +159,52 @@ static int qdec_init(void)
return err; return err;
} }
LOG_INF("QDEC initialized: %d deg/step, <=1 step per %d ms", LOG_INF("QDEC initialized: %d deg/step, <=1 step per %d ms",
QDEC_DEG_PER_STEP_EVENT, QDEC_EVENT_INTERVAL_MS); QDEC_DEG_PER_STEP_EVENT, QDEC_EVENT_INTERVAL_MS);
return 0; return 0;
} }
static void qdec_module_suspend(void)
{
int err;
if (!atomic_get(&qdec.active)) {
return;
}
atomic_set(&qdec.active, false);
(void)k_work_cancel_delayable(&qdec.emit_work);
qdec_reset_state();
err = qdec_device_set_enabled(false);
if (err) {
LOG_WRN("QDEC suspend failed: %d", err);
}
module_set_state(MODULE_STATE_STANDBY);
}
static void qdec_module_resume(void)
{
int err;
if (atomic_get(&qdec.active)) {
return;
}
err = qdec_device_set_enabled(true);
if (err) {
LOG_ERR("QDEC resume failed: %d", err);
module_set_state(MODULE_STATE_ERROR);
return;
}
qdec_reset_state();
atomic_set(&qdec.active, true);
module_set_state(MODULE_STATE_READY);
}
static bool app_event_handler(const struct app_event_header *aeh) static bool app_event_handler(const struct app_event_header *aeh)
{ {
if (is_module_state_event(aeh)) { if (is_module_state_event(aeh)) {
@@ -140,16 +216,28 @@ static bool app_event_handler(const struct app_event_header *aeh)
if (err) { if (err) {
module_set_state(MODULE_STATE_ERROR); module_set_state(MODULE_STATE_ERROR);
} else { } else {
module_set_state(MODULE_STATE_READY); qdec_module_resume();
} }
} }
return false; return false;
} }
if (is_power_down_event(aeh)) {
qdec_module_suspend();
return false;
}
if (is_wake_up_event(aeh)) {
qdec_module_resume();
return false;
}
__ASSERT_NO_MSG(false); __ASSERT_NO_MSG(false);
return false; return false;
} }
APP_EVENT_LISTENER(MODULE, app_event_handler); APP_EVENT_LISTENER(MODULE, app_event_handler);
APP_EVENT_SUBSCRIBE(MODULE, module_state_event); APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);

306
src/ui/display_ui.c Normal file
View File

@@ -0,0 +1,306 @@
#include <string.h>
#include <lvgl.h>
#include <zephyr/sys/printk.h>
#include "battery_status_event.h"
#include "display_ui.h"
#include "keyboard_led_event.h"
#define DISPLAY_SYMBOL_PLUG "\xEF\x87\xA6"
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,
};
struct display_ui_ctx
{
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;
};
static struct display_ui_ctx g_display_ui;
static const char *const g_status_texts[DISPLAY_STATUS_COUNT] = {
LV_SYMBOL_USB,
LV_SYMBOL_BLUETOOTH,
"1",
"A",
};
static lv_color_t display_ui_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);
}
static const char *display_ui_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;
}
static bool display_ui_status_is_active(enum display_status_id id,
const struct display_ui_model *model)
{
switch (id) {
case DISPLAY_STATUS_USB:
return model->mode == MODE_TYPE_USB;
case DISPLAY_STATUS_BLE:
return model->mode == MODE_TYPE_BLE;
case DISPLAY_STATUS_NUMLOCK:
return (model->led_mask & KEYBOARD_LED_MASK_NUM_LOCK) != 0U;
case DISPLAY_STATUS_CAPSLOCK:
return (model->led_mask & KEYBOARD_LED_MASK_CAPS_LOCK) != 0U;
default:
return false;
}
}
static void display_ui_create_status_chip(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);
g_display_ui.status_badges[id] = badge;
g_display_ui.status_labels[id] = label;
}
void display_ui_refresh_status_bar(const struct display_ui_model *model)
{
for (uint32_t i = 0; i < DISPLAY_STATUS_COUNT; i++) {
lv_obj_t *badge = g_display_ui.status_badges[i];
lv_obj_t *label = g_display_ui.status_labels[i];
bool active = display_ui_status_is_active((enum display_status_id)i, model);
if (!badge || !label) {
continue;
}
lv_obj_set_style_border_width(badge, 4, 0);
lv_obj_set_style_border_color(badge,
active ? model->theme_color :
model->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);
}
}
void display_ui_refresh_battery(const struct display_ui_model *model)
{
char battery_text[8];
lv_color_t battery_color;
const char *state_symbol = "";
lv_color_t state_color = lv_color_white();
if (!g_display_ui.battery_icon ||
!g_display_ui.battery_label ||
!g_display_ui.battery_state_label) {
return;
}
battery_color = display_ui_get_battery_color(model->battery_level);
snprintk(battery_text, sizeof(battery_text), "%u%%", model->battery_level);
if ((model->battery_flags & BATTERY_STATUS_FLAG_FULL) != 0U) {
state_symbol = DISPLAY_SYMBOL_PLUG;
state_color = lv_color_hex(0x4C9EF5);
} else if ((model->battery_flags & BATTERY_STATUS_FLAG_CHARGING) != 0U) {
state_symbol = LV_SYMBOL_CHARGE;
state_color = lv_color_hex(0xF4D35E);
}
lv_label_set_text(g_display_ui.battery_icon,
display_ui_get_battery_symbol(model->battery_level));
lv_obj_set_style_text_color(g_display_ui.battery_icon, battery_color, 0);
lv_label_set_text(g_display_ui.battery_label, battery_text);
lv_label_set_text(g_display_ui.battery_state_label, state_symbol);
lv_obj_set_style_text_color(g_display_ui.battery_state_label, state_color, 0);
}
void display_ui_refresh_datetime(const char *date_text, const char *time_text)
{
if (!g_display_ui.date_label || !g_display_ui.time_label) {
return;
}
lv_label_set_text(g_display_ui.date_label, date_text);
lv_label_set_text(g_display_ui.time_label, time_text);
}
void display_ui_refresh_all(const struct display_ui_model *model,
const char *date_text,
const char *time_text)
{
display_ui_refresh_status_bar(model);
display_ui_refresh_battery(model);
display_ui_refresh_datetime(date_text, time_text);
}
void display_ui_init(const struct display_ui_model *model,
const char *date_text,
const char *time_text)
{
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;
memset(&g_display_ui, 0, sizeof(g_display_ui));
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);
g_display_ui.date_label = lv_label_create(top_row);
lv_obj_set_style_text_font(g_display_ui.date_label, &ui_font_keyboard_small_18, 0);
lv_obj_set_style_text_color(g_display_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);
g_display_ui.battery_icon = lv_label_create(battery_wrap);
lv_obj_set_style_text_font(g_display_ui.battery_icon, &ui_font_keyboard_small_18, 0);
g_display_ui.battery_label = lv_label_create(battery_wrap);
lv_obj_set_style_text_font(g_display_ui.battery_label, &ui_font_keyboard_small_18, 0);
lv_obj_set_style_text_color(g_display_ui.battery_label, lv_color_hex(0xD8DEE9), 0);
g_display_ui.battery_state_label = lv_label_create(battery_wrap);
lv_obj_set_style_text_font(g_display_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);
g_display_ui.time_label = lv_label_create(middle_row);
lv_obj_set_style_text_font(g_display_ui.time_label, &ui_font_keyboard_time_48, 0);
lv_obj_set_style_text_color(g_display_ui.time_label, lv_color_white(), 0);
lv_obj_center(g_display_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_ui_create_status_chip(bottom_row, (enum display_status_id)i);
}
display_ui_refresh_all(model, date_text, time_text);
}

34
src/ui/display_ui.h Normal file
View File

@@ -0,0 +1,34 @@
#ifndef DISPLAY_UI_H
#define DISPLAY_UI_H
#include <stdint.h>
#include <lvgl.h>
#include "mode_event.h"
struct display_ui_model
{
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;
};
void display_ui_init(const struct display_ui_model *model,
const char *date_text,
const char *time_text);
void display_ui_refresh_all(const struct display_ui_model *model,
const char *date_text,
const char *time_text);
void display_ui_refresh_status_bar(const struct display_ui_model *model);
void display_ui_refresh_battery(const struct display_ui_model *model);
void display_ui_refresh_datetime(const char *date_text, const char *time_text);
#endif /* DISPLAY_UI_H */