Add support for encoder mapping. (#13286)
This commit is contained in:
		
							parent
							
								
									7121a228eb
								
							
						
					
					
						commit
						8d5eacb7dd
					
				| @ -25,6 +25,7 @@ GENERIC_FEATURES = \ | ||||
|     DYNAMIC_KEYMAP \
 | ||||
|     DYNAMIC_MACRO \
 | ||||
|     ENCODER \
 | ||||
|     ENCODER_MAP \
 | ||||
|     GRAVE_ESC \
 | ||||
|     HAPTIC \
 | ||||
|     KEY_LOCK \
 | ||||
|  | ||||
| @ -57,6 +57,7 @@ OTHER_OPTION_NAMES = \ | ||||
|   HELIX ZINC \
 | ||||
|   AUTOLOG_ENABLE \
 | ||||
|   DEBUG_ENABLE \
 | ||||
|   ENCODER_MAP_ENABLE \
 | ||||
|   ENCODER_ENABLE_CUSTOM \
 | ||||
|   GERMAN_ENABLE \
 | ||||
|   HAPTIC_ENABLE \
 | ||||
|  | ||||
| @ -67,9 +67,30 @@ Additionally, if one side does not have an encoder, you can specify `{}` for the | ||||
| #define ENCODER_RESOLUTIONS_RIGHT { 4 } | ||||
| ``` | ||||
| 
 | ||||
| ## Encoder map | ||||
| 
 | ||||
| Encoder mapping may be added to your `keymap.c`, which replicates the normal keyswitch layer handling functionality, but with encoders. Add this to your `rules.mk`: | ||||
| 
 | ||||
| ```make | ||||
| ENCODER_MAP_ENABLE = yes | ||||
| ``` | ||||
| 
 | ||||
| Your `keymap.c` will then need an encoder mapping defined (for four layers and two encoders): | ||||
| 
 | ||||
| ```c | ||||
| #if defined(ENCODER_MAP_ENABLE) | ||||
| const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][2] = { | ||||
|     [_BASE] =   { ENCODER_CCW_CW(KC_MS_WH_UP, KC_MS_WH_DOWN), ENCODER_CCW_CW(KC_VOLD, KC_VOLU)  }, | ||||
|     [_LOWER] =  { ENCODER_CCW_CW(RGB_HUD, RGB_HUI),           ENCODER_CCW_CW(RGB_SAD, RGB_SAI)  }, | ||||
|     [_RAISE] =  { ENCODER_CCW_CW(RGB_VAD, RGB_VAI),           ENCODER_CCW_CW(RGB_SPD, RGB_SPI)  }, | ||||
|     [_ADJUST] = { ENCODER_CCW_CW(RGB_RMOD, RGB_MOD),          ENCODER_CCW_CW(KC_RIGHT, KC_LEFT) }, | ||||
| }; | ||||
| #endif | ||||
| ``` | ||||
| 
 | ||||
| ## Callbacks | ||||
| 
 | ||||
| The callback functions can be inserted into your `<keyboard>.c`: | ||||
| When not using `ENCODER_MAP_ENABLE = yes`, the callback functions can be inserted into your `<keyboard>.c`: | ||||
| 
 | ||||
| ```c | ||||
| bool encoder_update_kb(uint8_t index, bool clockwise) { | ||||
|  | ||||
| @ -31,3 +31,16 @@ Note that the array indices are reversed same as the matrix and the values are o | ||||
| |`SH_OS`    |One shot swap hands: toggles while pressed or until next key press.      | | ||||
| 
 | ||||
| `SH_TT` swap-hands tap-toggle key is similar to [layer tap-toggle](feature_layers.md?id=switching-and-toggling-layers). Tapping repeatedly (5 taps by default) will toggle swap-hands on or off, like `SH_TG`. Tap-toggle count can be changed by defining a value for `TAPPING_TOGGLE`. | ||||
| 
 | ||||
| ## Encoder Mapping | ||||
| 
 | ||||
| When using an encoder mapping, it's also able to handle swapping encoders between sides, too. | ||||
| 
 | ||||
| Encoder indexes are defined as left-to-right, and the extent of the array needs to match the number of encoders on the keyboard. | ||||
| 
 | ||||
| As an example, if a split keyboard has a single encoder per side, you can swap the order by using the following code in your keymap: | ||||
| ```c | ||||
| #if defined(SWAP_HANDS_ENABLE) && defined(ENCODER_MAP_ENABLE) | ||||
| const uint8_t PROGMEM encoder_hand_swap_config[NUM_ENCODERS] = { 1, 0 }; | ||||
| #endif | ||||
| ``` | ||||
|  | ||||
| @ -17,5 +17,6 @@ BACKLIGHT_ENABLE = yes      # Enable keyboard backlight functionality | ||||
| RGBLIGHT_ENABLE = yes       # Enable keyboard RGB underglow | ||||
| AUDIO_ENABLE = no           # Audio output | ||||
| ENCODER_ENABLE = yes | ||||
| LTO_ENABLE = yes | ||||
| 
 | ||||
| LAYOUTS = 66_ansi 66_iso | ||||
|  | ||||
| @ -12,8 +12,7 @@ from milc import cli | ||||
| def pytest(cli): | ||||
|     """Run several linting/testing commands. | ||||
|     """ | ||||
|     nose2 = cli.run(['nose2', '-v', '-t' | ||||
|                      'lib/python', *cli.args.test], capture_output=False, stdin=DEVNULL) | ||||
|     nose2 = cli.run(['nose2', '-v', '-t', 'lib/python', *cli.args.test], capture_output=False, stdin=DEVNULL) | ||||
|     flake8 = cli.run(['flake8', 'lib/python'], capture_output=False, stdin=DEVNULL) | ||||
| 
 | ||||
|     return flake8.returncode | nose2.returncode | ||||
|  | ||||
| @ -14,9 +14,11 @@ GNU General Public License for more details. | ||||
| You should have received a copy of the GNU General Public License | ||||
| along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | ||||
| */ | ||||
| #include <limits.h> | ||||
| #include "host.h" | ||||
| #include "keycode.h" | ||||
| #include "keyboard.h" | ||||
| #include "keymap.h" | ||||
| #include "mousekey.h" | ||||
| #include "programmable_button.h" | ||||
| #include "command.h" | ||||
| @ -89,6 +91,7 @@ void action_exec(keyevent_t event) { | ||||
|     } | ||||
| 
 | ||||
| #ifdef SWAP_HANDS_ENABLE | ||||
|     // Swap hands handles both keys and encoders, if ENCODER_MAP_ENABLE is defined.
 | ||||
|     if (!IS_NOEVENT(event)) { | ||||
|         process_hand_swap(&event); | ||||
|     } | ||||
| @ -136,27 +139,65 @@ void action_exec(keyevent_t event) { | ||||
| } | ||||
| 
 | ||||
| #ifdef SWAP_HANDS_ENABLE | ||||
| extern const keypos_t PROGMEM hand_swap_config[MATRIX_ROWS][MATRIX_COLS]; | ||||
| #    ifdef ENCODER_MAP_ENABLE | ||||
| extern const uint8_t PROGMEM encoder_hand_swap_config[NUM_ENCODERS]; | ||||
| #    endif // ENCODER_MAP_ENABLE
 | ||||
| 
 | ||||
| bool swap_hands = false; | ||||
| bool swap_held  = false; | ||||
| 
 | ||||
| bool should_swap_hands(size_t index, uint8_t *swap_state, bool pressed) { | ||||
|     size_t  array_index = index / (CHAR_BIT); | ||||
|     size_t  bit_index   = index % (CHAR_BIT); | ||||
|     uint8_t bit_val     = 1 << bit_index; | ||||
|     bool    do_swap     = pressed ? swap_hands : swap_state[array_index] & bit_val; | ||||
|     return do_swap; | ||||
| } | ||||
| 
 | ||||
| void set_swap_hands_state(size_t index, uint8_t *swap_state, bool on) { | ||||
|     size_t  array_index = index / (CHAR_BIT); | ||||
|     size_t  bit_index   = index % (CHAR_BIT); | ||||
|     uint8_t bit_val     = 1 << bit_index; | ||||
|     if (on) { | ||||
|         swap_state[array_index] |= bit_val; | ||||
|     } else { | ||||
|         swap_state[array_index] &= ~bit_val; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /** \brief Process Hand Swap
 | ||||
|  * | ||||
|  * FIXME: Needs documentation. | ||||
|  */ | ||||
| void process_hand_swap(keyevent_t *event) { | ||||
|     static swap_state_row_t swap_state[MATRIX_ROWS]; | ||||
| 
 | ||||
|     keypos_t         pos     = event->key; | ||||
|     swap_state_row_t col_bit = (swap_state_row_t)1 << pos.col; | ||||
|     bool             do_swap = event->pressed ? swap_hands : swap_state[pos.row] & (col_bit); | ||||
| 
 | ||||
|     if (do_swap) { | ||||
|         event->key.row = pgm_read_byte(&hand_swap_config[pos.row][pos.col].row); | ||||
|         event->key.col = pgm_read_byte(&hand_swap_config[pos.row][pos.col].col); | ||||
|         swap_state[pos.row] |= col_bit; | ||||
|     } else { | ||||
|         swap_state[pos.row] &= ~(col_bit); | ||||
|     keypos_t pos = event->key; | ||||
|     if (pos.row < MATRIX_ROWS && pos.col < MATRIX_COLS) { | ||||
|         static uint8_t matrix_swap_state[((MATRIX_ROWS * MATRIX_COLS) + (CHAR_BIT)-1) / (CHAR_BIT)]; | ||||
|         size_t         index   = (size_t)(pos.row * MATRIX_COLS) + pos.col; | ||||
|         bool           do_swap = should_swap_hands(index, matrix_swap_state, event->pressed); | ||||
|         if (do_swap) { | ||||
|             event->key.row = pgm_read_byte(&hand_swap_config[pos.row][pos.col].row); | ||||
|             event->key.col = pgm_read_byte(&hand_swap_config[pos.row][pos.col].col); | ||||
|             set_swap_hands_state(index, matrix_swap_state, true); | ||||
|         } else { | ||||
|             set_swap_hands_state(index, matrix_swap_state, false); | ||||
|         } | ||||
|     } | ||||
| #    ifdef ENCODER_MAP_ENABLE | ||||
|     else if (pos.row == KEYLOC_ENCODER_CW || pos.row == KEYLOC_ENCODER_CCW) { | ||||
|         static uint8_t encoder_swap_state[((NUM_ENCODERS) + (CHAR_BIT)-1) / (CHAR_BIT)]; | ||||
|         size_t         index   = pos.col; | ||||
|         bool           do_swap = should_swap_hands(index, encoder_swap_state, event->pressed); | ||||
|         if (do_swap) { | ||||
|             event->key.row = pos.row; | ||||
|             event->key.col = pgm_read_byte(&encoder_hand_swap_config[pos.col]); | ||||
|             set_swap_hands_state(index, encoder_swap_state, true); | ||||
|         } else { | ||||
|             set_swap_hands_state(index, encoder_swap_state, false); | ||||
|         } | ||||
|     } | ||||
| #    endif // ENCODER_MAP_ENABLE
 | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
| @ -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)
 | ||||
|  | ||||
| @ -58,9 +58,14 @@ | ||||
| #    endif | ||||
| #endif | ||||
| 
 | ||||
| // Dynamic macro starts after dynamic keymaps
 | ||||
| // Dynamic encoders starts after dynamic keymaps
 | ||||
| #ifndef DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR | ||||
| #    define DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2)) | ||||
| #endif | ||||
| 
 | ||||
| // Dynamic macro starts after dynamic encoders
 | ||||
| #ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR | ||||
| #    define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2)) | ||||
| #    define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * NUM_ENCODERS * 2 * 2)) | ||||
| #endif | ||||
| 
 | ||||
| // Sanity check that dynamic keymaps fit in available EEPROM
 | ||||
| @ -89,6 +94,7 @@ void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t c | ||||
| } | ||||
| 
 | ||||
| uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) { | ||||
|     if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return KC_NO; | ||||
|     void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column); | ||||
|     // Big endian, so we can read/write EEPROM directly from host if we want
 | ||||
|     uint16_t keycode = eeprom_read_byte(address) << 8; | ||||
| @ -97,12 +103,36 @@ uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) | ||||
| } | ||||
| 
 | ||||
