From a27909cb48b56acc8f7578232b8706d58a7ff95e Mon Sep 17 00:00:00 2001 From: XiNGRZ Date: Wed, 5 Jul 2023 12:28:20 +0800 Subject: [PATCH] dynamic: Refactored for recent sensor changes in ZMK --- config/app/CMakeLists.txt | 2 +- config/app/behaviors/behavior_mouse_wheel.c | 45 ++++++++++++++ .../behavior_sensor_rotate_mouse_wheel.c | 62 ------------------- config/app/hid_mouse.c | 9 ++- config/app/include/app/hid_mouse.h | 4 +- config/drivers/sensor/knob/knob.c | 4 +- config/dts/behaviors/mouse_wheel.dtsi | 14 +++++ .../behaviors/sensor_rotate_mouse_wheel.dtsi | 7 ++- .../behaviors/zmk,behavior-mouse-wheel.yaml | 8 +++ ...mk,behavior-sensor-rotate-mouse-wheel.yaml | 19 ------ config/hw75_dynamic.keymap | 2 +- 11 files changed, 83 insertions(+), 93 deletions(-) create mode 100644 config/app/behaviors/behavior_mouse_wheel.c delete mode 100644 config/app/behaviors/behavior_sensor_rotate_mouse_wheel.c create mode 100644 config/dts/behaviors/mouse_wheel.dtsi create mode 100644 config/dts/bindings/behaviors/zmk,behavior-mouse-wheel.yaml delete mode 100644 config/dts/bindings/behaviors/zmk,behavior-sensor-rotate-mouse-wheel.yaml diff --git a/config/app/CMakeLists.txt b/config/app/CMakeLists.txt index 50444faf..14bfc98e 100644 --- a/config/app/CMakeLists.txt +++ b/config/app/CMakeLists.txt @@ -8,7 +8,7 @@ zephyr_library_sources_ifdef(CONFIG_HW75_HID_MOUSE hid_mouse.c) zephyr_library_sources_ifdef(CONFIG_HW75_INDICATOR indicator.c) zephyr_library_sources_ifdef(CONFIG_LVGL behaviors/behavior_lvgl_key_press.c) -zephyr_library_sources(behaviors/behavior_sensor_rotate_mouse_wheel.c) +zephyr_library_sources(behaviors/behavior_mouse_wheel.c) add_subdirectory_ifdef(CONFIG_HW75_USB_COMM usb_comm) diff --git a/config/app/behaviors/behavior_mouse_wheel.c b/config/app/behaviors/behavior_mouse_wheel.c new file mode 100644 index 00000000..52294bd7 --- /dev/null +++ b/config/app/behaviors/behavior_mouse_wheel.c @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022-2023 XiNGRZ + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_mouse_wheel + +#include +#include + +#include +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include +#include + +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) + +static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) +{ + return hid_mouse_wheel_report(binding->param1, true); +} + +static int on_keymap_binding_released(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) +{ + return hid_mouse_wheel_report(binding->param1, false); +} + +static const struct behavior_driver_api behavior_mouse_wheel_driver_api = { + .binding_pressed = on_keymap_binding_pressed, + .binding_released = on_keymap_binding_released, +}; + +static int behavior_mouse_wheel_init(const struct device *dev) +{ + ARG_UNUSED(dev); + return 0; +}; + +DEVICE_DT_INST_DEFINE(0, behavior_mouse_wheel_init, NULL, NULL, NULL, APPLICATION, + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_mouse_wheel_driver_api); + +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ diff --git a/config/app/behaviors/behavior_sensor_rotate_mouse_wheel.c b/config/app/behaviors/behavior_sensor_rotate_mouse_wheel.c deleted file mode 100644 index 31c77a86..00000000 --- a/config/app/behaviors/behavior_sensor_rotate_mouse_wheel.c +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2022-2023 XiNGRZ - * SPDX-License-Identifier: MIT - */ - -#define DT_DRV_COMPAT zmk_behavior_sensor_rotate_mouse_wheel - -#include -#include - -#include -LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); - -#include -#include - -#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) - -static int on_sensor_binding_triggered(struct zmk_behavior_binding *binding, - const struct device *sensor, - struct zmk_behavior_binding_event event) -{ - struct sensor_value value; - int err; - int direction; - - err = sensor_channel_get(sensor, SENSOR_CHAN_ROTATION, &value); - - if (err) { - LOG_WRN("Failed to ge sensor rotation value: %d", err); - return err; - } - - switch (value.val1) { - case 1: - direction = binding->param1; - break; - case -1: - direction = binding->param2; - break; - default: - return -ENOTSUP; - } - - return hid_mouse_wheel_report(direction); -} - -static const struct behavior_driver_api behavior_sensor_rotate_mouse_wheel_driver_api = { - .sensor_binding_triggered = on_sensor_binding_triggered -}; - -static int behavior_sensor_rotate_mouse_wheel_init(const struct device *dev) -{ - ARG_UNUSED(dev); - return 0; -}; - -DEVICE_DT_INST_DEFINE(0, behavior_sensor_rotate_mouse_wheel_init, NULL, NULL, NULL, APPLICATION, - CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, - &behavior_sensor_rotate_mouse_wheel_driver_api); - -#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ diff --git a/config/app/hid_mouse.c b/config/app/hid_mouse.c index ffe3ba95..0fae9681 100644 --- a/config/app/hid_mouse.c +++ b/config/app/hid_mouse.c @@ -54,13 +54,12 @@ static int hid_mouse_send_report(const uint8_t *report, size_t len) } } -int hid_mouse_wheel_report(int direction) +int hid_mouse_wheel_report(int direction, bool pressed) { - uint8_t wheel[] = { 0x00, 0x00, 0x00, (uint8_t)(direction & 0xFF) }; - hid_mouse_send_report(wheel, sizeof(wheel)); + uint8_t val = pressed ? (uint8_t)(direction & 0xFF) : 0x00; - uint8_t clear[] = { 0x00, 0x00, 0x00, 0x00 }; - hid_mouse_send_report(clear, sizeof(clear)); + uint8_t report[] = { 0x00, 0x00, 0x00, val }; + hid_mouse_send_report(report, sizeof(report)); return 0; } diff --git a/config/app/include/app/hid_mouse.h b/config/app/include/app/hid_mouse.h index 686c1ff1..85d6ccb6 100644 --- a/config/app/include/app/hid_mouse.h +++ b/config/app/include/app/hid_mouse.h @@ -5,4 +5,6 @@ #pragma once -int hid_mouse_wheel_report(int direction); +#include + +int hid_mouse_wheel_report(int direction, bool pressed); diff --git a/config/drivers/sensor/knob/knob.c b/config/drivers/sensor/knob/knob.c index c7b23f01..40b3344d 100644 --- a/config/drivers/sensor/knob/knob.c +++ b/config/drivers/sensor/knob/knob.c @@ -73,8 +73,8 @@ static int knob_channel_get(const struct device *dev, enum sensor_channel chan, } /* Knob is physically mount reversed */ - val->val1 = -data->delta; - val->val2 = 0; + val->val1 = 0; + val->val2 = -data->delta; return 0; } diff --git a/config/dts/behaviors/mouse_wheel.dtsi b/config/dts/behaviors/mouse_wheel.dtsi new file mode 100644 index 00000000..9c729302 --- /dev/null +++ b/config/dts/behaviors/mouse_wheel.dtsi @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2022-2023 XiNGRZ + * SPDX-License-Identifier: MIT + */ + +/ { + behaviors { + /omit-if-no-ref/ mw: behavior_mouse_wheel { + compatible = "zmk,behavior-mouse-wheel"; + label = "MOUSE_WHEEL"; + #binding-cells = <1>; + }; + }; +}; diff --git a/config/dts/behaviors/sensor_rotate_mouse_wheel.dtsi b/config/dts/behaviors/sensor_rotate_mouse_wheel.dtsi index 3c3523cc..0474dc40 100644 --- a/config/dts/behaviors/sensor_rotate_mouse_wheel.dtsi +++ b/config/dts/behaviors/sensor_rotate_mouse_wheel.dtsi @@ -3,12 +3,15 @@ * SPDX-License-Identifier: MIT */ +#include "mouse_wheel.dtsi" + / { behaviors { - /omit-if-no-ref/ mw: behavior_sensor_rotate_mouse_wheel { - compatible = "zmk,behavior-sensor-rotate-mouse-wheel"; + /omit-if-no-ref/ inc_dec_mw: behavior_sensor_rotate_mouse_wheel { + compatible = "zmk,behavior-sensor-rotate-var"; label = "ENC_MOUSE_WHEEL"; #sensor-binding-cells = <2>; + bindings = <&mw>, <&mw>; }; }; }; diff --git a/config/dts/bindings/behaviors/zmk,behavior-mouse-wheel.yaml b/config/dts/bindings/behaviors/zmk,behavior-mouse-wheel.yaml new file mode 100644 index 00000000..e259e5fb --- /dev/null +++ b/config/dts/bindings/behaviors/zmk,behavior-mouse-wheel.yaml @@ -0,0 +1,8 @@ +# Copyright (c) 2022-2023 XiNGRZ +# SPDX-License-Identifier: MIT + +description: Mouse wheel behavior + +compatible: "zmk,behavior-mouse-wheel" + +include: one_param.yaml diff --git a/config/dts/bindings/behaviors/zmk,behavior-sensor-rotate-mouse-wheel.yaml b/config/dts/bindings/behaviors/zmk,behavior-sensor-rotate-mouse-wheel.yaml deleted file mode 100644 index 4c82b623..00000000 --- a/config/dts/bindings/behaviors/zmk,behavior-sensor-rotate-mouse-wheel.yaml +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright (c) 2022-2023 XiNGRZ -# SPDX-License-Identifier: MIT - -description: Sensor rotate mouse wheel behavior - -compatible: "zmk,behavior-sensor-rotate-mouse-wheel" - -properties: - label: - type: string - required: true - "#sensor-binding-cells": - type: int - required: true - const: 2 - -sensor-binding-cells: - - param1 - - param2 diff --git a/config/hw75_dynamic.keymap b/config/hw75_dynamic.keymap index dc46ffb0..e8c90fb1 100644 --- a/config/hw75_dynamic.keymap +++ b/config/hw75_dynamic.keymap @@ -57,7 +57,7 @@ label = "滚动"; icon = [EF 86 8A]; bindings = <&trans &trans>; - sensor-bindings = <&mw MW_UP(1) MW_DN(1)>; + sensor-bindings = <&inc_dec_mw MW_UP(1) MW_DN(1)>; }; tasks {