diff options
| author | Nick Brassel <nick@tzarc.org> | 2022-03-09 19:29:00 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-09 19:29:00 +1100 |
| commit | 8d5eacb7dd76bfd45444ceb1efa9a9f840173552 (patch) | |
| tree | b6b8b641dd61f5de0c5b7ee1bf251f6a84043656 /quantum/action_layer.c | |
| parent | 7121a228eb204a0d697c97503ac7a28b762ab598 (diff) | |
Add support for encoder mapping. (#13286)
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 @@ | |||
| 1 | #include <limits.h> | ||
| 1 | #include <stdint.h> | 2 | #include <stdint.h> |
| 2 | #include "keyboard.h" | 3 | #include "keyboard.h" |
| 4 | #include "keymap.h" | ||
| 3 | #include "action.h" | 5 | #include "action.h" |
| 4 | #include "util.h" | 6 | #include "util.h" |
| 5 | #include "action_layer.h" | 7 | #include "action_layer.h" |
| @@ -223,19 +225,20 @@ void layer_debug(void) { | |||
| 223 | /** \brief source layer cache | 225 | /** \brief source layer cache |
| 224 | */ | 226 | */ |
| 225 | 227 | ||
| 226 | uint8_t source_layers_cache[(MATRIX_ROWS * MATRIX_COLS + 7) / 8][MAX_LAYER_BITS] = {{0}}; | 228 | uint8_t source_layers_cache[((MATRIX_ROWS * MATRIX_COLS) + (CHAR_BIT)-1) / (CHAR_BIT)][MAX_LAYER_BITS] = {{0}}; |
| 229 | # ifdef ENCODER_MAP_ENABLE | ||
| 230 | uint8_t encoder_source_layers_cache[(NUM_ENCODERS + (CHAR_BIT)-1) / (CHAR_BIT)][MAX_LAYER_BITS] = {{0}}; | ||
| 231 | # endif // ENCODER_MAP_ENABLE | ||
| 227 | 232 | ||
| 228 | /** \brief update source layers cache | 233 | /** \brief update source layers cache impl |
| 229 | * | 234 | * |
| 230 | * Updates the cached keys when changing layers | 235 | * Updates the supplied cache when changing layers |
| 231 | */ | 236 | */ |
| 232 | void update_source_layers_cache(keypos_t key, uint8_t layer) { | 237 | void update_source_layers_cache_impl(uint8_t layer, uint16_t entry_number, uint8_t cache[][MAX_LAYER_BITS]) { |
| 233 | const uint8_t key_number = key.col + (key.row * MATRIX_COLS); | 238 | const uint16_t storage_idx = entry_number / (CHAR_BIT); |
| 234 | const uint8_t storage_row = key_number / 8; | 239 | const uint8_t storage_bit = entry_number % (CHAR_BIT); |
| 235 | const uint8_t storage_bit = key_number % 8; | ||
| 236 | |||
| 237 | for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) { | 240 | for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) { |
| 238 | source_layers_cache[storage_row][bit_number] ^= (-((layer & (1U << bit_number)) != 0) ^ source_layers_cache[storage_row][bit_number]) & (1U << storage_bit); | 241 | cache[storage_idx][bit_number] ^= (-((layer & (1U << bit_number)) != 0) ^ cache[storage_idx][bit_number]) & (1U << storage_bit); |
| 239 | } | 242 | } |
| 240 | } | 243 | } |
| 241 | 244 | ||
| @@ -243,18 +246,52 @@ void update_source_layers_cache(keypos_t key, uint8_t layer) { | |||
| 243 | * | 246 | * |
| 244 | * reads the cached keys stored when the layer was changed | 247 | * reads the cached keys stored when the layer was changed |
| 245 | */ | 248 | */ |
| 246 | uint8_t read_source_layers_cache(keypos_t key) { | 249 | uint8_t read_source_layers_cache_impl(uint16_t entry_number, uint8_t cache[][MAX_LAYER_BITS]) { |
| 247 | const uint8_t key_number = key.col + (key.row * MATRIX_COLS); | 250 | const uint16_t storage_idx = entry_number / (CHAR_BIT); |
| 248 | const uint8_t storage_row = key_number / 8; | 251 | const uint8_t storage_bit = entry_number % (CHAR_BIT); |
| 249 | const uint8_t storage_bit = key_number % 8; | 252 | uint8_t layer = 0; |
| 250 | uint8_t layer = 0; | ||
| 251 | 253 | ||
| 252 | for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) { | 254 | for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) { |
| 253 | layer |= ((source_layers_cache[storage_row][bit_number] & (1U << storage_bit)) != 0) << bit_number; | 255 | layer |= ((cache[storage_idx][bit_number] & (1U << storage_bit)) != 0) << bit_number; |
| 254 | } | 256 | } |
| 255 | 257 | ||
| 256 | return layer; | 258 | return layer; |
| 257 | } | 259 | } |
| 260 | |||
| 261 | /** \brief update encoder source layers cache | ||
| 262 | * | ||
| 263 | * Updates the cached encoders when changing layers | ||
| 264 | */ | ||
| 265 | void update_source_layers_cache(keypos_t key, uint8_t layer) { | ||
| 266 | if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { | ||
| 267 | const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; | ||
| 268 | update_source_layers_cache_impl(layer, entry_number, source_layers_cache); | ||
| 269 | } | ||
| 270 | # ifdef ENCODER_MAP_ENABLE | ||
| 271 | else if (key.row == KEYLOC_ENCODER_CW || key.row == KEYLOC_ENCODER_CCW) { | ||
| 272 | const uint16_t entry_number = key.col; | ||
| 273 | update_source_layers_cache_impl(layer, entry_number, encoder_source_layers_cache); | ||
| 274 | } | ||
| 275 | # endif // ENCODER_MAP_ENABLE | ||
| 276 | } | ||
| 277 | |||
| 278 | /** \brief read source layers cache | ||
| 279 | * | ||
| 280 | * reads the cached keys stored when the layer was changed | ||
| 281 | */ | ||
| 282 | uint8_t read_source_layers_cache(keypos_t key) { | ||
| 283 | if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { | ||
| 284 | const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; | ||
| 285 | return read_source_layers_cache_impl(entry_number, source_layers_cache); | ||
| 286 | } | ||
| 287 | # ifdef ENCODER_MAP_ENABLE | ||
| 288 | else if (key.row == KEYLOC_ENCODER_CW || key.row == KEYLOC_ENCODER_CCW) { | ||
| 289 | const uint16_t entry_number = key.col; | ||
| 290 | return read_source_layers_cache_impl(entry_number, encoder_source_layers_cache); | ||
| 291 | } | ||
| 292 | # endif // ENCODER_MAP_ENABLE | ||
| 293 | return 0; | ||
| 294 | } | ||
| 258 | #endif | 295 | #endif |
| 259 | 296 | ||
| 260 | /** \brief Store or get action (FIXME: Needs better summary) | 297 | /** \brief Store or get action (FIXME: Needs better summary) |
