From f6c7e114260890292dcd12645db7fecd64167973 Mon Sep 17 00:00:00 2001 From: Drashna Jaelre Date: Tue, 16 Jul 2019 01:40:54 -0700 Subject: Remove the need to specify NUM_OF_ENCODERS for the Encoder feature (#6328) * Remove the need to set NUM_OF_ENCODERS Instead, calculate the size of the array, and use that instead * Add hack for split common support * Remove NUM_OF_ENCODERS from keyboard config Can be reverted, if needed --- quantum/encoder.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'quantum/encoder.c') diff --git a/quantum/encoder.c b/quantum/encoder.c index ddf6234ab8..31f00c346b 100644 --- a/quantum/encoder.c +++ b/quantum/encoder.c @@ -25,16 +25,14 @@ #define ENCODER_RESOLUTION 4 #endif -#ifndef NUMBER_OF_ENCODERS - #error "Number of encoders not defined by NUMBER_OF_ENCODERS" -#endif - #if !defined(ENCODERS_PAD_A) || !defined(ENCODERS_PAD_B) #error "No encoder pads defined by ENCODERS_PAD_A and ENCODERS_PAD_B" #endif -static pin_t encoders_pad_a[NUMBER_OF_ENCODERS] = ENCODERS_PAD_A; -static pin_t encoders_pad_b[NUMBER_OF_ENCODERS] = ENCODERS_PAD_B; + +#define NUMBER_OF_ENCODERS (sizeof(encoders_pad_a)/sizeof(pin_t)) +static pin_t encoders_pad_a[] = ENCODERS_PAD_A; +static pin_t encoders_pad_b[] = ENCODERS_PAD_B; static int8_t encoder_LUT[] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 }; -- cgit v1.2.3 From 36dd261d06e86ed90997486776f06b286a163cd8 Mon Sep 17 00:00:00 2001 From: Danny Date: Fri, 16 Aug 2019 19:46:41 -0400 Subject: Add support for different encoder pinout for right half of split keyboard (#6521) * Add support for different encoder pinouts for split keyboard * Update documentation for new encoder pinout feature --- quantum/encoder.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'quantum/encoder.c') diff --git a/quantum/encoder.c b/quantum/encoder.c index 31f00c346b..10d8cf7da0 100644 --- a/quantum/encoder.c +++ b/quantum/encoder.c @@ -16,6 +16,9 @@ */ #include "encoder.h" +#ifdef SPLIT_KEYBOARD + #include "split_util.h" +#endif // for memcpy #include @@ -54,6 +57,17 @@ void encoder_update_kb(int8_t index, bool clockwise) { } void encoder_init(void) { +#if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT) + if (!isLeftHand) { + const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT; + const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT; + for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) { + encoders_pad_a[i] = encoders_pad_a_right[i]; + encoders_pad_b[i] = encoders_pad_b_right[i]; + } + } +#endif + for (int i = 0; i < NUMBER_OF_ENCODERS; i++) { setPinInputHigh(encoders_pad_a[i]); setPinInputHigh(encoders_pad_b[i]); -- cgit v1.2.3 From b624f32f944acdc59dcb130674c09090c5c404cb Mon Sep 17 00:00:00 2001 From: skullY Date: Fri, 30 Aug 2019 11:19:03 -0700 Subject: clang-format changes --- quantum/encoder.c | 88 +++++++++++++++++++++++++------------------------------ 1 file changed, 40 insertions(+), 48 deletions(-) (limited to 'quantum/encoder.c') diff --git a/quantum/encoder.c b/quantum/encoder.c index 10d8cf7da0..b3b1cd9f2b 100644 --- a/quantum/encoder.c +++ b/quantum/encoder.c @@ -17,27 +17,25 @@ #include "encoder.h" #ifdef SPLIT_KEYBOARD - #include "split_util.h" +# include "split_util.h" #endif // for memcpy #include - #ifndef ENCODER_RESOLUTION - #define ENCODER_RESOLUTION 4 +# define ENCODER_RESOLUTION 4 #endif #if !defined(ENCODERS_PAD_A) || !defined(ENCODERS_PAD_B) - #error "No encoder pads defined by ENCODERS_PAD_A and ENCODERS_PAD_B" +# error "No encoder pads defined by ENCODERS_PAD_A and ENCODERS_PAD_B" #endif - -#define NUMBER_OF_ENCODERS (sizeof(encoders_pad_a)/sizeof(pin_t)) +#define NUMBER_OF_ENCODERS (sizeof(encoders_pad_a) / sizeof(pin_t)) static pin_t encoders_pad_a[] = ENCODERS_PAD_A; static pin_t encoders_pad_b[] = ENCODERS_PAD_B; -static int8_t encoder_LUT[] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 }; +static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0}; static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0}; @@ -48,64 +46,58 @@ static int8_t encoder_value[NUMBER_OF_ENCODERS * 2] = {0}; static int8_t encoder_value[NUMBER_OF_ENCODERS] = {0}; #endif -__attribute__ ((weak)) -void encoder_update_user(int8_t index, bool clockwise) { } +__attribute__((weak)) void encoder_update_user(int8_t index, bool clockwise) {} -__attribute__ ((weak)) -void encoder_update_kb(int8_t index, bool clockwise) { - encoder_update_user(index, clockwise); -} +__attribute__((weak)) void encoder_update_kb(int8_t index, bool clockwise) { encoder_update_user(index, clockwise); } void encoder_init(void) { #if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT) - if (!isLeftHand) { - const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT; - const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT; - for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) { - encoders_pad_a[i] = encoders_pad_a_right[i]; - encoders_pad_b[i] = encoders_pad_b_right[i]; + if (!isLeftHand) { + const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT; + const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT; + for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) { + encoders_pad_a[i] = encoders_pad_a_right[i]; + encoders_pad_b[i] = encoders_pad_b_right[i]; + } } - } #endif - for (int i = 0; i < NUMBER_OF_ENCODERS; i++) { - setPinInputHigh(encoders_pad_a[i]); - setPinInputHigh(encoders_pad_b[i]); + for (int i = 0; i < NUMBER_OF_ENCODERS; i++) { + setPinInputHigh(encoders_pad_a[i]); + setPinInputHigh(encoders_pad_b[i]); - encoder_state[i] = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1); - } + encoder_state[i] = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1); + } } void encoder_read(void) { - for (int i = 0; i < NUMBER_OF_ENCODERS; i++) { - encoder_state[i] <<= 2; - encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1); - encoder_value[i] += encoder_LUT[encoder_state[i] & 0xF]; - if (encoder_value[i] >= ENCODER_RESOLUTION) { - encoder_update_kb(i, false); + for (int i = 0; i < NUMBER_OF_ENCODERS; i++) { + encoder_state[i] <<= 2; + encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1); + encoder_value[i] += encoder_LUT[encoder_state[i] & 0xF]; + if (encoder_value[i] >= ENCODER_RESOLUTION) { + encoder_update_kb(i, false); + } + if (encoder_value[i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise + encoder_update_kb(i, true); + } + encoder_value[i] %= ENCODER_RESOLUTION; } - if (encoder_value[i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise - encoder_update_kb(i, true); - } - encoder_value[i] %= ENCODER_RESOLUTION; - } } #ifdef SPLIT_KEYBOARD -void encoder_state_raw(uint8_t* slave_state) { - memcpy(slave_state, encoder_state, sizeof(encoder_state)); -} +void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, encoder_state, sizeof(encoder_state)); } void encoder_update_raw(uint8_t* slave_state) { - for (int i = 0; i < NUMBER_OF_ENCODERS; i++) { - encoder_value[NUMBER_OF_ENCODERS + i] += encoder_LUT[slave_state[i] & 0xF]; - if (encoder_value[NUMBER_OF_ENCODERS + i] >= ENCODER_RESOLUTION) { - encoder_update_kb(NUMBER_OF_ENCODERS + i, false); - } - if (encoder_value[NUMBER_OF_ENCODERS + i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise - encoder_update_kb(NUMBER_OF_ENCODERS + i, true); + for (int i = 0; i < NUMBER_OF_ENCODERS; i++) { + encoder_value[NUMBER_OF_ENCODERS + i] += encoder_LUT[slave_state[i] & 0xF]; + if (encoder_value[NUMBER_OF_ENCODERS + i] >= ENCODER_RESOLUTION) { + encoder_update_kb(NUMBER_OF_ENCODERS + i, false); + } + if (encoder_value[NUMBER_OF_ENCODERS + i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise + encoder_update_kb(NUMBER_OF_ENCODERS + i, true); + } + encoder_value[NUMBER_OF_ENCODERS + i] %= ENCODER_RESOLUTION; } - encoder_value[NUMBER_OF_ENCODERS + i] %= ENCODER_RESOLUTION; - } } #endif -- cgit v1.2.3 From 46c49ae4e639509ceb6e81c460855d2eb89086b8 Mon Sep 17 00:00:00 2001 From: XScorpion2 Date: Wed, 18 Sep 2019 19:56:11 -0500 Subject: Updated split encoders so indexes are based on left hand encoders first (#6382) * Updated encoder.c so that split encoders are indexed based on left hand encoders first. This ensures when swapping master sides that code logic based on encoder index doesn't change. PR Review fixes * Removed extra define --- quantum/encoder.c | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) (limited to 'quantum/encoder.c') diff --git a/quantum/encoder.c b/quantum/encoder.c index b3b1cd9f2b..36a6403b36 100644 --- a/quantum/encoder.c +++ b/quantum/encoder.c @@ -40,8 +40,10 @@ 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[NUMBER_OF_ENCODERS] = {0}; #ifdef SPLIT_KEYBOARD -// slave half encoders come over as second set of encoders +// right half encoders come over as second set of encoders static int8_t encoder_value[NUMBER_OF_ENCODERS * 2] = {0}; +// row offsets for each hand +static uint8_t thisHand, thatHand; #else static int8_t encoder_value[NUMBER_OF_ENCODERS] = {0}; #endif @@ -68,20 +70,33 @@ void encoder_init(void) { encoder_state[i] = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1); } + +#ifdef SPLIT_KEYBOARD + thisHand = isLeftHand ? 0 : NUMBER_OF_ENCODERS; + thatHand = NUMBER_OF_ENCODERS - thisHand; +#endif +} + +static void encoder_update(int8_t index, uint8_t state) { + encoder_value[index] += encoder_LUT[state & 0xF]; + if (encoder_value[index] >= ENCODER_RESOLUTION) { + encoder_update_kb(index, false); + } + if (encoder_value[index] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise + encoder_update_kb(index, true); + } + encoder_value[index] %= ENCODER_RESOLUTION; } void encoder_read(void) { for (int i = 0; i < NUMBER_OF_ENCODERS; i++) { encoder_state[i] <<= 2; encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1); - encoder_value[i] += encoder_LUT[encoder_state[i] & 0xF]; - if (encoder_value[i] >= ENCODER_RESOLUTION) { - encoder_update_kb(i, false); - } - if (encoder_value[i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise - encoder_update_kb(i, true); - } - encoder_value[i] %= ENCODER_RESOLUTION; +#if SPLIT_KEYBOARD + encoder_update(i + thisHand, encoder_state[i]); +#else + encoder_update(i, encoder_state[i]); +#endif } } @@ -90,14 +105,7 @@ void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, encoder_state void encoder_update_raw(uint8_t* slave_state) { for (int i = 0; i < NUMBER_OF_ENCODERS; i++) { - encoder_value[NUMBER_OF_ENCODERS + i] += encoder_LUT[slave_state[i] & 0xF]; - if (encoder_value[NUMBER_OF_ENCODERS + i] >= ENCODER_RESOLUTION) { - encoder_update_kb(NUMBER_OF_ENCODERS + i, false); - } - if (encoder_value[NUMBER_OF_ENCODERS + i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise - encoder_update_kb(NUMBER_OF_ENCODERS + i, true); - } - encoder_value[NUMBER_OF_ENCODERS + i] %= ENCODER_RESOLUTION; + encoder_update(i + thatHand, slave_state[i]); } } #endif -- cgit v1.2.3 From 0f0c73f14a8ffb83a79b51582c0e7465c2411749 Mon Sep 17 00:00:00 2001 From: XScorpion2 Date: Fri, 15 Nov 2019 17:01:50 -0600 Subject: Updated slave encoder sync to reduce dropped pulses (#7325) * Updated slave encoder sync to reduce dropped pulses * Fixing encoder direction * Encoder behavior fixes, tested * Update keyboards/rgbkb/sol/keymaps/xulkal/rules.mk To make fauxpark happy Co-Authored-By: fauxpark * Update custom_encoder.c * Update rules.mk --- quantum/encoder.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'quantum/encoder.c') diff --git a/quantum/encoder.c b/quantum/encoder.c index 36a6403b36..e86a0045c6 100644 --- a/quantum/encoder.c +++ b/quantum/encoder.c @@ -38,14 +38,15 @@ static pin_t encoders_pad_b[] = ENCODERS_PAD_B; static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0}; static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0}; +static int8_t encoder_pulses[NUMBER_OF_ENCODERS] = {0}; #ifdef SPLIT_KEYBOARD // right half encoders come over as second set of encoders -static int8_t encoder_value[NUMBER_OF_ENCODERS * 2] = {0}; +static uint8_t encoder_value[NUMBER_OF_ENCODERS * 2] = {0}; // row offsets for each hand static uint8_t thisHand, thatHand; #else -static int8_t encoder_value[NUMBER_OF_ENCODERS] = {0}; +static uint8_t encoder_value[NUMBER_OF_ENCODERS] = {0}; #endif __attribute__((weak)) void encoder_update_user(int8_t index, bool clockwise) {} @@ -78,14 +79,16 @@ void encoder_init(void) { } static void encoder_update(int8_t index, uint8_t state) { - encoder_value[index] += encoder_LUT[state & 0xF]; - if (encoder_value[index] >= ENCODER_RESOLUTION) { - encoder_update_kb(index, false); - } - if (encoder_value[index] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise + encoder_pulses[index] += encoder_LUT[state & 0xF]; + if (encoder_pulses[index] >= ENCODER_RESOLUTION) { + encoder_value[index]++; encoder_update_kb(index, true); } - encoder_value[index] %= ENCODER_RESOLUTION; + if (encoder_pulses[index] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise + encoder_value[index]--; + encoder_update_kb(index, false); + } + encoder_pulses[index] %= ENCODER_RESOLUTION; } void encoder_read(void) { @@ -101,11 +104,22 @@ void encoder_read(void) { } #ifdef SPLIT_KEYBOARD -void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, encoder_state, sizeof(encoder_state)); } +void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, &encoder_value[thisHand], sizeof(uint8_t) * NUMBER_OF_ENCODERS); } void encoder_update_raw(uint8_t* slave_state) { for (int i = 0; i < NUMBER_OF_ENCODERS; i++) { - encoder_update(i + thatHand, slave_state[i]); + uint8_t index = i + thatHand; + int8_t delta = slave_state[i] - encoder_value[index]; + while (delta > 0) { + delta--; + encoder_value[index]++; + encoder_update_kb(index, true); + } + while (delta < 0) { + delta++; + encoder_value[index]--; + encoder_update_kb(index, false); + } } } #endif -- cgit v1.2.3 From a91c0c476507cb8c12840abb59bff34ab0de3c03 Mon Sep 17 00:00:00 2001 From: zvecr Date: Sun, 17 Nov 2019 14:02:26 +0000 Subject: Run clang-format manually to fix recently changed files --- quantum/encoder.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'quantum/encoder.c') diff --git a/quantum/encoder.c b/quantum/encoder.c index e86a0045c6..4aeb3d0cde 100644 --- a/quantum/encoder.c +++ b/quantum/encoder.c @@ -37,8 +37,8 @@ static pin_t encoders_pad_b[] = ENCODERS_PAD_B; static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0}; -static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0}; -static int8_t encoder_pulses[NUMBER_OF_ENCODERS] = {0}; +static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0}; +static int8_t encoder_pulses[NUMBER_OF_ENCODERS] = {0}; #ifdef SPLIT_KEYBOARD // right half encoders come over as second set of encoders @@ -84,7 +84,7 @@ static void encoder_update(int8_t index, uint8_t state) { encoder_value[index]++; encoder_update_kb(index, true); } - if (encoder_pulses[index] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise + if (encoder_pulses[index] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise encoder_value[index]--; encoder_update_kb(index, false); } @@ -109,7 +109,7 @@ void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, &encoder_valu void encoder_update_raw(uint8_t* slave_state) { for (int i = 0; i < NUMBER_OF_ENCODERS; i++) { uint8_t index = i + thatHand; - int8_t delta = slave_state[i] - encoder_value[index]; + int8_t delta = slave_state[i] - encoder_value[index]; while (delta > 0) { delta--; encoder_value[index]++; -- cgit v1.2.3