feat(events): Add trace_id to position, behavior and key events

With the trace_id it's possible for behaviors to keep track of which events were caused by which trigger.
This commit is contained in:
Okke Formsma 2021-02-07 14:26:53 +01:00
parent 87232cbc5a
commit 25fd5b2402
11 changed files with 51 additions and 18 deletions

View file

@ -19,4 +19,5 @@ struct zmk_behavior_binding_event {
int layer;
uint32_t position;
int64_t timestamp;
uint32_t trace_id;
};

View file

@ -17,12 +17,14 @@ struct zmk_keycode_state_changed {
uint8_t explicit_modifiers;
bool state;
int64_t timestamp;
uint32_t trace_id;
};
ZMK_EVENT_DECLARE(zmk_keycode_state_changed);
static inline struct zmk_keycode_state_changed_event *
zmk_keycode_state_changed_from_encoded(uint32_t encoded, bool pressed, int64_t timestamp) {
zmk_keycode_state_changed_from_encoded(uint32_t encoded, bool pressed, int64_t timestamp,
uint32_t trace_id) {
uint16_t page = HID_USAGE_PAGE(encoded) & 0xFF;
uint16_t id = HID_USAGE_ID(encoded);
uint8_t implicit_modifiers = 0x00;
@ -38,11 +40,13 @@ zmk_keycode_state_changed_from_encoded(uint32_t encoded, bool pressed, int64_t t
implicit_modifiers = SELECT_MODS(encoded);
}
return new_zmk_keycode_state_changed(
(struct zmk_keycode_state_changed){.usage_page = page,
.keycode = id,
.implicit_modifiers = implicit_modifiers,
.explicit_modifiers = explicit_modifiers,
.state = pressed,
.timestamp = timestamp});
return new_zmk_keycode_state_changed((struct zmk_keycode_state_changed){
.usage_page = page,
.keycode = id,
.implicit_modifiers = implicit_modifiers,
.explicit_modifiers = explicit_modifiers,
.state = pressed,
.timestamp = timestamp,
.trace_id = trace_id,
});
}

View file

@ -8,10 +8,15 @@
#include <zephyr.h>
#include <zmk/event_manager.h>
#include <zmk/matrix.h>
struct zmk_position_state_changed {
uint32_t position;
bool state;
int64_t timestamp;
int32_t trace_id;
};
ZMK_EVENT_DECLARE(zmk_position_state_changed);
ZMK_EVENT_DECLARE(zmk_position_state_changed);
uint32_t zmk_get_event_trace_id(uint32_t position, bool pressed);

View file

@ -9,6 +9,7 @@
#include <zephyr.h>
#include <zmk/event_manager.h>
#include <device.h>
struct zmk_sensor_event {
uint8_t sensor_number;
const struct device *sensor;

View file

@ -313,6 +313,7 @@ static void decide_hold_tap(struct active_hold_tap *hold_tap, enum decision_mome
struct zmk_behavior_binding_event event = {
.position = hold_tap->position,
.timestamp = hold_tap->timestamp,
.trace_id = hold_tap->trace_id,
};
struct zmk_behavior_binding binding;
@ -388,6 +389,7 @@ static int on_hold_tap_binding_released(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event sub_behavior_data = {
.position = hold_tap->position,
.timestamp = hold_tap->timestamp,
.trace_id = hold_tap->trace_id,
};
struct zmk_behavior_binding sub_behavior_binding;

View file

@ -21,15 +21,15 @@ static int behavior_key_press_init(const struct device *dev) { return 0; };
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);
return ZMK_EVENT_RAISE(
zmk_keycode_state_changed_from_encoded(binding->param1, true, event.timestamp));
return ZMK_EVENT_RAISE(zmk_keycode_state_changed_from_encoded(binding->param1, true,
event.timestamp, event.trace_id));
}
static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);
return ZMK_EVENT_RAISE(
zmk_keycode_state_changed_from_encoded(binding->param1, false, event.timestamp));
return ZMK_EVENT_RAISE(zmk_keycode_state_changed_from_encoded(binding->param1, false,
event.timestamp, event.trace_id));
}
static const struct behavior_driver_api behavior_key_press_driver_api = {

View file

@ -47,12 +47,12 @@ static int on_sensor_binding_triggered(struct zmk_behavior_binding *binding,
LOG_DBG("SEND %d", keycode);
ZMK_EVENT_RAISE(zmk_keycode_state_changed_from_encoded(keycode, true, timestamp));
ZMK_EVENT_RAISE(zmk_keycode_state_changed_from_encoded(keycode, true, timestamp, 0));
// TODO: Better way to do this?
k_msleep(5);
return ZMK_EVENT_RAISE(zmk_keycode_state_changed_from_encoded(keycode, false, timestamp));
return ZMK_EVENT_RAISE(zmk_keycode_state_changed_from_encoded(keycode, false, timestamp, 0));
}
static const struct behavior_driver_api behavior_sensor_rotate_key_press_driver_api = {

View file

@ -7,4 +7,15 @@
#include <kernel.h>
#include <zmk/events/position_state_changed.h>
ZMK_EVENT_IMPL(zmk_position_state_changed);
ZMK_EVENT_IMPL(zmk_position_state_changed);
static uint32_t zmk_last_event_trace_id = 0;
static uint32_t zmk_event_trace_ids[ZMK_KEYMAP_LEN] = {0};
uint32_t zmk_get_event_trace_id(uint32_t position, bool pressed) {
if (pressed) {
zmk_last_event_trace_id++;
zmk_event_trace_ids[position] = zmk_last_event_trace_id;
}
return zmk_event_trace_ids[position];
}

View file

@ -161,6 +161,7 @@ int zmk_keymap_apply_position_state(const struct zmk_position_state_changed *pos
.layer = layer,
.position = pos_ev->position,
.timestamp = pos_ev->timestamp,
.trace_id = pos_ev->trace_id,
};
LOG_DBG("layer: %d position: %d, binding name: %s", event.layer, event.position,

View file

@ -50,7 +50,11 @@ 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"));
ZMK_EVENT_RAISE(new_zmk_position_state_changed((struct zmk_position_state_changed){
.state = pressed, .position = position, .timestamp = k_uptime_get()}));
.state = pressed,
.position = position,
.timestamp = k_uptime_get(),
.trace_id = zmk_get_event_trace_id(position, pressed),
}));
}
}

View file

@ -72,7 +72,11 @@ static uint8_t split_central_notify_func(struct bt_conn *conn,
uint32_t position = (i * 8) + j;
bool pressed = position_state[i] & BIT(j);
struct zmk_position_state_changed ev = {
.position = position, .state = pressed, .timestamp = k_uptime_get()};
.position = position,
.state = pressed,
.timestamp = k_uptime_get(),
.trace_id = zmk_get_event_trace_id(position, pressed),
};
k_msgq_put(&peripheral_event_msgq, &ev, K_NO_WAIT);
k_work_submit(&peripheral_event_work);