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

Fix for encoders and support ENCODER_MAP_ENABLE on Planck rev7 #23967

Merged
merged 9 commits into from
Jul 7, 2024
7 changes: 7 additions & 0 deletions keyboards/planck/rev7/keymaps/default/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@
- etc.
*/
// #define MIDI_ADVANCED

/*
* Encoder options
*/
// #define PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY 20
// #define ENCODER_MAP_KEY_DELAY 10
// #define ENCODER_RESOLUTION 4
89 changes: 78 additions & 11 deletions keyboards/planck/rev7/keymaps/default/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,88 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
* `-----------------------------------------------------------------------------------'
*/
[_ADJUST] = LAYOUT_planck_grid(
_______, QK_BOOT, DB_TOGG, RGB_TOG, RGB_MOD, RGB_HUI, RGB_HUD, RGB_SAI, RGB_SAD, RGB_VAI, RGB_VAD, KC_DEL ,
_______, QK_BOOT, DB_TOGG, UG_TOGG, UG_NEXT, UG_HUEU, UG_HUED, UG_SATU, UG_SATD, UG_SPDU, UG_SPDD, KC_DEL ,
_______, EE_CLR, MU_NEXT, AU_ON, AU_OFF, AG_NORM, AG_SWAP, QWERTY, COLEMAK, DVORAK, PLOVER, _______,
_______, AU_PREV, AU_NEXT, MU_ON, MU_OFF, MI_ON, MI_OFF, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
)

};

#ifdef ENCODER_MAP_ENABLE
/* Rotary Encoders
*/
const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS] = {
/* Qwerty
* v- (index) Clockwise / Counter Clockwise v- (index) Clockwise / Counter Clockwise
* ,---------------------------------------------------------------------------------------.
* | (0) Vol- / Vol+ | | | | | | | | | | | (4) Vol- / Vol+ |
* |-----------------------+---+---+---+---+---+---+---+---+---+---+-----------------------|
* | (1) KC_MNXT / KC_MPRV | | | | | | | | | | | (5) KC_MNXT / KC_MPRV |
* |-----------------------+---+---+---+---+---+---+---+---+---+---+-----------------------|
* | (2) KC_WBAK / KC_WFWD | | | | | | | | | | | (6) KC_SPC / KC_ENT |
* |-----------------------+---+---+---+---+---+---+---+---+---+---+-----------------------|
* | (3) KC_LEFT / KC_RGHT | | | | | | | | | | (7) KC_DOWN / KC_UP |
* `---------------------------------------------------------------------------------------'
*/
[_QWERTY] = {
// LEFT SIDE (index 0 to 3)
ENCODER_CCW_CW(KC_VOLU, KC_VOLD),
ENCODER_CCW_CW(KC_MNXT, KC_MPRV),
ENCODER_CCW_CW(KC_WBAK, KC_WFWD),
ENCODER_CCW_CW(KC_LEFT, KC_RGHT),
// RIGHT SIDE (index 4 to 7)
ENCODER_CCW_CW(KC_VOLU, KC_VOLD),
ENCODER_CCW_CW(KC_MNXT, KC_MPRV),
ENCODER_CCW_CW(KC_SPC, KC_ENT),
ENCODER_CCW_CW(KC_DOWN, KC_UP)
},

/* Adjust (Lower + Raise)
* v- (index) Clockwise / Counter Clockwise v- (index) Clockwise / Counter Clockwise
* ,---------------------------------------------------------------------------------------.
* | (0) _______ / _______ | | | | | | | | | | | (4) _______ / _______ |
* |-----------------------+---+---+---+---+---+---+---+---+---+---+-----------------------|
* | (1) _______ / _______ | | | | | | | | | | | (5) _______ / _______ |
* |-----------------------+---+---+---+---+---+---+---+---+---+---+-----------------------|
* | (2) UG_NEXT / UG_PREV | | | | | | | | | | | (6) SAT- / SAT+ |
* |-----------------------+---+---+---+---+---+---+---+---+---+---+-----------------------|
* | (3) UG_VALD / UG_VALU | | | | | | | | | | (7) HUE- / HUE+ |
* `---------------------------------------------------------------------------------------'
*/
[_ADJUST] = {
// LEFT SIDE (index 0 to 3)
ENCODER_CCW_CW(_______, _______),
ENCODER_CCW_CW(_______, _______),
ENCODER_CCW_CW(UG_NEXT, UG_PREV),
ENCODER_CCW_CW(UG_VALD, UG_VALU),
// RIGHT SIDE (index 4 to 7)
ENCODER_CCW_CW(_______, _______),
ENCODER_CCW_CW(_______, _______),
ENCODER_CCW_CW(UG_SATD, UG_SATU),
ENCODER_CCW_CW(UG_HUEU, UG_HUED)
}
};
#endif
/* clang-format on */

#ifdef AUDIO_ENABLE
float plover_song[][2] = SONG(PLOVER_SOUND);
float plover_gb_song[][2] = SONG(PLOVER_GOODBYE_SOUND);
#endif

bool play_encoder_melody(uint8_t index, bool clockwise);

layer_state_t layer_state_set_user(layer_state_t state) {
return update_tri_layer_state(state, _LOWER, _RAISE, _ADJUST);
}

