Merge pull request #61 from petejohanson/sensors/sensor-keymap-bindings

Add sensor handling to keymaps.
This commit is contained in:
Pete Johanson 2020-07-22 16:30:05 -04:00 committed by GitHub
commit 36f36f21f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 409 additions and 153 deletions

View file

@ -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)

View file

@ -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>;
};
};
};

View file

@ -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)

View file

@ -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);

View file

@ -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);
}
@ -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);

View file

@ -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>

View file

@ -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>;
};
};
};

View file

@ -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

View file

@ -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

View file

@ -13,3 +13,6 @@ child-binding:
bindings:
type: phandle-array
required: true
sensor-bindings:
type: phandle-array
required: false

View file

@ -11,10 +11,6 @@
#include <device.h>
#include <zmk/keys.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @cond INTERNAL_HIDDEN
*
@ -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
@ -131,104 +77,31 @@ 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.
* @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_keycode_pressed(struct device *dev, u8_t usage_page, u32_t keycode);
__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_keycode_pressed(struct device *dev, u8_t usage_page, u32_t keycode)
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->keycode_pressed == NULL) {
if (api->sensor_binding_triggered == NULL) {
return -ENOTSUP;
}
return api->keycode_pressed(dev, usage_page, keycode);
return api->sensor_binding_triggered(dev, sensor, param1, param2);
}
/**
* @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.
*
* @retval 0 If successful.
* @retval Negative errno code if failure.
*/
__syscall int behavior_modifiers_released(struct device *dev, zmk_mod_flags modifiers);
static inline int z_impl_behavior_modifiers_released(struct device *dev, zmk_mod_flags modifiers)
{
const struct behavior_driver_api *api =
(const struct behavior_driver_api *)dev->driver_api;
if (api->modifiers_released == NULL) {
return -ENOTSUP;
}
return api->modifiers_released(dev, modifiers);
}
#ifdef __cplusplus
}
#endif
/**
* @}
*/

View file

@ -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

View file

@ -0,0 +1,19 @@
/*
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
*
* 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);

12
app/include/zmk/sensors.h Normal file
View file

@ -0,0 +1,12 @@
/*
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
*
* SPDX-License-Identifier: MIT
*/
#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)

View file

@ -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)

View file

@ -0,0 +1,10 @@
/*
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
*
* SPDX-License-Identifier: MIT
*/
#include <kernel.h>
#include <zmk/events/sensor-event.h>
ZMK_EVENT_IMPL(sensor_event);

View file

