Browse Source

Initial behavior for key presses on encoder rotate

xmkb
Pete Johanson 4 years ago
parent
commit
34ff6190bd
  1. 5
      app/CMakeLists.txt
  2. 16
      app/boards/shields/kyria/keymap/keymap.overlay
  3. 3
      app/dts/behaviors.dtsi
  4. 19
      app/dts/behaviors/sensor_rotate_key_press.dtsi
  5. 22
      app/dts/bindings/behaviors/zmk,behavior-sensor-rotate-key-press.yaml
  6. 2
      app/include/drivers/behavior.h
  7. 8
      app/include/dt-bindings/zmk/keys.h
  8. 1
      app/include/zmk/sensors.h
  9. 91
      app/src/behaviors/behavior_sensor_rotate_key_press.c
  10. 12
      app/src/keymap.c
  11. 6
      app/src/sensors.c

5
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/keymap.c)
target_sources(app PRIVATE src/hid_listener.c) target_sources(app PRIVATE src/hid_listener.c)
target_sources(app PRIVATE src/hid.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/event_manager.c)
target_sources(app PRIVATE src/events/position_state_changed.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/keycode_state_changed.c)
target_sources(app PRIVATE src/events/modifiers_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_key_press.c)
target_sources(app PRIVATE src/behaviors/behavior_reset.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_mod_tap.c)
target_sources(app PRIVATE src/behaviors/behavior_momentary_layer.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_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_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_listener.c)
target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split/bluetooth/service.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split/bluetooth/service.c)

16
app/boards/shields/kyria/keymap/keymap.overlay

@ -19,16 +19,18 @@
default: layer_0 { default: layer_0 {
label = "DEFAULT"; label = "DEFAULT";
// --------------------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------------------
// | ESC | Q | W | E | R | T | | Y | U | I | O | P | "|" | // | ESC | Q | W | E | R | T | | Y | U | I | O | P | \ |
// | <- | A | S | D | F | G | | H | J | K | L | ; | ' | // | 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 | , | . | / | - | // | 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 | // | GUI | DEL | RET | SPACE | ESC | | RET | SPACE | TAB | BSPC | R-ALT |
bindings = < 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 ESC &kp Q &kp W &kp E &kp R &kp T &kp Y &kp U &kp I &kp O &kp P &kp BSLH
&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 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 BSLH &kp MINUS &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 &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>;
}; };
}; };
}; };

3
app/dts/behaviors.dtsi

@ -2,4 +2,5 @@
#include <behaviors/transparent.dtsi> #include <behaviors/transparent.dtsi>
#include <behaviors/mod_tap.dtsi> #include <behaviors/mod_tap.dtsi>
#include <behaviors/momentary_layer.dtsi> #include <behaviors/momentary_layer.dtsi>
#include <behaviors/reset.dtsi> #include <behaviors/reset.dtsi>
#include <behaviors/sensor_rotate_key_press.dtsi>

19
app/dts/behaviors/sensor_rotate_key_press.dtsi

@ -0,0 +1,19 @@
#include <dt-bindings/zmk/keys.h>
/ {
behaviors {
enckp: behavior_sensor_rotate_key_press {
compatible = "zmk,behavior-sensor-rotate-key-press";
label = "ENC_KEY_PRESS";
usage_page = <USAGE_KEYPAD>;
#sensor-binding-cells = <2>;
};
enccp: behavior_sensor_rotate_consumer_press {
compatible = "zmk,behavior-sensor-rotate-key-press";
label = "ENC_CONSUMER_PRESS";
usage_page = <USAGE_CONSUMER>;
#sensor-binding-cells = <2>;
};
};
};

22
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

2
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); __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 *api =
(const struct behavior_driver_api *)dev->driver_api; (const struct behavior_driver_api *)dev->driver_api;

8
app/include/dt-bindings/zmk/keys.h

@ -42,8 +42,7 @@
#define NUM_0 0x27 #define NUM_0 0x27
#define RET 0x28 #define RET 0x28
#define ESC 0x29 #define ESC 0x29
#define DEL 0x2A #define BKSP 0x2A
#define BKSP DEL
#define TAB 0x2B #define TAB 0x2B
#define SPC 0x2C #define SPC 0x2C
#define MINUS 0x2D #define MINUS 0x2D
@ -62,6 +61,11 @@
#define F1 0x3A #define F1 0x3A
#define F2 0x3B #define F2 0x3B
#define HOME 0x4A
#define PGUP 0x4B
#define DEL 0x4C
#define END 0x4D
#define PGDN 0x4E
#define RARW 0x4F #define RARW 0x4F
#define LARW 0x50 #define LARW 0x50
#define DARW 0x51 #define DARW 0x51

