diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 1cee6b3e..c8356ce0 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 59d0c487..89fc70ff 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 b54e5328..823d92d8 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 68f9bec4..55acf45e 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 00000000..c56361d1 --- /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 34cbb954..1a3592e9 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 00000000..9bff0a90 --- /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 00000000..adb8ac5d --- /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 00000000..1b0302dd --- /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 dcfd0f1d..98466770 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 00000000..cfbeae0f --- /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