| void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) { | ||||
|     if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return; | ||||
|     void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column); | ||||
|     // Big endian, so we can read/write EEPROM directly from host if we want
 | ||||
|     eeprom_update_byte(address, (uint8_t)(keycode >> 8)); | ||||
|     eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF)); | ||||
| } | ||||
| 
 | ||||
| #ifdef ENCODER_MAP_ENABLE | ||||
| void *dynamic_keymap_encoder_to_eeprom_address(uint8_t layer, uint8_t encoder_id) { | ||||
|     return ((void *)DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR) + (layer * NUM_ENCODERS * 2 * 2) + (encoder_id * 2 * 2); | ||||
| } | ||||
| 
 | ||||
| uint16_t dynamic_keymap_get_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise) { | ||||
|     if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return KC_NO; | ||||
|     void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id); | ||||
|     // Big endian, so we can read/write EEPROM directly from host if we want
 | ||||
|     uint16_t keycode = ((uint16_t)eeprom_read_byte(address + (clockwise ? 0 : 2))) << 8; | ||||
|     keycode |= eeprom_read_byte(address + (clockwise ? 0 : 2) + 1); | ||||
|     return keycode; | ||||
| } | ||||
| 
 | ||||
| void dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise, uint16_t keycode) { | ||||
|     if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return; | ||||
|     void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id); | ||||
|     // Big endian, so we can read/write EEPROM directly from host if we want
 | ||||
