Skip to content

Commit

Permalink
Add support for encoder mapping. (qmk#13286)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzarc authored and zykrah committed Jul 2, 2022
1 parent 1227890 commit 67b5fe3
Show file tree
Hide file tree
Showing 16 changed files with 278 additions and 110 deletions.
1 change: 1 addition & 0 deletions builddefs/generic_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ GENERIC_FEATURES = \
DYNAMIC_KEYMAP \
DYNAMIC_MACRO \
ENCODER \
ENCODER_MAP \
GRAVE_ESC \
HAPTIC \
KEY_LOCK \
Expand Down
1 change: 1 addition & 0 deletions builddefs/show_options.mk
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ OTHER_OPTION_NAMES = \
HELIX ZINC \
AUTOLOG_ENABLE \
DEBUG_ENABLE \
ENCODER_MAP_ENABLE \
ENCODER_ENABLE_CUSTOM \
GERMAN_ENABLE \
HAPTIC_ENABLE \
Expand Down
23 changes: 22 additions & 1 deletion docs/feature_encoders.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions docs/feature_swap_hands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
1 change: 1 addition & 0 deletions keyboards/hnahkb/vn66/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions lib/python/qmk/cli/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
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
65 changes: 53 additions & 12 deletions quantum/action.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -90,6 +92,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);
}
Expand Down Expand Up @@ -137,27 +140,65 @@ if (QS_oneshot_timeout > 0) {
}

#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

Expand Down
75 changes: 57 additions & 18 deletions quantum/action_layer.c
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -227,45 +229,82 @@ 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_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++) {
cache[storage_idx][bit_number] ^= (-((layer & (1U << bit_number)) != 0) ^ cache[storage_idx][bit_number]) & (1U << storage_bit);
}
}

/** \brief read source layers cache
*
* reads the cached keys stored when the layer was changed
*/
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 |= ((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) {

#ifdef VIAL_ENABLE
if (key.row == VIAL_MATRIX_MAGIC) return;
#endif

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;

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);
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) {

#ifdef VIAL_ENABLE
if (key.row == VIAL_MATRIX_MAGIC) return 0;
#endif

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;

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;
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);
}

return layer;
# 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

Expand Down

0 comments on commit 67b5fe3

Please sign in to comment.