From 8c26298e463b20b196e07774957b80401af7ae96 Mon Sep 17 00:00:00 2001 From: Marek Wyborski <9861955+mwyborski@users.noreply.github.com> Date: Sun, 24 Apr 2022 13:35:54 +0200 Subject: [PATCH 1/2] Improve ENCODER_DEFAULT_POS to recognize lost ticks When resetting the pulses due to reaching the ENCODER_DEFAULT_POS no events were emitted, when Modified encoder_update to be more permissive about lost pulses when returning to the ENCODER_DEFAULT_POS to detect if a tick has happened. E.g. on resolution 4 rotary encoder all ticks with only 1, 2 or 3 detected pulses have been discarded in the original code, but when the rotary encoder returns ENCODER_DEFAULT_POS and pulses are present, we can be sure, that a tick has happened. This modification catches all these lost ticks with only 1,2 or 3 pulses, which results in a more reliable performance of the rotary encoder. --- quantum/encoder.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/quantum/encoder.c b/quantum/encoder.c index 105bed0147b0..a4e723e3a44b 100644 --- a/quantum/encoder.c +++ b/quantum/encoder.c @@ -163,7 +163,14 @@ static bool encoder_update(uint8_t index, uint8_t state) { index += thisHand; #endif encoder_pulses[i] += encoder_LUT[state & 0xF]; + +#ifdef ENCODER_DEFAULT_POS + if ( (encoder_pulses[i] >= resolution) || (encoder_pulses[i] <= -resolution)|| ((state & 0x3) == ENCODER_DEFAULT_POS) ) { + if (encoder_pulses[i] >= 1) { +#else if (encoder_pulses[i] >= resolution) { +#endif + encoder_value[index]++; changed = true; #ifdef ENCODER_MAP_ENABLE @@ -172,7 +179,12 @@ static bool encoder_update(uint8_t index, uint8_t state) { encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE); #endif // ENCODER_MAP_ENABLE } + +#ifdef ENCODER_DEFAULT_POS + if (encoder_pulses[i] <= -1) { +#else if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise +#endif encoder_value[index]--; changed = true; #ifdef ENCODER_MAP_ENABLE @@ -183,8 +195,7 @@ static bool encoder_update(uint8_t index, uint8_t state) { } encoder_pulses[i] %= resolution; #ifdef ENCODER_DEFAULT_POS - if ((state & 0x3) == ENCODER_DEFAULT_POS) { - encoder_pulses[i] = 0; + encoder_pulses[i] = 0; } #endif return changed; From 8ccaefe541aaad09c00e2fb5d271cf1ed00cf90c Mon Sep 17 00:00:00 2001 From: Marek Wyborski <9861955+mwyborski@users.noreply.github.com> Date: Sun, 24 Apr 2022 13:59:17 +0200 Subject: [PATCH 2/2] Update encoder.c corrected formatting --- quantum/encoder.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/quantum/encoder.c b/quantum/encoder.c index a4e723e3a44b..5f8a7ce080fa 100644 --- a/quantum/encoder.c +++ b/quantum/encoder.c @@ -165,37 +165,37 @@ static bool encoder_update(uint8_t index, uint8_t state) { encoder_pulses[i] += encoder_LUT[state & 0xF]; #ifdef ENCODER_DEFAULT_POS - if ( (encoder_pulses[i] >= resolution) || (encoder_pulses[i] <= -resolution)|| ((state & 0x3) == ENCODER_DEFAULT_POS) ) { + if ((encoder_pulses[i] >= resolution) || (encoder_pulses[i] <= -resolution) || ((state & 0x3) == ENCODER_DEFAULT_POS)) { if (encoder_pulses[i] >= 1) { #else if (encoder_pulses[i] >= resolution) { #endif - encoder_value[index]++; - changed = true; + encoder_value[index]++; + changed = true; #ifdef ENCODER_MAP_ENABLE - encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE); + encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE); #else // ENCODER_MAP_ENABLE encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE); #endif // ENCODER_MAP_ENABLE - } + } #ifdef ENCODER_DEFAULT_POS - if (encoder_pulses[i] <= -1) { + if (encoder_pulses[i] <= -1) { #else if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise #endif - encoder_value[index]--; - changed = true; + encoder_value[index]--; + changed = true; #ifdef ENCODER_MAP_ENABLE - encoder_exec_mapping(index, ENCODER_CLOCKWISE); + encoder_exec_mapping(index, ENCODER_CLOCKWISE); #else // ENCODER_MAP_ENABLE encoder_update_kb(index, ENCODER_CLOCKWISE); #endif // ENCODER_MAP_ENABLE - } - encoder_pulses[i] %= resolution; + } + encoder_pulses[i] %= resolution; #ifdef ENCODER_DEFAULT_POS - encoder_pulses[i] = 0; + encoder_pulses[i] = 0; } #endif return changed;