Working Timer
This commit is contained in:
parent
5f387c9128
commit
21eabb2a0d
1 changed files with 128 additions and 38 deletions
|
@ -20,58 +20,123 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
|
#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
|
||||||
|
|
||||||
static bool tap_dance_started = false;
|
#define ZMK_BHV_TAP_DANCE_POSITION_FREE ULONG_MAX
|
||||||
static int tap_counter = 0;
|
|
||||||
int64_t timestamp;
|
|
||||||
|
|
||||||
struct behavior_tap_dance_config {
|
struct behavior_tap_dance_config {
|
||||||
int tapping_term_ms;
|
uint32_t tapping_term_ms;
|
||||||
|
struct zmk_behavior_binding behavior;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct active_tap_dance {
|
||||||
|
// tap dance data
|
||||||
|
int tap_counter;
|
||||||
|
uint32_t position;
|
||||||
|
const struct behavior_tap_dance_config *config;
|
||||||
|
// timer data
|
||||||
|
bool timer_started;
|
||||||
|
bool timer_cancelled;
|
||||||
|
int64_t release_at;
|
||||||
|
struct k_delayed_work release_timer;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct active_tap_dance the_tap_dance;
|
||||||
|
|
||||||
|
static struct active_tap_dance *store_tap_dance(uint32_t position,
|
||||||
|
const struct behavior_tap_dance_config *config) {
|
||||||
|
struct active_tap_dance *const tap_dance = &the_tap_dance;
|
||||||
|
tap_dance->tap_counter = 0;
|
||||||
|
tap_dance->position = position;
|
||||||
|
tap_dance->config = config;
|
||||||
|
tap_dance->release_at = 0;
|
||||||
|
tap_dance->timer_cancelled = false;
|
||||||
|
tap_dance->timer_started = false;
|
||||||
|
return tap_dance;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void clear_tap_dance(struct active_tap_dance *tap_dance) {
|
||||||
|
LOG_DBG("Clearing Tap Dance");
|
||||||
|
tap_dance-> position = ZMK_BHV_TAP_DANCE_POSITION_FREE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int press_tap_dance_behavior(struct active_tap_dance *tap_dance,
|
||||||
|
int64_t timestamp) {
|
||||||
|
LOG_DBG("Press Tap Dance Behavior");
|
||||||
|
struct zmk_behavior_binding binding = {
|
||||||
|
.behavior_dev = tap_dance->config->behavior.behavior_dev,
|
||||||
|
};
|
||||||
|
struct zmk_behavior_binding_event event = {
|
||||||
|
.position = tap_dance->position,
|
||||||
|
.timestamp = timestamp,
|
||||||
|
};
|
||||||
|
return 0;
|
||||||
|
//return behavior_keymap_binding_pressed(&binding, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int release_tap_dance_behavior(struct active_tap_dance *tap_dance,
|
||||||
|
int64_t timestamp) {
|
||||||
|
LOG_DBG("Release Tap Dance Behavior");
|
||||||
|
struct zmk_behavior_binding binding = {
|
||||||
|
.behavior_dev = tap_dance->config->behavior.behavior_dev,
|
||||||
|
};
|
||||||
|
struct zmk_behavior_binding_event event = {
|
||||||
|
.position = tap_dance->position,
|
||||||
|
.timestamp = timestamp,
|
||||||
|
};
|
||||||
|
|
||||||
|
clear_tap_dance(tap_dance);
|
||||||
|
return 0;
|
||||||
|
//return behavior_keymap_binding_released(&binding, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int stop_timer(struct active_tap_dance *tap_dance) {
|
||||||
|
LOG_DBG("Stop Timer");
|
||||||
|
int timer_cancel_result = k_delayed_work_cancel(&tap_dance->release_timer);
|
||||||
|
if (timer_cancel_result == -EINPROGRESS) {
|
||||||
|
tap_dance->timer_cancelled = true;
|
||||||
|
}
|
||||||
|
return timer_cancel_result;
|
||||||
|
}
|
||||||
|
|
||||||
static int on_tap_dance_binding_pressed(struct zmk_behavior_binding *binding,
|
static int on_tap_dance_binding_pressed(struct zmk_behavior_binding *binding,
|
||||||
struct zmk_behavior_binding_event event) {
|
struct zmk_behavior_binding_event event) {
|
||||||
|
LOG_DBG("On Binding Pressed");
|
||||||
const struct device *dev = device_get_binding(binding->behavior_dev);
|
const struct device *dev = device_get_binding(binding->behavior_dev);
|
||||||
const struct behavior_tap_dance_config *cfg = dev->config;
|
const struct behavior_tap_dance_config *cfg = dev->config;
|
||||||
if (!tap_dance_started) {
|
struct active_tap_dance *tap_dance = &the_tap_dance;
|
||||||
tap_dance_started = true;
|
if (tap_dance != NULL) {
|
||||||
timestamp = k_uptime_get();
|
stop_timer(tap_dance);
|
||||||
LOG_DBG("timestamp at: %lld", timestamp);
|
release_tap_dance_behavior(tap_dance, event.timestamp);
|
||||||
}
|
}
|
||||||
else {
|
tap_dance = store_tap_dance(event.position, cfg);
|
||||||
if (k_uptime_get() <= (timestamp + cfg->tapping_term_ms)){
|
if (tap_dance == NULL) {
|
||||||
timestamp = k_uptime_get();
|
LOG_ERR("unable to store tap dance");
|
||||||
LOG_DBG("Updated timestamp: %lld", timestamp);
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
int32_t tapping_term_ms_left = (timestamp + cfg->tapping_term_ms) - k_uptime_get();
|
|
||||||
LOG_DBG("time left: %d", tapping_term_ms_left);
|
|
||||||
|
|
||||||
if (tapping_term_ms_left <= 0) {
|
press_tap_dance_behavior(tap_dance, event.timestamp);
|
||||||
LOG_DBG("TIME'S UP");
|
LOG_DBG("%d new tap_dance", event.position);
|
||||||
LOG_DBG("Counter reached on press: %d", tap_counter);
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
tap_dance_started = false;
|
|
||||||
tap_counter = 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
++ tap_counter;
|
|
||||||
}
|
|
||||||
LOG_DBG("Counter beginning at: %d", tap_counter);
|
|
||||||
|
|
||||||
|
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int on_tap_dance_binding_released(struct zmk_behavior_binding *binding,
|
static int on_tap_dance_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);
|
LOG_DBG("On Binding Released");
|
||||||
const struct behavior_tap_dance_config *cfg = dev->config;
|
struct active_tap_dance *tap_dance = &the_tap_dance;
|
||||||
int32_t tapping_term_ms_left = (timestamp + cfg->tapping_term_ms) - k_uptime_get();
|
if (tap_dance == NULL) {
|
||||||
if (tapping_term_ms_left <= 0) {
|
LOG_ERR("ACTIVE STICKY KEY CLEARED TOO EARLY");
|
||||||
LOG_DBG("Counter reached on release: %d", tap_counter);
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
tap_dance_started = false;
|
}
|
||||||
tap_counter = 0;
|
|
||||||
|
// No other key was pressed. Start the timer.
|
||||||
|
tap_dance->timer_started = true;
|
||||||
|
tap_dance->release_at = event.timestamp + tap_dance->config->tapping_term_ms;
|
||||||
|
// adjust timer in case this behavior was queued by a hold-tap
|
||||||
|
int32_t ms_left = tap_dance->release_at - k_uptime_get();
|
||||||
|
LOG_DBG("ms_left equal to: %d", ms_left);
|
||||||
|
if (ms_left > 0) {
|
||||||
|
k_delayed_work_submit(&tap_dance->release_timer, K_MSEC(ms_left));
|
||||||
}
|
}
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
}
|
||||||
|
@ -81,7 +146,32 @@ static const struct behavior_driver_api behavior_tap_dance_driver_api = {
|
||||||
.binding_released = on_tap_dance_binding_released,
|
.binding_released = on_tap_dance_binding_released,
|
||||||
};
|
};
|
||||||
|
|
||||||
static int behavior_tap_dance_init(const struct device *dev) { return 0; };
|
void behavior_tap_dance_timer_handler(struct k_work *item) {
|
||||||
|
LOG_DBG("Timer Handler Called");
|
||||||
|
LOG_DBG("Counter reached: %d", the_tap_dance.tap_counter);
|
||||||
|
struct active_tap_dance *tap_dance =
|
||||||
|
CONTAINER_OF(item, struct active_tap_dance, release_timer);
|
||||||
|
if (tap_dance->position == ZMK_BHV_TAP_DANCE_POSITION_FREE) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (tap_dance->timer_cancelled) {
|
||||||
|
tap_dance->timer_cancelled = false;
|
||||||
|
} else {
|
||||||
|
release_tap_dance_behavior(tap_dance, tap_dance->release_at);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int behavior_tap_dance_init(const struct device *dev) {
|
||||||
|
static bool init_first_run = true;
|
||||||
|
if (init_first_run) {
|
||||||
|
k_delayed_work_init(&the_tap_dance.release_timer,
|
||||||
|
behavior_tap_dance_timer_handler);
|
||||||
|
LOG_DBG("Hello World");
|
||||||
|
the_tap_dance.position = ZMK_BHV_TAP_DANCE_POSITION_FREE;
|
||||||
|
}
|
||||||
|
init_first_run = false;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
struct behavior_tap_dance_data {};
|
struct behavior_tap_dance_data {};
|
||||||
static struct behavior_tap_dance_data behavior_tap_dance_data;
|
static struct behavior_tap_dance_data behavior_tap_dance_data;
|
||||||
|
|
Loading…
Add table
Reference in a new issue