From e9cc5d505bf285b6c911578765eb320b98ca9745 Mon Sep 17 00:00:00 2001 From: David Buezas Date: Tue, 5 Dec 2023 13:09:16 +0100 Subject: [PATCH 01/14] Encoder improvements --- Marlin/Configuration_adv.h | 2 ++ Marlin/src/lcd/marlinui.cpp | 36 ++++++++++++++++++------------------ Marlin/src/lcd/menu/menu.cpp | 7 +++++++ 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 5871c99a215f..6a7e0dc30a6d 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -1583,6 +1583,8 @@ //#define DOUBLE_LCD_FRAMERATE // Not recommended for slow boards. #endif + //#define MENU_SCROLL_FEEDBACK // Beep when any menu is scrolled + // The timeout to return to the status screen from sub-menus //#define LCD_TIMEOUT_TO_STATUS 15000 // (ms) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 178f0b74ecc5..97d174b5e661 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -67,6 +67,8 @@ MarlinUI ui; constexpr uint8_t epps = ENCODER_PULSES_PER_STEP; +#define BLOCK_CLICK_AFTER_MOVEMENT_MS 100 + #if HAS_STATUS_MESSAGE #if ENABLED(STATUS_MESSAGE_SCROLLING) && ANY(HAS_WIRED_LCD, DWIN_LCD_PROUI) uint8_t MarlinUI::status_scroll_offset; // = 0 @@ -932,6 +934,7 @@ void MarlinUI::init() { void MarlinUI::update() { static uint16_t max_display_update_time = 0; + static millis_t last_encoder_full_step_movement = 0; millis_t ms = millis(); #if LED_POWEROFF_TIMEOUT > 0 @@ -982,7 +985,12 @@ void MarlinUI::init() { if (!touch_buttons) { // Integrated LCD click handling via button_pressed if (!external_control && button_pressed()) { - if (!wait_for_unclick) do_click(); // Handle the click + if (!wait_for_unclick) { + if (ELAPSED(ms, last_encoder_full_step_movement + BLOCK_CLICK_AFTER_MOVEMENT_MS)) + do_click(); // Handle the click + else + wait_for_unclick = true; + } } else wait_for_unclick = false; @@ -1019,19 +1027,7 @@ void MarlinUI::init() { uint8_t abs_diff = ABS(encoderDiff); #if ENCODER_PULSES_PER_STEP > 1 - // When reversing the encoder direction, a movement step can be missed because - // encoderDiff has a non-zero residual value, making the controller unresponsive. - // The fix clears the residual value when the encoder is idle. - // Also check if past half the threshold to compensate for missed single steps. static int8_t lastEncoderDiff; - - // Timeout? No decoder change since last check. 10 or 20 times per second. - if (encoderDiff == lastEncoderDiff && abs_diff <= epps / 2) // Same direction & size but not over a half-step? - encoderDiff = 0; // Clear residual pulses. - else if (WITHIN(abs_diff, epps / 2 + 1, epps - 1)) { // Past half of threshold? - abs_diff = epps; // Treat as a full step size - encoderDiff = (encoderDiff < 0 ? -1 : 1) * abs_diff; // ...in the spin direction. - } TERN_(HAS_TOUCH_SLEEP, if (lastEncoderDiff != encoderDiff) wakeup_screen()); lastEncoderDiff = encoderDiff; #endif @@ -1045,9 +1041,8 @@ void MarlinUI::init() { int32_t encoderMultiplier = 1; if (encoderRateMultiplierEnabled) { - const float encoderMovementSteps = float(abs_diff) / epps; - if (lastEncoderMovementMillis) { + const float encoderMovementSteps = float(abs_diff) / epps; // Note that the rate is always calculated between two passes through the // loop and that the abs of the encoderDiff value is tracked. const float encoderStepRate = encoderMovementSteps / float(ms - lastEncoderMovementMillis) * 1000; @@ -1076,9 +1071,14 @@ void MarlinUI::init() { #endif // ENCODER_RATE_MULTIPLIER - if (can_encode()) encoderPosition += (encoderDiff * encoderMultiplier) / epps; - - encoderDiff = 0; + int8_t fullSteps = encoderDiff / epps; + if (fullSteps != 0) { + last_encoder_full_step_movement = ms; + encoderDiff -= fullSteps * epps; + if (can_encode()) { + encoderPosition += (fullSteps * encoderMultiplier); + } + } } reset_status_timeout(ms); diff --git a/Marlin/src/lcd/menu/menu.cpp b/Marlin/src/lcd/menu/menu.cpp index 1c77d9a0923c..f148b0f4ffa3 100644 --- a/Marlin/src/lcd/menu/menu.cpp +++ b/Marlin/src/lcd/menu/menu.cpp @@ -269,6 +269,13 @@ void scroll_screen(const uint8_t limit, const bool is_menu) { } else encoderTopLine = encoderLine; + #ifdef MENU_SCROLL_FEEDBACK + static int8_t last_encoderLine = encoderLine; + if (encoderLine != last_encoderLine) { + BUZZ(2, 15000); + last_encoderLine = encoderLine; + } + #endif } #if HAS_LINE_TO_Z From 3967a1bcedd0dae4e0c4be8b222c6a6eae1573df Mon Sep 17 00:00:00 2001 From: David Buezas Date: Tue, 5 Dec 2023 14:42:33 +0100 Subject: [PATCH 02/14] Fully ignore 2 step jumps These are usually transient, so not setting `lastEncoderBits` is usually enough to avoid unexpected backward jumps. --- Marlin/src/lcd/marlinui.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 97d174b5e661..6c6e275c8706 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1393,9 +1393,10 @@ void MarlinUI::init() { #if HAS_ENCODER_WHEEL static uint8_t lastEncoderBits; + bool ignore = false; // Manage encoder rotation - #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: encoderDiff += encoderDirection; break; case _E2: encoderDiff -= encoderDirection; } + #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: encoderDiff += encoderDirection; break; case _E2: encoderDiff -= encoderDirection; break; default: ignore = true; } uint8_t enc = 0; if (buttons & EN_A) enc |= B01; @@ -1410,7 +1411,7 @@ void MarlinUI::init() { #if ALL(HAS_MARLINUI_MENU, AUTO_BED_LEVELING_UBL) external_encoder(); #endif - lastEncoderBits = enc; + if (!ignore) lastEncoderBits = enc; } #endif // HAS_ENCODER_WHEEL From 30048e194a5ba97cd7d069b80b82ed6d9edf9575 Mon Sep 17 00:00:00 2001 From: David Buezas Date: Tue, 5 Dec 2023 19:32:27 +0100 Subject: [PATCH 03/14] Use const --- Marlin/src/lcd/marlinui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 6c6e275c8706..92466d1297ee 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1071,7 +1071,7 @@ void MarlinUI::init() { #endif // ENCODER_RATE_MULTIPLIER - int8_t fullSteps = encoderDiff / epps; + const int8_t fullSteps = encoderDiff / epps; if (fullSteps != 0) { last_encoder_full_step_movement = ms; encoderDiff -= fullSteps * epps; From 4872aee4a00e32183c9619fd9648eeb44b89fbf8 Mon Sep 17 00:00:00 2001 From: David Buezas Date: Wed, 6 Dec 2023 19:12:17 +0100 Subject: [PATCH 04/14] Change macro names to match existing "MENU_SCROLL_FEEDBACK" and make duration and freq configurable --- Marlin/Configuration_adv.h | 7 ++++++- Marlin/src/lcd/menu/menu.cpp | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 6a7e0dc30a6d..206cac5b6cdd 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -1583,7 +1583,12 @@ //#define DOUBLE_LCD_FRAMERATE // Not recommended for slow boards. #endif - //#define MENU_SCROLL_FEEDBACK // Beep when any menu is scrolled + //#define BEEP_ON_SCROLL // Beep when any menu is scrolled + #if ENABLED(BEEP_ON_SCROLL) + #define SCROLL_BEEP_DURATION 2 + #define SCROLL_BEEP_FREQUENCY 15000 + #endif + // The timeout to return to the status screen from sub-menus //#define LCD_TIMEOUT_TO_STATUS 15000 // (ms) diff --git a/Marlin/src/lcd/menu/menu.cpp b/Marlin/src/lcd/menu/menu.cpp index f148b0f4ffa3..27565c8c0df7 100644 --- a/Marlin/src/lcd/menu/menu.cpp +++ b/Marlin/src/lcd/menu/menu.cpp @@ -269,10 +269,10 @@ void scroll_screen(const uint8_t limit, const bool is_menu) { } else encoderTopLine = encoderLine; - #ifdef MENU_SCROLL_FEEDBACK + #ifdef BEEP_ON_SCROLL static int8_t last_encoderLine = encoderLine; if (encoderLine != last_encoderLine) { - BUZZ(2, 15000); + BUZZ(SCROLL_BEEP_DURATION, SCROLL_BEEP_FREQUENCY); last_encoderLine = encoderLine; } #endif From 267662b08299fa91656b38ddcce8c25320eb11a3 Mon Sep 17 00:00:00 2001 From: David Buezas Date: Wed, 6 Dec 2023 20:07:12 +0100 Subject: [PATCH 05/14] Fix corner case of a click and a scroll happening on the same tick --- Marlin/src/lcd/marlinui.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 92466d1297ee..fb9ce8c90011 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1033,7 +1033,7 @@ void MarlinUI::init() { #endif const bool encoderPastThreshold = (abs_diff >= epps); - if (encoderPastThreshold || lcd_clicked) { + if (encoderPastThreshold) { if (encoderPastThreshold && TERN1(IS_TFTGLCD_PANEL, !external_control)) { #if ALL(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLIER) @@ -1075,7 +1075,7 @@ void MarlinUI::init() { if (fullSteps != 0) { last_encoder_full_step_movement = ms; encoderDiff -= fullSteps * epps; - if (can_encode()) { + if (can_encode() && !lcd_clicked) { encoderPosition += (fullSteps * encoderMultiplier); } } From 2fbca22282cc8571e931312d405ac3eb3850c16e Mon Sep 17 00:00:00 2001 From: David Buezas Date: Wed, 6 Dec 2023 20:44:49 +0100 Subject: [PATCH 06/14] Ignore quick direction changes --- Marlin/src/lcd/marlinui.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index fb9ce8c90011..8da109d4e4cb 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -23,6 +23,7 @@ #include "../inc/MarlinConfig.h" #include "../MarlinCore.h" // for printingIsPaused +#include "src/core/macros.h" #if LED_POWEROFF_TIMEOUT > 0 || ALL(HAS_WIRED_LCD, PRINTER_EVENT_LEDS) || (defined(LCD_BACKLIGHT_TIMEOUT_MINS) && defined(NEOPIXEL_BKGD_INDEX_FIRST)) #include "../feature/leds/leds.h" @@ -1073,6 +1074,15 @@ void MarlinUI::init() { const int8_t fullSteps = encoderDiff / epps; if (fullSteps != 0) { + static int8_t lastDir; + int8_t dir = SIGN(fullSteps); + if (encoderMultiplier != 1 && dir != lastDir) { + // Changed direction at high speed. + // This is most likely an artifact of skipped steps, keep previous direction instead. + encoderMultiplier *= -1; + } else { + lastDir = dir; + } last_encoder_full_step_movement = ms; encoderDiff -= fullSteps * epps; if (can_encode() && !lcd_clicked) { From cca1df9f7104046281f9b816279693513b70c9d7 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 8 Dec 2023 00:56:35 -0600 Subject: [PATCH 07/14] test no ENCODER_RATE_MULTIPLIER --- buildroot/tests/mega2560 | 1 + 1 file changed, 1 insertion(+) diff --git a/buildroot/tests/mega2560 b/buildroot/tests/mega2560 index d647a6ddb16c..1fbdd6b58b20 100755 --- a/buildroot/tests/mega2560 +++ b/buildroot/tests/mega2560 @@ -33,6 +33,7 @@ opt_enable AUTO_BED_LEVELING_UBL AVOID_OBSTACLES RESTORE_LEVELING_AFTER_G28 DEBU EMERGENCY_PARSER MULTI_NOZZLE_DUPLICATION CLASSIC_JERK LIN_ADVANCE ADVANCE_K_EXTRA QUICK_HOME \ SET_PROGRESS_MANUALLY SET_PROGRESS_PERCENT PRINT_PROGRESS_SHOW_DECIMALS SHOW_REMAINING_TIME \ ENCODER_NOISE_FILTER BABYSTEPPING BABYSTEP_XY NANODLP_Z_SYNC I2C_POSITION_ENCODERS M114_DETAIL +opt_disable ENCODER_RATE_MULTIPLIER exec_test $1 $2 "Azteeg X3 Pro | EXTRUDERS 5 | RRDFGSC | UBL | LIN_ADVANCE ..." "$3" # From ce172194e5efeda40715c8442c02da9b3d341133 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 8 Dec 2023 01:12:41 -0600 Subject: [PATCH 08/14] fix and clean up --- Marlin/src/lcd/marlinui.cpp | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 8da109d4e4cb..e3be59efcc48 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -23,7 +23,6 @@ #include "../inc/MarlinConfig.h" #include "../MarlinCore.h" // for printingIsPaused -#include "src/core/macros.h" #if LED_POWEROFF_TIMEOUT > 0 || ALL(HAS_WIRED_LCD, PRINTER_EVENT_LEDS) || (defined(LCD_BACKLIGHT_TIMEOUT_MINS) && defined(NEOPIXEL_BKGD_INDEX_FIRST)) #include "../feature/leds/leds.h" @@ -1074,20 +1073,17 @@ void MarlinUI::init() { const int8_t fullSteps = encoderDiff / epps; if (fullSteps != 0) { - static int8_t lastDir; - int8_t dir = SIGN(fullSteps); - if (encoderMultiplier != 1 && dir != lastDir) { - // Changed direction at high speed. - // This is most likely an artifact of skipped steps, keep previous direction instead. - encoderMultiplier *= -1; - } else { - lastDir = dir; - } + static bool lastFwd; + const bool fwd = fullSteps > 0; + // Direction change during fast spin is probably a glitch so go the other way + if (encoderMultiplier != 1 && fwd != lastFwd) + fullSteps *= -1; + + lastFwd = fwd; last_encoder_full_step_movement = ms; encoderDiff -= fullSteps * epps; - if (can_encode() && !lcd_clicked) { + if (can_encode() && !lcd_clicked) encoderPosition += (fullSteps * encoderMultiplier); - } } } From 23ec064f0d7fa5939b0cdd775189708d8b9765e8 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 8 Dec 2023 01:12:49 -0600 Subject: [PATCH 09/14] save this for later --- Marlin/Configuration_adv.h | 7 ------- Marlin/src/lcd/menu/menu.cpp | 7 ------- 2 files changed, 14 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 206cac5b6cdd..5871c99a215f 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -1583,13 +1583,6 @@ //#define DOUBLE_LCD_FRAMERATE // Not recommended for slow boards. #endif - //#define BEEP_ON_SCROLL // Beep when any menu is scrolled - #if ENABLED(BEEP_ON_SCROLL) - #define SCROLL_BEEP_DURATION 2 - #define SCROLL_BEEP_FREQUENCY 15000 - #endif - - // The timeout to return to the status screen from sub-menus //#define LCD_TIMEOUT_TO_STATUS 15000 // (ms) diff --git a/Marlin/src/lcd/menu/menu.cpp b/Marlin/src/lcd/menu/menu.cpp index 27565c8c0df7..1c77d9a0923c 100644 --- a/Marlin/src/lcd/menu/menu.cpp +++ b/Marlin/src/lcd/menu/menu.cpp @@ -269,13 +269,6 @@ void scroll_screen(const uint8_t limit, const bool is_menu) { } else encoderTopLine = encoderLine; - #ifdef BEEP_ON_SCROLL - static int8_t last_encoderLine = encoderLine; - if (encoderLine != last_encoderLine) { - BUZZ(SCROLL_BEEP_DURATION, SCROLL_BEEP_FREQUENCY); - last_encoderLine = encoderLine; - } - #endif } #if HAS_LINE_TO_Z From 116ea577a74d94f85def82ee1a32a4ccf9788b9a Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 8 Dec 2023 01:17:42 -0600 Subject: [PATCH 10/14] fix redundant condition, timeout reset --- Marlin/src/lcd/marlinui.cpp | 106 ++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index e3be59efcc48..91ef70d77ac1 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1033,60 +1033,60 @@ void MarlinUI::init() { #endif const bool encoderPastThreshold = (abs_diff >= epps); - if (encoderPastThreshold) { - if (encoderPastThreshold && TERN1(IS_TFTGLCD_PANEL, !external_control)) { - - #if ALL(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLIER) - - int32_t encoderMultiplier = 1; - - if (encoderRateMultiplierEnabled) { - if (lastEncoderMovementMillis) { - const float encoderMovementSteps = float(abs_diff) / epps; - // Note that the rate is always calculated between two passes through the - // loop and that the abs of the encoderDiff value is tracked. - const float encoderStepRate = encoderMovementSteps / float(ms - lastEncoderMovementMillis) * 1000; - - if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100; - else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10; - - // Enable to output the encoder steps per second value - //#define ENCODER_RATE_MULTIPLIER_DEBUG - #if ENABLED(ENCODER_RATE_MULTIPLIER_DEBUG) - SERIAL_ECHO_START(); - SERIAL_ECHOPGM("Enc Step Rate: ", encoderStepRate); - SERIAL_ECHOPGM(" Multiplier: ", encoderMultiplier); - SERIAL_ECHOPGM(" ENCODER_10X_STEPS_PER_SEC: ", ENCODER_10X_STEPS_PER_SEC); - SERIAL_ECHOPGM(" ENCODER_100X_STEPS_PER_SEC: ", ENCODER_100X_STEPS_PER_SEC); - SERIAL_EOL(); - #endif - } - - lastEncoderMovementMillis = ms; - } // encoderRateMultiplierEnabled - - #else - - constexpr int32_t encoderMultiplier = 1; - - #endif // ENCODER_RATE_MULTIPLIER - - const int8_t fullSteps = encoderDiff / epps; - if (fullSteps != 0) { - static bool lastFwd; - const bool fwd = fullSteps > 0; - // Direction change during fast spin is probably a glitch so go the other way - if (encoderMultiplier != 1 && fwd != lastFwd) - fullSteps *= -1; - - lastFwd = fwd; - last_encoder_full_step_movement = ms; - encoderDiff -= fullSteps * epps; - if (can_encode() && !lcd_clicked) - encoderPosition += (fullSteps * encoderMultiplier); - } + if (encoderPastThreshold && TERN1(IS_TFTGLCD_PANEL, !external_control)) { + + #if ALL(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLIER) + + int32_t encoderMultiplier = 1; + + if (encoderRateMultiplierEnabled) { + if (lastEncoderMovementMillis) { + const float encoderMovementSteps = float(abs_diff) / epps; + // Note that the rate is always calculated between two passes through the + // loop and that the abs of the encoderDiff value is tracked. + const float encoderStepRate = encoderMovementSteps / float(ms - lastEncoderMovementMillis) * 1000; + + if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100; + else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10; + + // Enable to output the encoder steps per second value + //#define ENCODER_RATE_MULTIPLIER_DEBUG + #if ENABLED(ENCODER_RATE_MULTIPLIER_DEBUG) + SERIAL_ECHO_START(); + SERIAL_ECHOPGM("Enc Step Rate: ", encoderStepRate); + SERIAL_ECHOPGM(" Multiplier: ", encoderMultiplier); + SERIAL_ECHOPGM(" ENCODER_10X_STEPS_PER_SEC: ", ENCODER_10X_STEPS_PER_SEC); + SERIAL_ECHOPGM(" ENCODER_100X_STEPS_PER_SEC: ", ENCODER_100X_STEPS_PER_SEC); + SERIAL_EOL(); + #endif + } + + lastEncoderMovementMillis = ms; + } // encoderRateMultiplierEnabled + + #else + + constexpr int32_t encoderMultiplier = 1; + + #endif // ENCODER_RATE_MULTIPLIER + + int8_t fullSteps = encoderDiff / epps; + if (fullSteps != 0) { + static bool lastFwd; + const bool fwd = fullSteps > 0; + // Direction change during fast spin is probably a glitch so go the other way + if (encoderMultiplier != 1 && fwd != lastFwd) + fullSteps *= -1; + + lastFwd = fwd; + last_encoder_full_step_movement = ms; + encoderDiff -= fullSteps * epps; + if (can_encode() && !lcd_clicked) + encoderPosition += (fullSteps * encoderMultiplier); } + } + if (encoderPastThreshold || lcd_clicked) { reset_status_timeout(ms); #if LCD_BACKLIGHT_TIMEOUT_MINS @@ -1100,7 +1100,7 @@ void MarlinUI::init() { #if LED_POWEROFF_TIMEOUT > 0 if (!powerManager.psu_on) leds.reset_timeout(ms); #endif - } // encoder activity + } #endif // HAS_ENCODER_ACTION From cd5d67e3f467d0783bc3049360d5f4f50ba8aab7 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 8 Dec 2023 01:22:25 -0600 Subject: [PATCH 11/14] next ms preferred over last ms --- Marlin/src/lcd/marlinui.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 91ef70d77ac1..35f20882ca38 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -934,8 +934,8 @@ void MarlinUI::init() { void MarlinUI::update() { static uint16_t max_display_update_time = 0; - static millis_t last_encoder_full_step_movement = 0; - millis_t ms = millis(); + static millis_t next_encoder_enable_ms = 0; + const millis_t ms = millis(); #if LED_POWEROFF_TIMEOUT > 0 leds.update_timeout(powerManager.psu_on); @@ -986,7 +986,7 @@ void MarlinUI::init() { // Integrated LCD click handling via button_pressed if (!external_control && button_pressed()) { if (!wait_for_unclick) { - if (ELAPSED(ms, last_encoder_full_step_movement + BLOCK_CLICK_AFTER_MOVEMENT_MS)) + if (ELAPSED(ms, next_encoder_enable_ms)) do_click(); // Handle the click else wait_for_unclick = true; @@ -1079,7 +1079,7 @@ void MarlinUI::init() { fullSteps *= -1; lastFwd = fwd; - last_encoder_full_step_movement = ms; + next_encoder_enable_ms = ms + BLOCK_CLICK_AFTER_MOVEMENT_MS; encoderDiff -= fullSteps * epps; if (can_encode() && !lcd_clicked) encoderPosition += (fullSteps * encoderMultiplier); From dd868e9c2a041bf2344a17fd2985cdbbbc810769 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 8 Dec 2023 01:25:31 -0600 Subject: [PATCH 12/14] longing for encoderDiff = 0 --- Marlin/src/lcd/marlinui.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 35f20882ca38..9982696ceb89 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -882,8 +882,8 @@ void MarlinUI::init() { void MarlinUI::external_encoder() { if (external_control && encoderDiff) { bedlevel.encoder_diff += encoderDiff; // Encoder for UBL G29 mesh editing - encoderDiff = 0; // Hide encoder events from the screen handler - refresh(LCDVIEW_REDRAW_NOW); // ...but keep the refresh. + encoderDiff = 0; // Hide encoder events from the screen handler + refresh(LCDVIEW_REDRAW_NOW); // ...but keep the refresh. } } From 6a9f9eea32755f9a1cf4bdac16a1be9588046ee3 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 8 Dec 2023 01:31:21 -0600 Subject: [PATCH 13/14] not needed unless ENCODER_RATE_MULTIPLIER --- Marlin/src/lcd/marlinui.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 9982696ceb89..22d340ed7533 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1072,13 +1072,16 @@ void MarlinUI::init() { int8_t fullSteps = encoderDiff / epps; if (fullSteps != 0) { - static bool lastFwd; - const bool fwd = fullSteps > 0; - // Direction change during fast spin is probably a glitch so go the other way - if (encoderMultiplier != 1 && fwd != lastFwd) - fullSteps *= -1; - lastFwd = fwd; + #if ENABLED(ENCODER_RATE_MULTIPLIER) + static bool lastFwd; + const bool fwd = fullSteps > 0; + // Direction change during fast spin is probably a glitch so go the other way + if (encoderMultiplier != 1 && fwd != lastFwd) + fullSteps *= -1; + lastFwd = fwd; + #endif + next_encoder_enable_ms = ms + BLOCK_CLICK_AFTER_MOVEMENT_MS; encoderDiff -= fullSteps * epps; if (can_encode() && !lcd_clicked) From 900c978233102a1dccfef053156934c4bcd63c82 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 9 Dec 2023 00:35:46 -0600 Subject: [PATCH 14/14] else --- Marlin/src/lcd/marlinui.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 22d340ed7533..4eb58f52aedf 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1076,10 +1076,10 @@ void MarlinUI::init() { #if ENABLED(ENCODER_RATE_MULTIPLIER) static bool lastFwd; const bool fwd = fullSteps > 0; - // Direction change during fast spin is probably a glitch so go the other way if (encoderMultiplier != 1 && fwd != lastFwd) - fullSteps *= -1; - lastFwd = fwd; + fullSteps *= -1; // Fast move and direction changed? Assume glitch. + else + lastFwd = fwd; // Slow move or lastFwd==fwd already. Remember dir. #endif next_encoder_enable_ms = ms + BLOCK_CLICK_AFTER_MOVEMENT_MS;