Skip to content

Commit

Permalink
refactor(hid): Use consistent constants in HID layer.
Browse files Browse the repository at this point in the history
* Add constants for main item values (e.g. data/array/absolute)
* Define and use constants for our report IDs.
  • Loading branch information
petejohanson committed Nov 8, 2023
1 parent 9db131d commit 9fa6723
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
58 changes: 42 additions & 16 deletions app/include/zmk/hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),

Expand All @@ -49,17 +78,15 @@ 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),
HID_USAGE_MIN8(0x00),
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
Expand All @@ -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)
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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,
};
Expand Down
9 changes: 5 additions & 4 deletions app/src/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <dt-bindings/zmk/modifiers.h>

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.
Expand Down

0 comments on commit 9fa6723

Please sign in to comment.