1
app/include/zmk/sensors.h

@ -7,5 +7,6 @@
#pragma once #pragma once
#define ZMK_KEYMAP_SENSORS_NODE DT_INST(0, zmk_keymap_sensors) #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_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) #define ZMK_KEYMAP_SENSORS_BY_IDX(idx) DT_PHANDLE_BY_IDX(ZMK_KEYMAP_SENSORS_NODE, sensors, idx)

91
app/src/behaviors/behavior_sensor_rotate_key_press.c

@ -0,0 +1,91 @@
/*
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
*
* SPDX-License-Identifier: MIT
*/
#define DT_DRV_COMPAT zmk_behavior_sensor_rotate_key_press
#include <device.h>
#include <drivers/behavior.h>
#include <logging/log.h>
#include <drivers/sensor.h>
#include <zmk/event-manager.h>
#include <zmk/events/keycode-state-changed.h>
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)

12
app/src/keymap.c

@ -64,7 +64,7 @@ static struct zmk_behavior_binding zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_
#endif #endif
}; };
#if ZMK_KEYMAP_HAS_SENSORS
#define _TRANSFORM_SENSOR_ENTRY(idx, layer) \ #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)), \ { .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))), \ .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
}; };
#endif /* ZMK_KEYMAP_HAS_SENSORS */
#define SET_LAYER_STATE(layer, state) \ #define SET_LAYER_STATE(layer, state) \
if (layer >= 32) \ if (layer >= 32) \
{ \ { \
@ -167,6 +169,7 @@ int zmk_keymap_position_state_changed(u32_t position, bool pressed)
return -ENOTSUP; return -ENOTSUP;
} }
#if ZMK_KEYMAP_HAS_SENSORS
int zmk_keymap_sensor_triggered(u8_t sensor_number, struct device *sensor) 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--) 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; return -ENOTSUP;
} }
#endif /* ZMK_KEYMAP_HAS_SENSORS */
int keymap_listener(const struct zmk_event_header *eh) int keymap_listener(const struct zmk_event_header *eh)
{ {
if (is_position_state_changed(eh)) { if (is_position_state_changed(eh)) {
const struct position_state_changed *ev = cast_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); return zmk_keymap_position_state_changed(ev->position, ev->state);
#if ZMK_KEYMAP_HAS_SENSORS
} else if (is_sensor_event(eh)) { } else if (is_sensor_event(eh)) {
const struct sensor_event *ev = cast_sensor_event(eh); const struct sensor_event *ev = cast_sensor_event(eh);
return zmk_keymap_sensor_triggered(ev->sensor_number, ev->sensor); return zmk_keymap_sensor_triggered(ev->sensor_number, ev->sensor);
#endif /* ZMK_KEYMAP_HAS_SENSORS */
} }
return -ENOTSUP; return -ENOTSUP;
@ -218,5 +225,8 @@ int keymap_listener(const struct zmk_event_header *eh)
ZMK_LISTENER(keymap, keymap_listener); ZMK_LISTENER(keymap, keymap_listener);
ZMK_SUBSCRIPTION(keymap, position_state_changed); ZMK_SUBSCRIPTION(keymap, position_state_changed);
#if ZMK_KEYMAP_HAS_SENSORS
ZMK_SUBSCRIPTION(keymap, sensor_event); ZMK_SUBSCRIPTION(keymap, sensor_event);
#endif /* ZMK_KEYMAP_HAS_SENSORS */

6
app/src/sensors.c

@ -16,6 +16,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/event-manager.h> #include <zmk/event-manager.h>
#include <zmk/events/sensor-event.h> #include <zmk/events/sensor-event.h>
#if ZMK_KEYMAP_HAS_SENSORS
struct sensors_data_item { struct sensors_data_item {
u8_t sensor_number; u8_t sensor_number;
struct device *dev; struct device *dev;
@ -78,4 +80,6 @@ static int zmk_sensors_init(struct device *_arg)
SYS_INIT(zmk_sensors_init, SYS_INIT(zmk_sensors_init,
APPLICATION, APPLICATION,
CONFIG_APPLICATION_INIT_PRIORITY); CONFIG_APPLICATION_INIT_PRIORITY);
#endif /* ZMK_KEYMAP_HAS_SENSORS */
Loading…
Cancel
Save