Skip to content

Commit

Permalink
fix: Reduce RAM usage in the keymap
Browse files Browse the repository at this point in the history
* When not building with runtime keymap support, make the keymap
  const.
  • Loading branch information
petejohanson committed Sep 17, 2024
1 parent 32d0a4b commit a9167b2
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions app/src/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static uint8_t keymap_layer_orders[ZMK_KEYMAP_LAYERS_LEN];
(DT_INST_FOREACH_CHILD_SEP(0, TRANSFORMED_LAYER, (, ))), \
(DT_INST_FOREACH_CHILD_STATUS_OKAY_SEP(0, TRANSFORMED_LAYER, (, ))))};

KEYMAP_VAR(zmk_keymap, )
KEYMAP_VAR(zmk_keymap, COND_CODE_1(IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE), (), (const)))

#if IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE)

Expand Down Expand Up @@ -238,8 +238,6 @@ zmk_keymap_get_layer_binding_at_idx(zmk_keymap_layer_id_t layer_id, uint8_t bind

static uint8_t zmk_keymap_layer_pending_changes[ZMK_KEYMAP_LAYERS_LEN][PENDING_ARRAY_SIZE];

#endif // IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE)

int zmk_keymap_set_layer_binding_at_idx(zmk_keymap_layer_id_t layer_id, uint8_t binding_idx,
struct zmk_behavior_binding binding) {
if (binding_idx >= ZMK_KEYMAP_LEN) {
Expand All @@ -248,18 +246,24 @@ int zmk_keymap_set_layer_binding_at_idx(zmk_keymap_layer_id_t layer_id, uint8_t

ASSERT_LAYER_VAL(layer_id, -EINVAL)

#if IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE)
uint8_t *pending = zmk_keymap_layer_pending_changes[layer_id];

WRITE_BIT(pending[binding_idx / 8], binding_idx % 8, 1);
#endif // IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE)

// TODO: Need a mutex to protect access to the keymap data?
memcpy(&zmk_keymap[layer_id][binding_idx], &binding, sizeof(binding));

return 0;
}

#else

int zmk_keymap_set_layer_binding_at_idx(zmk_keymap_layer_id_t layer_id, uint8_t binding_idx,
struct zmk_behavior_binding binding) {
return -ENOTSUP;
}

#endif // IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE)
#if IS_ENABLED(CONFIG_ZMK_KEYMAP_LAYER_REORDERING)

#if IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE)
Expand Down

0 comments on commit a9167b2

Please sign in to comment.