Skip to content

Commit

Permalink
Initial behavior for key presses on encoder rotate
Browse files Browse the repository at this point in the history
  • Loading branch information
petejohanson committed Jul 22, 2020
1 parent 4d73938 commit 34ff619
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 15 deletions.
5 changes: 3 additions & 2 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 9 additions & 7 deletions app/boards/shields/kyria/keymap/keymap.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
};
};
};
3 changes: 2 additions & 1 deletion app/dts/behaviors.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
#include <behaviors/transparent.dtsi>
#include <behaviors/mod_tap.dtsi>
#include <behaviors/momentary_layer.dtsi>
#include <behaviors/reset.dtsi>
#include <behaviors/reset.dtsi>
#include <behaviors/sensor_rotate_key_press.dtsi>
19 changes: 19 additions & 0 deletions app/dts/behaviors/sensor_rotate_key_press.dtsi
Original file line number Diff line number Diff line change
@@ -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>;
};
};
};
Original file line number Diff line number Diff line change
@@ -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 changes: 1 addition & 1 deletion app/include/drivers/behavior.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions app/include/dt-bindings/zmk/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/include/zmk/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
91 changes: 91 additions & 0 deletions app/src/behaviors/behavior_sensor_rotate_key_press.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2020 Peter Johanson <[email protected]>
*
* 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 changes: 11 additions & 1 deletion app/src/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))), \
Expand Down Expand Up @@ -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) \
{ \
Expand Down Expand Up @@ -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--)
Expand Down Expand Up @@ -203,20 +206,27 @@ 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;
}

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 */

6 changes: 5 additions & 1 deletion app/src/sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/event-manager.h>
#include <zmk/events/sensor-event.h>

#if ZMK_KEYMAP_HAS_SENSORS

struct sensors_data_item {
u8_t sensor_number;
struct device *dev;
Expand Down Expand Up @@ -78,4 +80,6 @@ static int zmk_sensors_init(struct device *_arg)

SYS_INIT(zmk_sensors_init,
APPLICATION,
CONFIG_APPLICATION_INIT_PRIORITY);
CONFIG_APPLICATION_INIT_PRIORITY);

#endif /* ZMK_KEYMAP_HAS_SENSORS */

0 comments on commit 34ff619

Please sign in to comment.