Skip to content

Commit

Permalink
Merge pull request #61 from petejohanson/sensors/sensor-keymap-bindings
Browse files Browse the repository at this point in the history
Add sensor handling to keymaps.
  • Loading branch information
petejohanson authored Jul 22, 2020
2 parents 5896c30 + 5958bfe commit 36f36f2
Show file tree
Hide file tree
Showing 18 changed files with 409 additions and 153 deletions.
3 changes: 3 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +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(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(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 = <&inc_dec_cp M_VOLU M_VOLD &inc_dec_kp PGUP PGDN>;
};
};
};
5 changes: 5 additions & 0 deletions app/boards/shields/kyria/kyria.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ RC(2,0) RC(2,1) RC(2,2) RC(2,3) RC(2,4) RC(2,5) RC(2,6) RC(2,7) RC(2,8) RC(2,9)
status = "disabled";
};

sensors {
compatible = "zmk,keymap-sensors";
sensors = <&left_encoder &right_encoder>;
};

// TODO: Encoder node(s)
// TODO: OLED node
// TODO: RGB node(s)
Expand Down
2 changes: 1 addition & 1 deletion app/drivers/zephyr/ec11.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct ec11_data {
struct device *dev;

sensor_trigger_handler_t handler;
struct sensor_trigger trigger;
const struct sensor_trigger *trigger;

#if defined(CONFIG_EC11_TRIGGER_OWN_THREAD)
K_THREAD_STACK_MEMBER(thread_stack, CONFIG_EC11_THREAD_STACK_SIZE);
Expand Down
4 changes: 2 additions & 2 deletions app/drivers/zephyr/ec11_trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static void ec11_thread_cb(void *arg)
struct device *dev = arg;
struct ec11_data *drv_data = dev->driver_data;

drv_data->handler(dev, &drv_data->trigger);
drv_data->handler(dev, drv_data->trigger);

setup_int(dev, true);
}
Expand Down Expand Up @@ -125,7 +125,7 @@ int ec11_trigger_set(struct device *dev,

k_msleep(5);

drv_data->trigger = *trig;
drv_data->trigger = trig;
drv_data->handler = handler;

setup_int(dev, true);
Expand Down
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 {
inc_dec_kp: behavior_sensor_rotate_key_press {
compatible = "zmk,behavior-sensor-rotate-key-press";
label = "ENC_KEY_PRESS";
usage_page = <USAGE_KEYPAD>;
#sensor-binding-cells = <2>;
};

inc_dec_cp: 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
9 changes: 9 additions & 0 deletions app/dts/bindings/zmk,keymap-sensors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
description: |
Allows defining the collection of sensors bound in the keymap layers
compatible: "zmk,keymap-sensors"

properties:
sensors:
type: phandles
required: true
3 changes: 3 additions & 0 deletions app/dts/bindings/zmk,layers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ child-binding:
bindings:
type: phandle-array
required: true
sensor-bindings:
type: phandle-array
required: false
147 changes: 10 additions & 137 deletions app/include/drivers/behavior.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
#include <device.h>
#include <zmk/keys.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @cond INTERNAL_HIDDEN
*
Expand All @@ -23,68 +19,18 @@ extern "C" {
* (Internal use only.)
*/

typedef int (*behavior_position_callback_t)(struct device *dev, u32_t position);
typedef int (*behavior_keymap_binding_callback_t)(struct device *dev, u32_t position, u32_t param1, u32_t param2);
typedef int (*behavior_keycode_callback_t)(struct device *dev, u8_t usage_page, u32_t keycode);
typedef int (*behavior_modifiers_callback_t)(struct device *dev, zmk_mod_flags modifiers);
typedef int (*behavior_sensor_keymap_binding_callback_t)(struct device *dev, struct device *sensor, u32_t param1, u32_t param2);

__subsystem struct behavior_driver_api {
behavior_position_callback_t position_pressed;
behavior_position_callback_t position_released;
behavior_keymap_binding_callback_t binding_pressed;
behavior_keymap_binding_callback_t binding_released;
behavior_keycode_callback_t keycode_pressed;
behavior_keycode_callback_t keycode_released;
behavior_modifiers_callback_t modifiers_pressed;
behavior_modifiers_callback_t modifiers_released;
behavior_sensor_keymap_binding_callback_t sensor_binding_triggered;
};
/**
* @endcond
*/

/**
* @brief Handle the key position being pressed
* @param dev Pointer to the device structure for the driver instance.
* @param position They key position that was pressed
*
* @retval 0 If successful.
* @retval Negative errno code if failure.
*/
__syscall int behavior_position_pressed(struct device *dev, u32_t position);

static inline int z_impl_behavior_position_pressed(struct device *dev, u32_t position)
{
const struct behavior_driver_api *api =
(const struct behavior_driver_api *)dev->driver_api;

if (api->position_pressed == NULL) {
return -ENOTSUP;
}

return api->position_pressed(dev, position);
}

/**
* @brief Handle the key position being released
* @param dev Pointer to the device structure for the driver instance.
* @param position They key position that was released
*
* @retval 0 If successful.
* @retval Negative errno code if failure.
*/
__syscall int behavior_position_released(struct device *dev, u32_t position);

static inline int z_impl_behavior_position_released(struct device *dev, u32_t position)
{
const struct behavior_driver_api *api =
(const struct behavior_driver_api *)dev->driver_api;

if (api->position_released == NULL) {
return -ENOTSUP;
}

return api->position_released(dev, position);
}

/**
* @brief Handle the keymap binding being pressed
Expand Down Expand Up @@ -131,103 +77,30 @@ static inline int z_impl_behavior_keymap_binding_released(struct device *dev, u3
return api->binding_released(dev, position, param1, param2);
}


/**
* @brief Handle the keycode being pressed
* @brief Handle the a sensor keymap binding being triggered
* @param dev Pointer to the device structure for the driver instance.
* @param usage_page The usage page for the keycode.
* @param keycode The keycode that is being pressed.
*
* @retval 0 If successful.
* @retval Negative errno code if failure.
*/
__syscall int behavior_keycode_pressed(struct device *dev, u8_t usage_page, u32_t keycode);

static inline int z_impl_behavior_keycode_pressed(struct device *dev, u8_t usage_page, u32_t keycode)
{
const struct behavior_driver_api *api =
(const struct behavior_driver_api *)dev->driver_api;

if (api->keycode_pressed == NULL) {
return -ENOTSUP;
}

return api->keycode_pressed(dev, usage_page, keycode);
}


/**
* @brief Handle the keycode being released
* @param dev Pointer to the device structure for the driver instance.
* @param usage_page The usage page for the keycode.
* @param keycode The keycode that is being pressed.
*
* @retval 0 If successful.
* @retval Negative errno code if failure.
*/
__syscall int behavior_keycode_released(struct device *dev, u8_t usage_page, u32_t keycode);

static inline int z_impl_behavior_keycode_released(struct device *dev, u8_t usage_page, u32_t keycode)
{
const struct behavior_driver_api *api =
(const struct behavior_driver_api *)dev->driver_api;

if (api->keycode_released == NULL) {
return -ENOTSUP;
}

return api->keycode_released(dev, usage_page, keycode);
}


/**
* @brief Handle the keycode being pressed
* @param dev Pointer to the device structure for the driver instance.
* @param keycode The keycode that is being pressed.
*
* @retval 0 If successful.
* @retval Negative errno code if failure.
*/
__syscall int behavior_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers);

static inline int z_impl_behavior_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers)
{
const struct behavior_driver_api *api =
(const struct behavior_driver_api *)dev->driver_api;

if (api->modifiers_pressed == NULL) {
return -ENOTSUP;
}

return api->modifiers_pressed(dev, modifiers);
}


/**
* @brief Handle the keycode being released
* @param dev Pointer to the device structure for the driver instance.
* @param keycode The keycode that is being pressed.
* @param sensor Pointer to the sensor device structure for the sensor driver instance.
* @param param1 User parameter specified at time of behavior binding.
* @param param2 User parameter specified at time of behavior binding.
*
* @retval 0 If successful.
* @retval Negative errno code if failure.
*/
__syscall int behavior_modifiers_released(struct device *dev, zmk_mod_flags modifiers);
__syscall int behavior_sensor_keymap_binding_triggered(struct device *dev, struct device *sensor, u32_t param1, u32_t param2);

static inline int z_impl_behavior_modifiers_released(struct device *dev, zmk_mod_flags modifiers)
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;

if (api->modifiers_released == NULL) {
if (api->sensor_binding_triggered == NULL) {
return -ENOTSUP;
}

return api->modifiers_released(dev, modifiers);
return api->sensor_binding_triggered(dev, sensor, param1, param2);
}

#ifdef __cplusplus
}
#endif

/**
* @}
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
19 changes: 19 additions & 0 deletions app/include/zmk/events/sensor-event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2020 Peter Johanson <[email protected]>
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <zephyr.h>
#include <zmk/event-manager.h>
#include <device.h>

struct sensor_event {
struct zmk_event_header header;
u8_t sensor_number;
struct device *sensor;
};

ZMK_EVENT_DECLARE(sensor_event);
Loading

0 comments on commit 36f36f2

Please sign in to comment.