Skip to content

Commit

Permalink
refactor(split): move central key handling out of Bluetooth directory
Browse files Browse the repository at this point in the history
  • Loading branch information
xudongzheng committed Dec 11, 2024
1 parent 6411543 commit d9bed67
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
11 changes: 11 additions & 0 deletions app/include/zmk/split/central.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (c) 2023 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <zmk/events/position_state_changed.h>

void zmk_position_state_change_handle(struct zmk_position_state_changed *ev);
4 changes: 4 additions & 0 deletions app/src/split/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ if (CONFIG_ZMK_SPLIT_BLE AND (NOT CONFIG_ZMK_SPLIT_ROLE_CENTRAL))
target_sources(app PRIVATE service.c)
endif()

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

if (CONFIG_ZMK_SPLIT_BLE)
add_subdirectory(bluetooth)
endif()
22 changes: 3 additions & 19 deletions app/src/split/bluetooth/central.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/behavior.h>
#include <zmk/sensors.h>
#include <zmk/split/bluetooth/uuid.h>
#include <zmk/split/central.h>
#include <zmk/split/service.h>
#include <zmk/event_manager.h>
#include <zmk/events/position_state_changed.h>
Expand Down Expand Up @@ -134,19 +135,6 @@ static bool is_scanning = false;

static const struct bt_uuid_128 split_service_uuid = BT_UUID_INIT_128(ZMK_SPLIT_BT_SERVICE_UUID);

K_MSGQ_DEFINE(peripheral_event_msgq, sizeof(struct zmk_position_state_changed),
CONFIG_ZMK_SPLIT_BLE_CENTRAL_POSITION_QUEUE_SIZE, 4);

void peripheral_event_work_callback(struct k_work *work) {
struct zmk_position_state_changed ev;
while (k_msgq_get(&peripheral_event_msgq, &ev, K_NO_WAIT) == 0) {
LOG_DBG("Trigger key position state change for %d", ev.position);
raise_zmk_position_state_changed(ev);
}
}

K_WORK_DEFINE(peripheral_event_work, peripheral_event_work_callback);

int peripheral_slot_index_for_conn(struct bt_conn *conn) {
for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) {
if (peripherals[i].conn == conn) {
Expand Down Expand Up @@ -194,9 +182,7 @@ int release_peripheral_slot(int index) {
.position = position,
.state = false,
.timestamp = k_uptime_get()};

k_msgq_put(&peripheral_event_msgq, &ev, K_NO_WAIT);
k_work_submit(&peripheral_event_work);
zmk_position_state_change_handle(&ev);
}
}
}
Expand Down Expand Up @@ -389,9 +375,7 @@ static uint8_t split_central_notify_func(struct bt_conn *conn,
.position = position,
.state = pressed,
.timestamp = k_uptime_get()};

k_msgq_put(&peripheral_event_msgq, &ev, K_NO_WAIT);
k_work_submit(&peripheral_event_work);
zmk_position_state_change_handle(&ev);
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions app/src/split/central.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#include <zephyr/types.h>
#include <zephyr/init.h>

#include <zmk/event_manager.h>
#include <zmk/events/position_state_changed.h>
#include <zmk/events/sensor_event.h>

#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

K_MSGQ_DEFINE(peripheral_event_msgq, sizeof(struct zmk_position_state_changed),
CONFIG_ZMK_SPLIT_BLE_CENTRAL_POSITION_QUEUE_SIZE, 4);

void peripheral_event_work_callback(struct k_work *work) {
struct zmk_position_state_changed ev;
while (k_msgq_get(&peripheral_event_msgq, &ev, K_NO_WAIT) == 0) {
LOG_DBG("Trigger key position state change for %d", ev.position);
raise_zmk_position_state_changed(ev);
}
}

K_WORK_DEFINE(peripheral_event_work, peripheral_event_work_callback);

void zmk_position_state_change_handle(struct zmk_position_state_changed *ev) {
k_msgq_put(&peripheral_event_msgq, ev, K_NO_WAIT);
k_work_submit(&peripheral_event_work);
}

0 comments on commit d9bed67

Please sign in to comment.