Update for 3.5 and refactor

This commit is contained in:
Nick Conway 2024-04-15 15:27:04 -04:00
parent aab348a142
commit d3cdc5915f
No known key found for this signature in database
GPG key ID: AA850592E4C1D453
5 changed files with 39 additions and 27 deletions

View file

@ -8,4 +8,3 @@
void zmk_leader_activate(int32_t timeout, bool timeout_on_activation, uint32_t position); void zmk_leader_activate(int32_t timeout, bool timeout_on_activation, uint32_t position);
void zmk_leader_deactivate(); void zmk_leader_deactivate();
bool zmk_leader_get_status();

View file

@ -23,3 +23,8 @@
* Gets the virtual key position to use for the combo with the given index. * Gets the virtual key position to use for the combo with the given index.
*/ */
#define ZMK_VIRTUAL_KEY_POSITION_COMBO(index) (ZMK_KEYMAP_LEN + ZMK_KEYMAP_SENSORS_LEN + (index)) #define ZMK_VIRTUAL_KEY_POSITION_COMBO(index) (ZMK_KEYMAP_LEN + ZMK_KEYMAP_SENSORS_LEN + (index))
/**
* Gets the virtual key position to use for the leader sequence with the given index.
*/
#define ZMK_VIRTUAL_KEY_POSITION_LEADER(index) (ZMK_KEYMAP_LEN + ZMK_KEYMAP_SENSORS_LEN + (index))

View file

@ -47,8 +47,8 @@ static const struct behavior_driver_api behavior_leader_key_driver_api = {
#define LEAD_INST(n) \ #define LEAD_INST(n) \
static struct behavior_leader_key_config behavior_leader_key_config_##n = { \ static struct behavior_leader_key_config behavior_leader_key_config_##n = { \
.timerless = DT_INST_PROP(n, timerless), .timeout_ms = DT_INST_PROP(n, timeout_ms)}; \ .timerless = DT_INST_PROP(n, timerless), .timeout_ms = DT_INST_PROP(n, timeout_ms)}; \
DEVICE_DT_INST_DEFINE(n, behavior_leader_key_init, NULL, NULL, \ BEHAVIOR_DT_INST_DEFINE(n, behavior_leader_key_init, NULL, NULL, \
&behavior_leader_key_config_##n, APPLICATION, \ &behavior_leader_key_config_##n, POST_KERNEL, \
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_leader_key_driver_api); CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_leader_key_driver_api);
DT_INST_FOREACH_STATUS_OKAY(LEAD_INST) DT_INST_FOREACH_STATUS_OKAY(LEAD_INST)

View file

