Send mouse messages from dedicated thread

This commit is contained in:
Alexander Krikun 2021-09-22 00:27:52 +03:00
parent 775117d590
commit c6c4e9b531
5 changed files with 37 additions and 9 deletions

View file

@ -161,7 +161,11 @@ int zmk_endpoints_send_mouse_report() {
#if IS_ENABLED(CONFIG_ZMK_BLE) #if IS_ENABLED(CONFIG_ZMK_BLE)
case ZMK_ENDPOINT_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); int err = zmk_hog_send_mouse_report(&mouse_report->body);
#endif
if (err) { if (err) {
LOG_ERR("FAILED TO SEND OVER HOG: %d", err); LOG_ERR("FAILED TO SEND OVER HOG: %d", err);
} }

View file

@ -327,6 +327,29 @@ int zmk_hog_send_mouse_report(struct zmk_hid_mouse_report_body *report) {
return 0; 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) { 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), k_work_q_start(&hog_work_q, hog_q_stack, K_THREAD_STACK_SIZEOF(hog_q_stack),
CONFIG_ZMK_BLE_THREAD_PRIORITY); CONFIG_ZMK_BLE_THREAD_PRIORITY);

View file

@ -31,7 +31,7 @@ config ZMK_MOUSE_DEDICATED_THREAD_STACK_SIZE
config ZMK_MOUSE_DEDICATED_THREAD_PRIORITY config ZMK_MOUSE_DEDICATED_THREAD_PRIORITY
int "Thread priority for dedicated mouse thread/queue" int "Thread priority for dedicated mouse thread/queue"
default 5 default 3
endif # ZMK_MOUSE_WORK_QUEUE_DEDICATED endif # ZMK_MOUSE_WORK_QUEUE_DEDICATED

View file

@ -49,13 +49,15 @@ static void mouse_tick_timer_handler(struct k_work *work) {
zmk_hid_mouse_movement_set(0, 0); zmk_hid_mouse_movement_set(0, 0);
zmk_hid_mouse_scroll_set(0, 0); zmk_hid_mouse_scroll_set(0, 0);
LOG_DBG("Raising mouse tick event"); 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(); zmk_endpoints_send_mouse_report();
} }
K_WORK_DEFINE(mouse_tick, &mouse_tick_timer_handler); K_WORK_DEFINE(mouse_tick, &mouse_tick_timer_handler);
void mouse_timer_cb(struct k_timer *dummy) { 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); k_work_submit_to_queue(zmk_mouse_work_q(), &mouse_tick);
} }

View file

@ -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) { static float speed(const struct mouse_config *config, float max_speed, int64_t duration_ms) {
// Calculate the speed based on MouseKeysAccel // Calculate the speed based on MouseKeysAccel
// See https://en.wikipedia.org/wiki/Mouse_keys // See https://en.wikipedia.org/wiki/Mouse_keys
if (duration_ms > config->time_to_max_speed_ms if (duration_ms > config->time_to_max_speed_ms || config->time_to_max_speed_ms == 0 ||
|| config->time_to_max_speed_ms == 0 config->acceleration_exponent == 0) {
|| config->acceleration_exponent == 0) {
return max_speed; return max_speed;
} }
float time_fraction = (float)duration_ms / config->time_to_max_speed_ms; float time_fraction = (float)duration_ms / config->time_to_max_speed_ms;
@ -80,12 +79,12 @@ static struct vector2d update_movement(struct vector2d *remainder,
} }
static void mouse_tick_handler(const struct zmk_mouse_tick *tick) { static void mouse_tick_handler(const struct zmk_mouse_tick *tick) {
struct vector2d move = struct vector2d move = update_movement(&move_remainder, &(tick->move_config), tick->max_move,
update_movement(&move_remainder, &(tick->move_config), tick->max_move, tick->timestamp, tick->start_time); tick->timestamp, tick->start_time);
zmk_hid_mouse_movement_update((int16_t)CLAMP(move.x, INT16_MIN, INT16_MAX), zmk_hid_mouse_movement_update((int16_t)CLAMP(move.x, INT16_MIN, INT16_MAX),
(int16_t)CLAMP(move.y, INT16_MIN, INT16_MAX)); (int16_t)CLAMP(move.y, INT16_MIN, INT16_MAX));
struct vector2d scroll = struct vector2d scroll = update_movement(&scroll_remainder, &(tick->scroll_config),
update_movement(&scroll_remainder, &(tick->scroll_config), tick->max_scroll, tick->timestamp, tick->start_time); tick->max_scroll, tick->timestamp, tick->start_time);
zmk_hid_mouse_scroll_update((int8_t)CLAMP(scroll.x, INT8_MIN, INT8_MAX), zmk_hid_mouse_scroll_update((int8_t)CLAMP(scroll.x, INT8_MIN, INT8_MAX),
(int8_t)CLAMP(scroll.y, INT8_MIN, INT8_MAX)); (int8_t)CLAMP(scroll.y, INT8_MIN, INT8_MAX));
} }