From 2100d4562a05d59b8db42d7ceb99636111bfdcab Mon Sep 17 00:00:00 2001 From: David Buezas Date: Sun, 4 Feb 2024 21:03:00 +0100 Subject: [PATCH] Advance encoder after a while if it has crossed the half a step threshold while changing direction --- Marlin/src/lcd/marlinui.cpp | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 33e807c16edc..527fccdd94e3 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -68,7 +68,8 @@ MarlinUI ui; constexpr uint8_t epps = ENCODER_PULSES_PER_STEP; #define BLOCK_CLICK_AFTER_MOVEMENT_MS 100 -#define RESET_ENCODER_AFTER_MS (1000/ENCODER_PULSES_PER_STEP) +#define RESET_ENCODER_AFTER_MS (2000/ENCODER_PULSES_PER_STEP) +#define ADVANCE_ENCODER_AFTER_MS (200/ENCODER_PULSES_PER_STEP) #if HAS_STATUS_MESSAGE #if ENABLED(STATUS_MESSAGE_SCROLLING) && ANY(HAS_WIRED_LCD, DWIN_LCD_PROUI) @@ -1056,26 +1057,29 @@ void MarlinUI::init() { if (TERN0(IS_RRW_KEYPAD, handle_keypad())) reset_status_timeout(ms); - uint8_t abs_diff = ABS(encoderDiff); - #if ENCODER_PULSES_PER_STEP > 1 static int8_t lastEncoderDiff; - static millis_t encoder_reset_timeout_ms; + static millis_t last_encoder_change_ms; + static int8_t last_full_step_dir; + static bool changed_dir; + if (lastEncoderDiff != encoderDiff) { TERN_(HAS_TOUCH_SLEEP, wakeup_screen()); - encoder_reset_timeout_ms = ms + RESET_ENCODER_AFTER_MS; - } else if (encoderDiff != 0 && ELAPSED(ms, encoder_reset_timeout_ms)) { - // In some machines, the haptic ticks physically get out of sync with the actual steps. - if (abs_diff <= epps / 2) // If below half a step - encoderDiff = 0; // Clear residual pulses. - else if (abs_diff < epps) { // If beyond half but below full - abs_diff = epps; // Treat as a full step size - encoderDiff = SIGN(encoderDiff) * abs_diff; // ...in the spin direction. - } + last_encoder_change_ms = ms; + } else if (encoderDiff != 0) { + // In some machines, the haptic ticks physically get out of sync with the actual steps. + if (ELAPSED(ms, last_encoder_change_ms + RESET_ENCODER_AFTER_MS)) { + encoderDiff = 0; // Clear residual pulses. + } else if (ELAPSED(ms, last_encoder_change_ms + ADVANCE_ENCODER_AFTER_MS)) { + changed_dir = SIGN(encoderDiff) != last_full_step_dir; + bool beyond_half_step = ABS(encoderDiff) > epps / 2; + if (changed_dir && beyond_half_step) encoderDiff = SIGN(encoderDiff) * epps; // Treat as a full step + } } lastEncoderDiff = encoderDiff; #endif + uint8_t abs_diff = ABS(encoderDiff); const bool encoderPastThreshold = (abs_diff >= epps); if (encoderPastThreshold && TERN1(IS_TFTGLCD_PANEL, !external_control)) { @@ -1118,8 +1122,12 @@ void MarlinUI::init() { if (fullSteps != 0) { next_click_enable_ms = ms + BLOCK_CLICK_AFTER_MOVEMENT_MS; encoderDiff -= fullSteps * epps; - if (can_encode() && !lcd_clicked) + if (can_encode() && !lcd_clicked) { encoderPosition += (fullSteps * encoderMultiplier); + #if ENCODER_PULSES_PER_STEP > 1 + last_full_step_dir = SIGN(fullSteps); + #endif + } } }