@ -4,6 +4,7 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/matrix.h>
#include <zmk/sensors.h>
#include <zmk/keymap.h>
#include <dt-bindings/zmk/matrix-transform.h>
#include <drivers/behavior.h>
@ -11,6 +12,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/event-manager.h>
#include <zmk/events/position-state-changed.h>
#include <zmk/events/sensor-event.h>
static u32_t zmk_keymap_layer_state = 0;
static u8_t zmk_keymap_layer_default = 0;
@ -62,6 +64,53 @@ 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))), \
.param2 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(LAYER_NODE(layer), sensor_bindings, idx, param2), (0), (DT_PHA_BY_IDX(LAYER_NODE(layer), sensor_bindings, idx, param2))), \
},
#define SENSOR_LAYER(idx) \
COND_CODE_1(DT_NODE_HAS_PROP(DT_PHANDLE_BY_IDX(ZMK_KEYMAP_NODE, layers, idx), sensor_bindings), \
({ UTIL_LISTIFY(DT_PROP_LEN(DT_PHANDLE_BY_IDX(ZMK_KEYMAP_NODE, layers, idx), sensor_bindings), _TRANSFORM_SENSOR_ENTRY, idx) }), \
(NULL))
static struct zmk_behavior_binding zmk_sensor_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_SENSORS_LEN] = {
#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE, layers, 0)
SENSOR_LAYER(0),
#endif
#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE, layers, 1)
SENSOR_LAYER(1),
#endif
#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE, layers, 2)
SENSOR_LAYER(2),
#endif
#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE, layers, 3)
SENSOR_LAYER(3),
#endif
#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE, layers, 4)
SENSOR_LAYER(4),
#endif
#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE, layers, 5)
SENSOR_LAYER(5),
#endif
#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE, layers, 6)
SENSOR_LAYER(6),
#endif
#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE, layers, 7)
SENSOR_LAYER(7),
#endif
#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE, layers, 8)
SENSOR_LAYER(8),
#endif
#if DT_PROP_HAS_IDX(ZMK_KEYMAP_NODE, layers, 9)
SENSOR_LAYER(9),
#endif
};
#endif /* ZMK_KEYMAP_HAS_SENSORS */
#define SET_LAYER_STATE(layer, state) \
if (layer >= 32) \
{ \
@ -90,7 +139,7 @@ int zmk_keymap_position_state_changed(u32_t position, bool pressed)
struct device *behavior;
int ret;
LOG_DBG("layer: %d position: %d, binding name: %s", layer, position, binding->behavior_dev);
LOG_DBG("layer: %d position: %d, binding name: %s", layer, position, log_strdup(binding->behavior_dev));
behavior = device_get_binding(binding->behavior_dev);
@ -120,15 +169,64 @@ 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--)
{
if (((zmk_keymap_layer_state & BIT(layer)) == BIT(layer) || layer == zmk_keymap_layer_default) && zmk_sensor_keymap[layer] != NULL)
{
struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_number];
struct device *behavior;
int ret;
LOG_DBG("layer: %d sensor_number: %d, binding name: %s", layer, sensor_number, log_strdup(binding->behavior_dev));
behavior = device_get_binding(binding->behavior_dev);
if (!behavior) {
LOG_DBG("No behavior assigned to %d on layer %d", sensor_number, layer);
continue;
}
ret = behavior_sensor_keymap_binding_triggered(behavior, sensor, binding->param1, binding->param2);
if (ret > 0) {
LOG_DBG("behavior processing to continue to next layer");
continue;
} else if (ret < 0) {
LOG_DBG("Behavior returned error: %d", ret);
return ret;
} else {
return ret;
}
}
}
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);
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)) {
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 0;
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 */

85
app/src/sensors.c Normal file
View file

@ -0,0 +1,85 @@
/*
* Copyright (c) 2020 Peter Johanson
*
* SPDX-License-Identifier: MIT
*/
#include <drivers/sensor.h>
#include <devicetree.h>
#include <init.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/sensors.h>
#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;
struct sensor_trigger trigger;
};
#define _SENSOR_ITEM(node) {.dev = NULL, .trigger = { .type = SENSOR_TRIG_DELTA, .chan = SENSOR_CHAN_ROTATION } },
#define SENSOR_ITEM(idx, _) COND_CODE_1(DT_NODE_HAS_STATUS(ZMK_KEYMAP_SENSORS_BY_IDX(idx),okay), (_SENSOR_ITEM(ZMK_KEYMAP_SENSORS_BY_IDX(idx))),())
static struct sensors_data_item sensors[] = {
UTIL_LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENSOR_ITEM, 0)
};
static void zmk_sensors_trigger_handler(struct device *dev, struct sensor_trigger *trigger)
{
int err;
struct sensors_data_item * item = CONTAINER_OF(trigger, struct sensors_data_item, trigger);
struct sensor_event *event;
LOG_DBG("sensor %d", item->sensor_number);
err = sensor_sample_fetch(dev);
if (err) {
LOG_WRN("Failed to fetch sample from device %d", err);
return;
}
event = new_sensor_event();
event->sensor_number = item->sensor_number;
event->sensor = dev;
ZMK_EVENT_RAISE(event);
}
static void zmk_sensors_init_item(const char *node, u8_t i, u8_t abs_i)
{
LOG_DBG("Init %s at index %d with sensor_number %d", node, i, abs_i);
sensors[i].dev = device_get_binding(node);
sensors[i].sensor_number = abs_i;
if (!sensors[i].dev) {
LOG_ERR("Failed to find device for %s", node);
}
sensor_trigger_set(sensors[i].dev, &sensors[i].trigger, zmk_sensors_trigger_handler);
}
#define _SENSOR_INIT(node) zmk_sensors_init_item(DT_LABEL(node), local_index++, absolute_index++);
#define SENSOR_INIT(idx, _i) COND_CODE_1(DT_NODE_HAS_STATUS(ZMK_KEYMAP_SENSORS_BY_IDX(idx),okay), (_SENSOR_INIT(ZMK_KEYMAP_SENSORS_BY_IDX(idx))),(absolute_index++;))
static int zmk_sensors_init(struct device *_arg)
{
int local_index = 0;
int absolute_index = 0;
UTIL_LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENSOR_INIT, 0)
return 0;
}
SYS_INIT(zmk_sensors_init,
APPLICATION,
CONFIG_APPLICATION_INIT_PRIORITY);
#endif /* ZMK_KEYMAP_HAS_SENSORS */