Skip to content

Commit

Permalink
Fix rotary encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
FWest98 committed Jun 5, 2022
1 parent 180132f commit 95fbce9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
18 changes: 13 additions & 5 deletions keyboards/keychron/q3/rev_0121/keymaps/via/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
1 change: 1 addition & 0 deletions keyboards/keychron/q3/rev_0121/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions quantum/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}
encoder_external_update[index] = true;
}

0 comments on commit 95fbce9

Please sign in to comment.