Skip to content

Commit

Permalink
Fine-tuning report, 16 buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
krikun98 committed Jan 28, 2022
1 parent b14b024 commit 54ac765
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
20 changes: 9 additions & 11 deletions app/include/zmk/hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,26 +190,24 @@ static const uint8_t zmk_hid_report_desc[] = {
0x05, 0x01, /* Usage Page (Generic Desktop Ctrls) */
0x09, 0x02, /* Usage (Mouse) */
0xA1, 0x01, /* Collection (Application) */
0x85, 0x04, /* Report ID (4) */
0x09, 0x01, /* Usage (Pointer) */
0xA1, 0x00, /* Collection (Physical) */
0x05, 0x09, /* Usage Page (Button) */
0x19, 0x01, /* Usage Minimum (0x01) */
0x29, 0x03, /* Usage Maximum (0x03) */
0x29, 0x10, /* Usage Maximum (0x10) */
0x15, 0x00, /* Logical Minimum (0) */
0x25, 0x01, /* Logical Maximum (1) */
0x95, 0x03, /* Report Count (3) */
0x95, 0x10, /* Report Count (16) */
0x75, 0x01, /* Report Size (1) */
0x81, 0x02, /* Input (Data,Var,Abs,No Wrap,Linear,...) */
0x95, 0x01, /* Report Count (1) */
0x75, 0x05, /* Report Size (5) */
0x81, 0x03, /* Input (Const,Var,Abs,No Wrap,Linear,...) */
0x05, 0x01, /* Usage Page (Generic Desktop Ctrls) */
0x16, 0x01, 0x80, /* Logical Minimum (-32767) */
0x26, 0xFF, 0x7F, /* Logical Maximum (32767) */
0x75, 0x10, /* Report Size (8) */
0x95, 0x02, /* Report Count (2) */
0x09, 0x30, /* Usage (X) */
0x09, 0x31, /* Usage (Y) */
0x15, 0x81, /* Logical Minimum (129) */
0x25, 0x7F, /* Logical Maximum (127) */
0x75, 0x08, /* Report Size (8) */
0x95, 0x02, /* Report Count (2) */
0x81, 0x06, /* Input (Data,Var,Rel,No Wrap,Linear,...) */
0xC0, /* End Collection */
0xC0, /* End Collection */
Expand Down Expand Up @@ -252,8 +250,8 @@ struct zmk_hid_consumer_report {

struct zmk_hid_mouse_report_body {
zmk_mouse_button_flags_t buttons;
int8_t x;
int8_t y;
int16_t x;
int16_t y;
} __packed;

struct zmk_hid_mouse_report {
Expand Down
4 changes: 2 additions & 2 deletions app/include/zmk/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
#include <zephyr.h>
#include <dt-bindings/zmk/mouse.h>

typedef uint8_t zmk_mouse_button_flags_t;
typedef uint8_t zmk_mouse_button_t;
typedef uint16_t zmk_mouse_button_flags_t;
typedef uint16_t zmk_mouse_button_t;
23 changes: 13 additions & 10 deletions app/src/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static struct zmk_hid_keyboard_report keyboard_report = {

static struct zmk_hid_consumer_report consumer_report = {.report_id = 2, .body = {.keys = {0}}};

static struct zmk_hid_mouse_report mouse_report = {.report_id = 3, .body = {
static struct zmk_hid_mouse_report mouse_report = {.report_id = 4, .body = {
.buttons = 0, .x = 0, .y = 0}};

// Keep track of how often a modifier was pressed.
Expand Down Expand Up @@ -184,7 +184,7 @@ void zmk_hid_consumer_clear() { memset(&consumer_report.body, 0, sizeof(consumer

// Keep track of how often a button was pressed.
// Only release the button if the count is 0.
static int explicit_button_counts[3] = {0, 0, 0};
static int explicit_button_counts[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
static zmk_mod_flags_t explicit_buttons = 0;

#define SET_MOUSE_BUTTONS(butts) \
Expand All @@ -194,21 +194,21 @@ static zmk_mod_flags_t explicit_buttons = 0;
}

int zmk_hid_mouse_button_press(zmk_mouse_button_t button) {
explicit_button_counts[button-5]++;
LOG_DBG("Button %d count %d", button, explicit_button_counts[button-5]);
explicit_button_counts[button]++;
LOG_DBG("Button %d count %d", button, explicit_button_counts[button]);
WRITE_BIT(explicit_buttons, button, true);
SET_MOUSE_BUTTONS(explicit_buttons);
return 0;
}

int zmk_hid_mouse_button_release(zmk_mouse_button_t button) {
if (explicit_button_counts[button-5] <= 0) {
if (explicit_button_counts[button] <= 0) {
LOG_ERR("Tried to release button %d too often", button);
return -EINVAL;
}
explicit_button_counts[button]--;
LOG_DBG("Button %d count: %d", button, explicit_button_counts[button-5]);
if (explicit_button_counts[button-5] == 0) {
LOG_DBG("Button %d count: %d", button, explicit_button_counts[button]);
if (explicit_button_counts[button] == 0) {
LOG_DBG("Button %d released", button);
WRITE_BIT(explicit_buttons, button, false);
}
Expand All @@ -217,7 +217,7 @@ int zmk_hid_mouse_button_release(zmk_mouse_button_t button) {
}

int zmk_hid_mouse_buttons_press(zmk_mouse_button_flags_t buttons) {
for (zmk_mod_t i = 5; i < 8; i++) {
for (zmk_mod_t i = 0; i < 16; i++) {
if (buttons & (1 << i)) {
zmk_hid_mouse_button_press(i);
}
Expand All @@ -226,15 +226,18 @@ int zmk_hid_mouse_buttons_press(zmk_mouse_button_flags_t buttons) {
}

int zmk_hid_mouse_buttons_release(zmk_mouse_button_flags_t buttons) {
for (zmk_mod_t i = 5; i < 8; i++) {
for (zmk_mod_t i = 0; i < 16; i++) {
if (buttons & (1 << i)) {
zmk_hid_mouse_button_release(i);
}
}
return 0;
}

void zmk_hid_mouse_clear() { memset(&mouse_report.body, 0, sizeof(mouse_report.body)); }
void zmk_hid_mouse_clear() {
memset(&mouse_report.body, 0, sizeof(mouse_report.body));
mouse_report.body.buffer = 1;
}

struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report() {
return &keyboard_report;
Expand Down

0 comments on commit 54ac765

Please sign in to comment.