Add timestamps to position and behavior events (#147)

* Add timestamps to position events and behaviors.

- Take original event timestamps into consideration so nested tap-holds have proper timing.
- Add position and timestamp to keycode state changed event so the one-shot behavior can properly identify other keypresses and timings.
- Add timestamp to position events received from peripheral

* reduce number of arguments to behaviors
This commit is contained in:
Okke Formsma
2020-10-10 23:32:53 +02:00
committed by GitHub
parent fc6c01fd69
commit d38740cebf
21 changed files with 248 additions and 122 deletions

View File

@@ -104,9 +104,14 @@ bool is_active_layer(u8_t layer, u32_t layer_state) {
return (layer_state & BIT(layer)) == BIT(layer) || layer == zmk_keymap_layer_default;
}
int zmk_keymap_apply_position_state(int layer, u32_t position, bool pressed) {
int zmk_keymap_apply_position_state(int layer, u32_t position, bool pressed, s64_t timestamp) {
struct zmk_behavior_binding *binding = &zmk_keymap[layer][position];
struct device *behavior;
struct zmk_behavior_binding_event event = {
.layer = layer,
.position = position,
.timestamp = timestamp,
};
LOG_DBG("layer: %d position: %d, binding name: %s", layer, position,
log_strdup(binding->behavior_dev));
@@ -119,20 +124,18 @@ int zmk_keymap_apply_position_state(int layer, u32_t position, bool pressed) {
}
if (pressed) {
return behavior_keymap_binding_pressed(behavior, position, binding->param1,
binding->param2);
return behavior_keymap_binding_pressed(binding, event);
} else {
return behavior_keymap_binding_released(behavior, position, binding->param1,
binding->param2);
return behavior_keymap_binding_released(binding, event);
}
}
int zmk_keymap_position_state_changed(u32_t position, bool pressed) {
int zmk_keymap_position_state_changed(u32_t position, bool pressed, s64_t timestamp) {
for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= zmk_keymap_layer_default; layer--) {
u32_t layer_state =
pressed ? zmk_keymap_layer_state : zmk_keymap_active_behavior_layer[position];
if (is_active_layer(layer, layer_state)) {
int ret = zmk_keymap_apply_position_state(layer, position, pressed);
int ret = zmk_keymap_apply_position_state(layer, position, pressed, timestamp);
zmk_keymap_active_behavior_layer[position] = zmk_keymap_layer_state;
@@ -171,8 +174,7 @@ int zmk_keymap_sensor_triggered(u8_t sensor_number, struct device *sensor) {
continue;
}
ret = behavior_sensor_keymap_binding_triggered(behavior, sensor, binding->param1,
binding->param2);
ret = behavior_sensor_keymap_binding_triggered(binding, sensor);
if (ret > 0) {
LOG_DBG("behavior processing to continue to next layer");
@@ -194,7 +196,7 @@ int zmk_keymap_sensor_triggered(u8_t sensor_number, struct device *sensor) {
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);
return zmk_keymap_position_state_changed(ev->position, ev->state, ev->timestamp);
#if ZMK_KEYMAP_HAS_SENSORS
} else if (is_sensor_event(eh)) {
const struct sensor_event *ev = cast_sensor_event(eh);