refactor(core): Extra position state change data struct.

* Separate header and data struct for the event.
* Remove duplicate struct in split code.
This commit is contained in:
Pete Johanson 2021-01-17 16:36:01 -05:00
parent 95acbd8859
commit 003db892ad
6 changed files with 31 additions and 34 deletions

View File

@ -9,11 +9,15 @@
#include <zephyr.h>
#include <zmk/event_manager.h>
struct position_state_changed {
struct zmk_event_header header;
struct zmk_position_state_changed_data {
uint32_t position;
bool state;
int64_t timestamp;
};
struct position_state_changed {
struct zmk_event_header header;
struct zmk_position_state_changed_data data;
};
ZMK_EVENT_DECLARE(position_state_changed);

View File

@ -92,7 +92,7 @@ static struct position_state_changed *find_captured_keydown_event(uint32_t posit
continue;
}
struct position_state_changed *position_event = cast_position_state_changed(eh);
if (position_event->position == position && position_event->state) {
if (position_event->data.position == position && position_event->data.state) {
last_match = position_event;
}
}
@ -143,8 +143,9 @@ static void release_captured_events() {
if (is_position_state_changed(captured_event)) {
struct position_state_changed *position_event =
cast_position_state_changed(captured_event);
LOG_DBG("Releasing key position event for position %d %s", position_event->position,
(position_event->state ? "pressed" : "released"));
LOG_DBG("Releasing key position event for position %d %s",
position_event->data.position,
(position_event->data.state ? "pressed" : "released"));
} else {
struct keycode_state_changed *modifier_event =
cast_keycode_state_changed(captured_event);
@ -391,12 +392,12 @@ static int position_state_changed_listener(const struct zmk_event_header *eh) {
struct position_state_changed *ev = cast_position_state_changed(eh);
if (undecided_hold_tap == NULL) {
LOG_DBG("%d bubble (no undecided hold_tap active)", ev->position);
LOG_DBG("%d bubble (no undecided hold_tap active)", ev->data.position);
return ZMK_EV_EVENT_BUBBLE;
}
if (undecided_hold_tap->position == ev->position) {
if (ev->state) { // keydown
if (undecided_hold_tap->position == ev->data.position) {
if (ev->data.state) { // keydown
LOG_ERR("hold-tap listener should be called before before most other listeners!");
return ZMK_EV_EVENT_BUBBLE;
} else { // keyup
@ -408,23 +409,23 @@ static int position_state_changed_listener(const struct zmk_event_header *eh) {
// If these events were queued, the timer event may be queued too late or not at all.
// We make a timer decision before the other key events are handled if the timer would
// have run out.
if (ev->timestamp >
if (ev->data.timestamp >
(undecided_hold_tap->timestamp + undecided_hold_tap->config->tapping_term_ms)) {
decide_hold_tap(undecided_hold_tap, HT_TIMER_EVENT);
}
if (!ev->state && find_captured_keydown_event(ev->position) == NULL) {
if (!ev->data.state && find_captured_keydown_event(ev->data.position) == NULL) {
// no keydown event has been captured, let it bubble.
// we'll catch modifiers later in modifier_state_changed_listener
LOG_DBG("%d bubbling %d %s event", undecided_hold_tap->position, ev->position,
ev->state ? "down" : "up");
LOG_DBG("%d bubbling %d %s event", undecided_hold_tap->position, ev->data.position,
ev->data.state ? "down" : "up");
return ZMK_EV_EVENT_BUBBLE;
}
LOG_DBG("%d capturing %d %s event", undecided_hold_tap->position, ev->position,
ev->state ? "down" : "up");
LOG_DBG("%d capturing %d %s event", undecided_hold_tap->position, ev->data.position,
ev->data.state ? "down" : "up");
capture_event(eh);
decide_hold_tap(undecided_hold_tap, ev->state ? HT_OTHER_KEY_DOWN : HT_OTHER_KEY_UP);
decide_hold_tap(undecided_hold_tap, ev->data.state ? HT_OTHER_KEY_DOWN : HT_OTHER_KEY_UP);
return ZMK_EV_EVENT_CAPTURED;
}

View File

@ -249,7 +249,8 @@ int zmk_keymap_sensor_triggered(uint8_t sensor_number, const struct device *sens
int keymap_listener(const struct zmk_event_header *eh) {
if (is_position_state_changed(eh)) {
const struct position_state_changed *ev = cast_position_state_changed(eh);
return zmk_keymap_position_state_changed(ev->position, ev->state, ev->timestamp);
return zmk_keymap_position_state_changed(ev->data.position, ev->data.state,
ev->data.timestamp);
#if ZMK_KEYMAP_HAS_SENSORS
} else if (is_sensor_event(eh)) {
const struct sensor_event *ev = cast_sensor_event(eh);

View File

@ -51,9 +51,8 @@ void zmk_kscan_process_msgq(struct k_work *item) {
LOG_DBG("Row: %d, col: %d, position: %d, pressed: %s\n", ev.row, ev.column, position,
(pressed ? "true" : "false"));
pos_ev = new_position_state_changed();
pos_ev->state = pressed;
pos_ev->position = position;
pos_ev->timestamp = k_uptime_get();
pos_ev->data = (struct zmk_position_state_changed_data){
.state = pressed, .position = position, .timestamp = k_uptime_get()};
ZMK_EVENT_RAISE(pos_ev);
}
}

View File

@ -33,22 +33,14 @@ 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_subscribe_params subscribe_params;
struct zmk_split_peripheral_event {
uint32_t position;
uint32_t state;
int32_t timestamp;
};
K_MSGQ_DEFINE(peripheral_event_msgq, sizeof(struct zmk_split_peripheral_event),
K_MSGQ_DEFINE(peripheral_event_msgq, sizeof(struct zmk_position_state_changed_data),
CONFIG_ZMK_SPLIT_BLE_CENTRAL_POSITION_QUEUE_SIZE, 4);
void peripheral_event_work_callback(struct k_work *work) {
struct zmk_split_peripheral_event ev;
struct zmk_position_state_changed_data 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;
pos_ev->data = ev;
LOG_DBG("Trigger key position state change for %d", ev.position);
ZMK_EVENT_RAISE(pos_ev);
@ -82,7 +74,7 @@ static uint8_t split_central_notify_func(struct bt_conn *conn,
if (changed_positions[i] & BIT(j)) {
uint32_t position = (i * 8) + j;
bool pressed = position_state[i] & BIT(j);
struct zmk_split_peripheral_event ev = {
struct zmk_position_state_changed_data ev = {
.position = position, .state = pressed, .timestamp = k_uptime_get()};
k_msgq_put(&peripheral_event_msgq, &ev, K_NO_WAIT);

View File

@ -23,10 +23,10 @@ int split_listener(const struct zmk_event_header *eh) {
LOG_DBG("");
if (is_position_state_changed(eh)) {
const struct position_state_changed *ev = cast_position_state_changed(eh);
if (ev->state) {
return zmk_split_bt_position_pressed(ev->position);
if (ev->data.state) {
return zmk_split_bt_position_pressed(ev->data.position);
} else {
return zmk_split_bt_position_released(ev->position);
return zmk_split_bt_position_released(ev->data.position);
}
}
return ZMK_EV_EVENT_BUBBLE;