Continuous mouse movement prototype
This commit is contained in:
parent
400e24608b
commit
3eebe8657b
3 changed files with 31 additions and 11 deletions
|
@ -10,6 +10,7 @@
|
|||
#include <zephyr.h>
|
||||
#include <zmk/event_manager.h>
|
||||
|
||||
|
||||
struct zmk_mouse_state_changed {
|
||||
uint32_t x;
|
||||
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,
|
||||
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});
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
@ -85,14 +86,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();
|
||||
}
|
||||
|
||||
|
@ -104,6 +122,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();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue