Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(split): wired split over serial support #2080

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions app/include/zmk/split/bluetooth/central.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <zmk/hid_indicators_types.h>
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)

int zmk_split_bt_invoke_behavior(uint8_t source, struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event, bool state);
int zmk_split_invoke_behavior(uint8_t source, struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event, bool state);

#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)

Expand Down
19 changes: 19 additions & 0 deletions app/include/zmk/split/central.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2023 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <zmk/events/position_state_changed.h>
#include <zmk/events/sensor_event.h>
#include <zmk/split/service.h>

void zmk_position_state_change_handle(struct zmk_position_state_changed *ev);

#if ZMK_KEYMAP_HAS_SENSORS
void zmk_sensor_event_handle(struct zmk_sensor_event *ev);
#endif

void send_split_run_impl(struct zmk_split_run_behavior_payload_wrapper *payload_wrapper);
28 changes: 28 additions & 0 deletions app/include/zmk/split/serial/serial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2023 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <zephyr/sys/ring_buffer.h>

// The serial protocol is defined for payloads of up to 254 bytes. This should
// be large enough to ensure that one message can be fully buffered.
#define SERIAL_BUF_SIZE 300

struct serial_device {
const struct device *dev;
uint8_t rx_buf[SERIAL_BUF_SIZE], tx_buf[SERIAL_BUF_SIZE];
struct ring_buf rx_rb, tx_rb;
struct k_work rx_work;

#ifdef CONFIG_ZMK_SPLIT_SERIAL_UART_POLL
bool poll;
struct k_work tx_work;
struct k_timer rx_timer;
#endif
};

void serial_handle_rx(uint32_t cmd, uint8_t *data, uint8_t len);
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <zmk/sensors.h>

#define ZMK_SPLIT_RUN_BEHAVIOR_DEV_LEN 9
#define ZMK_SPLIT_POS_STATE_LEN 16