|     eeprom_update_byte(address + (clockwise ? 0 : 2), (uint8_t)(keycode >> 8)); | ||||
|     eeprom_update_byte(address + (clockwise ? 0 : 2) + 1, (uint8_t)(keycode & 0xFF)); | ||||
| } | ||||
| #endif // ENCODER_MAP_ENABLE
 | ||||
| 
 | ||||
| void dynamic_keymap_reset(void) { | ||||
|     // Reset the keymaps in EEPROM to what is in flash.
 | ||||
|     // All keyboards using dynamic keymaps should define a layout
 | ||||
| @ -113,6 +143,12 @@ void dynamic_keymap_reset(void) { | ||||
|                 dynamic_keymap_set_keycode(layer, row, column, pgm_read_word(&keymaps[layer][row][column])); | ||||
|             } | ||||
|         } | ||||
| #ifdef ENCODER_MAP_ENABLE | ||||
|         for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) { | ||||
|             dynamic_keymap_set_encoder(layer, encoder, true, pgm_read_word(&encoder_map[layer][encoder][0])); | ||||
|             dynamic_keymap_set_encoder(layer, encoder, false, pgm_read_word(&encoder_map[layer][encoder][1])); | ||||
|         } | ||||
| #endif // ENCODER_MAP_ENABLE
 | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @ -148,9 +184,15 @@ void dynamic_keymap_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) { | ||||
| uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) { | ||||
|     if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { | ||||
|         return dynamic_keymap_get_keycode(layer, key.row, key.col); | ||||
|     } else { | ||||
|         return KC_NO; | ||||
|     } | ||||
| #ifdef ENCODER_MAP_ENABLE | ||||
|     else if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && key.row == KEYLOC_ENCODER_CW && key.col < NUM_ENCODERS) { | ||||
|         return dynamic_keymap_get_encoder(layer, key.col, true); | ||||
|     } else if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && key.row == KEYLOC_ENCODER_CCW && key.col < NUM_ENCODERS) { | ||||
|         return dynamic_keymap_get_encoder(layer, key.col, false); | ||||
|     } | ||||
| #endif // ENCODER_MAP_ENABLE
 | ||||
