valdur's mod

This commit is contained in:
darknao 2024-05-03 14:44:08 +02:00 committed by afiqzudinhadi
parent e1fc10651c
commit 01d0749411
9 changed files with 244 additions and 3 deletions

View file

@ -4,6 +4,8 @@
if (NOT CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
target_sources(app PRIVATE service.c)
target_sources(app PRIVATE peripheral.c)
target_sources(app PRIVATE peripheral_layers.c)
endif()
if (CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
target_sources(app PRIVATE central.c)

View file

@ -60,6 +60,8 @@ struct peripheral_slot {
uint16_t update_hid_indicators;
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
uint16_t selected_physical_layout_handle;
uint16_t update_layers_handle;
uint8_t position_state[POSITION_STATE_DATA_LEN];
uint8_t changed_positions[POSITION_STATE_DATA_LEN];
};
@ -219,6 +221,7 @@ int release_peripheral_slot(int index) {
#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
slot->update_hid_indicators = 0;
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
slot->update_layers_handle = 0;
return 0;
}
@ -620,6 +623,10 @@ static uint8_t split_central_chrc_discovery_func(struct bt_conn *conn,
LOG_DBG("Found update HID indicators handle");
slot->update_hid_indicators = bt_gatt_attr_value_handle(attr);
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
} else if (!bt_uuid_cmp(((struct bt_gatt_chrc *)attr->user_data)->uuid,
BT_UUID_DECLARE_128(ZMK_SPLIT_BT_UPDATE_LAYERS_UUID))) {
LOG_DBG("Found update Layers handle");
slot->update_layers_handle = bt_gatt_attr_value_handle(attr);
#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING)
} else if (!bt_uuid_cmp(((struct bt_gatt_chrc *)attr->user_data)->uuid,
BT_UUID_BAS_BATTERY_LEVEL)) {
@ -707,6 +714,8 @@ static uint8_t split_central_chrc_discovery_func(struct bt_conn *conn,
}
#endif // IS_ENABLED(CONFIG_ZMK_INPUT_SPLIT)
subscribed = subscribed && slot->update_layers_handle;
return subscribed ? BT_GATT_ITER_STOP : BT_GATT_ITER_CONTINUE;
}
@ -1145,6 +1154,40 @@ static struct settings_handler ble_central_settings_handler = {
#endif // IS_ENABLED(CONFIG_SETTINGS)
static uint32_t layers_for_peripheral = 0;
static void split_central_update_layers_callback(struct k_work *work) {
uint32_t layers = layers_for_peripheral;
for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) {
if (peripherals[i].state != PERIPHERAL_SLOT_STATE_CONNECTED) {
continue;
}
if (peripherals[i].update_layers_handle == 0) {
continue;
}
int err =
bt_gatt_write_without_response(peripherals[i].conn, peripherals[i].update_layers_handle,
&layers, sizeof(layers), true);
if (err) {
LOG_ERR("Failed to send layers to peripheral (err %d)", err);
} else {
LOG_DBG("Sent Layers over to peripheral");
}
}
}
static K_WORK_DEFINE(split_central_update_layers, split_central_update_layers_callback);
int zmk_split_central_update_layers(uint32_t new_layers) {
layers_for_peripheral = new_layers;
return k_work_submit_to_queue(&split_central_split_run_q, &split_central_update_layers);
}
// valdur layers done
static int zmk_split_bt_central_init(void) {
k_work_queue_start(&split_central_split_run_q, split_central_split_run_q_stack,
K_THREAD_STACK_SIZEOF(split_central_split_run_q_stack),

View file

@ -0,0 +1,13 @@
#include <zephyr/types.h>
#include <zephyr/sys/util.h>
#include <zmk/split/bluetooth/peripheral_layers.h>
static uint32_t peripheral_layers = 0;
void set_peripheral_layers_state(uint32_t new_layers) { peripheral_layers = new_layers; }
bool peripheral_layer_active(uint8_t layer) {
return (peripheral_layers & (BIT(layer))) == (BIT(layer));
};

View file

@ -31,6 +31,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
#include <zmk/events/hid_indicators_changed.h>
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
#include <zmk/split/bluetooth/peripheral_layers.h>
#include <zmk/events/sensor_event.h>
#include <zmk/sensors.h>
@ -139,6 +140,29 @@ static ssize_t split_svc_get_selected_phys_layout(struct bt_conn *conn,
return bt_gatt_attr_read(conn, attrs, buf, len, offset, &selected, sizeof(selected));
}
static uint32_t layers = 0;
static void split_svc_update_layers_callback(struct k_work *work) {
LOG_DBG("Setting peripheral layers: %x", layers);
set_peripheral_layers_state(layers);
}
static K_WORK_DEFINE(split_svc_update_layers_work, split_svc_update_layers_callback);
static ssize_t split_svc_update_layers(struct bt_conn *conn, const struct bt_gatt_attr *attr,
const void *buf, uint16_t len, uint16_t offset,
uint8_t flags) {
if (offset + len > sizeof(uint32_t)) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
memcpy((uint8_t *)&layers + offset, buf, len);
k_work_submit(&split_svc_update_layers_work);
return len;
}
#if IS_ENABLED(CONFIG_ZMK_INPUT_SPLIT)
static void split_input_events_ccc(const struct bt_gatt_attr *attr, uint16_t value) {
@ -204,8 +228,11 @@ BT_GATT_SERVICE_DEFINE(
BT_GATT_CHARACTERISTIC(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SELECT_PHYS_LAYOUT_UUID),
BT_GATT_CHRC_WRITE | BT_GATT_CHRC_READ,
BT_GATT_PERM_WRITE_ENCRYPT | BT_GATT_PERM_READ_ENCRYPT,
split_svc_get_selected_phys_layout, split_svc_select_phys_layout,
NULL), );
split_svc_get_selected_phys_layout, split_svc_select_phys_layout, NULL),
BT_GATT_CHARACTERISTIC(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_UPDATE_LAYERS_UUID),
BT_GATT_CHRC_WRITE_WITHOUT_RESP, BT_GATT_PERM_WRITE_ENCRYPT, NULL,
split_svc_update_layers, NULL), );
K_THREAD_STACK_DEFINE(service_q_stack, CONFIG_ZMK_SPLIT_BLE_PERIPHERAL_STACK_SIZE);