-
-
Notifications
You must be signed in to change notification settings - Fork 39.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update store_or_get_action to cache keycode on key press, return cached keycode on key release #24517
base: develop
Are you sure you want to change the base?
Update store_or_get_action to cache keycode on key press, return cached keycode on key release #24517
Changes from 12 commits
b7dfced
37d8824
e890fa0
9f3e5fa
d3b8178
751ad39
4edc916
168cabe
3727bc7
46d0724
1dc3373
f512383
1378e56
9f71517
610863f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -287,6 +287,64 @@ uint8_t read_source_layers_cache(keypos_t key) { | |
# endif // ENCODER_MAP_ENABLE | ||
return 0; | ||
} | ||
|
||
# ifdef KEYCODE_CACHE_ENABLE | ||
uint16_t keycode_map[((MATRIX_ROWS * MATRIX_COLS) + (CHAR_BIT)-1) / (CHAR_BIT)][16] = {{KC_NO}}; | ||
# ifdef ENCODER_MAP_ENABLE | ||
uint16_t encoder_keycode_map[(NUM_ENCODERS + (CHAR_BIT)-1) / (CHAR_BIT)][16] = {{KC_NO}}; | ||
# endif // ENCODER_MAP_ENABLE | ||
|
||
/** \brief update keycode map | ||
* | ||
* Updates map of keycodes when a key is pressed down | ||
*/ | ||
void update_keycode_map_impl(uint16_t entry_number, uint16_t keycode, uint16_t cache[][16]) { | ||
cache[entry_number / 16][entry_number % 16] = keycode; | ||
} | ||
|
||
/** \brief read keycode map | ||
* | ||
* reads from map of keycodes when a key is released | ||
*/ | ||
uint16_t read_keycode_map_impl(uint16_t entry_number, uint16_t cache[][16]) { | ||
return cache[entry_number / 16][entry_number % 16]; | ||
} | ||
|
||
/** \brief update keycode map | ||
* | ||
* Updates map of keycodes when a key is pressed down | ||
*/ | ||
void update_keycode_map(keypos_t key, uint16_t keycode) { | ||
if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { | ||
const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; | ||
update_keycode_map_impl(entry_number, keycode, keycode_map); | ||
} | ||
# ifdef ENCODER_MAP_ENABLE | ||
else if (key.row == KEYLOC_ENCODER_CW || key.row == KEYLOC_ENCODER_CCW) { | ||
const uint16_t entry_number = key.col; | ||
update_keycode_map_impl(entry_number, keycode, encoder_keycode_map); | ||
} | ||
# endif // ENCODER_MAP_ENABLE | ||
} | ||
|
||
/** \brief read keycode map | ||
* | ||
* reads from map of keycodes when a key is released | ||
*/ | ||
uint16_t read_keycode_map(keypos_t key) { | ||
if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { | ||
const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; | ||
return read_keycode_map_impl(entry_number, keycode_map); | ||
} | ||
# ifdef ENCODER_MAP_ENABLE | ||
else if (key.row == KEYLOC_ENCODER_CW || key.row == KEYLOC_ENCODER_CCW) { | ||
const uint16_t entry_number = key.col; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general you may need to consider There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since I removed the encoder handling code, is this safe to not consider? |
||
return read_keycode_map_impl(entry_number, encoder_keycode_map); | ||
} | ||
# endif // ENCODER_MAP_ENABLE | ||
return KC_NO; | ||
} | ||
# endif | ||
#endif | ||
|
||
/** \brief Store or get action (FIXME: Needs better summary) | ||
|
@@ -303,14 +361,27 @@ action_t store_or_get_action(bool pressed, keypos_t key) { | |
} | ||
|
||
uint8_t layer; | ||
|
||
# ifdef KEYCODE_CACHE_ENABLE | ||
uint16_t keycode; | ||
# endif | ||
if (pressed) { | ||
layer = layer_switch_get_layer(key); | ||
update_source_layers_cache(key, layer); | ||
# ifdef KEYCODE_CACHE_ENABLE | ||
keycode = keymap_key_to_keycode(layer, key); | ||
update_keycode_map(key, keycode); | ||
# endif | ||
} else { | ||
layer = read_source_layers_cache(key); | ||
# ifdef KEYCODE_CACHE_ENABLE | ||
keycode = read_keycode_map(key); | ||
# endif | ||
} | ||
# ifndef KEYCODE_CACHE_ENABLE | ||
return action_for_key(layer, key); | ||
# else | ||
return action_for_keycode(keycode); | ||
# endif | ||
#else | ||
return layer_switch_get_action(key); | ||
#endif | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks overcomplicated. The source layer cache code uses such tricks to avoid allocating a whole byte for each entry that needs just 2 bits in the most common case (4 layers); if you are storing the whole 16-bit keycode, you may just use
and
(Actually the encoder code might not need such handling at all, because the press and release events are emitted without running the rest of main loop code in between, therefore keymap updates should not happen.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a ton for the feedback, I cleaned up the overcomplicated keycode_map and also removed the encoder handling