Skip to content

Commit f55c2c1

Browse files
committed
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.
1 parent ebf9011 commit f55c2c1

File tree

16 files changed

+241
-20
lines changed

16 files changed

+241
-20
lines changed

app/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ target_sources(app PRIVATE src/events/sensor_event.c)
3939
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/events/ble_active_profile_changed.c)
4040
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/events/battery_state_changed.c)
4141
target_sources_ifdef(CONFIG_USB app PRIVATE src/events/usb_conn_state_changed.c)
42+
target_sources(app PRIVATE src/behaviors/behavior_reset.c)
43+
target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/behaviors/behavior_ext_power.c)
4244
if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL)
4345
target_sources(app PRIVATE src/behaviors/behavior_key_press.c)
44-
target_sources(app PRIVATE src/behaviors/behavior_reset.c)
4546
target_sources(app PRIVATE src/behaviors/behavior_hold_tap.c)
4647
target_sources(app PRIVATE src/behaviors/behavior_sticky_key.c)
4748
target_sources(app PRIVATE src/behaviors/behavior_momentary_layer.c)
@@ -51,7 +52,6 @@ if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL)
5152
target_sources(app PRIVATE src/behaviors/behavior_transparent.c)
5253
target_sources(app PRIVATE src/behaviors/behavior_none.c)
5354
target_sources(app PRIVATE src/behaviors/behavior_sensor_rotate_key_press.c)
54-
target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/behaviors/behavior_ext_power.c)
5555
target_sources(app PRIVATE src/combo.c)
5656
target_sources(app PRIVATE src/keymap.c)
5757
endif()

app/dts/behaviors/reset.dtsi

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
bootloader: behavior_reset_dfu {
1818
compatible = "zmk,behavior-reset";
19-
label = "BOOTLOADER_RESET";
19+
label = "BOOTLOAD";
2020
type = <RST_UF2>;
2121
#binding-cells = <0>;
2222
};

app/dts/behaviors/rgb_underglow.dtsi

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
behaviors {
99
rgb_ug: behavior_rgb_underglow {
1010
compatible = "zmk,behavior-rgb-underglow";
11-
label = "RGB_UNDERGLOW";
11+
label = "RGB_UG";
1212
#binding-cells = <2>;
1313
};
1414
};

app/include/drivers/behavior.h

+46
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <zephyr/types.h>
1010
#include <stddef.h>
11+
#include <sys/util.h>
12+
#include <string.h>
1113
#include <device.h>
1214
#include <zmk/keys.h>
1315
#include <zmk/behavior.h>
@@ -26,7 +28,14 @@ typedef int (*behavior_sensor_keymap_binding_callback_t)(struct zmk_behavior_bin
2628
const struct device *sensor,
2729
int64_t timestamp);
2830