@ -7,33 +7,37 @@
#define DT_DRV_COMPAT zmk_leader_sequences #define DT_DRV_COMPAT zmk_leader_sequences
#include <zephyr/device.h> #include <zephyr/device.h>
#include <drivers/behavior.h>
#include <zephyr/logging/log.h> #include <zephyr/logging/log.h>
#include <zephyr/sys/dlist.h> #include <zephyr/sys/dlist.h>
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
#include <drivers/behavior.h>
#include <zmk/behavior.h> #include <zmk/behavior.h>
#include <zmk/event_manager.h> #include <zmk/event_manager.h>
#include <zmk/events/position_state_changed.h> #include <zmk/events/position_state_changed.h>
#include <zmk/hid.h> #include <zmk/hid.h>
#include <zmk/matrix.h> #include <zmk/matrix.h>
#include <zmk/keymap.h> #include <zmk/keymap.h>
#include <zmk/virtual_key_position.h>
#include <zmk/leader.h> #include <zmk/leader.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
bool leader_status; #if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
int32_t press_count;
int32_t release_count; static bool leader_status;
int32_t timeout_ms; static int32_t press_count;
int32_t active_leader_position; static int32_t release_count;
int8_t layer; static int32_t timeout_ms;
bool first_release; static int32_t active_leader_position;
struct k_work_delayable release_timer; static int8_t layer;
int64_t release_at; static bool first_release;
bool timer_started; static struct k_work_delayable release_timer;
bool timer_cancelled; static int64_t release_at;
bool timerless; // static bool timer_started;
static bool timer_cancelled;
static bool timerless;
struct leader_seq_cfg { struct leader_seq_cfg {
int32_t key_positions[CONFIG_ZMK_LEADER_MAX_KEYS_PER_SEQUENCE]; int32_t key_positions[CONFIG_ZMK_LEADER_MAX_KEYS_PER_SEQUENCE];
@ -52,17 +56,17 @@ struct leader_seq_cfg {
// leader_pressed_keys is filled with an event when a key is pressed. // leader_pressed_keys is filled with an event when a key is pressed.
// The keys are removed from this array when they are released. // The keys are removed from this array when they are released.
// Once this array is empty, the behavior is released. // Once this array is empty, the behavior is released.
const struct zmk_position_state_changed static const struct zmk_position_state_changed
*leader_pressed_keys[CONFIG_ZMK_LEADER_MAX_KEYS_PER_SEQUENCE] = {NULL}; *leader_pressed_keys[CONFIG_ZMK_LEADER_MAX_KEYS_PER_SEQUENCE] = {NULL};
uint32_t current_sequence[CONFIG_ZMK_LEADER_MAX_KEYS_PER_SEQUENCE] = {-1}; static uint32_t current_sequence[CONFIG_ZMK_LEADER_MAX_KEYS_PER_SEQUENCE] = {-1};
// the set of candidate leader based on the currently leader_pressed_keys // the set of candidate leader based on the currently leader_pressed_keys
int num_candidates; static int num_candidates;
struct leader_seq_cfg *sequence_candidates[CONFIG_ZMK_LEADER_MAX_SEQUENCES_PER_KEY]; static struct leader_seq_cfg *sequence_candidates[CONFIG_ZMK_LEADER_MAX_SEQUENCES_PER_KEY];
int num_comp_candidates; static int num_comp_candidates;
struct leader_seq_cfg *completed_sequence_candidates[CONFIG_ZMK_LEADER_MAX_SEQUENCES_PER_KEY]; static struct leader_seq_cfg *completed_sequence_candidates[CONFIG_ZMK_LEADER_MAX_SEQUENCES_PER_KEY];
// a lookup dict that maps a key position to all sequences on that position // a lookup dict that maps a key position to all sequences on that position
struct leader_seq_cfg *sequence_lookup[ZMK_KEYMAP_LEN][CONFIG_ZMK_LEADER_MAX_SEQUENCES_PER_KEY] = { static struct leader_seq_cfg *sequence_lookup[ZMK_KEYMAP_LEN][CONFIG_ZMK_LEADER_MAX_SEQUENCES_PER_KEY] = {
NULL}; NULL};
// Store the leader key pointer in the leader array, one pointer for each key position // Store the leader key pointer in the leader array, one pointer for each key position
@ -339,7 +343,7 @@ ZMK_SUBSCRIPTION(leader, zmk_position_state_changed);
#define LEADER_INST(n) \ #define LEADER_INST(n) \
static struct leader_seq_cfg sequence_config_##n = { \ static struct leader_seq_cfg sequence_config_##n = { \
.virtual_key_position = ZMK_KEYMAP_LEN + __COUNTER__, \ .virtual_key_position = ZMK_VIRTUAL_KEY_POSITION_LEADER(__COUNTER__), \
.immediate_trigger = DT_PROP(n, immediate_trigger), \ .immediate_trigger = DT_PROP(n, immediate_trigger), \
.is_pressed = false, \ .is_pressed = false, \
.key_positions = DT_PROP(n, key_positions), \ .key_positions = DT_PROP(n, key_positions), \
@ -349,14 +353,16 @@ ZMK_SUBSCRIPTION(leader, zmk_position_state_changed);
.layers_len = DT_PROP_LEN(n, layers), \ .layers_len = DT_PROP_LEN(n, layers), \
}; };
DT_INST_FOREACH_CHILD(0, LEADER_INST)
#define INTITIALIAZE_LEADER_SEQUENCES(n) intitialiaze_leader_sequences(&sequence_config_##n); #define INTITIALIAZE_LEADER_SEQUENCES(n) intitialiaze_leader_sequences(&sequence_config_##n);
static int leader_init() { DT_INST_FOREACH_CHILD(0, LEADER_INST)
static int leader_init(void) {
k_work_init_delayable(&release_timer, behavior_leader_key_timer_handler); k_work_init_delayable(&release_timer, behavior_leader_key_timer_handler);
DT_INST_FOREACH_CHILD(0, INTITIALIAZE_LEADER_SEQUENCES); DT_INST_FOREACH_CHILD(0, INTITIALIAZE_LEADER_SEQUENCES);
return 0; return 0;
} }
SYS_INIT(leader_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); SYS_INIT(leader_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
#endif

View file

@ -35,6 +35,7 @@ module.exports = {
"features/bluetooth", "features/bluetooth",
"features/split-keyboards", "features/split-keyboards",
"features/combos", "features/combos",
"features/leader",
"features/conditional-layers", "features/conditional-layers",
"features/debouncing", "features/debouncing",
"features/displays", "features/displays",
@ -62,6 +63,7 @@ module.exports = {
"behaviors/mod-tap", "behaviors/mod-tap",
"behaviors/mod-morph", "behaviors/mod-morph",
"behaviors/macros", "behaviors/macros",
"behaviors/leader-key",
"behaviors/key-toggle", "behaviors/key-toggle",
"behaviors/sticky-key", "behaviors/sticky-key",
"behaviors/sticky-layer", "behaviors/sticky-layer",