Initial work on sensor bindings.
This commit is contained in:
parent
5896c3032b
commit
adfa1b5443
11 changed files with 203 additions and 3 deletions
|
@ -32,10 +32,12 @@ target_sources(app PRIVATE src/matrix_transform.c)
|
||||||
target_sources(app PRIVATE src/keymap.c)
|
target_sources(app PRIVATE src/keymap.c)
|
||||||
target_sources(app PRIVATE src/hid_listener.c)
|
target_sources(app PRIVATE src/hid_listener.c)
|
||||||
target_sources(app PRIVATE src/hid.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/event_manager.c)
|
||||||
target_sources(app PRIVATE src/events/position_state_changed.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/keycode_state_changed.c)
|
||||||
target_sources(app PRIVATE src/events/modifiers_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_key_press.c)
|
||||||
target_sources(app PRIVATE src/behaviors/behavior_reset.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_mod_tap.c)
|
||||||
|
|
|
@ -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";
|
status = "disabled";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
sensors {
|
||||||
|
compatible = "zmk,keymap-sensors";
|
||||||
|
sensors = <&left_encoder &right_encoder>;
|
||||||
|
};
|
||||||
|
|
||||||
// TODO: Encoder node(s)
|
// TODO: Encoder node(s)
|
||||||
// TODO: OLED node
|
// TODO: OLED node
|
||||||
// TODO: RGB node(s)
|
// TODO: RGB node(s)
|
||||||
|
|
|
@ -36,7 +36,7 @@ struct ec11_data {
|
||||||
struct device *dev;
|
struct device *dev;
|
||||||
|
|
||||||
sensor_trigger_handler_t handler;
|
sensor_trigger_handler_t handler;
|
||||||
struct sensor_trigger trigger;
|
const struct sensor_trigger *trigger;
|
||||||
|
|
||||||
#if defined(CONFIG_EC11_TRIGGER_OWN_THREAD)
|
#if defined(CONFIG_EC11_TRIGGER_OWN_THREAD)
|
||||||
K_THREAD_STACK_MEMBER(thread_stack, CONFIG_EC11_THREAD_STACK_SIZE);
|
K_THREAD_STACK_MEMBER(thread_stack, CONFIG_EC11_THREAD_STACK_SIZE);
|
||||||
|
|
|
@ -83,7 +83,7 @@ static void ec11_thread_cb(void *arg)
|
||||||
struct device *dev = arg;
|
struct device *dev = arg;
|
||||||
struct ec11_data *drv_data = dev->driver_data;
|
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);
|
setup_int(dev, true);
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ int ec11_trigger_set(struct device *dev,
|
||||||
|
|
||||||
k_msleep(5);
|
k_msleep(5);
|
||||||
|
|
||||||
drv_data->trigger = *trig;
|
drv_data->trigger = trig;
|
||||||
drv_data->handler = handler;
|
drv_data->handler = handler;
|
||||||
|
|
||||||
setup_int(dev, true);
|
setup_int(dev, true);
|
||||||
|
|
9
app/dts/bindings/zmk,keymap-sensors.yaml
Normal file
9
app/dts/bindings/zmk,keymap-sensors.yaml
Normal 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
|
|
@ -13,3 +13,6 @@ child-binding:
|
||||||
bindings:
|
bindings:
|
||||||
type: phandle-array
|
type: phandle-array
|
||||||
required: true
|
required: true
|
||||||
|
sensor-bindings:
|
||||||
|
type: phandle-array
|
||||||
|
required: false
|
||||||
|
|
19
app/include/zmk/events/sensor-event.h
Normal file
19
app/include/zmk/events/sensor-event.h
Normal 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 <drivers/sensor.h>
|
||||||
|
|
||||||
|
struct sensor_event {
|
||||||
|
struct zmk_event_header header;
|
||||||
|
u8_t sensor_number;
|
||||||
|
struct sensor_value value;
|
||||||
|
};
|
||||||
|
|
||||||
|
ZMK_EVENT_DECLARE(sensor_event);
|
11
app/include/zmk/sensors.h
Normal file
11
app/include/zmk/sensors.h
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
/*
|
||||||
|
* 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_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)
|
10
app/src/events/sensor_event.c
Normal file
10
app/src/events/sensor_event.c
Normal 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);
|
|
@ -4,6 +4,7 @@
|
||||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
#include <zmk/matrix.h>
|
#include <zmk/matrix.h>
|
||||||
|
#include <zmk/sensors.h>
|
||||||
#include <zmk/keymap.h>
|
#include <zmk/keymap.h>
|
||||||
#include <dt-bindings/zmk/matrix-transform.h>
|
#include <dt-bindings/zmk/matrix-transform.h>
|
||||||
#include <drivers/behavior.h>
|
#include <drivers/behavior.h>
|
||||||
|
@ -11,6 +12,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
#include <zmk/event-manager.h>
|
#include <zmk/event-manager.h>
|
||||||
#include <zmk/events/position-state-changed.h>
|
#include <zmk/events/position-state-changed.h>
|
||||||
|
#include <zmk/events/sensor-event.h>
|
||||||
|
|
||||||
static u32_t zmk_keymap_layer_state = 0;
|
static u32_t zmk_keymap_layer_state = 0;
|
||||||
static u8_t zmk_keymap_layer_default = 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
|
#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) \
|
#define SET_LAYER_STATE(layer, state) \
|
||||||
if (layer >= 32) \
|
if (layer >= 32) \
|
||||||
{ \
|
{ \
|
||||||
|
@ -125,10 +172,15 @@ int keymap_listener(const struct zmk_event_header *eh)
|
||||||
if (is_position_state_changed(eh)) {
|
if (is_position_state_changed(eh)) {
|
||||||
const struct position_state_changed *ev = cast_position_state_changed(eh);
|
const struct position_state_changed *ev = cast_position_state_changed(eh);
|
||||||
zmk_keymap_position_state_changed(ev->position, ev->state);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZMK_LISTENER(keymap, keymap_listener);
|
ZMK_LISTENER(keymap, keymap_listener);
|
||||||
ZMK_SUBSCRIPTION(keymap, position_state_changed);
|
ZMK_SUBSCRIPTION(keymap, position_state_changed);
|
||||||
|
ZMK_SUBSCRIPTION(keymap, sensor_event);
|
||||||
|
|
||||||
|
|
89
app/src/sensors.c
Normal file
89
app/src/sensors.c
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
/*
|
||||||
|
* 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>
|
||||||
|
|
||||||
|
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);
|
Loading…
Reference in a new issue