Skip to content
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

Add support for Encoders mapped into the keymap array #12982

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion builddefs/generic_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ GENERIC_FEATURES = \
DIP_SWITCH \
DYNAMIC_KEYMAP \
DYNAMIC_MACRO \
ENCODER \
GRAVE_ESC \
HAPTIC \
KEY_LOCK \
Expand Down
9 changes: 9 additions & 0 deletions common_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,15 @@ ifeq ($(strip $(TERMINAL_ENABLE)), yes)
OPT_DEFS += -DUSER_PRINT
endif


ifeq ($(strip $(ENCODER_ENABLE)), yes)
SRC += $(QUANTUM_DIR)/encoder.c
OPT_DEFS += -DENCODER_ENABLE
ifeq ($(strip $(ENCODER_KEYMAP_MAPPING_ENABLE)), yes)
OPT_DEFS += -DENCODER_KEYMAP_MAPPING_ENABLE
endif
endif

ifeq ($(strip $(VIA_ENABLE)), yes)
DYNAMIC_KEYMAP_ENABLE := yes
RAW_ENABLE := yes
Expand Down
28 changes: 28 additions & 0 deletions docs/feature_encoders.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,34 @@ If you are using different pinouts for the encoders on each half of a split keyb
#define ENCODER_RESOLUTIONS_RIGHT { 2, 4 }
```

?> Note: When using Encoders with split keyboards, it assumes that you have double the number of encoders. If not all encoders are populated, then you may need to skip indexes.

## Mapping Encoder Functions to the Keymap

By enabling this config, you can place the keycodes for the encoders directly in the keymap itself. If you have extra spots in your matrix (eg, not populated), you can use those locations. To enable this you need to add a define that lists the row and column location to use.

Add this ton your `rules.mk`:

```make
ENCODER_KEYMAP_MAPPING_ENABLE = yes
```

And then add the configuration for the encoders in your `config.h`:

```c
#define ENCODER_KEYMAPPING { { 4, 0 }, { 4, 1 } }, { { 5, 0 }, { 5, 1 } }
```

This tells it to use row 4, column 0 for counter clockwise, row 4 column 1 for clockwise, and then row 5 column 0 and 1 for the second encoder.

This works with most keycodes. Tap-Hold keys may not work well for this, nor will special keys, like tap dances. However, this will work with quantum keycodes, as well as custom macros.

Additionally, this will explicitly disable callback functionality if defined, and solely handle the encoders through the keymap. Alternatievly, you can add `#define ENCODER_PROCESS_CALLBACKS` to allow the callbacks to continue working. And returning false in the callbacks will prevent it from handling the encoder keymap configuration.

?> Note: With Split Keyboards, you will want to define all of the possible positions. Meaning you should have double the number of encoder locations as you have "pads" defined for the encoder.

?> Note: When adding support for this, you may want to consider adding an additional layout for those that don't wish to use this functionality.

## Callbacks

The callback functions can be inserted into your `<keyboard>.c`:
Expand Down
1 change: 1 addition & 0 deletions keyboards/monarch/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define ENCODERS_PAD_A { B4 }
#define ENCODERS_PAD_B { B5 }
#define ENCODER_RESOLUTION 1
#define ENCODER_KEYMAPPING { { 5, 0 }, { 5, 1 } }

//LEDS A6
#define BACKLIGHT_PIN A6
Expand Down
37 changes: 0 additions & 37 deletions keyboards/monarch/keymaps/via/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,3 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
_______, _______
)
};

keyevent_t encoder_ccw = {
.key = (keypos_t){.row = 5, .col = 0},
.pressed = false
};

keyevent_t encoder_cw = {
.key = (keypos_t){.row = 5, .col = 1},
.pressed = false
};

void matrix_scan_user(void) {
if (IS_PRESSED(encoder_ccw)) {
encoder_ccw.pressed = false;
encoder_ccw.time = (timer_read() | 1);
action_exec(encoder_ccw);
}

if (IS_PRESSED(encoder_cw)) {
encoder_cw.pressed = false;
encoder_cw.time = (timer_read() | 1);
action_exec(encoder_cw);
}
}

bool encoder_update_user(uint8_t index, bool clockwise) {
if (clockwise) {
encoder_cw.pressed = true;
encoder_cw.time = (timer_read() | 1);
action_exec(encoder_cw);
} else {
encoder_ccw.pressed = true;
encoder_ccw.time = (timer_read() | 1);
action_exec(encoder_ccw);
}
return true;
}
1 change: 1 addition & 0 deletions keyboards/monarch/keymaps/via/rules.mk
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
VIA_ENABLE = yes
ENCODER_KEYMAP_MAPPING_ENABLE = yes
11 changes: 0 additions & 11 deletions keyboards/nightly_boards/adellein/adellein.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,3 @@
*/