|     return KC_NO; | ||||
| } | ||||
| 
 | ||||
| uint8_t dynamic_keymap_macro_get_count(void) { | ||||
|  | ||||
| @ -22,7 +22,11 @@ uint8_t  dynamic_keymap_get_layer_count(void); | ||||
| void *   dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column); | ||||
| uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column); | ||||
| void     dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode); | ||||
| void     dynamic_keymap_reset(void); | ||||
| #ifdef ENCODER_MAP_ENABLE | ||||
| uint16_t dynamic_keymap_get_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise); | ||||
| void     dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise, uint16_t keycode); | ||||
| #endif // ENCODER_MAP_ENABLE
 | ||||
| void dynamic_keymap_reset(void); | ||||
| // These get/set the keycodes as stored in the EEPROM buffer
 | ||||
| // Data is big-endian 16-bit values (the keycodes)
 | ||||
| // Order is by layer/row/column
 | ||||
|  | ||||
| @ -23,6 +23,10 @@ | ||||
| // for memcpy
 | ||||
| #include <string.h> | ||||
| 
 | ||||
| #ifndef ENCODER_MAP_KEY_DELAY | ||||
| #    define ENCODER_MAP_KEY_DELAY 2 | ||||
| #endif | ||||
| 
 | ||||
