Skip to content

Commit

Permalink
feat(mouse): Add input configs for data mods.
Browse files Browse the repository at this point in the history
* Add ability to swap X/Y, invert X and Y values, and apply a
  scalar multiplier/divisor.
  • Loading branch information
petejohanson committed Dec 4, 2023
1 parent a56c849 commit 505343b
Show file tree
Hide file tree
Showing 17 changed files with 325 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ target_sources(app PRIVATE src/activity.c)
target_sources(app PRIVATE src/kscan.c)
target_sources(app PRIVATE src/matrix_transform.c)
target_sources_ifdef(CONFIG_ZMK_MOUSE app PRIVATE src/mouse/main.c)
target_sources_ifdef(CONFIG_ZMK_MOUSE app PRIVATE src/mouse/input_config.c)
target_sources(app PRIVATE src/sensors.c)
target_sources_ifdef(CONFIG_ZMK_WPM app PRIVATE src/wpm.c)
target_sources(app PRIVATE src/event_manager.c)
Expand Down
24 changes: 24 additions & 0 deletions app/dts/bindings/zmk,input-configs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
description: |
Allows post-processing of input events based on the configuration
compatible: "zmk,input-configs"

child-binding:
description: "A configuration for a given input device"

properties:
device:
type: phandle
required: true
xy-swap:
type: boolean
x-invert:
type: boolean
y-invert:
type: boolean
scale-multiplier:
type: int
default: 1
scale-divisor:
type: int
default: 1
21 changes: 21 additions & 0 deletions app/include/zmk/mouse/input_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2023 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <zephyr/kernel.h>
#include <zephyr/device.h>

struct zmk_input_config {
const struct device *dev;
bool xy_swap;
bool x_invert;
bool y_invert;
uint16_t scale_multiplier;
uint16_t scale_divisor;
};

const struct zmk_input_config *zmk_input_config_get_for_device(const struct device *dev);
16 changes: 9 additions & 7 deletions app/src/behaviors/behavior_input_two_axis.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,17 @@ static void tick_work_cb(struct k_work *work) {
}
}

