feat(led): 添加键盘LED状态管理模块

- 新增keyboard_led_state_event事件用于处理USB/BLE HID输出报告中的LED状态
- 实现led_state_module模块,管理Num Lock指示灯和BLE状态指示灯
- 定义LED状态效果映射,包括熄灭、常亮、慢闪、快闪等效果
- 将hid_keymap_def.h从configuration目录移至inc目录
- 在BLE和USB HID模块中添加对输出报告LED掩码的解析和处理
- 配置DTS中的led_1为可用状态,更新CMakeLists.txt构建配置
This commit is contained in:
2026-03-16 11:39:27 +08:00
parent cd8101428d
commit 7587df7553
10 changed files with 364 additions and 6 deletions

View File

@@ -15,6 +15,7 @@
#include "hid_report_descriptor.h"
#include "hid_protocol_event.h"
#include "hid_report_event.h"
#include "keyboard_led_state_event.h"
#include "mode_event.h"
#include "usb_hid_event.h"
@@ -55,6 +56,8 @@ struct usb_hid_ctx {
enum usb_hid_usbd_state usbd_state;
enum hid_protocol_type current_protocol;
bool num_lock_known;
bool num_lock_on;
};
static struct usb_hid_ctx g_usb_hid = {
@@ -85,6 +88,25 @@ static void publish_usb_hid_state(void)
APP_EVENT_SUBMIT(event);
}
/* 从主机输出报告同步 Num Lock 位,并在状态变化时发布事件。 */
static void publish_num_lock_state_from_led_mask(uint8_t led_mask)
{
bool new_num_lock = (led_mask & BIT(0)) != 0U;
if (g_usb_hid.num_lock_known && (g_usb_hid.num_lock_on == new_num_lock)) {
return;
}
g_usb_hid.num_lock_known = true;
g_usb_hid.num_lock_on = new_num_lock;
struct keyboard_led_state_event *event = new_keyboard_led_state_event();
event->led_mask = led_mask;
event->num_lock = new_num_lock;
APP_EVENT_SUBMIT(event);
}
static void recompute_hid_state(void)
{
/* 兼容现有调用点:对外仅发布 enable + usbd 状态。 */
@@ -118,8 +140,11 @@ static int hid_stub_set_report(const struct device *dev,
ARG_UNUSED(dev);
ARG_UNUSED(type);
ARG_UNUSED(id);
ARG_UNUSED(len);
ARG_UNUSED(buf);
if ((len > 0U) && (buf != NULL)) {
publish_num_lock_state_from_led_mask(buf[0]);
}
return 0;
}
@@ -188,8 +213,10 @@ static void hid_stub_input_done(const struct device *dev, const uint8_t *report)
static void hid_stub_output_report(const struct device *dev, uint16_t len, const uint8_t *buf)
{
ARG_UNUSED(dev);
ARG_UNUSED(len);
ARG_UNUSED(buf);
if ((len > 0U) && (buf != NULL)) {
publish_num_lock_state_from_led_mask(buf[0]);
}
}
static void hid_iface_ready_cb(const struct device *dev, bool ready)