From 9fa6723e4d74a569ff31eb47f9f469461cd7e1fb Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Wed, 8 Nov 2023 01:23:54 +0000 Subject: [PATCH] refactor(hid): Use consistent constants in HID layer. * Add constants for main item values (e.g. data/array/absolute) * Define and use constants for our report IDs. --- app/include/zmk/hid.h | 58 +++++++++++++++++++++++++++++++------------ app/src/hid.c | 9 ++++--- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/app/include/zmk/hid.h b/app/include/zmk/hid.h index a499d0afdcba..65286678d03a 100644 --- a/app/include/zmk/hid.h +++ b/app/include/zmk/hid.h @@ -18,11 +18,42 @@ #define COLLECTION_REPORT 0x03 +#define ZMK_HID_MAIN_VAL_DATA (0x00 << 0) +#define ZMK_HID_MAIN_VAL_CONST (0x01 << 0) + +#define ZMK_HID_MAIN_VAL_ARRAY (0x00 << 1) +#define ZMK_HID_MAIN_VAL_VAR (0x01 << 1) + +#define ZMK_HID_MAIN_VAL_ABS (0x00 << 2) +#define ZMK_HID_MAIN_VAL_REL (0x01 << 2) + +#define ZMK_HID_MAIN_VAL_NO_WRAP (0x00 << 3) +#define ZMK_HID_MAIN_VAL_WRAP (0x01 << 3) + +#define ZMK_HID_MAIN_VAL_LIN (0x00 << 4) +#define ZMK_HID_MAIN_VAL_NON_LIN (0x01 << 4) + +#define ZMK_HID_MAIN_VAL_PREFERRED (0x00 << 5) +#define ZMK_HID_MAIN_VAL_NO_PREFERRED (0x01 << 5) + +#define ZMK_HID_MAIN_VAL_NO_NULL (0x00 << 6) +#define ZMK_HID_MAIN_VAL_NULL (0x01 << 6) + +#define ZMK_HID_MAIN_VAL_NON_VOL (0x00 << 7) +#define ZMK_HID_MAIN_VAL_VOL (0x01 << 7) + +#define ZMK_HID_MAIN_VAL_BIT_FIELD (0x00 << 8) +#define ZMK_HID_MAIN_VAL_BUFFERED_BYTES (0x01 << 8) + +#define ZMK_HID_KEYBOARD_REPORT_ID 0x01 +#define ZMK_HID_CONSUMER_REPORT_ID 0x02 +#define ZMK_HID_MOUSE_REPORT_ID 0x03 + static const uint8_t zmk_hid_report_desc[] = { HID_USAGE_PAGE(HID_USAGE_GEN_DESKTOP), HID_USAGE(HID_USAGE_GD_KEYBOARD), HID_COLLECTION(HID_COLLECTION_APPLICATION), - HID_REPORT_ID(0x01), + HID_REPORT_ID(ZMK_HID_KEYBOARD_REPORT_ID), HID_USAGE_PAGE(HID_USAGE_KEY), HID_USAGE_MIN8(HID_USAGE_KEY_KEYBOARD_LEFTCONTROL), HID_USAGE_MAX8(HID_USAGE_KEY_KEYBOARD_RIGHT_GUI), @@ -31,14 +62,12 @@ static const uint8_t zmk_hid_report_desc[] = { HID_REPORT_SIZE(0x01), HID_REPORT_COUNT(0x08), - /* INPUT (Data,Var,Abs) */ - HID_INPUT(0x02), + HID_INPUT(ZMK_HID_MAIN_VAL_DATA | ZMK_HID_MAIN_VAL_VAR | ZMK_HID_MAIN_VAL_ABS), HID_USAGE_PAGE(HID_USAGE_KEY), HID_REPORT_SIZE(0x08), HID_REPORT_COUNT(0x01), - /* INPUT (Cnst,Var,Abs) */ - HID_INPUT(0x03), + HID_INPUT(ZMK_HID_MAIN_VAL_CONST | ZMK_HID_MAIN_VAL_VAR | ZMK_HID_MAIN_VAL_ABS), HID_USAGE_PAGE(HID_USAGE_KEY), @@ -49,8 +78,7 @@ static const uint8_t zmk_hid_report_desc[] = { HID_USAGE_MAX8(ZMK_HID_KEYBOARD_NKRO_MAX_USAGE), HID_REPORT_SIZE(0x01), HID_REPORT_COUNT(ZMK_HID_KEYBOARD_NKRO_MAX_USAGE + 1), - /* INPUT (Data,Ary,Abs) */ - HID_INPUT(0x02), + HID_INPUT(ZMK_HID_MAIN_VAL_DATA | ZMK_HID_MAIN_VAL_VAR | ZMK_HID_MAIN_VAL_ABS), #elif IS_ENABLED(CONFIG_ZMK_HID_REPORT_TYPE_HKRO) HID_LOGICAL_MIN8(0x00), HID_LOGICAL_MAX16(0xFF, 0x00), @@ -58,8 +86,7 @@ static const uint8_t zmk_hid_report_desc[] = { HID_USAGE_MAX8(0xFF), HID_REPORT_SIZE(0x08), HID_REPORT_COUNT(CONFIG_ZMK_HID_KEYBOARD_REPORT_SIZE), - /* INPUT (Data,Ary,Abs) */ - HID_INPUT(0x00), + HID_INPUT(ZMK_HID_MAIN_VAL_DATA | ZMK_HID_MAIN_VAL_ARRAY | ZMK_HID_MAIN_VAL_ABS), #else #error "A proper HID report type must be selected" #endif @@ -68,7 +95,7 @@ static const uint8_t zmk_hid_report_desc[] = { HID_USAGE_PAGE(HID_USAGE_CONSUMER), HID_USAGE(HID_USAGE_CONSUMER_CONSUMER_CONTROL), HID_COLLECTION(HID_COLLECTION_APPLICATION), - HID_REPORT_ID(0x02), + HID_REPORT_ID(ZMK_HID_CONSUMER_REPORT_ID), HID_USAGE_PAGE(HID_USAGE_CONSUMER), #if IS_ENABLED(CONFIG_ZMK_HID_CONSUMER_REPORT_USAGES_BASIC) @@ -87,14 +114,13 @@ static const uint8_t zmk_hid_report_desc[] = { #error "A proper consumer HID report usage range must be selected" #endif HID_REPORT_COUNT(CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE), - /* INPUT (Data,Ary,Abs) */ - HID_INPUT(0x00), + HID_INPUT(ZMK_HID_MAIN_VAL_DATA | ZMK_HID_MAIN_VAL_ARRAY | ZMK_HID_MAIN_VAL_ABS), HID_END_COLLECTION, HID_USAGE_PAGE(HID_USAGE_GD), HID_USAGE(HID_USAGE_GD_MOUSE), HID_COLLECTION(HID_COLLECTION_APPLICATION), - HID_REPORT_ID(0x04), + HID_REPORT_ID(ZMK_HID_MOUSE_REPORT_ID), HID_USAGE(HID_USAGE_GD_POINTER), HID_COLLECTION(HID_COLLECTION_PHYSICAL), HID_USAGE_PAGE(HID_USAGE_BUTTON), @@ -104,11 +130,11 @@ static const uint8_t zmk_hid_report_desc[] = { HID_LOGICAL_MAX8(0x01), HID_REPORT_SIZE(0x01), HID_REPORT_COUNT(0x5), - HID_INPUT(0x02), + HID_INPUT(ZMK_HID_MAIN_VAL_DATA | ZMK_HID_MAIN_VAL_VAR | ZMK_HID_MAIN_VAL_ABS), // Constant padding for the last 3 bits. HID_REPORT_SIZE(0x03), HID_REPORT_COUNT(0x01), - HID_INPUT(0x01), + HID_INPUT(ZMK_HID_MAIN_VAL_CONST | ZMK_HID_MAIN_VAL_VAR | ZMK_HID_MAIN_VAL_ABS), // Some OSes ignore pointer devices without X/Y data. HID_USAGE_PAGE(HID_USAGE_GEN_DESKTOP), HID_USAGE(HID_USAGE_GD_X), @@ -118,7 +144,7 @@ static const uint8_t zmk_hid_report_desc[] = { HID_LOGICAL_MAX8(0x7F), HID_REPORT_SIZE(0x08), HID_REPORT_COUNT(0x03), - HID_INPUT(0x06), + HID_INPUT(ZMK_HID_MAIN_VAL_DATA | ZMK_HID_MAIN_VAL_VAR | ZMK_HID_MAIN_VAL_REL), HID_END_COLLECTION, HID_END_COLLECTION, }; diff --git a/app/src/hid.c b/app/src/hid.c index b088bbbd6838..012edd8c7ee6 100644 --- a/app/src/hid.c +++ b/app/src/hid.c @@ -12,12 +12,13 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include static struct zmk_hid_keyboard_report keyboard_report = { - .report_id = 1, .body = {.modifiers = 0, ._reserved = 0, .keys = {0}}}; + .report_id = ZMK_HID_KEYBOARD_REPORT_ID, .body = {.modifiers = 0, ._reserved = 0, .keys = {0}}}; -static struct zmk_hid_consumer_report consumer_report = {.report_id = 2, .body = {.keys = {0}}}; +static struct zmk_hid_consumer_report consumer_report = {.report_id = ZMK_HID_CONSUMER_REPORT_ID, + .body = {.keys = {0}}}; -static struct zmk_hid_mouse_report mouse_report = { - .report_id = 4, .body = {.buttons = 0}}; +static struct zmk_hid_mouse_report mouse_report = {.report_id = ZMK_HID_MOUSE_REPORT_ID, + .body = {.buttons = 0}}; // Keep track of how often a modifier was pressed. // Only release the modifier if the count is 0.