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

@@ -8,6 +8,8 @@
#include <zephyr/types.h>
#include <stddef.h>
#include <sys/util.h>
#include <string.h>
#include <device.h>
#include <zmk/keys.h>
#include <zmk/behavior.h>
@@ -26,7 +28,14 @@ typedef int (*behavior_sensor_keymap_binding_callback_t)(struct zmk_behavior_bin
const struct device *sensor,
int64_t timestamp);
enum behavior_locality {
BEHAVIOR_LOCALITY_CENTRAL,
BEHAVIOR_LOCALITY_EVENT_SOURCE,
BEHAVIOR_LOCALITY_GLOBAL
};
__subsystem struct behavior_driver_api {
enum behavior_locality locality;
behavior_keymap_binding_callback_t binding_convert_central_state_dependent_params;
behavior_keymap_binding_callback_t binding_pressed;
behavior_keymap_binding_callback_t binding_released;
@@ -60,6 +69,28 @@ static inline int z_impl_behavior_keymap_binding_convert_central_state_dependent
return api->binding_convert_central_state_dependent_params(binding, event);
}
/**
* @brief Determine where the behavior should be run
* @param behavior Pointer to the device structure for the driver instance.
*
* @retval Zero if successful.
* @retval Negative errno code if failure.
*/
__syscall int behavior_get_locality(const struct device *behavior,
enum behavior_locality *locality);
static inline int z_impl_behavior_get_locality(const struct device *behavior,
enum behavior_locality *locality) {
if (behavior == NULL) {
return -EINVAL;
}
const struct behavior_driver_api *api = (const struct behavior_driver_api *)behavior->api;
*locality = api->locality;
return 0;
}
/**
* @brief Handle the keymap binding being pressed
* @param dev Pointer to the device structure for the driver instance.
@@ -75,6 +106,11 @@ __syscall int behavior_keymap_binding_pressed(struct zmk_behavior_binding *bindi
static inline int z_impl_behavior_keymap_binding_pressed(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
const struct device *dev = device_get_binding(binding->behavior_dev);
if (dev == NULL) {
return -EINVAL;
}
const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->api;
if (api->binding_pressed == NULL) {
@@ -98,6 +134,11 @@ __syscall int behavior_keymap_binding_released(struct zmk_behavior_binding *bind
static inline int z_impl_behavior_keymap_binding_released(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
const struct device *dev = device_get_binding(binding->behavior_dev);
if (dev == NULL) {
return -EINVAL;
}
const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->api;
if (api->binding_released == NULL) {
@@ -125,6 +166,11 @@ static inline int
z_impl_behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *binding,
const struct device *sensor, int64_t timestamp) {
const struct device *dev = device_get_binding(binding->behavior_dev);
if (dev == NULL) {
return -EINVAL;
}
const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->api;
if (api->sensor_binding_triggered == NULL) {

View File

@@ -8,10 +8,19 @@
#include <zephyr.h>
#include <zmk/event_manager.h>
#include <bluetooth/addr.h>
#if IS_ENABLED(CONFIG_ZMK_BLE)
typedef const bt_addr_le_t *zmk_position_state_changed_source_t;
#else
typedef void *zmk_position_state_changed_source_t;
#endif
struct zmk_position_state_changed {
zmk_position_state_changed_source_t source;
uint32_t position;
bool state;
int64_t timestamp;
};
ZMK_EVENT_DECLARE(zmk_position_state_changed);
ZMK_EVENT_DECLARE(zmk_position_state_changed);

View File

@@ -6,6 +6,8 @@
#pragma once
#include <zmk/events/position_state_changed.h>
typedef uint32_t zmk_keymap_layers_state_t;
uint8_t zmk_keymap_layer_default();
@@ -18,7 +20,8 @@ int zmk_keymap_layer_toggle(uint8_t layer);
int zmk_keymap_layer_to(uint8_t layer);
const char *zmk_keymap_layer_label(uint8_t layer);
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);
#define ZMK_KEYMAP_EXTRACT_BINDING(idx, drv_inst) \
{ \

View File

@@ -0,0 +1,8 @@
#pragma once
#include <bluetooth/addr.h>
#include <zmk/behavior.h>
int zmk_split_bt_invoke_behavior(const bt_addr_le_t *source, struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event, bool state);

View File

@@ -6,5 +6,19 @@
#pragma once
#define ZMK_SPLIT_RUN_BEHAVIOR_DEV_LEN 9
struct zmk_split_run_behavior_data {
uint8_t position;
uint8_t state;
uint32_t param1;
uint32_t param2;
} __packed;
struct zmk_split_run_behavior_payload {
struct zmk_split_run_behavior_data data;
char behavior_dev[ZMK_SPLIT_RUN_BEHAVIOR_DEV_LEN];
} __packed;
int zmk_split_bt_position_pressed(uint8_t position);
int zmk_split_bt_position_released(uint8_t position);

View File

@@ -15,3 +15,4 @@
#define ZMK_BT_SPLIT_UUID(num) BT_UUID_128_ENCODE(num, 0x0096, 0x7107, 0xc967, 0xc5cfb1c2482a)
#define ZMK_SPLIT_BT_SERVICE_UUID ZMK_BT_SPLIT_UUID(0x00000000)
#define ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID ZMK_BT_SPLIT_UUID(0x00000001)
#define ZMK_SPLIT_BT_CHAR_RUN_BEHAVIOR_UUID ZMK_BT_SPLIT_UUID(0x00000002)