Skip to content

Commit

Permalink
Send mouse messages from dedicated thread
Browse files Browse the repository at this point in the history
  • Loading branch information
krikun98 committed Jan 28, 2022
1 parent 992bbc0 commit 8088585
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app/src/endpoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ int zmk_endpoints_send_mouse_report() {

#if IS_ENABLED(CONFIG_ZMK_BLE)
case ZMK_ENDPOINT_BLE: {
#if IS_ENABLED(CONFIG_ZMK_MOUSE_WORK_QUEUE_DEDICATED)
int err = zmk_hog_send_mouse_report_direct(&mouse_report->body);
#else
int err = zmk_hog_send_mouse_report(&mouse_report->body);
#endif
if (err) {
LOG_ERR("FAILED TO SEND OVER HOG: %d", err);
}
Expand Down
23 changes: 23 additions & 0 deletions app/src/hog.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,29 @@ int zmk_hog_send_mouse_report(struct zmk_hid_mouse_report_body *report) {
return 0;
};

int zmk_hog_send_mouse_report_direct(struct zmk_hid_mouse_report_body *report) {
struct bt_conn *conn = destination_connection();
if (conn == NULL) {
return 1;
}

struct bt_gatt_notify_params notify_params = {
.attr = &hog_svc.attrs[13],
.data = report,
.len = sizeof(*report),
};

int err = bt_gatt_notify_cb(conn, &notify_params);
if (err) {
LOG_DBG("Error notifying %d", err);
return err;
}

bt_conn_unref(conn);

return 0;
};

int zmk_hog_init(const struct device *_arg) {
k_work_q_start(&hog_work_q, hog_q_stack, K_THREAD_STACK_SIZEOF(hog_q_stack),
CONFIG_ZMK_BLE_THREAD_PRIORITY);
Expand Down
2 changes: 1 addition & 1 deletion app/src/mouse/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ config ZMK_MOUSE_DEDICATED_THREAD_STACK_SIZE

config ZMK_MOUSE_DEDICATED_THREAD_PRIORITY
int "Thread priority for dedicated mouse thread/queue"
default 5
default 3

endif # ZMK_MOUSE_WORK_QUEUE_DEDICATED

Expand Down
4 changes: 3 additions & 1 deletion app/src/mouse/key_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ static void mouse_tick_timer_handler(struct k_work *work) {
zmk_hid_mouse_movement_set(0, 0);
zmk_hid_mouse_scroll_set(0, 0);
LOG_DBG("Raising mouse tick event");
ZMK_EVENT_RAISE(zmk_mouse_tick(move_speed, scroll_speed, move_config, scroll_config, &start_time));
ZMK_EVENT_RAISE(
zmk_mouse_tick(move_speed, scroll_speed, move_config, scroll_config, &start_time));
zmk_endpoints_send_mouse_report();
}

K_WORK_DEFINE(mouse_tick, &mouse_tick_timer_handler);

void mouse_timer_cb(struct k_timer *dummy) {
LOG_DBG("Submitting mouse work to queue");
k_work_submit_to_queue(zmk_mouse_work_q(), &mouse_tick);
}

Expand Down
13 changes: 6 additions & 7 deletions app/src/mouse/tick_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ static int64_t ms_since_start(int64_t start, int64_t now, int64_t delay) {
static float speed(const struct mouse_config *config, float max_speed, int64_t duration_ms) {
// Calculate the speed based on MouseKeysAccel
// See https://en.wikipedia.org/wiki/Mouse_keys
if (duration_ms > config->time_to_max_speed_ms
|| config->time_to_max_speed_ms == 0
|| config->acceleration_exponent == 0) {
if (duration_ms > config->time_to_max_speed_ms || config->time_to_max_speed_ms == 0 ||
config->acceleration_exponent == 0) {
return max_speed;
}
float time_fraction = (float)duration_ms / config->time_to_max_speed_ms;
Expand Down Expand Up @@ -80,12 +79,12 @@ static struct vector2d update_movement(struct vector2d *remainder,
}

static void mouse_tick_handler(const struct zmk_mouse_tick *tick) {
struct vector2d move =
update_movement(&move_remainder, &(tick->move_config), tick->max_move, tick->timestamp, tick->start_time);
struct vector2d move = update_movement(&move_remainder, &(tick->move_config), tick->max_move,
tick->timestamp, tick->start_time);
zmk_hid_mouse_movement_update((int16_t)CLAMP(move.x, INT16_MIN, INT16_MAX),
(int16_t)CLAMP(move.y, INT16_MIN, INT16_MAX));
struct vector2d scroll =
update_movement(&scroll_remainder, &(tick->scroll_config), tick->max_scroll, tick->timestamp, tick->start_time);
struct vector2d scroll = update_movement(&scroll_remainder, &(tick->scroll_config),
tick->max_scroll, tick->timestamp, tick->start_time);
zmk_hid_mouse_scroll_update((int8_t)CLAMP(scroll.x, INT8_MIN, INT8_MAX),
(int8_t)CLAMP(scroll.y, INT8_MIN, INT8_MAX));
}
Expand Down

0 comments on commit 8088585

Please sign in to comment.