static void set_start_times_for_activity(struct movement_state_2d *state) {
if (state->x.speed != 0 && state->x.start_time == 0) {
state->x.start_time = k_uptime_get();
}

if (state->y.speed != 0 && state->y.start_time == 0) {
state->y.start_time = k_uptime_get();
static void set_start_times_for_activity_1d(struct movement_state_1d *state) {
if (state->speed != 0 && state->start_time == 0) {
state->start_time = k_uptime_get();
} else if (state->speed == 0) {
state->start_time = 0;
}
}
static void set_start_times_for_activity(struct movement_state_2d *state) {
set_start_times_for_activity_1d(&state->x);
set_start_times_for_activity_1d(&state->y);
}

static void update_work_scheduling(const struct device *dev) {
struct behavior_input_two_axis_data *data = dev->data;
Expand Down
38 changes: 38 additions & 0 deletions app/src/mouse/hid_input_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <zephyr/dt-bindings/input/input-event-codes.h>

#include <zmk/mouse.h>
#include <zmk/mouse/input_config.h>
#include <zmk/endpoints.h>
#include <zmk/hid.h>

Expand Down Expand Up @@ -52,7 +53,44 @@ void handle_key_code(struct input_event *evt) {
}
}

static void swap_xy(struct input_event *evt) {
switch (evt->code) {
case INPUT_REL_X:
evt->code = INPUT_REL_Y;
break;
case INPUT_REL_Y:
evt->code = INPUT_REL_X;
break;
}
}

static void filter_with_input_config(struct input_event *evt) {
if (!evt->dev) {
return;
}

const struct zmk_input_config *cfg = zmk_input_config_get_for_device(evt->dev);

if (!cfg) {
return;
}

if (cfg->xy_swap) {
swap_xy(evt);
}

if ((cfg->x_invert && evt->code == INPUT_REL_X) ||
(cfg->y_invert && evt->code == INPUT_REL_Y)) {
evt->value = -(evt->value);
}

evt->value = (int16_t)((evt->value * cfg->scale_multiplier) / cfg->scale_divisor);
}

void input_handler(struct input_event *evt) {
// First, filter to update the event data as needed.
filter_with_input_config(evt);

switch (evt->type) {
case INPUT_EV_REL:
handle_rel_code(evt);
Expand Down
42 changes: 42 additions & 0 deletions app/src/mouse/input_config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2023 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#include <zmk/mouse/input_config.h>
#include <zephyr/device.h>

#define DT_DRV_COMPAT zmk_input_configs

#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)

#define CHILD_CONFIG(inst) \
{ \
.dev = DEVICE_DT_GET(DT_PHANDLE(inst, device)), \
.xy_swap = DT_PROP(inst, xy_swap), \
.x_invert = DT_PROP(inst, x_invert), \
.y_invert = DT_PROP(inst, y_invert), \
.scale_multiplier = DT_PROP(inst, scale_multiplier), \
.scale_divisor = DT_PROP(inst, scale_divisor), \
},

const struct zmk_input_config configs[] = {DT_INST_FOREACH_CHILD(0, CHILD_CONFIG)};

const struct zmk_input_config *zmk_input_config_get_for_device(const struct device *dev) {
for (int i = 0; i < ARRAY_SIZE(configs); i++) {
if (configs[i].dev == dev) {
return &configs[i];
}
}

return NULL;
}

#else

const struct zmk_input_config *zmk_input_config_get_for_device(const struct device *dev) {
return NULL;
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s/.*hid_mouse_//p
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
movement_update: Mouse movement updated to -1/0
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to -3/0
movement_update: Mouse movement updated to -3/-3
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to -3/0
movement_update: Mouse movement updated to -3/-3
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to -5/0
movement_update: Mouse movement updated to -5/-3
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to -5/0
movement_update: Mouse movement updated to -5/-5
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 0/-5
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <behaviors.dtsi>
#include <dt-bindings/zmk/keys.h>
#include <dt-bindings/zmk/kscan_mock.h>
#include <dt-bindings/zmk/mouse.h>

/ {
keymap {
compatible = "zmk,keymap";
label ="Default keymap";

default_layer {
bindings = <
&mmv MOVE_LEFT &mmv MOVE_UP
&none &none
>;
};
};

input_configs {
compatible = "zmk,input-configs";

mmv {
device = <&mmv>;
scale-multiplier = <5>;
scale-divisor = <3>;
};
};
};


&kscan {
events = <
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_PRESS(0,1,100)
ZMK_MOCK_RELEASE(0,0,10)
ZMK_MOCK_RELEASE(0,1,10)
>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s/.*hid_mouse_//p
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
movement_update: Mouse movement updated to 1/0
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 2/0
movement_update: Mouse movement updated to 2/2
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 2/0
movement_update: Mouse movement updated to 2/2
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 3/0
movement_update: Mouse movement updated to 3/2
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 3/0
movement_update: Mouse movement updated to 3/3
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 0/3
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <behaviors.dtsi>
#include <dt-bindings/zmk/keys.h>
#include <dt-bindings/zmk/kscan_mock.h>
#include <dt-bindings/zmk/mouse.h>

/ {
keymap {
compatible = "zmk,keymap";
label ="Default keymap";

default_layer {
bindings = <
&mmv MOVE_LEFT &mmv MOVE_UP
&none &none
>;
};
};

input_configs {
compatible = "zmk,input-configs";

mmv {
device = <&mmv>;
x-invert;
y-invert;
};
};
};


&kscan {
events = <
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_PRESS(0,1,100)
ZMK_MOCK_RELEASE(0,0,10)
ZMK_MOCK_RELEASE(0,1,10)
>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s/.*hid_mouse_//p
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
movement_update: Mouse movement updated to 0/-1
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 0/-2
movement_update: Mouse movement updated to -2/-2
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 0/-2
movement_update: Mouse movement updated to -2/-2
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 0/-3
movement_update: Mouse movement updated to -2/-3
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 0/-3
movement_update: Mouse movement updated to -3/-3
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to -3/0
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <behaviors.dtsi>
#include <dt-bindings/zmk/keys.h>
#include <dt-bindings/zmk/kscan_mock.h>
#include <dt-bindings/zmk/mouse.h>

/ {
keymap {
compatible = "zmk,keymap";
label ="Default keymap";

default_layer {
bindings = <
&mmv MOVE_LEFT &mmv MOVE_UP
&none &none
>;
};
};

input_configs {
compatible = "zmk,input-configs";

mmv {
device = <&mmv>;
xy-swap;
};
};
};


&kscan {
events = <
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_PRESS(0,1,100)
ZMK_MOCK_RELEASE(0,0,10)
ZMK_MOCK_RELEASE(0,1,10)
>;
};
11 changes: 4 additions & 7 deletions app/tests/mouse-keys/mouse-move/move_x/keycode_events.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to -3/0
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 3/0
movement_update: Mouse movement updated to 1/0
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 5/0
movement_update: Mouse movement updated to 2/0
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 5/0
movement_update: Mouse movement updated to 2/0
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 6/0
movement_update: Mouse movement updated to 3/0
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
movement_update: Mouse movement updated to 6/0
scroll_set: Mouse scroll set to 0/0
movement_set: Mouse movement set to 0/0
Loading

0 comments on commit 505343b

Please sign in to comment.