Continuous mouse movement prototype

This commit is contained in:
Dmitry Tsykunov 2021-04-29 18:11:28 +03:00 committed by Alexander Krikun
parent dc2e30d5db
commit ee51487a76
3 changed files with 31 additions and 11 deletions

View file

@ -10,6 +10,7 @@
#include <zephyr.h> #include <zephyr.h>
#include <zmk/event_manager.h> #include <zmk/event_manager.h>
struct zmk_mouse_state_changed { struct zmk_mouse_state_changed {
uint32_t x; uint32_t x;
uint32_t y; uint32_t y;
@ -23,10 +24,8 @@ static inline struct zmk_mouse_state_changed_event *
zmk_mouse_state_changed_from_encoded(uint32_t encoded, bool pressed, zmk_mouse_state_changed_from_encoded(uint32_t encoded, bool pressed,
int64_t timestamp) { int64_t timestamp) {
uint32_t x = (binding->param1 & 0xFFFF0000) >> 16; uint32_t x = (encoded & 0xFFFF0000) >> 16;
uint32_t y = binding->param1 & 0x0000FFFF; uint32_t y = encoded & 0x0000FFFF;
LOG_DBG("x: 0x%02X y: 0x%02X", x, y);
zmk_hid_mouse_movement_release(x, y);
return new_zmk_mouse_state_changed((struct zmk_mouse_state_changed){ return new_zmk_mouse_state_changed((struct zmk_mouse_state_changed){
.x = x, .y = y, .state = pressed, .timestamp = timestamp}); .x = x, .y = y, .state = pressed, .timestamp = timestamp});

View file

@ -17,29 +17,30 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); 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 behavior_mouse_move_init(const struct device *dev) { return 0; };
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) { struct zmk_behavior_binding_event event) {
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1); LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);
return ZMK_EVENT_RAISE(zmk_mouse_state_changed_from_encoded(binding->param1, true, int res = ZMK_EVENT_RAISE(zmk_mouse_state_changed_from_encoded(binding->param1, true,
event.timestamp); event.timestamp));
return res;
} }
static int on_keymap_binding_released(struct zmk_behavior_binding *binding, static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) { struct zmk_behavior_binding_event event) {
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1); LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);
return ZMK_EVENT_RAISE(zmk_mouse_state_changed_from_encoded(binding->param1, false, 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 = { static const struct behavior_driver_api behavior_mouse_move_driver_api = {
.binding_pressed = on_keymap_binding_pressed, .binding_released = on_keymap_binding_released}; .binding_pressed = on_keymap_binding_pressed, .binding_released = on_keymap_binding_released};
#define KP_INST(n) \ #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, \ NULL, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \
&behavior_mouse_move_driver_api); &behavior_mouse_move_driver_api);

View file

@ -11,6 +11,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/event_manager.h> #include <zmk/event_manager.h>
#include <zmk/events/keycode_state_changed.h> #include <zmk/events/keycode_state_changed.h>
#include <zmk/events/mouse_state_changed.h>
#include <zmk/events/modifiers_state_changed.h> #include <zmk/events/modifiers_state_changed.h>
#include <zmk/hid.h> #include <zmk/hid.h>
#include <dt-bindings/zmk/hid_usage_pages.h> #include <dt-bindings/zmk/hid_usage_pages.h>
@ -106,14 +107,31 @@ static int hid_listener_keycode_released(const struct zmk_keycode_state_changed
return zmk_endpoints_send_report(ev->usage_page); 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) { static int hid_listener_mouse_pressed(const struct zmk_mouse_state_changed *ev) {
int err; int err;
LOG_DBG("x: 0x%02X, y: 0x%02X", ev->x, ev->y); LOG_DBG("x: 0x%02X, y: 0x%02X", ev->x, ev->y);
err = zmk_hid_mouse_movement_press(ev->x, ev->y); err = zmk_hid_mouse_movement_press(ev->x, ev->y);
if (err) { if (err) {
LOG_ERR("Unable press button"); LOG_ERR("Unable to press button");
return err; 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(); return zmk_endpoints_send_mouse_report();
} }
@ -125,6 +143,8 @@ static int hid_listener_mouse_released(const struct zmk_mouse_state_changed *ev)
LOG_ERR("Unable to release button"); LOG_ERR("Unable to release button");
return err; return err;
} }
// race condition?
mouse_is_moving_counter -= 1;
return zmk_endpoints_send_mouse_report(); return zmk_endpoints_send_mouse_report();
} }