31+
enum behavior_locality {
32+
BEHAVIOR_LOCALITY_CENTRAL,
33+
BEHAVIOR_LOCALITY_EVENT_SOURCE,
34+
BEHAVIOR_LOCALITY_GLOBAL
35+
};
36+
2937
__subsystem struct behavior_driver_api {
38+
enum behavior_locality locality;
3039
behavior_keymap_binding_callback_t binding_to_absolute;
3140
behavior_keymap_binding_callback_t binding_pressed;
3241
behavior_keymap_binding_callback_t binding_released;
@@ -61,6 +70,28 @@ z_impl_behavior_keymap_binding_to_absolute(struct zmk_behavior_binding *binding,
6170
return api->binding_to_absolute(binding, event);
6271
}
6372

73+
/**
74+
* @brief Determine where the behavior should be run
75+
* @param behavior Pointer to the device structure for the driver instance.
76+
*
77+
* @retval Zero if successful.
78+
* @retval Negative errno code if failure.
79+
*/
80+
__syscall int behavior_get_locality(const struct device *behavior,
81+
enum behavior_locality *locality);
82+
83+
static inline int z_impl_behavior_get_locality(const struct device *behavior,
84+
enum behavior_locality *locality) {
85+
if (behavior == NULL) {
86+
return -EINVAL;
87+
}
88+
89+
const struct behavior_driver_api *api = (const struct behavior_driver_api *)behavior->api;
90+
*locality = api->locality;
91+
92+
return 0;
93+
}
94+
6495
/**
6596
* @brief Handle the keymap binding being pressed
6697
* @param dev Pointer to the device structure for the driver instance.
@@ -76,6 +107,11 @@ __syscall int behavior_keymap_binding_pressed(struct zmk_behavior_binding *bindi
76107
static inline int z_impl_behavior_keymap_binding_pressed(struct zmk_behavior_binding *binding,
77108
struct zmk_behavior_binding_event event) {
78109
const struct device *dev = device_get_binding(binding->behavior_dev);
110+
111+
if (dev == NULL) {
112+
return -EINVAL;
113+
}
114+
79115
const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->api;
80116

81117
if (api->binding_pressed == NULL) {
@@ -99,6 +135,11 @@ __syscall int behavior_keymap_binding_released(struct zmk_behavior_binding *bind
99135
static inline int z_impl_behavior_keymap_binding_released(struct zmk_behavior_binding *binding,
100136
struct zmk_behavior_binding_event event) {
101137
const struct device *dev = device_get_binding(binding->behavior_dev);
138+
139+
if (dev == NULL) {
140+
return -EINVAL;
141+
}
142+
102143
const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->api;
103144

104145
if (api->binding_released == NULL) {
@@ -126,6 +167,11 @@ static inline int
126167
z_impl_behavior_sensor_keymap_binding_triggered(struct zmk_behavior_binding *binding,
127168
const struct device *sensor, int64_t timestamp) {
128169
const struct device *dev = device_get_binding(binding->behavior_dev);
170+
171+
if (dev == NULL) {
172+
return -EINVAL;
173+
}
174+
129175
const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->api;
130176

131177
if (api->sensor_binding_triggered == NULL) {

app/include/zmk/events/position_state_changed.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@
88

99
#include <zephyr.h>
1010
#include <zmk/event_manager.h>
11+
#include <bluetooth/addr.h>
12+
13+
#if IS_ENABLED(CONFIG_ZMK_BLE)
14+
typedef const bt_addr_le_t *zmk_position_state_changed_source_t;
15+
#else
16+
typedef void *zmk_position_state_changed_source_t;
17+
#endif
18+
1119
struct zmk_position_state_changed {
20+
zmk_position_state_changed_source_t source;
1221
uint32_t position;
1322
bool state;
1423
int64_t timestamp;
1524
};
1625

17-
ZMK_EVENT_DECLARE(zmk_position_state_changed);
26+
ZMK_EVENT_DECLARE(zmk_position_state_changed);

app/include/zmk/keymap.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#pragma once
88

9+
#include <zmk/events/position_state_changed.h>
10+
911
typedef uint32_t zmk_keymap_layers_state_t;
1012

1113
uint8_t zmk_keymap_layer_default();
@@ -18,4 +20,5 @@ int zmk_keymap_layer_toggle(uint8_t layer);
1820
int zmk_keymap_layer_to(uint8_t layer);
1921
const char *zmk_keymap_layer_label(uint8_t layer);
2022

21-
int zmk_keymap_position_state_changed(uint32_t position, bool pressed, int64_t timestamp);
23+
int zmk_keymap_position_state_changed(zmk_position_state_changed_source_t source, uint32_t position,
24+
bool pressed, int64_t timestamp);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
#pragma once
3+
4+
#include <bluetooth/addr.h>
5+
#include <zmk/behavior.h>
6+
7+
int zmk_split_bt_invoke_behavior(const bt_addr_le_t *source, struct zmk_behavior_binding *binding,
8+
struct zmk_behavior_binding_event event, bool state);

app/include/zmk/split/bluetooth/service.h

+14
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,19 @@
66

77
#pragma once
88

9+
#define ZMK_SPLIT_RUN_BEHAVIOR_DEV_LEN 9
10+
11+
struct zmk_split_run_behavior_data {
12+
uint8_t position;
13+
uint8_t state;
14+
uint32_t param1;
15+
uint32_t param2;
16+
} __packed;
17+
18+
struct zmk_split_run_behavior_payload {
19+
struct zmk_split_run_behavior_data data;
20+
char behavior_dev[ZMK_SPLIT_RUN_BEHAVIOR_DEV_LEN];
21+
} __packed;
22+
923
int zmk_split_bt_position_pressed(uint8_t position);
1024
int zmk_split_bt_position_released(uint8_t position);

app/include/zmk/split/bluetooth/uuid.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
#define ZMK_BT_SPLIT_UUID(num) BT_UUID_128_ENCODE(num, 0x0096, 0x7107, 0xc967, 0xc5cfb1c2482a)
1616
#define ZMK_SPLIT_BT_SERVICE_UUID ZMK_BT_SPLIT_UUID(0x00000000)
1717
#define ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID ZMK_BT_SPLIT_UUID(0x00000001)
18+
#define ZMK_SPLIT_BT_CHAR_RUN_BEHAVIOR_UUID ZMK_BT_SPLIT_UUID(0x00000002)

app/src/behaviors/behavior_ext_power.c

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static const struct behavior_driver_api behavior_ext_power_driver_api = {
6767
.binding_to_absolute = on_keymap_binding_to_absolute,
6868
.binding_pressed = on_keymap_binding_pressed,
6969
.binding_released = on_keymap_binding_released,
70+
.locality = BEHAVIOR_LOCALITY_GLOBAL,
7071
};
7172

7273
DEVICE_AND_API_INIT(behavior_ext_power, DT_INST_LABEL(0), behavior_ext_power_init, NULL, NULL,

app/src/behaviors/behavior_reset.c

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
3535

3636
static const struct behavior_driver_api behavior_reset_driver_api = {
3737
.binding_pressed = on_keymap_binding_pressed,
38+
.locality = BEHAVIOR_LOCALITY_EVENT_SOURCE,
3839
};
3940

4041
#define RST_INST(n) \

app/src/behaviors/behavior_rgb_underglow.c

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ static const struct behavior_driver_api behavior_rgb_underglow_driver_api = {
8484
.binding_to_absolute = on_keymap_binding_to_absolute,
8585
.binding_pressed = on_keymap_binding_pressed,
8686
.binding_released = on_keymap_binding_released,
87+
.locality = BEHAVIOR_LOCALITY_GLOBAL,
8788
};
8889

8990
DEVICE_AND_API_INIT(behavior_rgb_underglow, DT_INST_LABEL(0), behavior_rgb_underglow_init, NULL,

app/src/keymap.c

+52-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7+
#define IS_BLE_CENTRAL \
8+
(IS_ENABLED(CONFIG_ZMK_SPLIT) && IS_ENABLED(CONFIG_ZMK_BLE) && \
9+
IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL))
10+
711
#include <sys/util.h>
12+
#include <bluetooth/bluetooth.h>
813
#include <logging/log.h>
914
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
1015

@@ -14,6 +19,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
1419
#include <drivers/behavior.h>
1520
#include <zmk/behavior.h>
1621

22+
#if IS_BLE_CENTRAL
23+
#include <zmk/split/bluetooth/central.h>
24+
#endif
25+
1726
#include <zmk/event_manager.h>
1827
#include <zmk/events/position_state_changed.h>
1928
#include <zmk/events/layer_state_changed.h>
@@ -160,7 +169,17 @@ const char *zmk_keymap_layer_label(uint8_t layer) {
160169
return zmk_keymap_layer_names[layer];
161170
}
162171

163-
int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed, int64_t timestamp) {
172+
int invoke_locally(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event,
173+
bool pressed) {
174+
if (pressed) {
175+
return behavior_keymap_binding_pressed(binding, event);
176+
} else {
177+
return behavior_keymap_binding_released(binding, event);
178+
}
179+
}
180+
181+
int zmk_keymap_apply_position_state(zmk_position_state_changed_source_t source, int layer,
182+
uint32_t position, bool pressed, int64_t timestamp) {
164183
// We want to make a copy of this, since it may be converted from
165184
// relative to absolute before being invoked
166185
struct zmk_behavior_binding binding = zmk_keymap[layer][position];
@@ -177,7 +196,7 @@ int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed,
177196
behavior = device_get_binding(binding.behavior_dev);
178197

179198
if (!behavior) {
180-
LOG_DBG("No behavior assigned to %d on layer %d", position, layer);
199+
LOG_WRN("No behavior assigned to %d on layer %d", position, layer);
181200
return 1;
182201
}
183202

@@ -187,20 +206,44 @@ int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed,
187206
return err;
188207
}
189208

190-
if (pressed) {
191-
return behavior_keymap_binding_pressed(&binding, event);
192-
} else {
193-
return behavior_keymap_binding_released(&binding, event);
209+
enum behavior_locality locality = BEHAVIOR_LOCALITY_CENTRAL;
210+
err = behavior_get_locality(behavior, &locality);
211+
if (err) {
212+
LOG_ERR("Failed to get behavior locality %d", err);
213+
return err;
214+
}
215+
216+
switch (locality) {
217+
case BEHAVIOR_LOCALITY_CENTRAL:
218+
return invoke_locally(&binding, event, pressed);
219+
case BEHAVIOR_LOCALITY_EVENT_SOURCE:
220+
#if IS_BLE_CENTRAL
221+
if (!bt_addr_le_cmp(source, BT_ADDR_LE_NONE)) {
222+
return invoke_locally(&binding, event, pressed);
223+
} else {
224+
return zmk_split_bt_invoke_behavior(source, &binding, event, pressed);
225+
}
226+
#else
227+
return invoke_locally(&binding, event, pressed);
228+
#endif
229+
case BEHAVIOR_LOCALITY_GLOBAL:
230+
#if IS_BLE_CENTRAL
231+
zmk_split_bt_invoke_behavior(BT_ADDR_LE_ANY, &binding, event, pressed);
232+
#endif
233+
return invoke_locally(&binding, event, pressed);
194234
}
235+
236+
return -ENOTSUP;
195237
}
196238

197-
int zmk_keymap_position_state_changed(uint32_t position, bool pressed, int64_t timestamp) {
239+
int zmk_keymap_position_state_changed(zmk_position_state_changed_source_t source, uint32_t position,
240+
bool pressed, int64_t timestamp) {
198241
if (pressed) {
199242
zmk_keymap_active_behavior_layer[position] = _zmk_keymap_layer_state;
200243
}
201244
for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= _zmk_keymap_layer_default; layer--) {
202245
if (zmk_keymap_layer_active_with_state(layer, zmk_keymap_active_behavior_layer[position])) {
203-
int ret = zmk_keymap_apply_position_state(layer, position, pressed, timestamp);
246+
int ret = zmk_keymap_apply_position_state(source, layer, position, pressed, timestamp);
204247
if (ret > 0) {
205248
LOG_DBG("behavior processing to continue to next layer");
206249
continue;
@@ -257,7 +300,7 @@ int zmk_keymap_sensor_triggered(uint8_t sensor_number, const struct device *sens
257300
int keymap_listener(const zmk_event_t *eh) {
258301
const struct zmk_position_state_changed *pos_ev;
259302
if ((pos_ev = as_zmk_position_state_changed(eh)) != NULL) {
260-
return zmk_keymap_position_state_changed(pos_ev->position, pos_ev->state,
303+
return zmk_keymap_position_state_changed(pos_ev->source, pos_ev->position, pos_ev->state,
261304
pos_ev->timestamp);
262305
}
263306

app/src/kscan.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <zephyr.h>
88
#include <device.h>
9+
#include <bluetooth/addr.h>
910
#include <drivers/kscan.h>
1011
#include <logging/log.h>
1112

@@ -49,8 +50,12 @@ void zmk_kscan_process_msgq(struct k_work *item) {
4950
uint32_t position = zmk_matrix_transform_row_column_to_position(ev.row, ev.column);
5051
LOG_DBG("Row: %d, col: %d, position: %d, pressed: %s\n", ev.row, ev.column, position,
5152
(pressed ? "true" : "false"));
52-
ZMK_EVENT_RAISE(new_zmk_position_state_changed((struct zmk_position_state_changed){
53-
.state = pressed, .position = position, .timestamp = k_uptime_get()}));
53+
ZMK_EVENT_RAISE(new_zmk_position_state_changed((struct zmk_position_state_changed) {
54+
#if IS_ENABLED(CONFIG_ZMK_BLE)
55+
.source = BT_ADDR_LE_NONE,
56+
#endif
57+
.state = pressed, .position = position, .timestamp = k_uptime_get()
58+
}));
5459
}
5560
}
5661

0 commit comments

Comments
 (0)