bool process_record_user(uint16_t keycode, keyrecord_t *record) {
#ifdef ENCODER_MAP_ENABLE
if (IS_ENCODEREVENT(record->event) && record->event.pressed) {
play_encoder_melody(record->event.key.col, record->event.type == ENCODER_CCW_EVENT);
}
#endif
switch (keycode) {
case QWERTY:
if (record->event.pressed) {
Expand Down Expand Up @@ -228,13 +291,13 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {

/* clang-format off */
float melody[8][2][2] = {
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
};
/* clang-format on */
Expand All @@ -251,7 +314,7 @@ float melody[8][2][2] = {
#define ET12_MAJOR_THIRD 1.259921
#define ET12_PERFECT_FOURTH 1.33484
#define ET12_TRITONE 1.414214
#define ET12_PERFECT_FIFTH 1.498307
#define ET12_PERFECT_FIFTH 1.498307

deferred_token tokens[8];

Expand All @@ -260,7 +323,7 @@ uint32_t reset_note(uint32_t trigger_time, void *note) {
return 0;
}

bool encoder_update_user(uint8_t index, bool clockwise) {
bool play_encoder_melody(uint8_t index, bool clockwise) {
cancel_deferred_exec(tokens[index]);
if (clockwise) {
melody[index][1][0] = melody[index][1][0] * ET12_MINOR_SECOND;
Expand All @@ -275,6 +338,10 @@ bool encoder_update_user(uint8_t index, bool clockwise) {
return false;
}

bool encoder_update_user(uint8_t index, bool clockwise) {
return play_encoder_melody(index, clockwise);
}

bool dip_switch_update_user(uint8_t index, bool active) {
switch (index) {
case 0: {
Expand Down Expand Up @@ -303,4 +370,4 @@ bool dip_switch_update_user(uint8_t index, bool active) {
}
}
return true;
}
}
22 changes: 15 additions & 7 deletions keyboards/planck/rev7/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,21 @@ bool matrix_scan_custom(matrix_row_t current_matrix[]) {
return changed;
}

#if defined(ENCODER_ENABLE) || defined(ENCODER_MAP_ENABLE)
#if !defined(PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY)
# define PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY 10
#endif

void encoder_quadrature_init_pin(uint8_t index, bool pad_b) {
}

uint8_t encoder_quadrature_read_pin(uint8_t index, bool pad_b) {
pin_t pin = pad_b ? B13: B12;
gpio_set_pin_input_high(pin);
gpio_write_pin_low(matrix_row_pins[index]);
wait_us(10);
uint8_t ret = gpio_read_pin(pin) ? 1 : 0;
gpio_set_pin_input_low(matrix_row_pins[index]);
gpio_set_pin_input_low(pin);
pin_t col_pin = pad_b ? B13 : B12;
gpio_set_pin_output(col_pin);
gpio_write_pin_high(col_pin);
wait_us(PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY);
uint8_t ret = gpio_read_pin(matrix_row_pins[index]) ? 0 : 1;
gpio_set_pin_input_low(col_pin);
return ret;
}
#endif // ENCODER_ENABLE || ENCODER_MAP_ENABLE
37 changes: 33 additions & 4 deletions keyboards/planck/rev7/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_to

## Encoders

Encoders must have matching pulse & detent resolutions (e.g. 24/24) for the scanning to work properly. Multiple encoders can be used at the same time, and are zero-indexed (compared to being one-indexed on the PCB's silkscreen) in the `encoder_update_user(uint8_t index, bool clockwise)` function:
Encoders must have matching pulse & detent resolutions (e.g. 24/24) for the scanning to work properly. Multiple encoders can be used at the same time.
If an encoder has a switch built-in, it's connected to the key at that location with index number:

```
,-----------------------------------------------------------------------------------.
Expand All @@ -28,7 +29,35 @@ Encoders must have matching pulse & detent resolutions (e.g. 24/24) for the scan
`-----------------------------------------------------------------------------------'
```

If an encoder has a switch built-in, it's connected to the key at that location. On the default keymap, each encoder will play its own rising/falling tone sequence when rotated, and will reset the pitch after one second of inactivity. The encoder map feature is not currently supported.
Planck rev7 supports `ENCODER_ENABLE` and `ENCODER_MAP_ENABLE`. If both `ENCODER_MAP_ENABLE` and `ENCODER_ENABLE` are defined, `ENCODER_MAP_ENABLE` takes precedence. On the default keymap, each encoder will play its own rising/falling tone sequence when rotated, and will reset the pitch after one second of inactivity.

### With ENCODER_ENABLE

Define it as follows in `rules.mk`:

```
ENCODER_ENABLE = yes
```

Zero-indexed (compared to being one-indexed on the PCB's silkscreen) in the `encoder_update_user(uint8_t index, bool clockwise)` function.

### With ENCODER_MAP_ENABLE

Define it as follows in `rules.mk`:

```
ENCODER_ENABLE = yes
ENCODER_MAP_ENABLE = yes
```

If you enable `ENCODER_MAP_ENABLE`, define `const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS]` and configure your keycodes. If you enable `ENCODER_MAP_ENABLE`, `encoder_update_user` is not used directly.

Additionally, you can use the following `config.h` options:

```c
#define ENCODER_MAP_KEY_DELAY 10
#define ENCODER_RESOLUTION 4
```

## Some Planck-specific config.h options:

Expand All @@ -37,6 +66,6 @@ If an encoder has a switch built-in, it's connected to the key at that location.
#define PLANCK_WATCHDOG_TIMEOUT 1.0
// disables the watchdog timer - you may want to disable the watchdog timer if you use longer macros
#define PLANCK_WATCHDOG_DISABLE
// the resolution of the encoders used in the encoder matrix
#define PLANCK_ENCODER_RESOLUTION 4
// Sets the time to wait for the rotary encoder pin state to stabilize while scanning (Default is 20(us))
#define PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY 20
```
Loading