fix: Direction detection for rotary encoder on startup (#3445)

This commit is contained in:
philmoz
2023-04-16 17:08:59 +10:00
committed by GitHub
parent 5b0e47ff42
commit b3c7a95aa2
@@ -46,10 +46,15 @@ volatile uint32_t rotencDt = 0;
void rotaryEncoderCheck() void rotaryEncoderCheck()
{ {
static uint8_t state = 0; static uint8_t state = 0;
static uint8_t re_count = 0;
uint8_t pins = ROTARY_ENCODER_POSITION(); uint8_t pins = ROTARY_ENCODER_POSITION();
#if defined(ROTARY_ENCODER_SUPPORT_BUGGY_WIRING) #if defined(ROTARY_ENCODER_SUPPORT_BUGGY_WIRING)
if (pins != (state & 0x03) && !(readKeys() & (1 << KEY_ENTER))) { if (pins != (state & 0x03) && !(readKeys() & (1 << KEY_ENTER))) {
if (re_count == 0) {
// Need at least 2 values to correctly determine initial direction
re_count = 1;
} else {
if ((pins ^ (state & 0x03)) == 0x03) { if ((pins ^ (state & 0x03)) == 0x03) {
if (pins == 3) { if (pins == 3) {
rotencValue += INC_ROT_2; rotencValue += INC_ROT_2;
@@ -63,11 +68,23 @@ void rotaryEncoderCheck()
rotencValue += INC_ROT; rotencValue += INC_ROT;
} }
} }
if (re_count == 1)
{
re_count = 2;
// Assume 1st value is same direction as 2nd value
rotencValue = rotencValue * 2;
}
}
state &= ~0x03; state &= ~0x03;
state |= pins; state |= pins;
} }
#else #else
if (pins != state && !(readKeys() & (1 << KEY_ENTER))) { if (pins != state && !(readKeys() & (1 << KEY_ENTER))) {
if (re_count == 0) {
// Need at least 2 values to correctly determine initial direction
re_count = 1;
} else {
#if defined(ROTARY_ENCODER_INVERTED) #if defined(ROTARY_ENCODER_INVERTED)
if (!(state & 0x01) ^ ((pins & 0x02) >> 1)) { if (!(state & 0x01) ^ ((pins & 0x02) >> 1)) {
#else #else
@@ -77,6 +94,14 @@ void rotaryEncoderCheck()
} else { } else {
rotencValue += INC_ROT; rotencValue += INC_ROT;
} }
if (re_count == 1)
{
re_count = 2;
// Assume 1st value is same direction as 2nd value
rotencValue = rotencValue * 2;
}
}
state = pins; state = pins;
} }
#endif #endif