#include "adellein.h"

void matrix_scan_kb(void) {
encoder_action_unregister();
matrix_scan_user();
}

bool encoder_update_kb(uint8_t index, bool clockwise) {
// if (!encoder_update_user(index, clockwise)) return false;
encoder_action_register(index, clockwise);
return true;
};
2 changes: 0 additions & 2 deletions keyboards/nightly_boards/adellein/adellein.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#pragma once

#include "quantum.h"
#include "encoder_action.h"

/* This is a shortcut to help you visually see your layout.
*
Expand Down Expand Up @@ -52,4 +51,3 @@
{ K20, K21, K22, K23, K24, K25, K26, K27, K28, K29, K2A, K2B, K2C }, \
{ K30, E00A, K32, E00B, K34, K35, KC_NO, K37, KC_NO, K39, KC_NO, KC_NO, K3C }, \
}

8 changes: 1 addition & 7 deletions keyboards/nightly_boards/adellein/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#endif

/* Encoders */

#define ENCODERS 1

#define ENCODERS_PAD_A { C7 }
#define ENCODERS_PAD_B { C6 }

#define ENCODERS_CW_KEY { { 3, 3 } }
#define ENCODERS_CCW_KEY { { 1, 3 } }
#define ENCODER_KEYMAPPING { { 3, 3 }, { 3, 1} }

/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
#define DEBOUNCE 5
Expand All @@ -74,5 +70,3 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define LOCKING_SUPPORT_ENABLE
/* Locking resynchronize hack */
#define LOCKING_RESYNC_ENABLE


51 changes: 0 additions & 51 deletions keyboards/nightly_boards/adellein/encoder_action.c

This file was deleted.

21 changes: 0 additions & 21 deletions keyboards/nightly_boards/adellein/encoder_action.h

This file was deleted.

2 changes: 1 addition & 1 deletion keyboards/nightly_boards/adellein/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ ENCODER_ENABLE = yes # Enable Rotary Encoders
LTO_ENABLE = yes

# Added encoder Action
SRC += encoder_action.c
ENCODER_KEYMAP_MAPPING_ENABLE = yes
5 changes: 1 addition & 4 deletions keyboards/nightly_boards/n40_o/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

/* Encoders */

#define ENCODERS 3

#define ENCODERS_PAD_A { F6, F4 , B6 }
#define ENCODERS_PAD_B { F7, F5 , C6 }

#define ENCODERS_CW_KEY { { 2, 4 },{ 4, 4 },{ 6, 4 } }
#define ENCODERS_CCW_KEY { { 1, 4 },{ 3, 4 },{ 5, 4 } }
#define ENCODER_KEYMAPPING { { 4, 1 }, { 4, 2 } }, { { 4, 3 }, { 4, 4 } }, { { 4, 5 }, { 4, 6 } }

/* Audio */

Expand Down
51 changes: 0 additions & 51 deletions keyboards/nightly_boards/n40_o/encoder_action.c

This file was deleted.

21 changes: 0 additions & 21 deletions keyboards/nightly_boards/n40_o/encoder_action.h

This file was deleted.

11 changes: 0 additions & 11 deletions keyboards/nightly_boards/n40_o/n40_o.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,3 @@
*/

#include "n40_o.h"

void matrix_scan_kb(void) {
encoder_action_unregister();
matrix_scan_user();
}

bool encoder_update_kb(uint8_t index, bool clockwise) {
// if (!encoder_update_user(index, clockwise)) return false;
encoder_action_register(index, clockwise);
return true;
};
1 change: 0 additions & 1 deletion keyboards/nightly_boards/n40_o/n40_o.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#pragma once

#include "quantum.h"
#include "encoder_action.h"

/* This is a shortcut to help you visually see your layout.
*
Expand Down
2 changes: 1 addition & 1 deletion keyboards/nightly_boards/n40_o/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ ENCODER_ENABLE = yes # Enable Rotary Encoders
LTO_ENABLE = yes

# Added encoder Action
SRC += encoder_action.c
ENCODER_KEYMAP_MAPPING_ENABLE = yes
5 changes: 1 addition & 4 deletions keyboards/nightly_boards/n60_s/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

/* Encoders */

#define ENCODERS 1

#define ENCODERS_PAD_A { D3 }
#define ENCODERS_PAD_B { D5 }

#define ENCODERS_CW_KEY { { 1, 5 } }
#define ENCODERS_CCW_KEY { { 0, 5 } }
#define ENCODER_KEYMAPPING { { 5, 0 }, { 5, 1 } }

/* Audio */

Expand Down
Loading