From 34ff6190bd6177705bed075c5d52f54e87930591 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Wed, 22 Jul 2020 11:08:52 -0400 Subject: [PATCH] Initial behavior for key presses on encoder rotate --- app/CMakeLists.txt | 5 +- .../shields/kyria/keymap/keymap.overlay | 16 ++-- app/dts/behaviors.dtsi | 3 +- .../behaviors/sensor_rotate_key_press.dtsi | 19 ++++ .../zmk,behavior-sensor-rotate-key-press.yaml | 22 +++++ app/include/drivers/behavior.h | 2 +- app/include/dt-bindings/zmk/keys.h | 8 +- app/include/zmk/sensors.h | 1 + .../behavior_sensor_rotate_key_press.c | 91 +++++++++++++++++++ app/src/keymap.c | 12 ++- app/src/sensors.c | 6 +- 11 files changed, 170 insertions(+), 15 deletions(-) create mode 100644 app/dts/behaviors/sensor_rotate_key_press.dtsi create mode 100644 app/dts/bindings/behaviors/zmk,behavior-sensor-rotate-key-press.yaml create mode 100644 app/src/behaviors/behavior_sensor_rotate_key_press.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index c8356ce0..45c68856 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -32,17 +32,18 @@ target_sources(app PRIVATE src/matrix_transform.c) target_sources(app PRIVATE src/keymap.c) target_sources(app PRIVATE src/hid_listener.c) target_sources(app PRIVATE src/hid.c) -target_sources_ifdef(CONFIG_SENSOR app PRIVATE src/sensors.c) +target_sources(app PRIVATE src/sensors.c) target_sources(app PRIVATE src/event_manager.c) target_sources(app PRIVATE src/events/position_state_changed.c) target_sources(app PRIVATE src/events/keycode_state_changed.c) target_sources(app PRIVATE src/events/modifiers_state_changed.c) -target_sources_ifdef(CONFIG_SENSOR app PRIVATE src/events/sensor_event.c) +target_sources(app PRIVATE src/events/sensor_event.c) target_sources(app PRIVATE src/behaviors/behavior_key_press.c) target_sources(app PRIVATE src/behaviors/behavior_reset.c) target_sources(app PRIVATE src/behaviors/behavior_mod_tap.c) target_sources(app PRIVATE src/behaviors/behavior_momentary_layer.c) target_sources(app PRIVATE src/behaviors/behavior_transparent.c) +target_sources(app PRIVATE src/behaviors/behavior_sensor_rotate_key_press.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split_listener.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split/bluetooth/service.c) diff --git a/app/boards/shields/kyria/keymap/keymap.overlay b/app/boards/shields/kyria/keymap/keymap.overlay index 0c00aef8..398882dc 100644 --- a/app/boards/shields/kyria/keymap/keymap.overlay +++ b/app/boards/shields/kyria/keymap/keymap.overlay @@ -19,16 +19,18 @@ default: layer_0 { label = "DEFAULT"; // --------------------------------------------------------------------------------------------------------------------------------- -// | ESC | Q | W | E | R | T | | Y | U | I | O | P | "|" | -// | <- | A | S | D | F | G | | H | J | K | L | ; | ' | -// | SHIFT | Z | X | C | V | B | L SHIFT | L SHIFT | | L SHIFT | L SHIFT | N | M | , | . | / | - | +// | ESC | Q | W | E | R | T | | Y | U | I | O | P | \ | +// | TAB | A | S | D | F | G | | H | J | K | L | ; | ' | +// | SHIFT | Z | X | C | V | B | L SHIFT | L SHIFT | | L SHIFT | L SHIFT | N | M | , | . | / | CTRL | // | GUI | DEL | RET | SPACE | ESC | | RET | SPACE | TAB | BSPC | R-ALT | bindings = < - &kp ESC &kp Q &kp W &kp E &kp R &kp T &kp Y &kp U &kp I &kp O &kp P &kp PIPE - &kp BKSP &kp A &kp S &kp D &kp F &kp G &kp H &kp J &kp K &kp L &kp SCLN &kp QUOT - &kp LSFT &kp Z &kp X &kp C &kp V &kp B &kp LSFT &kp LSFT &kp LSFT &kp LSFT &kp N &kp M &kp CMMA &kp DOT &kp BSLH &kp MINUS - &kp LGUI &kp DEL &kp RET &kp SPC &kp ESC &kp RET &kp SPC &kp TAB &kp BKSP &kp RALT + &kp ESC &kp Q &kp W &kp E &kp R &kp T &kp Y &kp U &kp I &kp O &kp P &kp BSLH + &kp TAB &kp A &kp S &kp D &kp F &kp G &kp H &kp J &kp K &kp L &kp SCLN &kp QUOT + &kp LSFT &kp Z &kp X &kp C &kp V &kp B &kp LSFT &kp LSFT &kp LSFT &kp LSFT &kp N &kp M &kp CMMA &kp DOT &kp FSLH &kp RCTL + &kp LGUI &kp DEL &kp RET &kp SPC &kp ESC &kp RET &kp SPC &kp TAB &kp BKSP &kp RALT >; + + sensor-bindings = <&enccp M_VOLU M_VOLD &enckp PGUP PGDN>; }; }; }; diff --git a/app/dts/behaviors.dtsi b/app/dts/behaviors.dtsi index 86cd7b72..39784799 100644 --- a/app/dts/behaviors.dtsi +++ b/app/dts/behaviors.dtsi @@ -2,4 +2,5 @@ #include #include #include -#include \ No newline at end of file +#include +#include \ No newline at end of file diff --git a/app/dts/behaviors/sensor_rotate_key_press.dtsi b/app/dts/behaviors/sensor_rotate_key_press.dtsi new file mode 100644 index 00000000..ef52fbab --- /dev/null +++ b/app/dts/behaviors/sensor_rotate_key_press.dtsi @@ -0,0 +1,19 @@ +#include + +/ { + behaviors { + enckp: behavior_sensor_rotate_key_press { + compatible = "zmk,behavior-sensor-rotate-key-press"; + label = "ENC_KEY_PRESS"; + usage_page = ; + #sensor-binding-cells = <2>; + }; + + enccp: behavior_sensor_rotate_consumer_press { + compatible = "zmk,behavior-sensor-rotate-key-press"; + label = "ENC_CONSUMER_PRESS"; + usage_page = ; + #sensor-binding-cells = <2>; + }; + }; +}; diff --git a/app/dts/bindings/behaviors/zmk,behavior-sensor-rotate-key-press.yaml b/app/dts/bindings/behaviors/zmk,behavior-sensor-rotate-key-press.yaml new file mode 100644 index 00000000..bbf35373 --- /dev/null +++ b/app/dts/bindings/behaviors/zmk,behavior-sensor-rotate-key-press.yaml @@ -0,0 +1,22 @@ +# Copyright (c) 2020, Pete Johanson +# SPDX-License-Identifier: MIT + +description: Sensor rotate key press/release behavior + +compatible: "zmk,behavior-sensor-rotate-key-press" + +properties: + label: + type: string + required: true + "#sensor-binding-cells": + type: int + required: true + const: 2 + usage_page: + type: int + default: 0 + +sensor-binding-cells: + - param1 + - param2 diff --git a/app/include/drivers/behavior.h b/app/include/drivers/behavior.h index 106f6793..c24d11b3 100644 --- a/app/include/drivers/behavior.h +++ b/app/include/drivers/behavior.h @@ -89,7 +89,7 @@ static inline int z_impl_behavior_keymap_binding_released(struct device *dev, u3 */ __syscall int behavior_sensor_keymap_binding_triggered(struct device *dev, struct device *sensor, u32_t param1, u32_t param2); -static inline int behavior_sensor_keymap_binding_triggered(struct device *dev, struct device *sensor, u32_t param1, u32_t param2) +static inline int z_impl_behavior_sensor_keymap_binding_triggered(struct device *dev, struct device *sensor, u32_t param1, u32_t param2) { const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->driver_api; diff --git a/app/include/dt-bindings/zmk/keys.h b/app/include/dt-bindings/zmk/keys.h index 78b81961..f35a00ff 100644 --- a/app/include/dt-bindings/zmk/keys.h +++ b/app/include/dt-bindings/zmk/keys.h @@ -42,8 +42,7 @@ #define NUM_0 0x27 #define RET 0x28 #define ESC 0x29 -#define DEL 0x2A -#define BKSP DEL +#define BKSP 0x2A #define TAB 0x2B #define SPC 0x2C #define MINUS 0x2D @@ -62,6 +61,11 @@ #define F1 0x3A #define F2 0x3B +#define HOME 0x4A +#define PGUP 0x4B +#define DEL 0x4C +#define END 0x4D +#define PGDN 0x4E #define RARW 0x4F #define LARW 0x50 #define DARW 0x51 diff --git a/app/include/zmk/sensors.h b/app/include/zmk/sensors.h index adb8ac5d..c60cd4be 100644 --- a/app/include/zmk/sensors.h +++ b/app/include/zmk/sensors.h @@ -7,5 +7,6 @@ #pragma once #define ZMK_KEYMAP_SENSORS_NODE DT_INST(0, zmk_keymap_sensors) +#define ZMK_KEYMAP_HAS_SENSORS DT_NODE_EXISTS(ZMK_KEYMAP_SENSORS_NODE) #define ZMK_KEYMAP_SENSORS_LEN DT_PROP_LEN(ZMK_KEYMAP_SENSORS_NODE, sensors) #define ZMK_KEYMAP_SENSORS_BY_IDX(idx) DT_PHANDLE_BY_IDX(ZMK_KEYMAP_SENSORS_NODE, sensors, idx) \ No newline at end of file diff --git a/app/src/behaviors/behavior_sensor_rotate_key_press.c b/app/src/behaviors/behavior_sensor_rotate_key_press.c new file mode 100644 index 00000000..fb0fb852 --- /dev/null +++ b/app/src/behaviors/behavior_sensor_rotate_key_press.c @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_sensor_rotate_key_press + +#include +#include +#include + +#include +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +struct behavior_sensor_rotate_key_press_config { + u8_t usage_page; +}; +struct behavior_sensor_rotate_key_press_data { }; + +static int behavior_sensor_rotate_key_press_init(struct device *dev) +{ + return 0; +}; + + +static int on_sensor_binding_triggered(struct device *dev, struct device *sensor, u32_t increment_keycode, u32_t decrement_keycode) +{ + const struct behavior_sensor_rotate_key_press_config *cfg = dev->config_info; + struct sensor_value value; + int err; + u32_t keycode; + struct keycode_state_changed *ev; + LOG_DBG("usage_page 0x%02X inc keycode 0x%02X dec keycode 0x%02X", cfg->usage_page, increment_keycode, decrement_keycode); + + 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: + keycode = increment_keycode; + break; + case -1: + keycode = decrement_keycode; + break; + default: + return -ENOTSUP; + } + + LOG_DBG("SEND %d", keycode); + + + ev = new_keycode_state_changed(); + ev->usage_page = cfg->usage_page; + ev->keycode = keycode; + ev->state = true; + ZMK_EVENT_RAISE(ev); + + // TODO: Better way to do this? + k_msleep(5); + + ev = new_keycode_state_changed(); + ev->usage_page = cfg->usage_page; + ev->keycode = keycode; + ev->state = false; + return ZMK_EVENT_RAISE(ev); +} + +static const struct behavior_driver_api behavior_sensor_rotate_key_press_driver_api = { + .sensor_binding_triggered = on_sensor_binding_triggered +}; + +#define KP_INST(n) \ + static const struct behavior_sensor_rotate_key_press_config behavior_sensor_rotate_key_press_config_##n = { \ + .usage_page = DT_INST_PROP(n, usage_page) \ + }; \ + static struct behavior_sensor_rotate_key_press_data behavior_sensor_rotate_key_press_data_##n; \ + DEVICE_AND_API_INIT(behavior_sensor_rotate_key_press_##n, DT_INST_LABEL(n), behavior_sensor_rotate_key_press_init, \ + &behavior_sensor_rotate_key_press_data_##n, \ + &behavior_sensor_rotate_key_press_config_##n, \ + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ + &behavior_sensor_rotate_key_press_driver_api); + +DT_INST_FOREACH_STATUS_OKAY(KP_INST) \ No newline at end of file diff --git a/app/src/keymap.c b/app/src/keymap.c index d07f4885..24e249dd 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -64,7 +64,7 @@ static struct zmk_behavior_binding zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_ #endif }; - +#if ZMK_KEYMAP_HAS_SENSORS #define _TRANSFORM_SENSOR_ENTRY(idx, layer) \ { .behavior_dev = DT_LABEL(DT_PHANDLE_BY_IDX(DT_PHANDLE_BY_IDX(ZMK_KEYMAP_NODE, layers, layer), sensor_bindings, idx)), \ .param1 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(LAYER_NODE(layer), sensor_bindings, idx, param1), (0), (DT_PHA_BY_IDX(LAYER_NODE(layer), sensor_bindings, idx, param1))), \ @@ -109,6 +109,8 @@ static struct zmk_behavior_binding zmk_sensor_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_ #endif }; +#endif /* ZMK_KEYMAP_HAS_SENSORS */ + #define SET_LAYER_STATE(layer, state) \ if (layer >= 32) \ { \ @@ -167,6 +169,7 @@ int zmk_keymap_position_state_changed(u32_t position, bool pressed) return -ENOTSUP; } +#if ZMK_KEYMAP_HAS_SENSORS int zmk_keymap_sensor_triggered(u8_t sensor_number, struct device *sensor) { for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= zmk_keymap_layer_default; layer--) @@ -203,14 +206,18 @@ int zmk_keymap_sensor_triggered(u8_t sensor_number, struct device *sensor) return -ENOTSUP; } +#endif /* ZMK_KEYMAP_HAS_SENSORS */ + int keymap_listener(const struct zmk_event_header *eh) { if (is_position_state_changed(eh)) { const struct position_state_changed *ev = cast_position_state_changed(eh); return zmk_keymap_position_state_changed(ev->position, ev->state); +#if ZMK_KEYMAP_HAS_SENSORS } else if (is_sensor_event(eh)) { const struct sensor_event *ev = cast_sensor_event(eh); return zmk_keymap_sensor_triggered(ev->sensor_number, ev->sensor); +#endif /* ZMK_KEYMAP_HAS_SENSORS */ } return -ENOTSUP; @@ -218,5 +225,8 @@ int keymap_listener(const struct zmk_event_header *eh) ZMK_LISTENER(keymap, keymap_listener); ZMK_SUBSCRIPTION(keymap, position_state_changed); + +#if ZMK_KEYMAP_HAS_SENSORS ZMK_SUBSCRIPTION(keymap, sensor_event); +#endif /* ZMK_KEYMAP_HAS_SENSORS */ diff --git a/app/src/sensors.c b/app/src/sensors.c index d1135c09..0a86109c 100644 --- a/app/src/sensors.c +++ b/app/src/sensors.c @@ -16,6 +16,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include +#if ZMK_KEYMAP_HAS_SENSORS + struct sensors_data_item { u8_t sensor_number; struct device *dev; @@ -78,4 +80,6 @@ static int zmk_sensors_init(struct device *_arg) SYS_INIT(zmk_sensors_init, APPLICATION, - CONFIG_APPLICATION_INIT_PRIORITY); \ No newline at end of file + CONFIG_APPLICATION_INIT_PRIORITY); + +#endif /* ZMK_KEYMAP_HAS_SENSORS */ \ No newline at end of file