Skip to content

Commit

Permalink
Bluetooth tuning, mouse wheel and movement backend
Browse files Browse the repository at this point in the history
  • Loading branch information
krikun98 committed Jan 28, 2022
1 parent 54ac765 commit 016256b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 25 deletions.
1 change: 1 addition & 0 deletions app/include/zmk/endpoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ int zmk_endpoints_toggle();
enum zmk_endpoint zmk_endpoints_selected();

int zmk_endpoints_send_report(uint16_t usage_page);
int zmk_endpoints_send_mouse_report();
22 changes: 20 additions & 2 deletions app/include/zmk/hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ static const uint8_t zmk_hid_report_desc[] = {
0x09, 0x30, /* Usage (X) */
0x09, 0x31, /* Usage (Y) */
0x81, 0x06, /* Input (Data,Var,Rel,No Wrap,Linear,...) */
0x15, 0x81, /* Logical Minimum (-127) */
0x25, 0x7F, /* Logical Maximum (127) */
0x75, 0x08, /* Report Count (8) */
0x95, 0x01, /* Report Size (1) */
0x09, 0x38, /* Usage (Wheel) */
0x81, 0x06, /* Input (Data,Var,Rel,No Wrap,Linear,...) */
0x05, 0x0C, /* Usage Page (Consumer) */ // Horizontal wheel
0x0A, 0x38, 0x02, /* Usage (AC Pan) */
0x15, 0x81, /* Logical Minimum (-127) */
0x25, 0x7f, /* Logical Maximum (127) */
0x95, 0x01, /* Report Count (1) */
0x81, 0x06, /* Input (Data,Var,Rel,No Wrap,Linear,...) */
0xC0, /* End Collection */
0xC0, /* End Collection */
};
Expand Down Expand Up @@ -250,8 +262,10 @@ struct zmk_hid_consumer_report {

struct zmk_hid_mouse_report_body {
zmk_mouse_button_flags_t buttons;
int16_t x;
int16_t y;
uint16_t x;
uint16_t y;
uint8_t wheel_vert;
uint8_t wheel_hor;
} __packed;

struct zmk_hid_mouse_report {
Expand All @@ -278,6 +292,10 @@ int zmk_hid_mouse_button_press(zmk_mouse_button_t button);
int zmk_hid_mouse_button_release(zmk_mouse_button_t button);
int zmk_hid_mouse_buttons_press(zmk_mouse_button_flags_t buttons);
int zmk_hid_mouse_buttons_release(zmk_mouse_button_flags_t buttons);
int zmk_hid_mouse_movement_press(uint16_t x, uint16_t y);
int zmk_hid_mouse_movement_release(uint16_t x, uint16_t y);
int zmk_hid_mouse_wheel_press(uint8_t hor, uint8_t vert);
int zmk_hid_mouse_wheel_release(uint8_t hor, uint8_t vert);
void zmk_hid_mouse_clear();

struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report();
Expand Down
33 changes: 16 additions & 17 deletions app/src/endpoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,22 @@ static int send_consumer_report() {
}
}

static int send_mouse_report() {
int zmk_endpoints_send_report(uint16_t usage_page) {

LOG_DBG("usage page 0x%02X", usage_page);
switch (usage_page) {
case HID_USAGE_KEY:
return send_keyboard_report();
case HID_USAGE_CONSUMER:
return send_consumer_report();
default:
LOG_ERR("Unsupported usage page %d", usage_page);
return -ENOTSUP;
}
}

int zmk_endpoints_send_mouse_report() {
LOG_ERR("SENDING MOUSE REPORT");
struct zmk_hid_mouse_report *mouse_report = zmk_hid_get_mouse_report();

switch (current_endpoint) {
Expand Down Expand Up @@ -161,22 +176,6 @@ static int send_mouse_report() {
}
}

int zmk_endpoints_send_report(uint16_t usage_page) {

LOG_DBG("usage page 0x%02X", usage_page);
switch (usage_page) {
case HID_USAGE_KEY:
return send_keyboard_report();
case HID_USAGE_CONSUMER:
return send_consumer_report();
case HID_USAGE_GD:
return send_mouse_report();
default:
LOG_ERR("Unsupported usage page %d", usage_page);
return -ENOTSUP;
}
}

#if IS_ENABLED(CONFIG_SETTINGS)

static int endpoints_handle_set(const char *name, size_t len, settings_read_cb read_cb,
Expand Down
53 changes: 49 additions & 4 deletions app/src/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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 = 4, .body = {
.buttons = 0, .x = 0, .y = 0}};
.buttons = 0, .x = 0, .y = 0, .wheel_hor = 0, .wheel_vert = 0}};

// Keep track of how often a modifier was pressed.
// Only release the modifier if the count is 0.
Expand Down Expand Up @@ -186,6 +186,10 @@ void zmk_hid_consumer_clear() { memset(&consumer_report.body, 0, sizeof(consumer
// Only release the button if the count is 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;
static int16_t curr_x = 0;
static int16_t curr_y = 0;
static int8_t curr_hor = 0;
static int8_t curr_vert = 0;

#define SET_MOUSE_BUTTONS(butts) \
{ \
Expand Down Expand Up @@ -234,11 +238,52 @@ int zmk_hid_mouse_buttons_release(zmk_mouse_button_flags_t buttons) {
return 0;
}

void zmk_hid_mouse_clear() {
memset(&mouse_report.body, 0, sizeof(mouse_report.body));
mouse_report.body.buffer = 1;
#define SET_MOUSE_MOVEMENT(coor_x, coor_y) \
{ \
mouse_report.body.x = coor_x; \
LOG_DBG("Mouse movement x set to 0x%02X", mouse_report.body.x); \
mouse_report.body.y = coor_y; \
LOG_DBG("Mouse movement y set to 0x%02X", mouse_report.body.y); \
}

int zmk_hid_mouse_movement_press(uint16_t x, uint16_t y) {
curr_x += x;
curr_y += y;
SET_MOUSE_MOVEMENT(curr_x, curr_y);
return 0;
}

int zmk_hid_mouse_movement_release(uint16_t x, uint16_t y) {
curr_x -= x;
curr_y -= y;
SET_MOUSE_MOVEMENT(curr_x, curr_y);
return 0;
}

#define SET_MOUSE_WHEEL(horiz, vertic) \
{ \
mouse_report.body.wheel_hor = horiz; \
LOG_DBG("Mouse wheel hor set to 0x%02X", mouse_report.body.wheel_hor); \
mouse_report.body.wheel_vert = vertic; \
LOG_DBG("Mouse wheel vert set to 0x%02X", mouse_report.body.wheel_vert); \
}

int zmk_hid_mouse_wheel_press(uint8_t hor, uint8_t vert) {
curr_hor += hor;
curr_vert += vert;
SET_MOUSE_WHEEL(curr_hor, curr_vert);
return 0;
}

int zmk_hid_mouse_wheel_release(uint8_t hor, uint8_t vert) {
curr_hor -= hor;
curr_vert -= vert;
SET_MOUSE_WHEEL(curr_hor, curr_vert);
return 0;
}

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

struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report() {
return &keyboard_report;
}
Expand Down
10 changes: 8 additions & 2 deletions app/src/hog.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static struct hids_report consumer_input = {
};

static struct hids_report mouse_input = {
.id = 0x03,
.id = 0x04,
.type = HIDS_INPUT,
};

Expand Down Expand Up @@ -187,6 +187,8 @@ K_MSGQ_DEFINE(zmk_hog_keyboard_msgq, sizeof(struct zmk_hid_keyboard_report_body)
void send_keyboard_report_callback(struct k_work *work) {
struct zmk_hid_keyboard_report_body report;

LOG_ERR("Sending keyboard callback");

while (k_msgq_get(&zmk_hog_keyboard_msgq, &report, K_NO_WAIT) == 0) {
struct bt_conn *conn = destination_connection();
if (conn == NULL) {
Expand All @@ -212,6 +214,7 @@ K_WORK_DEFINE(hog_keyboard_work, send_keyboard_report_callback);

int zmk_hog_send_keyboard_report(struct zmk_hid_keyboard_report_body *report) {
int err = k_msgq_put(&zmk_hog_keyboard_msgq, report, K_MSEC(100));
LOG_ERR("Sending keyboard report");
if (err) {
switch (err) {
case -EAGAIN: {
Expand Down Expand Up @@ -288,14 +291,16 @@ CONFIG_ZMK_BLE_MOUSE_REPORT_QUEUE_SIZE, 4);
void send_mouse_report_callback(struct k_work *work) {
struct zmk_hid_mouse_report_body report;

LOG_ERR("Sending mouse callback");

while (k_msgq_get(&zmk_hog_mouse_msgq, &report, K_NO_WAIT) == 0) {
struct bt_conn *conn = destination_connection();
if (conn == NULL) {
return;
}

struct bt_gatt_notify_params notify_params = {
.attr = &hog_svc.attrs[10],
.attr = &hog_svc.attrs[13],
.data = &report,
.len = sizeof(report),
};
Expand All @@ -313,6 +318,7 @@ K_WORK_DEFINE(hog_mouse_work, send_mouse_report_callback);

int zmk_hog_send_mouse_report(struct zmk_hid_mouse_report_body *report) {
int err = k_msgq_put(&zmk_hog_mouse_msgq, report, K_MSEC(100));
LOG_ERR("Sending mouse report");
if (err) {
switch (err) {
case -EAGAIN: {
Expand Down

0 comments on commit 016256b

Please sign in to comment.