Bluetooth tuning, mouse wheel and movement backend

This commit is contained in:
Alexander Krikun 2021-04-29 11:29:53 +03:00
parent 9b09974734
commit e585af2aa1
5 changed files with 94 additions and 25 deletions

View file

@ -13,3 +13,4 @@ int zmk_endpoints_toggle();
enum zmk_endpoint zmk_endpoints_selected(); enum zmk_endpoint zmk_endpoints_selected();
int zmk_endpoints_send_report(uint16_t usage_page); int zmk_endpoints_send_report(uint16_t usage_page);
int zmk_endpoints_send_mouse_report();

View file

@ -207,6 +207,18 @@ static const uint8_t zmk_hid_report_desc[] = {
0x09, 0x30, /* Usage (X) */ 0x09, 0x30, /* Usage (X) */
0x09, 0x31, /* Usage (Y) */ 0x09, 0x31, /* Usage (Y) */
0x81, 0x06, /* Input (Data,Var,Rel,No Wrap,Linear,...) */ 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 */
0xC0, /* End Collection */ 0xC0, /* End Collection */
}; };
@ -248,8 +260,10 @@ struct zmk_hid_consumer_report {
struct zmk_hid_mouse_report_body { struct zmk_hid_mouse_report_body {
zmk_mouse_button_flags_t buttons; zmk_mouse_button_flags_t buttons;
int16_t x; uint16_t x;
int16_t y; uint16_t y;
uint8_t wheel_vert;
uint8_t wheel_hor;
} __packed; } __packed;
struct zmk_hid_mouse_report { struct zmk_hid_mouse_report {
@ -276,6 +290,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_button_release(zmk_mouse_button_t button);
int zmk_hid_mouse_buttons_press(zmk_mouse_button_flags_t buttons); 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_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(); void zmk_hid_mouse_clear();
struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report(); struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report();

View file

@ -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(); struct zmk_hid_mouse_report *mouse_report = zmk_hid_get_mouse_report();
switch (current_endpoint) { switch (current_endpoint) {
@ -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) #if IS_ENABLED(CONFIG_SETTINGS)
static int endpoints_handle_set(const char *name, size_t len, settings_read_cb read_cb, static int endpoints_handle_set(const char *name, size_t len, settings_read_cb read_cb,

View file

@ -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_consumer_report consumer_report = {.report_id = 2, .body = {.keys = {0}}};
static struct zmk_hid_mouse_report mouse_report = {.report_id = 4, .body = { 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. // Keep track of how often a modifier was pressed.
// Only release the modifier if the count is 0. // Only release the modifier if the count is 0.
@ -177,6 +177,10 @@ void zmk_hid_consumer_clear() { memset(&consumer_report.body, 0, sizeof(consumer
// Only release the button if the count is 0. // 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 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 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) \ #define SET_MOUSE_BUTTONS(butts) \
{ \ { \
@ -225,11 +229,52 @@ int zmk_hid_mouse_buttons_release(zmk_mouse_button_flags_t buttons) {
return 0; return 0;
} }
void zmk_hid_mouse_clear() { #define SET_MOUSE_MOVEMENT(coor_x, coor_y) \
memset(&mouse_report.body, 0, sizeof(mouse_report.body)); { \
mouse_report.body.buffer = 1; 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() { struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report() {
return &keyboard_report; return &keyboard_report;
} }

View file

@ -57,7 +57,7 @@ static struct hids_report consumer_input = {
}; };
static struct hids_report mouse_input = { static struct hids_report mouse_input = {
.id = 0x03, .id = 0x04,
.type = HIDS_INPUT, .type = HIDS_INPUT,
}; };
@ -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) { void send_keyboard_report_callback(struct k_work *work) {
struct zmk_hid_keyboard_report_body report; 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) { while (k_msgq_get(&zmk_hog_keyboard_msgq, &report, K_NO_WAIT) == 0) {
struct bt_conn *conn = destination_connection(); struct bt_conn *conn = destination_connection();
if (conn == NULL) { if (conn == NULL) {
@ -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 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)); int err = k_msgq_put(&zmk_hog_keyboard_msgq, report, K_MSEC(100));
LOG_ERR("Sending keyboard report");
if (err) { if (err) {
switch (err) { switch (err) {
case -EAGAIN: { case -EAGAIN: {
@ -288,6 +291,8 @@ CONFIG_ZMK_BLE_MOUSE_REPORT_QUEUE_SIZE, 4);
void send_mouse_report_callback(struct k_work *work) { void send_mouse_report_callback(struct k_work *work) {
struct zmk_hid_mouse_report_body report; 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) { while (k_msgq_get(&zmk_hog_mouse_msgq, &report, K_NO_WAIT) == 0) {
struct bt_conn *conn = destination_connection(); struct bt_conn *conn = destination_connection();
if (conn == NULL) { if (conn == NULL) {
@ -295,7 +300,7 @@ void send_mouse_report_callback(struct k_work *work) {
} }
struct bt_gatt_notify_params notify_params = { struct bt_gatt_notify_params notify_params = {
.attr = &hog_svc.attrs[10], .attr = &hog_svc.attrs[13],
.data = &report, .data = &report,
.len = sizeof(report), .len = sizeof(report),
}; };
@ -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 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)); int err = k_msgq_put(&zmk_hog_mouse_msgq, report, K_MSEC(100));
LOG_ERR("Sending mouse report");
if (err) { if (err) {
switch (err) { switch (err) {
case -EAGAIN: { case -EAGAIN: {