feat(events): 添加功能位图状态事件并移除旧的按键功能事件
- 添加新的 function_bitmap_state_event 事件类型用于跟踪功能键位图状态 - 移除已废弃的 key_function_event 事件及其相关文件 - 更新 CMakeLists.txt 中的源文件列表以包含新事件文件 - 修改协议定义文件 device_comm.options 和 device_comm.proto 以使用位图方式传输功能键状态而不是单独的按键事件 - 更新键盘核心模块中的位图处理逻辑,添加 usage_to_bitmap_pos 辅助函数来正确定位修饰键和普通按键的位置 - 修改报告构建逻辑以正确处理新的位图布局 - 更新协议模块以处理新的功能位图状态事件和 LED 状态事件 - 实现协议模块中的 ACK、错误响应和 LED 状态编码功能
This commit is contained in:
@@ -15,10 +15,10 @@
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include "encoder_event.h"
|
||||
#include "function_bitmap_state_event.h"
|
||||
#include "function_bitmap_update_event.h"
|
||||
#include "keyboard_core.h"
|
||||
#include "keyboard_hid_report_event.h"
|
||||
#include "key_function_event.h"
|
||||
#include "mode_switch_event.h"
|
||||
#include "set_protocol_event.h"
|
||||
|
||||
@@ -146,13 +146,40 @@ static const struct keymap_entry *keymap_get(uint16_t key_id)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static bool usage_bitmap_test(const uint8_t *bitmap, uint16_t usage_id)
|
||||
static bool usage_to_bitmap_pos(uint16_t usage_id, uint8_t *byte_idx,
|
||||
uint8_t *bit_idx)
|
||||
{
|
||||
if ((bitmap == NULL) || (usage_id > KEYBOARD_PROTOCOL_USAGE_MAX)) {
|
||||
if ((byte_idx == NULL) || (bit_idx == NULL)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bitmap[usage_id / 8U] & BIT(usage_id % 8U)) != 0U;
|
||||
if ((usage_id >= KEYBOARD_USAGE_FIRST_MODIFIER) &&
|
||||
(usage_id <= KEYBOARD_USAGE_LAST_MODIFIER)) {
|
||||
*byte_idx = 0U;
|
||||
*bit_idx = (uint8_t)(usage_id - KEYBOARD_USAGE_FIRST_MODIFIER);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (usage_id <= KEYBOARD_NKRO_USAGE_MAX) {
|
||||
*byte_idx = (uint8_t)(1U + (usage_id / 8U));
|
||||
*bit_idx = (uint8_t)(usage_id % 8U);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool usage_bitmap_test(const uint8_t *bitmap, uint16_t usage_id)
|
||||
{
|
||||
uint8_t byte_idx;
|
||||
uint8_t bit_idx;
|
||||
|
||||
if ((bitmap == NULL) ||
|
||||
!usage_to_bitmap_pos(usage_id, &byte_idx, &bit_idx)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bitmap[byte_idx] & BIT(bit_idx)) != 0U;
|
||||
}
|
||||
|
||||
static bool usage_bitmap_write(uint8_t *bitmap, uint16_t usage_id, bool pressed)
|
||||
@@ -161,13 +188,12 @@ static bool usage_bitmap_write(uint8_t *bitmap, uint16_t usage_id, bool pressed)
|
||||
uint8_t bit_idx;
|
||||
bool was_pressed;
|
||||
|
||||
if ((bitmap == NULL) || (usage_id > KEYBOARD_PROTOCOL_USAGE_MAX)) {
|
||||
if ((bitmap == NULL) ||
|
||||
!usage_to_bitmap_pos(usage_id, &byte_idx, &bit_idx)) {
|
||||
LOG_WRN("Unsupported keyboard usage 0x%04x", usage_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
byte_idx = usage_id / 8U;
|
||||
bit_idx = usage_id % 8U;
|
||||
was_pressed = (bitmap[byte_idx] & BIT(bit_idx)) != 0U;
|
||||
|
||||
if (was_pressed == pressed) {
|
||||
@@ -227,12 +253,14 @@ static void build_boot_report(uint8_t report[KEYBOARD_BOOT_REPORT_SIZE])
|
||||
|
||||
build_effective_hid_bitmap(effective_hid_bitmap);
|
||||
memset(report, 0, KEYBOARD_BOOT_REPORT_SIZE);
|
||||
report[0] = effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES - 1U];
|
||||
report[0] = effective_hid_bitmap[0];
|
||||
report[1] = KEYBOARD_BOOT_RESERVED_BYTE;
|
||||
|
||||
for (uint16_t usage_id = 0; usage_id <= KEYBOARD_NKRO_USAGE_MAX; usage_id++) {
|
||||
uint8_t byte_idx = usage_id / 8U;
|
||||
uint8_t bit_idx = usage_id % 8U;
|
||||
uint8_t byte_idx;
|
||||
uint8_t bit_idx;
|
||||
|
||||
(void)usage_to_bitmap_pos(usage_id, &byte_idx, &bit_idx);
|
||||
|
||||
if ((effective_hid_bitmap[byte_idx] & BIT(bit_idx)) == 0U) {
|
||||
continue;
|
||||
@@ -254,8 +282,8 @@ static void build_nkro_report(uint8_t report[KEYBOARD_NKRO_REPORT_SIZE])
|
||||
uint8_t effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||
|
||||
build_effective_hid_bitmap(effective_hid_bitmap);
|
||||
report[0] = effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES - 1U];
|
||||
memcpy(&report[1], effective_hid_bitmap, KEYBOARD_NKRO_BITMAP_BYTES);
|
||||
report[0] = effective_hid_bitmap[0];
|
||||
memcpy(&report[1], &effective_hid_bitmap[1], KEYBOARD_NKRO_BITMAP_BYTES);
|
||||
}
|
||||
|
||||
static uint16_t active_consumer_usage_get(void)
|
||||
@@ -386,15 +414,9 @@ static void emit_all_reports(bool force)
|
||||
}
|
||||
}
|
||||
|
||||
static void emit_function_release_events(void)
|
||||
static void emit_function_state_event(void)
|
||||
{
|
||||
for (uint16_t usage_id = 0; usage_id <= KEYBOARD_PROTOCOL_USAGE_MAX; usage_id++) {
|
||||
if (!usage_bitmap_test(keyboard_state.function_pressed_bitmap, usage_id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
submit_key_function_event(usage_id, KEY_FUNCTION_ACTION_RELEASE);
|
||||
}
|
||||
(void)submit_function_bitmap_state_event(keyboard_state.pressed_usage_bitmap);
|
||||
}
|
||||
|
||||
static void emit_release_reports(enum mode_switch_mode mode)
|
||||
@@ -451,7 +473,7 @@ static void module_pause(void)
|
||||
if (mode_valid) {
|
||||
emit_release_reports(current_mode);
|
||||
}
|
||||
emit_function_release_events();
|
||||
emit_function_state_event();
|
||||
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
@@ -497,10 +519,7 @@ static bool handle_button_event(const struct button_event *event)
|
||||
}
|
||||
|
||||
if (routed_to_function) {
|
||||
submit_key_function_event(entry->usage_id,
|
||||
event->pressed ?
|
||||
KEY_FUNCTION_ACTION_PRESS :
|
||||
KEY_FUNCTION_ACTION_RELEASE);
|
||||
emit_function_state_event();
|
||||
} else {
|
||||
emit_keys_report(false);
|
||||
}
|
||||
@@ -526,7 +545,7 @@ static bool handle_mode_switch_event(const struct mode_switch_event *event)
|
||||
mode_changed = mode_valid && (current_mode != event->mode);
|
||||
if (mode_changed) {
|
||||
emit_release_reports(current_mode);
|
||||
emit_function_release_events();
|
||||
emit_function_state_event();
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user