From 95fbce9628191522bf9acb07808812b5c5463152 Mon Sep 17 00:00:00 2001 From: Floris Westerman Date: Mon, 6 Jun 2022 01:13:03 +0200 Subject: [PATCH] Fix rotary encoder --- .../keychron/q3/rev_0121/keymaps/via/keymap.c | 18 +++++++++++++----- keyboards/keychron/q3/rev_0121/rules.mk | 1 + quantum/encoder.c | 7 +++++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/keyboards/keychron/q3/rev_0121/keymaps/via/keymap.c b/keyboards/keychron/q3/rev_0121/keymaps/via/keymap.c index 40fff018570f..44525cc291e6 100644 --- a/keyboards/keychron/q3/rev_0121/keymaps/via/keymap.c +++ b/keyboards/keychron/q3/rev_0121/keymaps/via/keymap.c @@ -92,15 +92,23 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS), }; +#if defined(ENCODER_MAP_ENABLE) +const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][2] = { + [MAC_BASE] = { ENCODER_CCW_CW(KC_VOLD, KC_VOLU) }, + [MAC_FN] = { ENCODER_CCW_CW(KC_VOLD, KC_VOLU) }, + [WIN_BASE] = { ENCODER_CCW_CW(KC_VOLD, KC_VOLU) }, + [WIN_FN] = { ENCODER_CCW_CW(KC_VOLD, KC_VOLU) }, +}; +#endif + #if defined(VIA_ENABLE) && defined(ENCODER_ENABLE) -#define ENCODERS 1 -static uint8_t encoder_state[ENCODERS] = {0}; -static keypos_t encoder_cw[ENCODERS] = {{ 8, 5 }}; -static keypos_t encoder_ccw[ENCODERS] = {{ 7, 5 }}; +static uint8_t encoder_state[NUM_ENCODERS] = {0}; +static keypos_t encoder_cw[NUM_ENCODERS] = {{ 8, 5 }}; +static keypos_t encoder_ccw[NUM_ENCODERS] = {{ 7, 5 }}; void encoder_action_unregister(void) { - for (int index = 0; index < ENCODERS; ++index) { + for (int index = 0; index < NUM_ENCODERS; ++index) { if (encoder_state[index]) { keyevent_t encoder_event = (keyevent_t) { .key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index], diff --git a/keyboards/keychron/q3/rev_0121/rules.mk b/keyboards/keychron/q3/rev_0121/rules.mk index 89879902a710..b43e77cdeffc 100644 --- a/keyboards/keychron/q3/rev_0121/rules.mk +++ b/keyboards/keychron/q3/rev_0121/rules.mk @@ -17,6 +17,7 @@ BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality RGBLIGHT_ENABLE = no # Enable keyboard RGB underglow AUDIO_ENABLE = no # Audio output ENCODER_ENABLE = yes # Enable Encoder +ENCODER_MAP_ENABLE = no # Disable Encoder map DIP_SWITCH_ENABLE = yes RGB_MATRIX_ENABLE = yes RGB_MATRIX_DRIVER = CKLED2001 diff --git a/quantum/encoder.c b/quantum/encoder.c index 66ef0ded3c6b..8aa5d4995c9e 100644 --- a/quantum/encoder.c +++ b/quantum/encoder.c @@ -55,6 +55,7 @@ static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, static uint8_t encoder_state[NUM_ENCODERS] = {0}; static int8_t encoder_pulses[NUM_ENCODERS] = {0}; +static bool encoder_external_update[NUM_ENCODERS] = { false }; // encoder counts static uint8_t thisCount; @@ -194,10 +195,11 @@ bool encoder_read(void) { bool changed = false; for (uint8_t i = 0; i < thisCount; i++) { uint8_t new_status = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1); - if ((encoder_state[i] & 0x3) != new_status) { + if ((encoder_state[i] & 0x3) != new_status || encoder_external_update[i]) { encoder_state[i] <<= 2; encoder_state[i] |= new_status; changed |= encoder_update(i, encoder_state[i]); + encoder_external_update[i] = false; } } return changed; @@ -246,4 +248,5 @@ void encoder_insert_state(uint8_t index) { encoder_state[index] <<= 2; encoder_state[index] |= (readPin(encoders_pad_a[index]) << 0) | (readPin(encoders_pad_b[index]) << 1); encoder_pulses[index] += encoder_LUT[encoder_state[index] & 0xF]; -} \ No newline at end of file + encoder_external_update[index] = true; +}