| #if !defined(ENCODER_RESOLUTIONS) && !defined(ENCODER_RESOLUTION) | ||||
| #    define ENCODER_RESOLUTION 4 | ||||
| #endif | ||||
| @ -135,6 +139,16 @@ void encoder_init(void) { | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| #ifdef ENCODER_MAP_ENABLE | ||||
| static void encoder_exec_mapping(uint8_t index, bool clockwise) { | ||||
|     // The delays below cater for Windows and its wonderful requirements.
 | ||||
|     action_exec(clockwise ? ENCODER_CW_EVENT(index, true) : ENCODER_CCW_EVENT(index, true)); | ||||
|     wait_ms(ENCODER_MAP_KEY_DELAY); | ||||
|     action_exec(clockwise ? ENCODER_CW_EVENT(index, false) : ENCODER_CCW_EVENT(index, false)); | ||||
|     wait_ms(ENCODER_MAP_KEY_DELAY); | ||||
| } | ||||
| #endif // ENCODER_MAP_ENABLE
 | ||||
| 
 | ||||
| static bool encoder_update(uint8_t index, uint8_t state) { | ||||
|     bool    changed = false; | ||||
|     uint8_t i       = index; | ||||
| @ -152,12 +166,20 @@ static bool encoder_update(uint8_t index, uint8_t state) { | ||||
|     if (encoder_pulses[i] >= resolution) { | ||||
|         encoder_value[index]++; | ||||
|         changed = true; | ||||
| #ifdef ENCODER_MAP_ENABLE | ||||
|         encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE); | ||||
| #else  // ENCODER_MAP_ENABLE
 | ||||
|         encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE); | ||||
| #endif // ENCODER_MAP_ENABLE
 | ||||
|     } | ||||
|     if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
 | ||||
|         encoder_value[index]--; | ||||
|         changed = true; | ||||
| #ifdef ENCODER_MAP_ENABLE | ||||
|         encoder_exec_mapping(index, ENCODER_CLOCKWISE); | ||||
| #else  // ENCODER_MAP_ENABLE
 | ||||
|         encoder_update_kb(index, ENCODER_CLOCKWISE); | ||||
| #endif // ENCODER_MAP_ENABLE
 | ||||
|     } | ||||
|     encoder_pulses[i] %= resolution; | ||||
| #ifdef ENCODER_DEFAULT_POS | ||||
| @ -197,13 +219,21 @@ void encoder_update_raw(uint8_t *slave_state) { | ||||
|             delta--; | ||||
|             encoder_value[index]++; | ||||
|             changed = true; | ||||
| #    ifdef ENCODER_MAP_ENABLE | ||||
|             encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE); | ||||
| #    else  // ENCODER_MAP_ENABLE
 | ||||
|             encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE); | ||||
| #    endif // ENCODER_MAP_ENABLE
 | ||||
