diff --git a/app/include/drivers/behavior.h b/app/include/drivers/behavior.h index cca9272a..106f6793 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 9bff0a90..f881ac5c 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 98466770..d07f4885 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 cfbeae0f..d1135c09 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) {