Skip to content

Commit

Permalink
refactor(hid): Use proper defines for HID values.
Browse files Browse the repository at this point in the history
* Add report ID defines and use them consistently.
* Add defines for main item value flags to avoid magic constants.
  • Loading branch information
petejohanson committed Nov 8, 2023
1 parent 34c8b3f commit a5a95cb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
51 changes: 38 additions & 13 deletions app/include/zmk/hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,43 @@

#define ZMK_HID_KEYBOARD_NKRO_MAX_USAGE HID_USAGE_KEY_KEYPAD_EQUAL

#define COLLECTION_REPORT 0x03
// See https://www.usb.org/sites/default/files/hid1_11.pdf section 6.2.2.4 Main Items

#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_REPORT_ID_KEYBOARD 0x01
#define ZMK_HID_REPORT_ID_CONSUMER 0x02

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_REPORT_ID_KEYBOARD),
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 @@ -30,14 +60,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 @@ -48,17 +76,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 @@ -67,7 +93,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_REPORT_ID_CONSUMER),
HID_USAGE_PAGE(HID_USAGE_CONSUMER),

#if IS_ENABLED(CONFIG_ZMK_HID_CONSUMER_REPORT_USAGES_BASIC)
Expand All @@ -86,8 +112,7 @@ 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,
};

Expand Down
5 changes: 3 additions & 2 deletions app/src/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ 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_REPORT_ID_KEYBOARD, .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_REPORT_ID_CONSUMER,
.body = {.keys = {0}}};

// Keep track of how often a modifier was pressed.
// Only release the modifier if the count is 0.
Expand Down
4 changes: 2 additions & 2 deletions app/src/hog.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ enum {
};

static struct hids_report input = {
.id = 0x01,
.id = ZMK_HID_REPORT_ID_KEYBOARD,
.type = HIDS_INPUT,
};

static struct hids_report consumer_input = {
.id = 0x02,
.id = ZMK_HID_REPORT_ID_CONSUMER,
.type = HIDS_INPUT,
};

Expand Down

0 comments on commit a5a95cb

Please sign in to comment.