Skip to content

Commit

Permalink
Continuous mouse movement prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Tsykunov authored and krikun98 committed Jan 28, 2022
1 parent dc2e30d commit ee51487
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
7 changes: 3 additions & 4 deletions app/include/zmk/events/mouse_state_changed.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <zephyr.h>
#include <zmk/event_manager.h>


struct zmk_mouse_state_changed {
uint32_t x;
uint32_t y;
Expand All @@ -23,10 +24,8 @@ static inline struct zmk_mouse_state_changed_event *
zmk_mouse_state_changed_from_encoded(uint32_t encoded, bool pressed,
int64_t timestamp) {

uint32_t x = (binding->param1 & 0xFFFF0000) >> 16;
uint32_t y = binding->param1 & 0x0000FFFF;
LOG_DBG("x: 0x%02X y: 0x%02X", x, y);
zmk_hid_mouse_movement_release(x, y);
uint32_t x = (encoded & 0xFFFF0000) >> 16;
uint32_t y = encoded & 0x0000FFFF;

return new_zmk_mouse_state_changed((struct zmk_mouse_state_changed){
.x = x, .y = y, .state = pressed, .timestamp = timestamp});
Expand Down
13 changes: 7 additions & 6 deletions app/src/behaviors/behavior_mouse_move.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,30 @@

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

ZMK_EVENT_DECLARE(zmk_mouse_move);

static int behavior_mouse_move_init(const struct device *dev) { return 0; };


static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);
return ZMK_EVENT_RAISE(zmk_mouse_state_changed_from_encoded(binding->param1, true,
event.timestamp);
int res = ZMK_EVENT_RAISE(zmk_mouse_state_changed_from_encoded(binding->param1, true,
event.timestamp));
return res;
}

static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);

return ZMK_EVENT_RAISE(zmk_mouse_state_changed_from_encoded(binding->param1, false,
event.timestamp);
event.timestamp));
}

static const struct behavior_driver_api behavior_mouse_move_driver_api = {
.binding_pressed = on_keymap_binding_pressed, .binding_released = on_keymap_binding_released};

#define KP_INST(n) \
DEVICE_AND_API_INIT(behavior_mouse_move_##n, DT_INST_LABEL(n), behavior_mouse_move_init, NULL, \
DEVICE_AND_API_INIT(behavior_mouse_move_##n, DT_INST_LABEL(n), behavior_mouse_move_init, NULL, \
NULL, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \
&behavior_mouse_move_driver_api);

Expand Down
22 changes: 21 additions & 1 deletion app/src/hid_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#include <zmk/event_manager.h>
#include <zmk/events/keycode_state_changed.h>
#include <zmk/events/mouse_state_changed.h>
#include <zmk/events/modifiers_state_changed.h>
#include <zmk/hid.h>
#include <dt-bindings/zmk/hid_usage_pages.h>
Expand Down Expand Up @@ -106,14 +107,31 @@ static int hid_listener_keycode_released(const struct zmk_keycode_state_changed
return zmk_endpoints_send_report(ev->usage_page);
}

static int mouse_is_moving_counter = 0;

void mouse_timer_cb(struct k_timer *dummy);

K_TIMER_DEFINE(mouse_timer, mouse_timer_cb, NULL);

void mouse_timer_cb(struct k_timer *dummy)
{
if (mouse_is_moving_counter != 0) {
zmk_endpoints_send_mouse_report();
k_timer_start(&mouse_timer, K_MSEC(10), K_NO_WAIT);
}
}

static int hid_listener_mouse_pressed(const struct zmk_mouse_state_changed *ev) {
int err;
LOG_DBG("x: 0x%02X, y: 0x%02X", ev->x, ev->y);
err = zmk_hid_mouse_movement_press(ev->x, ev->y);
if (err) {
LOG_ERR("Unable press button");
LOG_ERR("Unable to press button");
return err;
}
// race condition?
mouse_is_moving_counter += 1;
k_timer_start(&mouse_timer, K_MSEC(10), K_NO_WAIT);
return zmk_endpoints_send_mouse_report();
}

Expand All @@ -125,6 +143,8 @@ static int hid_listener_mouse_released(const struct zmk_mouse_state_changed *ev)
LOG_ERR("Unable to release button");
return err;
}
// race condition?
mouse_is_moving_counter -= 1;
return zmk_endpoints_send_mouse_report();
}

Expand Down

0 comments on commit ee51487

Please sign in to comment.