|         } | ||||
|         while (delta < 0) { | ||||
|             delta++; | ||||
|             encoder_value[index]--; | ||||
|             changed = true; | ||||
| #    ifdef ENCODER_MAP_ENABLE | ||||
|             encoder_exec_mapping(index, ENCODER_CLOCKWISE); | ||||
| #    else  // ENCODER_MAP_ENABLE
 | ||||
|             encoder_update_kb(index, ENCODER_CLOCKWISE); | ||||
| #    endif // ENCODER_MAP_ENABLE
 | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -55,3 +55,9 @@ void encoder_update_raw(uint8_t* slave_state); | ||||
| #endif // NUM_ENCODERS
 | ||||
| 
 | ||||
| #define NUM_ENCODERS_MAX_PER_SIDE MAX(NUM_ENCODERS_LEFT, NUM_ENCODERS_RIGHT) | ||||
| 
 | ||||
| #ifdef ENCODER_MAP_ENABLE | ||||
| #    define ENCODER_CCW_CW(ccw, cw) \ | ||||
|         { (cw), (ccw) } | ||||
| extern const uint16_t encoder_map[][NUM_ENCODERS][2]; | ||||
| #endif // ENCODER_MAP_ENABLE
 | ||||
|  | ||||
| @ -40,25 +40,47 @@ typedef struct { | ||||
| /* equivalent test of keypos_t */ | ||||
| #define KEYEQ(keya, keyb) ((keya).row == (keyb).row && (keya).col == (keyb).col) | ||||
| 
 | ||||
| /* special keypos_t entries */ | ||||
| #define KEYLOC_TICK 255 | ||||
| #define KEYLOC_COMBO 254 | ||||
| #define KEYLOC_ENCODER_CW 253 | ||||
| #define KEYLOC_ENCODER_CCW 252 | ||||
| 
 | ||||
| /* Rules for No Event:
 | ||||
|  * 1) (time == 0) to handle (keyevent_t){} as empty event | ||||
|  * 2) Matrix(255, 255) to make TICK event available | ||||
|  */ | ||||
| static inline bool IS_NOEVENT(keyevent_t event) { | ||||
|     return event.time == 0 || (event.key.row == 255 && event.key.col == 255); | ||||
|     return event.time == 0 || (event.key.row == KEYLOC_TICK && event.key.col == KEYLOC_TICK); | ||||
| } | ||||
| static inline bool IS_KEYEVENT(keyevent_t event) { | ||||
|     return event.key.row < MATRIX_ROWS && event.key.col < MATRIX_COLS; | ||||
| } | ||||
| static inline bool IS_COMBOEVENT(keyevent_t event) { | ||||
|     return event.key.row == KEYLOC_COMBO; | ||||
| } | ||||
| static inline bool IS_ENCODEREVENT(keyevent_t event) { | ||||
|     return event.key.row == KEYLOC_ENCODER_CW || event.key.row == KEYLOC_ENCODER_CCW; | ||||
| } | ||||
| static inline bool IS_PRESSED(keyevent_t event) { | ||||
|     return (!IS_NOEVENT(event) && event.pressed); | ||||
|     return !IS_NOEVENT(event) && event.pressed; | ||||
| } | ||||
| static inline bool IS_RELEASED(keyevent_t event) { | ||||
|     return (!IS_NOEVENT(event) && !event.pressed); | ||||
|     return !IS_NOEVENT(event) && !event.pressed; | ||||
| } | ||||
| 
 | ||||
| /* Common keyevent object factory */ | ||||
| #define MAKE_KEYPOS(row_num, col_num) ((keypos_t){.row = (row_num), .col = (col_num)}) | ||||
| #define MAKE_KEYEVENT(row_num, col_num, press) ((keyevent_t){.key = MAKE_KEYPOS((row_num), (col_num)), .pressed = (press), .time = (timer_read() | 1)}) | ||||
| 
 | ||||
| /* Tick event */ | ||||
| #define TICK                                                                                    \ | ||||
|     (keyevent_t) {                                                                              \ | ||||
|         .key = (keypos_t){.row = 255, .col = 255}, .pressed = false, .time = (timer_read() | 1) \ | ||||
|     } | ||||
| #define TICK MAKE_KEYEVENT(KEYLOC_TICK, KEYLOC_TICK, false) | ||||
| 
 | ||||
| #ifdef ENCODER_MAP_ENABLE | ||||
| /* Encoder events */ | ||||
| #    define ENCODER_CW_EVENT(enc_id, press) MAKE_KEYEVENT(KEYLOC_ENCODER_CW, (enc_id), (press)) | ||||
| #    define ENCODER_CCW_EVENT(enc_id, press) MAKE_KEYEVENT(KEYLOC_ENCODER_CCW, (enc_id), (press)) | ||||
| #endif // ENCODER_MAP_ENABLE
 | ||||
| 
 | ||||
| /* it runs once at early stage of startup before keyboard_init. */ | ||||
| void keyboard_setup(void); | ||||
|  | ||||
| @ -32,6 +32,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||
| // #include "print.h"
 | ||||
| #include "debug.h" | ||||
| #include "keycode_config.h" | ||||
| #include "gpio.h" // for pin_t | ||||
| 
 | ||||
| // ChibiOS uses RESET in its FlagStatus enumeration
 | ||||
| // Therefore define it as QK_BOOTLOADER here, to avoid name collision
 | ||||
| @ -49,3 +50,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||
| uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key); | ||||
| 
 | ||||
| extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS]; | ||||
| 
 | ||||
