Skip to content

Commit 18d0d2f

Browse files
authored
refactor(combos): Reduce combo RAM usage (#2837)
Properly mark combo configs as `const` so they are place in flash, not RAM.
1 parent 8575fc8 commit 18d0d2f

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

Diff for: app/src/combo.c

+19-19
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct combo_cfg {
4343
};
4444

4545
struct active_combo {
46-
struct combo_cfg *combo;
46+
const struct combo_cfg *combo;
4747
// key_positions_pressed is filled with key_positions when the combo is pressed.
4848
// The keys are removed from this array when they are released.
4949
// Once this array is empty, the behavior is released.
@@ -53,7 +53,7 @@ struct active_combo {
5353
};
5454

5555
struct combo_candidate {
56-
struct combo_cfg *combo;
56+
const struct combo_cfg *combo;
5757
// the time after which this behavior should be removed from candidates.
5858
// by keeping track of when the candidate should be cleared there is no
5959
// possibility of accidental releases.
@@ -66,9 +66,9 @@ struct zmk_position_state_changed_event pressed_keys[CONFIG_ZMK_COMBO_MAX_KEYS_P
6666
// the set of candidate combos based on the currently pressed_keys
6767
struct combo_candidate candidates[CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY];
6868
// the last candidate that was completely pressed
69-
struct combo_cfg *fully_pressed_combo = NULL;
69+
const struct combo_cfg *fully_pressed_combo = NULL;
7070
// a lookup dict that maps a key position to all combos on that position
71-
struct combo_cfg *combo_lookup[ZMK_KEYMAP_LEN][CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY] = {NULL};
71+
const struct combo_cfg *combo_lookup[ZMK_KEYMAP_LEN][CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY] = {NULL};
7272
// combos that have been activated and still have (some) keys pressed
7373
// this array is always contiguous from 0.
7474
struct active_combo active_combos[CONFIG_ZMK_COMBO_MAX_PRESSED_COMBOS] = {NULL};
@@ -90,18 +90,18 @@ static void store_last_tapped(int64_t timestamp) {
9090

9191
// Store the combo key pointer in the combos array, one pointer for each key position
9292
// The combos are sorted shortest-first, then by virtual-key-position.
93-
static int initialize_combo(struct combo_cfg *new_combo) {
93+
static int initialize_combo(const struct combo_cfg *new_combo) {
9494
for (int i = 0; i < new_combo->key_position_len; i++) {
9595
int32_t position = new_combo->key_positions[i];
9696
if (position >= ZMK_KEYMAP_LEN) {
9797
LOG_ERR("Unable to initialize combo, key position %d does not exist", position);
9898
return -EINVAL;
9999
}
100100

101-
struct combo_cfg *insert_combo = new_combo;
101+
const struct combo_cfg *insert_combo = new_combo;
102102
bool set = false;
103103
for (int j = 0; j < CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY; j++) {
104-
struct combo_cfg *combo_at_j = combo_lookup[position][j];
104+
const struct combo_cfg *combo_at_j = combo_lookup[position][j];
105105
if (combo_at_j == NULL) {
106106
combo_lookup[position][j] = insert_combo;
107107
set = true;
@@ -125,7 +125,7 @@ static int initialize_combo(struct combo_cfg *new_combo) {
125125
return 0;
126126
}
127127

128-
static bool combo_active_on_layer(struct combo_cfg *combo, uint8_t layer) {
128+
static bool combo_active_on_layer(const struct combo_cfg *combo, uint8_t layer) {
129129
if (combo->layers[0] == -1) {
130130
// -1 in the first layer position is global layer scope
131131
return true;
@@ -138,15 +138,15 @@ static bool combo_active_on_layer(struct combo_cfg *combo, uint8_t layer) {
138138
return false;
139139
}
140140

141-
static bool is_quick_tap(struct combo_cfg *combo, int64_t timestamp) {
141+
static bool is_quick_tap(const struct combo_cfg *combo, int64_t timestamp) {
142142
return (last_tapped_timestamp + combo->require_prior_idle_ms) > timestamp;
143143
}
144144

145145
static int setup_candidates_for_first_keypress(int32_t position, int64_t timestamp) {
146146
int number_of_combo_candidates = 0;
147147
uint8_t highest_active_layer = zmk_keymap_highest_layer_active();
148148
for (int i = 0; i < CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY; i++) {
149-
struct combo_cfg *combo = combo_lookup[position][i];
149+
const struct combo_cfg *combo = combo_lookup[position][i];
150150
if (combo == NULL) {
151151
return number_of_combo_candidates;
152152
}
@@ -166,8 +166,8 @@ static int filter_candidates(int32_t position) {
166166
int matches = 0, lookup_idx = 0, candidate_idx = 0;
167167
while (lookup_idx < CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY &&
168168
candidate_idx < CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY) {
169-
struct combo_cfg *candidate = candidates[candidate_idx].combo;
170-
struct combo_cfg *lookup = combo_lookup[position][lookup_idx];
169+
const struct combo_cfg *candidate = candidates[candidate_idx].combo;
170+
const struct combo_cfg *lookup = combo_lookup[position][lookup_idx];
171171
if (candidate == NULL || lookup == NULL) {
172172
break;
173173
}
@@ -207,7 +207,7 @@ static int64_t first_candidate_timeout() {
207207
return first_timeout;
208208
}
209209

210-
static inline bool candidate_is_completely_pressed(struct combo_cfg *candidate) {
210+
static inline bool candidate_is_completely_pressed(const struct combo_cfg *candidate) {
211211
// this code assumes set(pressed_keys) <= set(candidate->key_positions)
212212
// this invariant is enforced by filter_candidates
213213
// since events may have been reraised after clearing one or more slots at
@@ -288,7 +288,7 @@ static int release_pressed_keys() {
288288
return count;
289289
}
290290

291-
static inline int press_combo_behavior(struct combo_cfg *combo, int32_t timestamp) {
291+
static inline int press_combo_behavior(const struct combo_cfg *combo, int32_t timestamp) {
292292
struct zmk_behavior_binding_event event = {
293293
.position = combo->virtual_key_position,
294294
.timestamp = timestamp,
@@ -302,7 +302,7 @@ static inline int press_combo_behavior(struct combo_cfg *combo, int32_t timestam
302302
return zmk_behavior_invoke_binding(&combo->behavior, event, true);
303303
}
304304

305-
static inline int release_combo_behavior(struct combo_cfg *combo, int32_t timestamp) {
305+
static inline int release_combo_behavior(const struct combo_cfg *combo, int32_t timestamp) {
306306
struct zmk_behavior_binding_event event = {
307307
.position = combo->virtual_key_position,
308308
.timestamp = timestamp,
@@ -330,7 +330,7 @@ static void move_pressed_keys_to_active_combo(struct active_combo *active_combo)
330330
pressed_keys_count -= combo_length;
331331
}
332332

333-
static struct active_combo *store_active_combo(struct combo_cfg *combo) {
333+
static struct active_combo *store_active_combo(const struct combo_cfg *combo) {
334334
for (int i = 0; i < CONFIG_ZMK_COMBO_MAX_PRESSED_COMBOS; i++) {
335335
if (active_combos[i].combo == NULL) {
336336
active_combos[i].combo = combo;
@@ -344,7 +344,7 @@ static struct active_combo *store_active_combo(struct combo_cfg *combo) {
344344
return NULL;
345345
}
346346

347-
static void activate_combo(struct combo_cfg *combo) {
347+
static void activate_combo(const struct combo_cfg *combo) {
348348
struct active_combo *active_combo = store_active_combo(combo);
349349
if (active_combo == NULL) {
350350
// unable to store combo
@@ -438,7 +438,7 @@ static int position_state_down(const zmk_event_t *ev, struct zmk_position_state_
438438
}
439439
update_timeout_task();
440440

441-
struct combo_cfg *candidate_combo = candidates[0].combo;
441+
const struct combo_cfg *candidate_combo = candidates[0].combo;
442442
LOG_DBG("combo: capturing position event %d", data->position);
443443
int ret = capture_pressed_key(data);
444444
switch (num_candidates) {
@@ -521,7 +521,7 @@ ZMK_SUBSCRIPTION(combo, zmk_position_state_changed);
521521
ZMK_SUBSCRIPTION(combo, zmk_keycode_state_changed);
522522

523523
#define COMBO_INST(n) \
524-
static struct combo_cfg combo_config_##n = { \
524+
static const struct combo_cfg combo_config_##n = { \
525525
.timeout_ms = DT_PROP(n, timeout_ms), \
526526
.require_prior_idle_ms = DT_PROP(n, require_prior_idle_ms), \
527527
.key_positions = DT_PROP(n, key_positions), \

0 commit comments

Comments
 (0)