Skip to content

Commit

Permalink
dynamic: Refactored for recent sensor changes in ZMK
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Oct 11, 2023
1 parent 0bc0f2a commit a27909c
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 93 deletions.
2 changes: 1 addition & 1 deletion config/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
45 changes: 45 additions & 0 deletions config/app/behaviors/behavior_mouse_wheel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2022-2023 XiNGRZ
* SPDX-License-Identifier: MIT
*/

#define DT_DRV_COMPAT zmk_behavior_mouse_wheel

#include <zephyr/device.h>
#include <drivers/behavior.h>

#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#include <zmk/behavior.h>
#include <app/hid_mouse.h>

#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) */
62 changes: 0 additions & 62 deletions config/app/behaviors/behavior_sensor_rotate_mouse_wheel.c

This file was deleted.

9 changes: 4 additions & 5 deletions config/app/hid_mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion config/app/include/app/hid_mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@

#pragma once

int hid_mouse_wheel_report(int direction);
#include <stdbool.h>

int hid_mouse_wheel_report(int direction, bool pressed);
4 changes: 2 additions & 2 deletions config/drivers/sensor/knob/knob.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 14 additions & 0 deletions config/dts/behaviors/mouse_wheel.dtsi
Original file line number Diff line number Diff line change
@@ -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>;
};
};
};
7 changes: 5 additions & 2 deletions config/dts/behaviors/sensor_rotate_mouse_wheel.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
};
};
};
8 changes: 8 additions & 0 deletions config/dts/bindings/behaviors/zmk,behavior-mouse-wheel.yaml
Original file line number Diff line number Diff line change
@@ -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

This file was deleted.

2 changes: 1 addition & 1 deletion config/hw75_dynamic.keymap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a27909c

Please sign in to comment.