| #ifdef ENCODER_MAP_ENABLE | ||||
| // Ensure we have a forward declaration for the encoder map
 | ||||
| #    include "encoder.h" | ||||
| #endif | ||||
|  | ||||
| @ -148,6 +148,15 @@ action_t action_for_keycode(uint16_t keycode) { | ||||
| 
 | ||||
| // translates key to keycode
 | ||||
| __attribute__((weak)) uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) { | ||||
|     // Read entire word (16bits)
 | ||||
|     return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]); | ||||
|     if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { | ||||
|         return pgm_read_word(&keymaps[layer][key.row][key.col]); | ||||
|     } | ||||
| #ifdef ENCODER_MAP_ENABLE | ||||
|     else if (key.row == KEYLOC_ENCODER_CW && key.col < NUM_ENCODERS) { | ||||
|         return pgm_read_word(&encoder_map[layer][key.col][0]); | ||||
|     } else if (key.row == KEYLOC_ENCODER_CCW && key.col < NUM_ENCODERS) { | ||||
|         return pgm_read_word(&encoder_map[layer][key.col][1]); | ||||
|     } | ||||
| #endif // ENCODER_MAP_ENABLE
 | ||||
|     return KC_NO; | ||||
| } | ||||
|  | ||||
| @ -88,8 +88,6 @@ static queued_combo_t combo_buffer[COMBO_BUFFER_LENGTH]; | ||||
| 
 | ||||
| #define INCREMENT_MOD(i) i = (i + 1) % COMBO_BUFFER_LENGTH | ||||
| 
 | ||||
| #define COMBO_KEY_POS ((keypos_t){.col = 254, .row = 254}) | ||||
| 
 | ||||
| #ifndef EXTRA_SHORT_COMBOS | ||||
| /* flags are their own elements in combo_t struct. */ | ||||
| #    define COMBO_ACTIVE(combo) (combo->active) | ||||
| @ -140,12 +138,7 @@ static queued_combo_t combo_buffer[COMBO_BUFFER_LENGTH]; | ||||
| static inline void release_combo(uint16_t combo_index, combo_t *combo) { | ||||
|     if (combo->keycode) { | ||||
|         keyrecord_t record = { | ||||
|             .event = | ||||
|                 { | ||||
|                     .key     = COMBO_KEY_POS, | ||||
|                     .time    = timer_read() | 1, | ||||
|                     .pressed = false, | ||||
|                 }, | ||||
|             .event   = MAKE_KEYEVENT(KEYLOC_COMBO, KEYLOC_COMBO, false), | ||||
|             .keycode = combo->keycode, | ||||
|         }; | ||||
| #ifndef NO_ACTION_TAPPING | ||||
| @ -325,7 +318,7 @@ void apply_combo(uint16_t combo_index, combo_t *combo) { | ||||
|         if (ALL_COMBO_KEYS_ARE_DOWN(state, key_count)) { | ||||
|             // this in the end executes the combo when the key_buffer is dumped.
 | ||||
|             record->keycode   = combo->keycode; | ||||
|             record->event.key = COMBO_KEY_POS; | ||||
|             record->event.key = MAKE_KEYPOS(KEYLOC_COMBO, KEYLOC_COMBO); | ||||
| 
 | ||||
|             qrecord->combo_index = combo_index; | ||||
|             ACTIVATE_COMBO(combo); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user