struct sensor_event {
uint8_t sensor_index;
Expand All @@ -30,8 +31,18 @@ struct zmk_split_run_behavior_payload {
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);
int zmk_split_bt_sensor_triggered(uint8_t sensor_index,
const struct zmk_sensor_channel_data channel_data[],
size_t channel_data_size);
struct zmk_split_run_behavior_payload_wrapper {
uint8_t source;
struct zmk_split_run_behavior_payload payload;
};

int zmk_split_position_pressed(uint8_t position);
int zmk_split_position_released(uint8_t position);
int zmk_split_sensor_triggered(uint8_t sensor_index,
const struct zmk_sensor_channel_data channel_data[],
size_t channel_data_size);

void send_position_state_impl(uint8_t *state, int len);
#if ZMK_KEYMAP_HAS_SENSORS
void send_sensor_state_impl(struct sensor_event *event, int len);
#endif
4 changes: 2 additions & 2 deletions app/src/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,15 @@ int zmk_keymap_apply_position_state(uint8_t source, int layer, uint32_t position
if (source == ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL) {
return invoke_locally(&binding, event, pressed);
} else {
return zmk_split_bt_invoke_behavior(source, &binding, event, pressed);
return zmk_split_invoke_behavior(source, &binding, event, pressed);
}
#else
return invoke_locally(&binding, event, pressed);
#endif
case BEHAVIOR_LOCALITY_GLOBAL:
#if ZMK_BLE_IS_CENTRAL
for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) {
zmk_split_bt_invoke_behavior(i, &binding, event, pressed);
zmk_split_invoke_behavior(i, &binding, event, pressed);
}
#endif
return invoke_locally(&binding, event, pressed);
Expand Down
19 changes: 18 additions & 1 deletion app/src/split/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# Copyright (c) 2022 The ZMK Contributors
# SPDX-License-Identifier: MIT

if (NOT CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
if (CONFIG_ZMK_SPLIT_BLE OR CONFIG_ZMK_SPLIT_SERIAL)
target_sources(app PRIVATE listener.c)
target_sources(app PRIVATE service.c)
endif()
endif()

if (CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
if (CONFIG_ZMK_SPLIT_BLE OR CONFIG_ZMK_SPLIT_SERIAL)
target_sources(app PRIVATE central.c)
endif()
endif()

if (CONFIG_ZMK_SPLIT_BLE)
add_subdirectory(bluetooth)
endif()
endif()

if (CONFIG_ZMK_SPLIT_SERIAL)
add_subdirectory(serial)
endif()
47 changes: 44 additions & 3 deletions app/src/split/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,59 @@ menuconfig ZMK_SPLIT

if ZMK_SPLIT

config ZMK_SPLIT_INIT_PRIORITY
int "Split Init Priority"
default 50

config ZMK_SPLIT_ROLE_CENTRAL
bool "Split central device"

choice ZMK_SPLIT_TRANSPORT
prompt "Split transport"
if ZMK_SPLIT_ROLE_CENTRAL

config ZMK_SPLIT_CENTRAL_POSITION_QUEUE_SIZE
int "Max number of key position state events to queue when received from peripherals"
default 5

config ZMK_SPLIT_CENTRAL_SPLIT_RUN_STACK_SIZE
int "Split central write thread stack size"
default 512

config ZMK_SPLIT_CENTRAL_SPLIT_RUN_QUEUE_SIZE
int "Max number of behavior run events to queue to send to the peripheral(s)"
default 5

config ZMK_SPLIT_CENTRAL_PRIORITY
int "Split central thread priority"
default 5

endif # ZMK_SPLIT_ROLE_CENTRAL

if !ZMK_SPLIT_ROLE_CENTRAL

config ZMK_SPLIT_PERIPHERAL_STACK_SIZE
int "Split peripheral notify thread stack size"
default 756

config ZMK_SPLIT_PERIPHERAL_PRIORITY
int "Split peripheral notify thread priority"
default 5

config ZMK_SPLIT_PERIPHERAL_POSITION_QUEUE_SIZE
int "Max number of key position state events to queue to send to the central"
default 10

endif #!ZMK_SPLIT_ROLE_CENTRAL

config ZMK_SPLIT_BLE
bool "BLE"
default ZMK_SPLIT && ZMK_BLE
depends on ZMK_BLE
select BT_USER_PHY_UPDATE
select BT_AUTO_PHY_UPDATE

endchoice
config ZMK_SPLIT_SERIAL
bool "Serial"
select RING_BUFFER

config ZMK_SPLIT_PERIPHERAL_HID_INDICATORS
bool "Peripheral HID Indicators"
Expand All @@ -30,3 +70,4 @@ config ZMK_SPLIT_PERIPHERAL_HID_INDICATORS
endif

rsource "bluetooth/Kconfig"
rsource "serial/Kconfig"
4 changes: 2 additions & 2 deletions app/src/split/bluetooth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# SPDX-License-Identifier: MIT

if (NOT CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
target_sources(app PRIVATE split_listener.c)
target_sources(app PRIVATE service.c)
target_sources(app PRIVATE peripheral.c)
endif()

if (CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
target_sources(app PRIVATE central.c)
endif()

if (CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_PROXY)
target_sources(app PRIVATE central_bas_proxy.c)
endif()
endif()
24 changes: 0 additions & 24 deletions app/src/split/bluetooth/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,6 @@ config ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_PROXY

endif

config ZMK_SPLIT_BLE_CENTRAL_POSITION_QUEUE_SIZE
int "Max number of key position state events to queue when received from peripherals"
default 5

config ZMK_SPLIT_BLE_CENTRAL_SPLIT_RUN_STACK_SIZE
int "BLE split central write thread stack size"
default 512

config ZMK_SPLIT_BLE_CENTRAL_SPLIT_RUN_QUEUE_SIZE
int "Max number of behavior run events to queue to send to the peripheral(s)"
default 5

config ZMK_SPLIT_BLE_PREF_INT
int "Connection interval to use for split central/peripheral connection"
default 6
Expand All @@ -74,18 +62,6 @@ endif # ZMK_SPLIT_ROLE_CENTRAL

if !ZMK_SPLIT_ROLE_CENTRAL

config ZMK_SPLIT_BLE_PERIPHERAL_STACK_SIZE
int "BLE split peripheral notify thread stack size"
default 756

config ZMK_SPLIT_BLE_PERIPHERAL_PRIORITY
int "BLE split peripheral notify thread priority"
default 5

config ZMK_SPLIT_BLE_PERIPHERAL_POSITION_QUEUE_SIZE
int "Max number of key position state events to queue to send to the central"
default 10

config BT_MAX_PAIRED
default 1

Expand Down
Loading