feature(split): behavior locality support.

* GATT characteristic allowing passng data + behavior
  label to invoke the behavior on the peripheral side.
* Behaviors have a locality setting to specify where they run.
* Build reset/power/RGB on peripheral.
This commit is contained in:
Pete Johanson
2020-12-20 09:49:42 -05:00
parent c74ae45e17
commit 47f873b038
17 changed files with 247 additions and 20 deletions

View File

@@ -4,7 +4,12 @@
* SPDX-License-Identifier: MIT
*/
#define IS_BLE_CENTRAL \
(IS_ENABLED(CONFIG_ZMK_SPLIT) && IS_ENABLED(CONFIG_ZMK_BLE) && \
IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL))
#include <sys/util.h>
#include <bluetooth/bluetooth.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
@@ -14,6 +19,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <drivers/behavior.h>
#include <zmk/behavior.h>
#if IS_BLE_CENTRAL
#include <zmk/split/bluetooth/central.h>
#endif
#include <zmk/event_manager.h>
#include <zmk/events/position_state_changed.h>
#include <zmk/events/layer_state_changed.h>
@@ -152,7 +161,17 @@ const char *zmk_keymap_layer_label(uint8_t layer) {
return zmk_keymap_layer_names[layer];
}
int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed, int64_t timestamp) {
int invoke_locally(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event,
bool pressed) {
if (pressed) {
return behavior_keymap_binding_pressed(binding, event);
} else {
return behavior_keymap_binding_released(binding, event);
}
}
int zmk_keymap_apply_position_state(zmk_position_state_changed_source_t source, int layer,
uint32_t position, bool pressed, int64_t timestamp) {
// We want to make a copy of this, since it may be converted from
// relative to absolute before being invoked
struct zmk_behavior_binding binding = zmk_keymap[layer][position];
@@ -169,7 +188,7 @@ int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed,
behavior = device_get_binding(binding.behavior_dev);
if (!behavior) {
LOG_DBG("No behavior assigned to %d on layer %d", position, layer);
LOG_WRN("No behavior assigned to %d on layer %d", position, layer);
return 1;
}
@@ -179,20 +198,44 @@ int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed,
return err;
}
if (pressed) {
return behavior_keymap_binding_pressed(&binding, event);
} else {
return behavior_keymap_binding_released(&binding, event);
enum behavior_locality locality = BEHAVIOR_LOCALITY_CENTRAL;
err = behavior_get_locality(behavior, &locality);
if (err) {
LOG_ERR("Failed to get behavior locality %d", err);
return err;
}
switch (locality) {
case BEHAVIOR_LOCALITY_CENTRAL:
return invoke_locally(&binding, event, pressed);
case BEHAVIOR_LOCALITY_EVENT_SOURCE:
#if IS_BLE_CENTRAL
if (!bt_addr_le_cmp(source, BT_ADDR_LE_NONE)) {
return invoke_locally(&binding, event, pressed);
} else {
return zmk_split_bt_invoke_behavior(source, &binding, event, pressed);
}
#else
return invoke_locally(&binding, event, pressed);
#endif
case BEHAVIOR_LOCALITY_GLOBAL:
#if IS_BLE_CENTRAL
zmk_split_bt_invoke_behavior(BT_ADDR_LE_ANY, &binding, event, pressed);
#endif
return invoke_locally(&binding, event, pressed);
}
return -ENOTSUP;
}
int zmk_keymap_position_state_changed(uint32_t position, bool pressed, int64_t timestamp) {
int zmk_keymap_position_state_changed(zmk_position_state_changed_source_t source, uint32_t position,
bool pressed, int64_t timestamp) {
if (pressed) {
zmk_keymap_active_behavior_layer[position] = _zmk_keymap_layer_state;
}
for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= _zmk_keymap_layer_default; layer--) {
if (zmk_keymap_layer_active_with_state(layer, zmk_keymap_active_behavior_layer[position])) {
int ret = zmk_keymap_apply_position_state(layer, position, pressed, timestamp);
int ret = zmk_keymap_apply_position_state(source, layer, position, pressed, timestamp);
if (ret > 0) {
LOG_DBG("behavior processing to continue to next layer");
continue;
@@ -249,7 +292,7 @@ int zmk_keymap_sensor_triggered(uint8_t sensor_number, const struct device *sens
int keymap_listener(const zmk_event_t *eh) {
const struct zmk_position_state_changed *pos_ev;
if ((pos_ev = as_zmk_position_state_changed(eh)) != NULL) {
return zmk_keymap_position_state_changed(pos_ev->position, pos_ev->state,
return zmk_keymap_position_state_changed(pos_ev->source, pos_ev->position, pos_ev->state,
pos_ev->timestamp);
}