diff options
Diffstat (limited to 'quantum/action_layer.c')
-rw-r--r-- | quantum/action_layer.c | 67 |
1 files changed, 52 insertions, 15 deletions
diff --git a/quantum/action_layer.c b/quantum/action_layer.c index e20eedee40..4d2921354b 100644 --- a/quantum/action_layer.c +++ b/quantum/action_layer.c @@ -1,5 +1,7 @@ +#include <limits.h> #include <stdint.h> #include "keyboard.h" +#include "keymap.h" #include "action.h" #include "util.h" #include "action_layer.h" @@ -223,19 +225,20 @@ void layer_debug(void) { /** \brief source layer cache */ -uint8_t source_layers_cache[(MATRIX_ROWS * MATRIX_COLS + 7) / 8][MAX_LAYER_BITS] = {{0}}; +uint8_t source_layers_cache[((MATRIX_ROWS * MATRIX_COLS) + (CHAR_BIT)-1) / (CHAR_BIT)][MAX_LAYER_BITS] = {{0}}; +# ifdef ENCODER_MAP_ENABLE +uint8_t encoder_source_layers_cache[(NUM_ENCODERS + (CHAR_BIT)-1) / (CHAR_BIT)][MAX_LAYER_BITS] = {{0}}; +# endif // ENCODER_MAP_ENABLE -/** \brief update source layers cache +/** \brief update source layers cache impl * - * Updates the cached keys when changing layers + * Updates the supplied cache when changing layers */ -void update_source_layers_cache(keypos_t key, uint8_t layer) { - const uint8_t key_number = key.col + (key.row * MATRIX_COLS); - const uint8_t storage_row = key_number / 8; - const uint8_t storage_bit = key_number % 8; - +void update_source_layers_cache_impl(uint8_t layer, uint16_t entry_number, uint8_t cache[][MAX_LAYER_BITS]) { + const uint16_t storage_idx = entry_number / (CHAR_BIT); + const uint8_t storage_bit = entry_number % (CHAR_BIT); for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) { - source_layers_cache[storage_row][bit_number] ^= (-((layer & (1U << bit_number)) != 0) ^ source_layers_cache[storage_row][bit_number]) & (1U << storage_bit); + cache[storage_idx][bit_number] ^= (-((layer & (1U << bit_number)) != 0) ^ cache[storage_idx][bit_number]) & (1U << storage_bit); } } @@ -243,18 +246,52 @@ void update_source_layers_cache(keypos_t key, uint8_t layer) { * * reads the cached keys stored when the layer was changed */ -uint8_t read_source_layers_cache(keypos_t key) { - const uint8_t key_number = key.col + (key.row * MATRIX_COLS); - const uint8_t storage_row = key_number / 8; - const uint8_t storage_bit = key_number % 8; - uint8_t layer = 0; +uint8_t read_source_layers_cache_impl(uint16_t entry_number, uint8_t cache[][MAX_LAYER_BITS]) { + const uint16_t storage_idx = entry_number / (CHAR_BIT); + const uint8_t storage_bit = entry_number % (CHAR_BIT); + uint8_t layer = 0; for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) { - layer |= ((source_layers_cache[storage_row][bit_number] & (1U << storage_bit)) != 0) << bit_number; + layer |= ((cache[storage_idx][bit_number] & (1U << storage_bit)) != 0) << bit_number; } return layer; } + +/** \brief update encoder source layers cache + * + * Updates the cached encoders when changing layers + */ +void update_source_layers_cache(keypos_t key, uint8_t layer) { + if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { + const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; + update_source_layers_cache_impl(layer, entry_number, source_layers_cache); + } +# ifdef ENCODER_MAP_ENABLE + else if (key.row == KEYLOC_ENCODER_CW || key.row == KEYLOC_ENCODER_CCW) { + const uint16_t entry_number = key.col; + update_source_layers_cache_impl(layer, entry_number, encoder_source_layers_cache); + } +# endif // ENCODER_MAP_ENABLE +} + +/** \brief read source layers cache + * + * reads the cached keys stored when the layer was changed + */ +uint8_t read_source_layers_cache(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_source_layers_cache_impl(entry_number, source_layers_cache); + } +# ifdef ENCODER_MAP_ENABLE + else if (key.row == KEYLOC_ENCODER_CW || key.row == KEYLOC_ENCODER_CCW) { + const uint16_t entry_number = key.col; + return read_source_layers_cache_impl(entry_number, encoder_source_layers_cache); + } +# endif // ENCODER_MAP_ENABLE + return 0; +} #endif /** \brief Store or get action (FIXME: Needs better summary) |