fix(split): Use queue/work for peripheral events.

* Avoid corruption by using work to process
  peripheral key position events on the main
  work thread, like local kscan events are.
* FIxes #221
This commit is contained in:
Pete Johanson 2020-12-03 00:12:21 -05:00
parent 177b28f01d
commit bb9a4dd758

View file

@ -33,6 +33,29 @@ static struct bt_uuid_128 uuid = BT_UUID_INIT_128(ZMK_SPLIT_BT_SERVICE_UUID);
static struct bt_gatt_discover_params discover_params; static struct bt_gatt_discover_params discover_params;
static struct bt_gatt_subscribe_params subscribe_params; static struct bt_gatt_subscribe_params subscribe_params;
struct zmk_split_peripheral_event {
u32_t position;
u32_t state;
s32_t timestamp;
};
K_MSGQ_DEFINE(peripheral_event_msgq, sizeof(struct zmk_split_peripheral_event), 4, 4);
void peripheral_event_work_callback(struct k_work *work) {
struct zmk_split_peripheral_event ev;
while (k_msgq_get(&peripheral_event_msgq, &ev, K_NO_WAIT) == 0) {
struct position_state_changed *pos_ev = new_position_state_changed();
pos_ev->position = ev.position;
pos_ev->state = ev.state;
pos_ev->timestamp = ev.timestamp;
LOG_DBG("Trigger key position state change for %d", ev.position);
ZMK_EVENT_RAISE(pos_ev);
}
}
K_WORK_DEFINE(peripheral_event_work, peripheral_event_work_callback);
static u8_t split_central_notify_func(struct bt_conn *conn, struct bt_gatt_subscribe_params *params, static u8_t split_central_notify_func(struct bt_conn *conn, struct bt_gatt_subscribe_params *params,
const void *data, u16_t length) { const void *data, u16_t length) {
static u8_t position_state[POSITION_STATE_DATA_LEN]; static u8_t position_state[POSITION_STATE_DATA_LEN];
@ -57,13 +80,11 @@ static u8_t split_central_notify_func(struct bt_conn *conn, struct bt_gatt_subsc
if (changed_positions[i] & BIT(j)) { if (changed_positions[i] & BIT(j)) {
u32_t position = (i * 8) + j; u32_t position = (i * 8) + j;
bool pressed = position_state[i] & BIT(j); bool pressed = position_state[i] & BIT(j);
struct position_state_changed *pos_ev = new_position_state_changed(); struct zmk_split_peripheral_event ev = {
pos_ev->position = position; .position = position, .state = pressed, .timestamp = k_uptime_get()};
pos_ev->state = pressed;
pos_ev->timestamp = k_uptime_get();
LOG_DBG("Trigger key position state change for %d", position); k_msgq_put(&peripheral_event_msgq, &ev, K_NO_WAIT);
ZMK_EVENT_RAISE(pos_ev); k_work_submit(&peripheral_event_work);
} }
} }
} }