Skip to content

Commit

Permalink
More sensor keymap work.
Browse files Browse the repository at this point in the history
  • Loading branch information
petejohanson committed Jul 22, 2020
1 parent adfa1b5 commit 4d73938
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 154 deletions.
147 changes: 10 additions & 137 deletions app/include/drivers/behavior.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
#include <device.h>
#include <zmk/keys.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @cond INTERNAL_HIDDEN
*
Expand All @@ -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
Expand Down Expand Up @@ -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

/**
* @}
Expand Down
4 changes: 2 additions & 2 deletions app/include/zmk/events/sensor-event.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

#include <zephyr.h>
#include <zmk/event-manager.h>
#include <drivers/sensor.h>
#include <device.h>

struct sensor_event {
struct zmk_event_header header;
u8_t sensor_number;
struct sensor_value value;
struct device *sensor;
};

ZMK_EVENT_DECLARE(sensor_event);
44 changes: 40 additions & 4 deletions app/src/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
14 changes: 3 additions & 11 deletions app/src/sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;

Expand All @@ -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);
}
Expand All @@ -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)
{
Expand Down

0 comments on commit 4d73938

Please sign in to comment.