From 54f907bfe91f9cf452f7cee18f2534187763c2bc Mon Sep 17 00:00:00 2001 From: "Y.KEISUKE" Date: Mon, 8 Jul 2024 07:45:35 +0900 Subject: [PATCH] Fix for encoders and support ENCODER_MAP_ENABLE on Planck rev7 (#23967) Co-authored-by: Nick Brassel --- .../planck/rev7/keymaps/default/config.h | 7 ++ .../planck/rev7/keymaps/default/keymap.c | 89 ++++++++++++++++--- keyboards/planck/rev7/matrix.c | 22 +++-- keyboards/planck/rev7/readme.md | 37 +++++++- 4 files changed, 133 insertions(+), 22 deletions(-) diff --git a/keyboards/planck/rev7/keymaps/default/config.h b/keyboards/planck/rev7/keymaps/default/config.h index fbbab996e1..937d6a5e41 100644 --- a/keyboards/planck/rev7/keymaps/default/config.h +++ b/keyboards/planck/rev7/keymaps/default/config.h @@ -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 diff --git a/keyboards/planck/rev7/keymaps/default/keymap.c b/keyboards/planck/rev7/keymaps/default/keymap.c index 4ac4c0de4f..85f5097332 100644 --- a/keyboards/planck/rev7/keymaps/default/keymap.c +++ b/keyboards/planck/rev7/keymaps/default/keymap.c @@ -147,13 +147,69 @@ 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 @@ -161,11 +217,18 @@ 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) { @@ -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 */ @@ -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]; @@ -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; @@ -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: { @@ -303,4 +370,4 @@ bool dip_switch_update_user(uint8_t index, bool active) { } } return true; -} \ No newline at end of file +} diff --git a/keyboards/planck/rev7/matrix.c b/keyboards/planck/rev7/matrix.c index 777bd6a7fe..44f532db65 100644 --- a/keyboards/planck/rev7/matrix.c +++ b/keyboards/planck/rev7/matrix.c @@ -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 diff --git a/keyboards/planck/rev7/readme.md b/keyboards/planck/rev7/readme.md index 6a4df37704..940976bfce 100644 --- a/keyboards/planck/rev7/readme.md +++ b/keyboards/planck/rev7/readme.md @@ -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: ``` ,-----------------------------------------------------------------------------------. @@ -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: @@ -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 ```