refactor(sensors): Sensor event channel data, resolution tweaks.

* Refactor sensor events to include channel data,
  necessary for prop split encoders, and avoiding duplicate calls,
  to fetch channel data twice, etc.
* More consistent behavior driver API.
* Allow setting triggers per resolution at the behavior level optionally.
This commit is contained in:
Peter Johanson
2021-09-01 03:49:18 +00:00
committed by Pete Johanson
parent dcf5e75fa6
commit 2244bd3d81
12 changed files with 258 additions and 83 deletions

View File

@@ -12,6 +12,7 @@
#include <string.h>
#include <zephyr/device.h>
#include <zmk/keys.h>
#include <zmk/sensors.h>
#include <zmk/behavior.h>
/**
@@ -24,9 +25,10 @@
typedef int (*behavior_keymap_binding_callback_t)(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event);
typedef int (*behavior_sensor_keymap_binding_callback_t)(struct zmk_behavior_binding *binding,
const struct device *sensor,
struct zmk_behavior_binding_event event);
typedef int (*behavior_sensor_keymap_binding_callback_t)(
struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event,
const struct zmk_sensor_config *sensor_config, size_t channel_data_size,
const struct zmk_sensor_channel_data channel_data[channel_data_size]);
enum behavior_locality {
BEHAVIOR_LOCALITY_CENTRAL,
@@ -158,14 +160,15 @@ static inline int z_impl_behavior_keymap_binding_released(struct zmk_behavior_bi
* @retval 0 If successful.
* @retval Negative errno code if failure.
*/
__syscall int behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *binding,
const struct device *sensor,
struct zmk_behavior_binding_event event);
__syscall int behavior_sensor_keymap_binding_triggered(
struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event,
const struct zmk_sensor_config *sensor_config, size_t channel_data_size,
const struct zmk_sensor_channel_data *channel_data);
static inline int
z_impl_behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *binding,
const struct device *sensor,
struct zmk_behavior_binding_event event) {
static inline int z_impl_behavior_sensor_keymap_binding_triggered(
struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event,
const struct zmk_sensor_config *sensor_config, size_t channel_data_size,
const struct zmk_sensor_channel_data *channel_data) {
const struct device *dev = device_get_binding(binding->behavior_dev);
if (dev == NULL) {
@@ -178,7 +181,8 @@ z_impl_behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *bin
return -ENOTSUP;
}
return api->sensor_binding_triggered(binding, sensor, event);
return api->sensor_binding_triggered(binding, event, sensor_config, channel_data_size,
channel_data);
}
/**

View File

@@ -6,12 +6,21 @@
#pragma once
#include <zephyr/kernel.h>
#include <zephyr/drivers/sensor.h>
#include <zmk/event_manager.h>
#include <zephyr/device.h>
#include <zmk/sensors.h>
#include <device.h>
// TODO: Move to Kconfig when we need more than one channel
#define ZMK_SENSOR_EVENT_MAX_CHANNELS 1
struct zmk_sensor_event {
uint8_t sensor_number;
const struct device *sensor;
uint8_t sensor_position;
size_t channel_data_size;
struct zmk_sensor_channel_data channel_data[ZMK_SENSOR_EVENT_MAX_CHANNELS];
int64_t timestamp;
};

View File

@@ -6,6 +6,9 @@
#pragma once
#include <drivers/sensor.h>
#define _SENSOR_CHILD_LEN(node) 1 +
#define ZMK_KEYMAP_SENSORS_NODE DT_INST(0, zmk_keymap_sensors)
#define ZMK_KEYMAP_HAS_SENSORS DT_NODE_HAS_STATUS(ZMK_KEYMAP_SENSORS_NODE, okay)
#define ZMK_KEYMAP_SENSORS_BY_IDX(idx) DT_PHANDLE_BY_IDX(ZMK_KEYMAP_SENSORS_NODE, sensors, idx)
@@ -15,3 +18,14 @@
#else
#define ZMK_KEYMAP_SENSORS_LEN 0
#endif
const struct zmk_sensor_config *zmk_sensors_get_config_at_position(uint8_t sensor_position);
struct zmk_sensor_config {
uint16_t triggers_per_rotation;
};
struct zmk_sensor_channel_data {
enum sensor_channel channel;
struct sensor_value value;
};

View File

@@ -14,6 +14,11 @@
*/
#define ZMK_VIRTUAL_KEY_POSITION_SENSOR(index) (ZMK_KEYMAP_LEN + (index))
/**
* Gets the sensor number from the virtual key position.
*/
#define ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(vkp) ((vkp)-ZMK_KEYMAP_LEN)
/**
* Gets the virtual key position to use for the combo with the given index.
*/