Refactor turbo key

This commit is contained in:
Nick Conway 2023-06-04 02:00:00 -04:00
parent 595fc42196
commit 4cb986a306
2 changed files with 50 additions and 86 deletions

View file

@ -25,16 +25,17 @@ struct behavior_turbo_config {
const struct zmk_behavior_binding binding; const struct zmk_behavior_binding binding;
}; };
#define ZMK_BHV_TURBO_MAX_ACTIVE 5 struct behavior_turbo_data {
struct active_turbo {
const struct behavior_turbo_config *config;
uint32_t position; uint32_t position;
bool is_active; bool is_active;
bool is_pressed; bool is_pressed;
int32_t press_time; int32_t press_time;
int tap_ms;
int wait_ms;
struct zmk_behavior_binding binding;
// Timer Data // Timer Data
bool timer_started; bool timer_started;
bool timer_cancelled; bool timer_cancelled;
@ -43,107 +44,67 @@ struct active_turbo {
struct k_work_delayable release_timer; struct k_work_delayable release_timer;
}; };
struct active_turbo active_turbos[ZMK_BHV_TURBO_MAX_ACTIVE] = {}; static int behavior_turbo_key_init(const struct device *dev) { return 0; };
static struct active_turbo *find_active_turbo(uint32_t position) { static int stop_timer(struct behavior_turbo_data *data) {
for (int i = 0; i < ZMK_BHV_TURBO_MAX_ACTIVE; i++) { int timer_cancel_result = k_work_cancel_delayable(&data->release_timer);
if (active_turbos[i].is_active) {
return &active_turbos[i];
}
}
return NULL;
}
static int new_turbo(uint32_t position, const struct behavior_turbo_config *config,
struct active_turbo **turbo) {
for (int i = 0; i < ZMK_BHV_TURBO_MAX_ACTIVE; i++) {
struct active_turbo *const ref_turbo = &active_turbos[i];
if (!ref_turbo->is_active) {
ref_turbo->is_active = true;
ref_turbo->position = position;
ref_turbo->config = config;
ref_turbo->is_pressed = true;
ref_turbo->press_time = k_uptime_get();
ref_turbo->release_at = 0;
ref_turbo->timer_started = true;
ref_turbo->timer_cancelled = false;
*turbo = ref_turbo;
return 0;
}
}
return -ENOMEM;
};
static int stop_timer(struct active_turbo *turbo) {
int timer_cancel_result = k_work_cancel_delayable(&turbo->release_timer);
if (timer_cancel_result == -EINPROGRESS) { if (timer_cancel_result == -EINPROGRESS) {
// too late to cancel, we'll let the timer handler clear up. // too late to cancel, we'll let the timer handler clear up.
turbo->timer_cancelled = true; data->timer_cancelled = true;
} }
return timer_cancel_result; return timer_cancel_result;
} }
static void clear_turbo(struct active_turbo *turbo) { static void clear_turbo(struct behavior_turbo_data *data) {
LOG_DBG("Turbo deactivated"); LOG_DBG("Turbo deactivated");
turbo->is_active = false; data->is_active = false;
stop_timer(turbo); stop_timer(data);
} }
static void reset_timer(struct active_turbo *turbo, struct zmk_behavior_binding_event event) { static void reset_timer(struct behavior_turbo_data *data, struct zmk_behavior_binding_event event) {
turbo->release_at = event.timestamp + turbo->config->wait_ms; data->release_at = event.timestamp + data->wait_ms;
int32_t ms_left = turbo->release_at - k_uptime_get(); LOG_DBG("Resetting turbo timer: %d + %d = %d", event.timestamp, data->wait_ms,
data->release_at);
int32_t ms_left = data->release_at - k_uptime_get();
if (ms_left > 0) { if (ms_left > 0) {
k_work_schedule(&turbo->release_timer, K_MSEC(ms_left)); k_work_schedule(&data->release_timer, K_MSEC(ms_left));
LOG_DBG("Successfully reset turbo timer at position %d", turbo->position); LOG_DBG("Successfully reset turbo timer at position %d", data->position);
} }
} }
static void behavior_turbo_timer_handler(struct k_work *item) { static void behavior_turbo_timer_handler(struct k_work *item) {
struct active_turbo *turbo = CONTAINER_OF(item, struct active_turbo, release_timer); struct behavior_turbo_data *data =
if (!turbo->is_active) { CONTAINER_OF(item, struct behavior_turbo_data, release_timer);
if (!data->is_active) {
return; return;
} }
if (turbo->timer_cancelled) { if (data->timer_cancelled) {
return; return;
} }
LOG_DBG("Turbo timer reached."); LOG_DBG("Turbo timer reached.");
struct zmk_behavior_binding_event event = {.position = turbo->position, struct zmk_behavior_binding_event event = {.position = data->position,
.timestamp = k_uptime_get()}; .timestamp = k_uptime_get()};
zmk_behavior_queue_add(event.position, turbo->config->binding, true, turbo->config->tap_ms); zmk_behavior_queue_add(event.position, data->binding, true, data->tap_ms);
zmk_behavior_queue_add(event.position, turbo->config->binding, false, 0); zmk_behavior_queue_add(event.position, data->binding, false, 0);
reset_timer(turbo, event); reset_timer(data, event);
} }
static int behavior_turbo_key_init(const struct device *dev) {
static bool init_first_run = true;
if (init_first_run) {
for (int i = 0; i < ZMK_BHV_TURBO_MAX_ACTIVE; i++) {
k_work_init_delayable(&active_turbos[i].release_timer, behavior_turbo_timer_handler);
clear_turbo(&active_turbos[i]);
}
}
init_first_run = false;
return 0;
};
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) { struct zmk_behavior_binding_event event) {
const struct device *dev = device_get_binding(binding->behavior_dev); const struct device *dev = device_get_binding(binding->behavior_dev);
const struct behavior_turbo_config *cfg = dev->config; const struct behavior_turbo_config *cfg = dev->config;
struct behavior_turbo_data *data = dev->data;
struct active_turbo *turbo; if (!data->is_active) {
turbo = find_active_turbo(event.position); data->is_active = true;
if (turbo == NULL) {
if (new_turbo(event.position, cfg, &turbo) == -ENOMEM) { LOG_DBG("%d started new turbo", event.position);
LOG_ERR("Unable to create new turbo. Insufficient space in active_turbos[]."); k_work_init_delayable(&data->release_timer, behavior_turbo_timer_handler);
return ZMK_BEHAVIOR_OPAQUE; zmk_behavior_queue_add(event.position, cfg->binding, true, cfg->tap_ms);
} zmk_behavior_queue_add(event.position, cfg->binding, false, 0);
LOG_DBG("%d created new turbo", event.position); reset_timer(data, event);
zmk_behavior_queue_add(event.position, turbo->config->binding, true, turbo->config->tap_ms);
zmk_behavior_queue_add(event.position, turbo->config->binding, false, 0);
reset_timer(turbo, event);
} else { } else {
clear_turbo(turbo); clear_turbo(data);
} }
return ZMK_BEHAVIOR_OPAQUE; return ZMK_BEHAVIOR_OPAQUE;
} }
@ -152,15 +113,14 @@ static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) { struct zmk_behavior_binding_event event) {
const struct device *dev = device_get_binding(binding->behavior_dev); const struct device *dev = device_get_binding(binding->behavior_dev);
const struct behavior_turbo_config *cfg = dev->config; const struct behavior_turbo_config *cfg = dev->config;
struct behavior_turbo_data *data = dev->data;
struct active_turbo *turbo; if (&data->is_active == false) {
turbo = find_active_turbo(event.position); data->is_pressed = false;
if (turbo != NULL) { int32_t elapsedTime = k_uptime_get() - data->press_time;
turbo->is_pressed = false;
int32_t elapsedTime = k_uptime_get() - turbo->press_time;
LOG_DBG("turbo elapsed time: %d", elapsedTime); LOG_DBG("turbo elapsed time: %d", elapsedTime);
if (elapsedTime > cfg->toggle_term_ms) { if (elapsedTime > cfg->toggle_term_ms) {
clear_turbo(turbo); clear_turbo(data);
} }
} }
return 0; return 0;
@ -186,8 +146,12 @@ static const struct behavior_driver_api behavior_turbo_key_driver_api = {
.wait_ms = DT_INST_PROP(n, wait_ms), \ .wait_ms = DT_INST_PROP(n, wait_ms), \
.toggle_term_ms = DT_INST_PROP(n, toggle_term_ms), \ .toggle_term_ms = DT_INST_PROP(n, toggle_term_ms), \
.binding = _TRANSFORM_ENTRY(0, n)}; \ .binding = _TRANSFORM_ENTRY(0, n)}; \
DEVICE_DT_INST_DEFINE(n, behavior_turbo_key_init, NULL, NULL, &behavior_turbo_config_##n, \ static struct behavior_turbo_data behavior_turbo_data_##n = { \
APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ .tap_ms = DT_INST_PROP(n, tap_ms), \
&behavior_turbo_key_driver_api); .wait_ms = DT_INST_PROP(n, wait_ms), \
.binding = _TRANSFORM_ENTRY(0, n)}; \
DEVICE_DT_INST_DEFINE(n, behavior_turbo_key_init, NULL, &behavior_turbo_data_##n, \
&behavior_turbo_config_##n, APPLICATION, \
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_turbo_key_driver_api);
DT_INST_FOREACH_STATUS_OKAY(TURBO_INST) DT_INST_FOREACH_STATUS_OKAY(TURBO_INST)

View file

@ -14,7 +14,7 @@
}; };
t2: turbo2 { t2: turbo2 {
compatible = "zmk,behavior-turbo-key"; compatible = "zmk,behavior-turbo-key";
label = "turbo"; label = "turbo2";
#binding-cells = <0>; #binding-cells = <0>;
tap-ms = <5>; tap-ms = <5>;
wait-ms = <300>; wait-ms = <300>;