feat(behaviors): Add reusable sensor behaviors.

* Add new sensor behaviors that either take full bindings
  add definition, or accept parameters when bound in the
  keymap.
* Remove existing hard-coded key press sensor behavior
  and instead leverage new generic sensor behaviors to
  achieve the same functionality.

Co-authored-by: nick@conway.dev
This commit is contained in:
Nick Conway
2022-05-23 16:33:08 -04:00
committed by Pete Johanson
parent 9a73650041
commit 3db163aa2c
17 changed files with 301 additions and 107 deletions

View File

@@ -0,0 +1,77 @@
---
title: Sensor Rotation
sidebar_label: Sensor Rotation
---
## Summary
The Sensor Rotation behavior triggers a different behavior, depending on whether the sensor is rotated clockwise or counter-clockwise. Two variants of this behavior are available, allowing either fully specifying the
two behaviors and their parameters together, or allowing binding the sensor rotation with different clockwise and counterclockwise parameters in the keymap itself.
## Sensor Rotation
The standard sensor rotation behavior allows fully binding behaviors to be invoked:
- If rotated counter-clockwise, the first bound behavior is triggered.
- If rotated clockwise, the second bound behavior is triggered.
### Configuration
Here is an example that binds the [RGB Underglow Behavior](/docs/behaviors/underglow.md) to change the RGB brightness:
```
/ {
behaviors {
rgb_encoder: rgb_encoder {
compatible = "zmk,behavior-sensor-rotate";
label = "RGB_ENCODER";
#sensor-binding-cells = <0>;
bindings = <&rgb_ug RGB_BRD>, <&rgb_ug RGB_BRI>;
};
};
keymap {
compatible = "zmk,keymap";
base {
...
sensor-bindings = <&rgb_encoder>;
}
};
};
```
## Variable Sensor Rotation
The variable sensor rotation behavior is configured with two behaviors that each expect a single parameter,
allowing the sensor rotation instance to be bound with two parameters at usage time.
- If rotated counter-clockwise, the first bound behavior is triggered with the first parameter passed to the sensor rotation.
- If rotated clockwise, the second bound behavior is triggered with the second parameter passed to the sensor rotation.
### Configuration
Here is an example, showing how send key presses on rotation:
First, defining the sensor rotation itself, binding the [Key Press Behavior](/docs/behaviors/key-press.md) twice, then binding it in the `sensor-bindings` property of a keymap layer:
```
/ {
behaviors {
rot_kp: behavior_sensor_rotate_kp {
compatible = "zmk,behavior-sensor-rotate-var";
label = "ENC_KP";
#sensor-binding-cells = <2>;
bindings = <&kp>, <&kp>;
};
};
keymap {
compatible = "zmk,keymap";
base {
...
sensor-bindings = <&rot_kp PG_DN PG_UP>;
}
}
};
```

View File

@@ -23,17 +23,17 @@ Keyboards and macropads with encoder support will typically take the two EC11 pi
### Rotation
Rotation is handled separately as a type of sensor. The behavior for this is set in `sensor-bindings`, which is defined in each keymap layer in the following format:
Rotation is handled separately as a type of sensor. The behavior for this is set in `sensor-bindings`. See [Sensor Rotation](../behaviors/sensor-rotate.md) for customizing this behavior.
```
sensor-bindings = <BINDING CW_KEY CCW_KEY>;
sensor-bindings = <BINDING [CW_KEY] [CCW_KEY]>;
```
- `BINDING`, for now, has only one behavior available; `&inc_dec_kp` for key presses (see [Key Press](../behaviors/key-press.md) for details on available keycodes).
- `BINDING` is either a user-defined behavior, or `&inc_dec_kp` for key presses (see [Key Press](../behaviors/key-press.md) for details on available keycodes).
- `CW_KEY` is the keycode activated by a clockwise turn.
- `CCW_KEY` is the keycode activated by a counter-clockwise turn.
Additional encoders can be configured by adding more `BINDING CW_KEY CCW_KEY` sets immediately after the first.
Additional encoders can be configured by adding more bindings immediately after the first.
As an example, a complete `sensor-bindings` for a Kyria with two encoders could look like:

View File

@@ -35,6 +35,7 @@ module.exports = {
"behaviors/tap-dance",
"behaviors/caps-word",
"behaviors/key-repeat",
"behaviors/sensor-rotate",
"behaviors/reset",
"behaviors/bluetooth",
"behaviors/outputs",