From adfa1b5443e0a7ccaa973d69aac59ffbf8607d2f Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Wed, 22 Jul 2020 02:13:52 -0400 Subject: [PATCH 1/4] Initial work on sensor bindings. --- app/CMakeLists.txt | 2 + app/boards/shields/kyria/kyria.dtsi | 5 ++ app/drivers/zephyr/ec11.h | 2 +- app/drivers/zephyr/ec11_trigger.c | 4 +- app/dts/bindings/zmk,keymap-sensors.yaml | 9 +++ app/dts/bindings/zmk,layers.yaml | 3 + app/include/zmk/events/sensor-event.h | 19 +++++ app/include/zmk/sensors.h | 11 +++ app/src/events/sensor_event.c | 10 +++ app/src/keymap.c | 52 ++++++++++++++ app/src/sensors.c | 89 ++++++++++++++++++++++++ 11 files changed, 203 insertions(+), 3 deletions(-) create mode 100644 app/dts/bindings/zmk,keymap-sensors.yaml create mode 100644 app/include/zmk/events/sensor-event.h create mode 100644 app/include/zmk/sensors.h create mode 100644 app/src/events/sensor_event.c create mode 100644 app/src/sensors.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 1cee6b3e38e..c8356ce052e 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -32,10 +32,12 @@ 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/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/behaviors/behavior_key_press.c) target_sources(app PRIVATE src/behaviors/behavior_reset.c) target_sources(app PRIVATE src/behaviors/behavior_mod_tap.c) diff --git a/app/boards/shields/kyria/kyria.dtsi b/app/boards/shields/kyria/kyria.dtsi index 59d0c487817..89fc70ff3d1 100644 --- a/app/boards/shields/kyria/kyria.dtsi +++ b/app/boards/shields/kyria/kyria.dtsi @@ -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) diff --git a/app/drivers/zephyr/ec11.h b/app/drivers/zephyr/ec11.h index b54e53280f6..823d92d8f0f 100644 --- a/app/drivers/zephyr/ec11.h +++ b/app/drivers/zephyr/ec11.h @@ -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); diff --git a/app/drivers/zephyr/ec11_trigger.c b/app/drivers/zephyr/ec11_trigger.c index 68f9bec4446..55acf45e17f 100644 --- a/app/drivers/zephyr/ec11_trigger.c +++ b/app/drivers/zephyr/ec11_trigger.c @@ -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); diff --git a/app/dts/bindings/zmk,keymap-sensors.yaml b/app/dts/bindings/zmk,keymap-sensors.yaml new file mode 100644 index 00000000000..c56361d1adb --- /dev/null +++ b/app/dts/bindings/zmk,keymap-sensors.yaml @@ -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 diff --git a/app/dts/bindings/zmk,layers.yaml b/app/dts/bindings/zmk,layers.yaml index 34cbb9544e2..1a3592e9e51 100644 --- a/app/dts/bindings/zmk,layers.yaml +++ b/app/dts/bindings/zmk,layers.yaml @@ -13,3 +13,6 @@ child-binding: bindings: type: phandle-array required: true + sensor-bindings: + type: phandle-array + required: false diff --git a/app/include/zmk/events/sensor-event.h b/app/include/zmk/events/sensor-event.h new file mode 100644 index 00000000000..9bff0a90eb5 --- /dev/null +++ b/app/include/zmk/events/sensor-event.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include +#include +#include + +struct sensor_event { + struct zmk_event_header header; + u8_t sensor_number; + struct sensor_value value; +}; + +ZMK_EVENT_DECLARE(sensor_event); \ No newline at end of file diff --git a/app/include/zmk/sensors.h b/app/include/zmk/sensors.h new file mode 100644 index 00000000000..adb8ac5d2b1 --- /dev/null +++ b/app/include/zmk/sensors.h @@ -0,0 +1,11 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#define ZMK_KEYMAP_SENSORS_NODE DT_INST(0, zmk_keymap_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) \ No newline at end of file diff --git a/app/src/events/sensor_event.c b/app/src/events/sensor_event.c new file mode 100644 index 00000000000..1b0302dd7a7 --- /dev/null +++ b/app/src/events/sensor_event.c @@ -0,0 +1,10 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#include +#include + +ZMK_EVENT_IMPL(sensor_event); \ No newline at end of file diff --git a/app/src/keymap.c b/app/src/keymap.c index dcfd0f1d21d..98466770fc6 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -4,6 +4,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include +#include #include #include #include @@ -11,6 +12,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include +#include static u32_t zmk_keymap_layer_state = 0; static u8_t zmk_keymap_layer_default = 0; @@ -62,6 +64,51 @@ static struct zmk_behavior_binding zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_ #endif }; + +#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 +}; + #define SET_LAYER_STATE(layer, state) \ if (layer >= 32) \ { \ @@ -125,10 +172,15 @@ 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); + } else if (is_sensor_event(eh)) { + const struct sensor_event *ev = cast_sensor_event(eh); + // TODO: DO SOMETHING WITH IT! } + return 0; } ZMK_LISTENER(keymap, keymap_listener); ZMK_SUBSCRIPTION(keymap, position_state_changed); +ZMK_SUBSCRIPTION(keymap, sensor_event); diff --git a/app/src/sensors.c b/app/src/sensors.c new file mode 100644 index 00000000000..cfbeae0ff35 --- /dev/null +++ b/app/src/sensors.c @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#include +#include +#include + +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include +#include +#include + +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(node, _) COND_CODE_1(DT_NODE_HAS_STATUS(node,okay), (_SENSOR_ITEM(node)),()) + +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 sensor_value val; + 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; + + err = sensor_channel_get(dev, SENSOR_CHAN_ROTATION, &event->value); + if (err) { + k_free(event); + LOG_WRN("Failed to get the value for the rotation channel: %d", err); + } + + LOG_DBG("val1 %d val2 %d", val.val1, val.val2); + + 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(node, _i) COND_CODE_1(DT_NODE_HAS_STATUS(node,okay), (_SENSOR_INIT(node)),(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); \ No newline at end of file From 4d73938cb5fd5e9b8d94d4e775fd8fba7eb83e3c Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Wed, 22 Jul 2020 10:10:04 -0400 Subject: [PATCH 2/4] More sensor keymap work. --- app/include/drivers/behavior.h | 147 ++------------------------ app/include/zmk/events/sensor-event.h | 4 +- app/src/keymap.c | 44 +++++++- app/src/sensors.c | 14 +-- 4 files changed, 55 insertions(+), 154 deletions(-) diff --git a/app/include/drivers/behavior.h b/app/include/drivers/behavior.h index cca9272a857..106f6793eff 100644 --- a/app/include/drivers/behavior.h +++ b/app/include/drivers/behavior.h @@ -11,10 +11,6 @@ #include #include -#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,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 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 /** * @} diff --git a/app/include/zmk/events/sensor-event.h b/app/include/zmk/events/sensor-event.h index 9bff0a90eb5..f881ac5c24c 100644 --- a/app/include/zmk/events/sensor-event.h +++ b/app/include/zmk/events/sensor-event.h @@ -8,12 +8,12 @@ #include #include -#include +#include struct sensor_event { struct zmk_event_header header; u8_t sensor_number; - struct sensor_value value; + struct device *sensor; }; ZMK_EVENT_DECLARE(sensor_event); \ No newline at end of file diff --git a/app/src/keymap.c b/app/src/keymap.c index 98466770fc6..d07f4885f4a 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -137,7 +137,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); @@ -167,17 +167,53 @@ int zmk_keymap_position_state_changed(u32_t position, bool pressed) return -ENOTSUP; } +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; +} + 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); } else if (is_sensor_event(eh)) { const struct sensor_event *ev = cast_sensor_event(eh); - // TODO: DO SOMETHING WITH IT! + return zmk_keymap_sensor_triggered(ev->sensor_number, ev->sensor); } - return 0; + return -ENOTSUP; } ZMK_LISTENER(keymap, keymap_listener); diff --git a/app/src/sensors.c b/app/src/sensors.c index cfbeae0ff35..d1135c09f14 100644 --- a/app/src/sensors.c +++ b/app/src/sensors.c @@ -23,7 +23,7 @@ struct sensors_data_item { }; #define _SENSOR_ITEM(node) {.dev = NULL, .trigger = { .type = SENSOR_TRIG_DELTA, .chan = SENSOR_CHAN_ROTATION } }, -#define SENSOR_ITEM(node, _) COND_CODE_1(DT_NODE_HAS_STATUS(node,okay), (_SENSOR_ITEM(node)),()) +#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) @@ -32,7 +32,6 @@ static struct sensors_data_item sensors[] = { static void zmk_sensors_trigger_handler(struct device *dev, struct sensor_trigger *trigger) { int err; - struct sensor_value val; struct sensors_data_item * item = CONTAINER_OF(trigger, struct sensors_data_item, trigger); struct sensor_event *event; @@ -46,14 +45,7 @@ static void zmk_sensors_trigger_handler(struct device *dev, struct sensor_trigge event = new_sensor_event(); event->sensor_number = item->sensor_number; - - err = sensor_channel_get(dev, SENSOR_CHAN_ROTATION, &event->value); - if (err) { - k_free(event); - LOG_WRN("Failed to get the value for the rotation channel: %d", err); - } - - LOG_DBG("val1 %d val2 %d", val.val1, val.val2); + event->sensor = dev; ZMK_EVENT_RAISE(event); } @@ -73,7 +65,7 @@ static void zmk_sensors_init_item(const char *node, u8_t i, u8_t abs_i) } #define _SENSOR_INIT(node) zmk_sensors_init_item(DT_LABEL(node), local_index++, absolute_index++); -#define SENSOR_INIT(node, _i) COND_CODE_1(DT_NODE_HAS_STATUS(node,okay), (_SENSOR_INIT(node)),(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) { From 34ff6190bd6177705bed075c5d52f54e87930591 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Wed, 22 Jul 2020 11:08:52 -0400 Subject: [PATCH 3/4] Initial behavior for key presses on encoder rotate --- app/CMakeLists.txt | 5 +- .../shields/kyria/keymap/keymap.overlay | 16 ++-- app/dts/behaviors.dtsi | 3 +- .../behaviors/sensor_rotate_key_press.dtsi | 19 ++++ .../zmk,behavior-sensor-rotate-key-press.yaml | 22 +++++ app/include/drivers/behavior.h | 2 +- app/include/dt-bindings/zmk/keys.h | 8 +- app/include/zmk/sensors.h | 1 + .../behavior_sensor_rotate_key_press.c | 91 +++++++++++++++++++ app/src/keymap.c | 12 ++- app/src/sensors.c | 6 +- 11 files changed, 170 insertions(+), 15 deletions(-) create mode 100644 app/dts/behaviors/sensor_rotate_key_press.dtsi create mode 100644 app/dts/bindings/behaviors/zmk,behavior-sensor-rotate-key-press.yaml create mode 100644 app/src/behaviors/behavior_sensor_rotate_key_press.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index c8356ce052e..45c688563d4 100644 --- a/app/CMakeLists.txt +++ b/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/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) diff --git a/app/boards/shields/kyria/keymap/keymap.overlay b/app/boards/shields/kyria/keymap/keymap.overlay index 0c00aef8ceb..398882dcb6b 100644 --- a/app/boards/shields/kyria/keymap/keymap.overlay +++ b/app/boards/shields/kyria/keymap/keymap.overlay @@ -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>; }; }; }; diff --git a/app/dts/behaviors.dtsi b/app/dts/behaviors.dtsi index 86cd7b72f5f..39784799433 100644 --- a/app/dts/behaviors.dtsi +++ b/app/dts/behaviors.dtsi @@ -2,4 +2,5 @@ #include #include #include -#include \ No newline at end of file +#include +#include \ No newline at end of file diff --git a/app/dts/behaviors/sensor_rotate_key_press.dtsi b/app/dts/behaviors/sensor_rotate_key_press.dtsi new file mode 100644 index 00000000000..ef52fbab7f8 --- /dev/null +++ b/app/dts/behaviors/sensor_rotate_key_press.dtsi @@ -0,0 +1,19 @@ +#include + +/ { + behaviors { + enckp: behavior_sensor_rotate_key_press { + compatible = "zmk,behavior-sensor-rotate-key-press"; + label = "ENC_KEY_PRESS"; + usage_page = ; + #sensor-binding-cells = <2>; + }; + + enccp: behavior_sensor_rotate_consumer_press { + compatible = "zmk,behavior-sensor-rotate-key-press"; + label = "ENC_CONSUMER_PRESS"; + usage_page = ; + #sensor-binding-cells = <2>; + }; + }; +}; diff --git a/app/dts/bindings/behaviors/zmk,behavior-sensor-rotate-key-press.yaml b/app/dts/bindings/behaviors/zmk,behavior-sensor-rotate-key-press.yaml new file mode 100644 index 00000000000..bbf35373982 --- /dev/null +++ b/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 diff --git a/app/include/drivers/behavior.h b/app/include/drivers/behavior.h index 106f6793eff..c24d11b35c1 100644 --- a/app/include/drivers/behavior.h +++ b/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); -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; diff --git a/app/include/dt-bindings/zmk/keys.h b/app/include/dt-bindings/zmk/keys.h index 78b81961d08..f35a00ffabf 100644 --- a/app/include/dt-bindings/zmk/keys.h +++ b/app/include/dt-bindings/zmk/keys.h @@ -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 diff --git a/app/include/zmk/sensors.h b/app/include/zmk/sensors.h index adb8ac5d2b1..c60cd4bea08 100644 --- a/app/include/zmk/sensors.h +++ b/app/include/zmk/sensors.h @@ -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) \ No newline at end of file diff --git a/app/src/behaviors/behavior_sensor_rotate_key_press.c b/app/src/behaviors/behavior_sensor_rotate_key_press.c new file mode 100644 index 00000000000..fb0fb852005 --- /dev/null +++ b/app/src/behaviors/behavior_sensor_rotate_key_press.c @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_sensor_rotate_key_press + +#include +#include +#include + +#include +#include +#include + +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) \ No newline at end of file diff --git a/app/src/keymap.c b/app/src/keymap.c index d07f4885f4a..24e249dd62c 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -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))), \ @@ -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) \ { \ @@ -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--) @@ -203,14 +206,18 @@ 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; @@ -218,5 +225,8 @@ int keymap_listener(const struct zmk_event_header *eh) 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 */ diff --git a/app/src/sensors.c b/app/src/sensors.c index d1135c09f14..0a86109cc8e 100644 --- a/app/src/sensors.c +++ b/app/src/sensors.c @@ -16,6 +16,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include +#if ZMK_KEYMAP_HAS_SENSORS + struct sensors_data_item { u8_t sensor_number; struct device *dev; @@ -78,4 +80,6 @@ static int zmk_sensors_init(struct device *_arg) SYS_INIT(zmk_sensors_init, APPLICATION, - CONFIG_APPLICATION_INIT_PRIORITY); \ No newline at end of file + CONFIG_APPLICATION_INIT_PRIORITY); + +#endif /* ZMK_KEYMAP_HAS_SENSORS */ \ No newline at end of file From 5958bfed0bd764c50d1a926a29a419b6f99018d5 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Wed, 22 Jul 2020 16:24:31 -0400 Subject: [PATCH 4/4] Naming tweaks for the sensor behavior. --- app/boards/shields/kyria/keymap/keymap.overlay | 2 +- app/dts/behaviors/sensor_rotate_key_press.dtsi | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/boards/shields/kyria/keymap/keymap.overlay b/app/boards/shields/kyria/keymap/keymap.overlay index 398882dcb6b..53101ff94c6 100644 --- a/app/boards/shields/kyria/keymap/keymap.overlay +++ b/app/boards/shields/kyria/keymap/keymap.overlay @@ -30,7 +30,7 @@ &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>; + sensor-bindings = <&inc_dec_cp M_VOLU M_VOLD &inc_dec_kp PGUP PGDN>; }; }; }; diff --git a/app/dts/behaviors/sensor_rotate_key_press.dtsi b/app/dts/behaviors/sensor_rotate_key_press.dtsi index ef52fbab7f8..b094be5824e 100644 --- a/app/dts/behaviors/sensor_rotate_key_press.dtsi +++ b/app/dts/behaviors/sensor_rotate_key_press.dtsi @@ -2,14 +2,14 @@ / { behaviors { - enckp: behavior_sensor_rotate_key_press { + inc_dec_kp: behavior_sensor_rotate_key_press { compatible = "zmk,behavior-sensor-rotate-key-press"; label = "ENC_KEY_PRESS"; usage_page = ; #sensor-binding-cells = <2>; }; - enccp: behavior_sensor_rotate_consumer_press { + inc_dec_cp: behavior_sensor_rotate_consumer_press { compatible = "zmk,behavior-sensor-rotate-key-press"; label = "ENC_CONSUMER_PRESS"; usage_page = ;