From 5be8b4ec6b17976b0c6755959c7a9a5ae444d48e Mon Sep 17 00:00:00 2001 From: David Buezas Date: Tue, 23 Jan 2024 22:04:38 +0100 Subject: [PATCH 01/37] New encoder logic with debouncing --- Marlin/src/lcd/marlinui.cpp | 117 ++++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 53 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 23cb0a70580b..25260098fa0b 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 @@ -963,6 +965,7 @@ void MarlinUI::init() { void MarlinUI::update() { static uint16_t max_display_update_time = 0; + static millis_t next_encoder_enable_ms = 0; const millis_t ms = millis(); #if LED_POWEROFF_TIMEOUT > 0 @@ -1013,7 +1016,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, next_encoder_enable_ms)) + do_click(); // Handle the click + else + wait_for_unclick = true; + } } else wait_for_unclick = false; @@ -1050,68 +1058,59 @@ 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 const bool encoderPastThreshold = (abs_diff >= epps); - if (encoderPastThreshold || lcd_clicked) { - if (encoderPastThreshold && TERN1(IS_TFTGLCD_PANEL, !external_control)) { + if (encoderPastThreshold && TERN1(IS_TFTGLCD_PANEL, !external_control)) { - #if ALL(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLIER) + #if ALL(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLIER) - int32_t encoderMultiplier = 1; + int32_t encoderMultiplier = 1; - if (encoderRateMultiplierEnabled) { + 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 + } - if (lastEncoderMovementMillis) { - // 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 + lastEncoderMovementMillis = ms; + } // encoderRateMultiplierEnabled - constexpr int32_t encoderMultiplier = 1; + #else - #endif // ENCODER_RATE_MULTIPLIER + constexpr int32_t encoderMultiplier = 1; - if (can_encode()) encoderPosition += (encoderDiff * encoderMultiplier) / epps; + #endif // ENCODER_RATE_MULTIPLIER - encoderDiff = 0; + int8_t fullSteps = encoderDiff / epps; + if (fullSteps != 0) { + next_encoder_enable_ms = ms + BLOCK_CLICK_AFTER_MOVEMENT_MS; + encoderDiff -= fullSteps * epps; + if (can_encode() && !lcd_clicked) + encoderPosition += (fullSteps * encoderMultiplier); } + } + if (encoderPastThreshold || lcd_clicked) { reset_status_timeout(ms); #if HAS_BACKLIGHT_TIMEOUT @@ -1422,14 +1421,26 @@ void MarlinUI::init() { } // next_button_update_ms #if HAS_ENCODER_WHEEL + #define ENCODER_DEBOUNCE_MS 2 static uint8_t lastEncoderBits; - - // Manage encoder rotation - #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: encoderDiff += encoderDirection; break; case _E2: encoderDiff -= encoderDirection; } - - uint8_t enc = 0; - if (buttons & EN_A) enc |= B01; - if (buttons & EN_B) enc |= B10; + static uint8_t enc; + static uint8_t buttons_was = buttons; + static uint32_t en_A_blocked_ms; + static uint32_t en_B_blocked_ms; + + const bool en_A = (buttons & EN_A); + const bool en_B = (buttons & EN_B); + const bool en_A_was = (buttons_was & EN_A); + const bool en_B_was = (buttons_was & EN_B); + + buttons_was = buttons; + + if (en_A != en_A_was) en_A_blocked_ms = now + ENCODER_DEBOUNCE_MS; + else if (ELAPSED(now, en_A_blocked_ms)) SET_BIT_TO(enc, 0, en_A); + if (en_B != en_B_was) en_B_blocked_ms = now + ENCODER_DEBOUNCE_MS; + else if (ELAPSED(now, en_B_blocked_ms)) SET_BIT_TO(enc, 1, en_B); + + #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: encoderDiff += encoderDirection; break; case _E2: encoderDiff -= encoderDirection; break; } if (enc != lastEncoderBits) { switch (enc) { case 0: ENCODER_SPIN(1, 2); break; From 6d6b16375af5d8bfb44e3ed92a3cc089115c840e Mon Sep 17 00:00:00 2001 From: David Buezas Date: Fri, 26 Jan 2024 18:18:22 +0100 Subject: [PATCH 02/37] Increase debouncing time to 3ms and fix unused variable warning --- 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 25260098fa0b..3869c8400772 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1057,9 +1057,9 @@ void MarlinUI::init() { uint8_t abs_diff = ABS(encoderDiff); - #if ENCODER_PULSES_PER_STEP > 1 + #if ALL(ENCODER_PULSES_PER_STEP > 1, HAS_TOUCH_SLEEP) static int8_t lastEncoderDiff; - TERN_(HAS_TOUCH_SLEEP, if (lastEncoderDiff != encoderDiff) wakeup_screen()); + if (lastEncoderDiff != encoderDiff) wakeup_screen(): lastEncoderDiff = encoderDiff; #endif @@ -1421,7 +1421,7 @@ void MarlinUI::init() { } // next_button_update_ms #if HAS_ENCODER_WHEEL - #define ENCODER_DEBOUNCE_MS 2 + #define ENCODER_DEBOUNCE_MS 3 static uint8_t lastEncoderBits; static uint8_t enc; static uint8_t buttons_was = buttons; From c88345d99e7e15ac702a0bdc24e25d467dbd112d Mon Sep 17 00:00:00 2001 From: David Buezas Date: Tue, 30 Jan 2024 20:44:07 +0100 Subject: [PATCH 03/37] Add encoder reset mechanism to avoid off-by-one-step bug in Ender 3 --- Marlin/src/lcd/marlinui.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 3869c8400772..045b76709063 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -68,6 +68,7 @@ MarlinUI ui; constexpr uint8_t epps = ENCODER_PULSES_PER_STEP; #define BLOCK_CLICK_AFTER_MOVEMENT_MS 100 +#define RESET_ENCODER_AFTER_MS 500 #if HAS_STATUS_MESSAGE #if ENABLED(STATUS_MESSAGE_SCROLLING) && ANY(HAS_WIRED_LCD, DWIN_LCD_PROUI) @@ -965,7 +966,7 @@ void MarlinUI::init() { void MarlinUI::update() { static uint16_t max_display_update_time = 0; - static millis_t next_encoder_enable_ms = 0; + static millis_t next_click_enable_ms = 0; const millis_t ms = millis(); #if LED_POWEROFF_TIMEOUT > 0 @@ -1017,7 +1018,7 @@ void MarlinUI::init() { // Integrated LCD click handling via button_pressed if (!external_control && button_pressed()) { if (!wait_for_unclick) { - if (ELAPSED(ms, next_encoder_enable_ms)) + if (ELAPSED(ms, next_click_enable_ms)) do_click(); // Handle the click else wait_for_unclick = true; @@ -1057,9 +1058,17 @@ void MarlinUI::init() { uint8_t abs_diff = ABS(encoderDiff); - #if ALL(ENCODER_PULSES_PER_STEP > 1, HAS_TOUCH_SLEEP) + #if ENCODER_PULSES_PER_STEP > 1 static int8_t lastEncoderDiff; - if (lastEncoderDiff != encoderDiff) wakeup_screen(): + static millis_t encoder_reset_timeout_ms; + if (lastEncoderDiff != encoderDiff) { + TERN_(HAS_TOUCH_SLEEP, wakeup_screen()); + encoder_reset_timeout_ms = ms + RESET_ENCODER_AFTER_MS; + } else if (ELAPSED(ms, encoder_reset_timeout_ms)) { + // Reset encoder substeps after a while. + // This solves the issue of the haptic ticks of some encoders physically getting out of sync with the actual steps after a while . + encoderDiff = 0; + } lastEncoderDiff = encoderDiff; #endif @@ -1103,12 +1112,12 @@ void MarlinUI::init() { int8_t fullSteps = encoderDiff / epps; if (fullSteps != 0) { - next_encoder_enable_ms = ms + BLOCK_CLICK_AFTER_MOVEMENT_MS; + next_click_enable_ms = ms + BLOCK_CLICK_AFTER_MOVEMENT_MS; encoderDiff -= fullSteps * epps; if (can_encode() && !lcd_clicked) encoderPosition += (fullSteps * encoderMultiplier); } - } + } if (encoderPastThreshold || lcd_clicked) { reset_status_timeout(ms); @@ -1425,8 +1434,8 @@ void MarlinUI::init() { static uint8_t lastEncoderBits; static uint8_t enc; static uint8_t buttons_was = buttons; - static uint32_t en_A_blocked_ms; - static uint32_t en_B_blocked_ms; + static millis_t en_A_blocked_ms; + static millis_t en_B_blocked_ms; const bool en_A = (buttons & EN_A); const bool en_B = (buttons & EN_B); From 92c4c7d36cf8af076500f6dc9a9a5d3c93990b82 Mon Sep 17 00:00:00 2001 From: David Buezas Date: Fri, 2 Feb 2024 23:57:31 +0100 Subject: [PATCH 04/37] Encoder reset timeout depends on steps per pulses. Reset may advance encoderDiff if passed half a step. --- Marlin/src/lcd/marlinui.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 045b76709063..33e807c16edc 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -68,7 +68,7 @@ MarlinUI ui; constexpr uint8_t epps = ENCODER_PULSES_PER_STEP; #define BLOCK_CLICK_AFTER_MOVEMENT_MS 100 -#define RESET_ENCODER_AFTER_MS 500 +#define RESET_ENCODER_AFTER_MS (1000/ENCODER_PULSES_PER_STEP) #if HAS_STATUS_MESSAGE #if ENABLED(STATUS_MESSAGE_SCROLLING) && ANY(HAS_WIRED_LCD, DWIN_LCD_PROUI) @@ -1064,10 +1064,14 @@ void MarlinUI::init() { if (lastEncoderDiff != encoderDiff) { TERN_(HAS_TOUCH_SLEEP, wakeup_screen()); encoder_reset_timeout_ms = ms + RESET_ENCODER_AFTER_MS; - } else if (ELAPSED(ms, encoder_reset_timeout_ms)) { - // Reset encoder substeps after a while. - // This solves the issue of the haptic ticks of some encoders physically getting out of sync with the actual steps after a while . - encoderDiff = 0; + } 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. + } } lastEncoderDiff = encoderDiff; #endif From 2100d4562a05d59b8db42d7ceb99636111bfdcab Mon Sep 17 00:00:00 2001 From: David Buezas Date: Sun, 4 Feb 2024 21:03:00 +0100 Subject: [PATCH 05/37] 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 + } } } From c9e079222340008518a0cd45beb73b6efd3ae66f Mon Sep 17 00:00:00 2001 From: David Buezas Date: Sun, 4 Feb 2024 23:28:18 +0100 Subject: [PATCH 06/37] Revert "Advance encoder after a while if it has crossed the half a step threshold while changing direction" This reverts commit 2100d4562a05d59b8db42d7ceb99636111bfdcab. --- Marlin/src/lcd/marlinui.cpp | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 527fccdd94e3..33e807c16edc 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -68,8 +68,7 @@ MarlinUI ui; constexpr uint8_t epps = ENCODER_PULSES_PER_STEP; #define BLOCK_CLICK_AFTER_MOVEMENT_MS 100 -#define RESET_ENCODER_AFTER_MS (2000/ENCODER_PULSES_PER_STEP) -#define ADVANCE_ENCODER_AFTER_MS (200/ENCODER_PULSES_PER_STEP) +#define RESET_ENCODER_AFTER_MS (1000/ENCODER_PULSES_PER_STEP) #if HAS_STATUS_MESSAGE #if ENABLED(STATUS_MESSAGE_SCROLLING) && ANY(HAS_WIRED_LCD, DWIN_LCD_PROUI) @@ -1057,29 +1056,26 @@ 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 last_encoder_change_ms; - static int8_t last_full_step_dir; - static bool changed_dir; - + static millis_t encoder_reset_timeout_ms; if (lastEncoderDiff != encoderDiff) { TERN_(HAS_TOUCH_SLEEP, wakeup_screen()); - 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 - } + 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. + } } lastEncoderDiff = encoderDiff; #endif - uint8_t abs_diff = ABS(encoderDiff); const bool encoderPastThreshold = (abs_diff >= epps); if (encoderPastThreshold && TERN1(IS_TFTGLCD_PANEL, !external_control)) { @@ -1122,12 +1118,8 @@ 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 - } } } From 32fd50dc370bd6673f9e6c1b42456b797cf60c00 Mon Sep 17 00:00:00 2001 From: David Buezas Date: Sun, 4 Feb 2024 23:28:30 +0100 Subject: [PATCH 07/37] Revert "Encoder reset timeout depends on steps per pulses. Reset may advance encoderDiff if passed half a step." This reverts commit 92c4c7d36cf8af076500f6dc9a9a5d3c93990b82. --- Marlin/src/lcd/marlinui.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 33e807c16edc..045b76709063 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -68,7 +68,7 @@ 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 500 #if HAS_STATUS_MESSAGE #if ENABLED(STATUS_MESSAGE_SCROLLING) && ANY(HAS_WIRED_LCD, DWIN_LCD_PROUI) @@ -1064,14 +1064,10 @@ void MarlinUI::init() { 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. - } + } else if (ELAPSED(ms, encoder_reset_timeout_ms)) { + // Reset encoder substeps after a while. + // This solves the issue of the haptic ticks of some encoders physically getting out of sync with the actual steps after a while . + encoderDiff = 0; } lastEncoderDiff = encoderDiff; #endif From fc1d80b0aa016c4d9ee721f9b05fa3b8b897a175 Mon Sep 17 00:00:00 2001 From: David Buezas Date: Sun, 4 Feb 2024 23:29:56 +0100 Subject: [PATCH 08/37] Make RESET_ENCODER_AFTER_MS depend on ENCODER_PULSES_PER_STEP --- 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 045b76709063..745c8fec1a43 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -68,7 +68,7 @@ MarlinUI ui; constexpr uint8_t epps = ENCODER_PULSES_PER_STEP; #define BLOCK_CLICK_AFTER_MOVEMENT_MS 100 -#define RESET_ENCODER_AFTER_MS 500 +#define RESET_ENCODER_AFTER_MS (2000/ENCODER_PULSES_PER_STEP) #if HAS_STATUS_MESSAGE #if ENABLED(STATUS_MESSAGE_SCROLLING) && ANY(HAS_WIRED_LCD, DWIN_LCD_PROUI) From 407327553c6594581e4ff106ecd483305aab00c9 Mon Sep 17 00:00:00 2001 From: David Buezas Date: Mon, 5 Feb 2024 20:14:35 +0100 Subject: [PATCH 09/37] Compute abs_diff after step reset --- Marlin/src/lcd/marlinui.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 1509e5b4a557..44d90b838836 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1036,8 +1036,6 @@ 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; @@ -1052,6 +1050,7 @@ void MarlinUI::init() { lastEncoderDiff = encoderDiff; #endif + uint8_t abs_diff = ABS(encoderDiff); const bool encoderPastThreshold = (abs_diff >= epps); if (encoderPastThreshold && TERN1(IS_TFTGLCD_PANEL, !external_control)) { From df74c621a37dcc007de6c1cfad2407ae17d465f7 Mon Sep 17 00:00:00 2001 From: David Buezas Date: Mon, 5 Feb 2024 20:41:49 +0100 Subject: [PATCH 10/37] Fix no screen wakeup if encoder has 1 pulse per step --- 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 44d90b838836..0f2c926062b3 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1036,11 +1036,12 @@ void MarlinUI::init() { if (TERN0(IS_RRW_KEYPAD, handle_keypad())) reset_status_timeout(ms); + static int8_t lastEncoderDiff; + if (lastEncoderDiff != encoderDiff) wake_display(); + #if ENCODER_PULSES_PER_STEP > 1 - static int8_t lastEncoderDiff; static millis_t encoder_reset_timeout_ms; if (lastEncoderDiff != encoderDiff) { - wake_display(); encoder_reset_timeout_ms = ms + RESET_ENCODER_AFTER_MS; } else if (ELAPSED(ms, encoder_reset_timeout_ms)) { // Reset encoder substeps after a while. From 160c46d64da125d90fcfaff8ab1a6141446808a3 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 7 Feb 2024 00:50:01 -0600 Subject: [PATCH 11/37] misc. cleanup --- Marlin/src/lcd/marlinui.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 0f2c926062b3..fbbe81c45c00 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -68,7 +68,7 @@ MarlinUI ui; constexpr uint8_t epps = ENCODER_PULSES_PER_STEP; #define BLOCK_CLICK_AFTER_MOVEMENT_MS 100 -#define RESET_ENCODER_AFTER_MS (2000/ENCODER_PULSES_PER_STEP) +#define RESET_ENCODER_AFTER_MS (2000UL / (ENCODER_PULSES_PER_STEP)) #if HAS_STATUS_MESSAGE #if ENABLED(STATUS_MESSAGE_SCROLLING) && ANY(HAS_WIRED_LCD, DWIN_LCD_PROUI) @@ -1043,15 +1043,17 @@ void MarlinUI::init() { static millis_t encoder_reset_timeout_ms; if (lastEncoderDiff != encoderDiff) { encoder_reset_timeout_ms = ms + RESET_ENCODER_AFTER_MS; - } else if (ELAPSED(ms, encoder_reset_timeout_ms)) { - // Reset encoder substeps after a while. - // This solves the issue of the haptic ticks of some encoders physically getting out of sync with the actual steps after a while . - encoderDiff = 0; + } + else if (ELAPSED(ms, encoder_reset_timeout_ms)) { + // Reset encoder substeps after a while. + // This solves the issue of the haptic ticks of some encoders + // physically getting out of sync with the actual steps after a while. + encoderDiff = 0; } lastEncoderDiff = encoderDiff; #endif - uint8_t abs_diff = ABS(encoderDiff); + const uint8_t abs_diff = ABS(encoderDiff); const bool encoderPastThreshold = (abs_diff >= epps); if (encoderPastThreshold && TERN1(IS_TFTGLCD_PANEL, !external_control)) { @@ -1411,22 +1413,20 @@ void MarlinUI::init() { #if HAS_ENCODER_WHEEL #define ENCODER_DEBOUNCE_MS 3 - static uint8_t lastEncoderBits; - static uint8_t enc; + static uint8_t lastEncoderBits, enc; static uint8_t buttons_was = buttons; - static millis_t en_A_blocked_ms; - static millis_t en_B_blocked_ms; + static millis_t en_A_blocked_ms, en_B_blocked_ms; - const bool en_A = (buttons & EN_A); - const bool en_B = (buttons & EN_B); - const bool en_A_was = (buttons_was & EN_A); - const bool en_B_was = (buttons_was & EN_B); + const bool en_A = (buttons & EN_A), + en_B = (buttons & EN_B), + en_A_was = (buttons_was & EN_A), + en_B_was = (buttons_was & EN_B); buttons_was = buttons; - if (en_A != en_A_was) en_A_blocked_ms = now + ENCODER_DEBOUNCE_MS; + if (en_A != en_A_was) en_A_blocked_ms = now + (ENCODER_DEBOUNCE_MS); else if (ELAPSED(now, en_A_blocked_ms)) SET_BIT_TO(enc, 0, en_A); - if (en_B != en_B_was) en_B_blocked_ms = now + ENCODER_DEBOUNCE_MS; + if (en_B != en_B_was) en_B_blocked_ms = now + (ENCODER_DEBOUNCE_MS); else if (ELAPSED(now, en_B_blocked_ms)) SET_BIT_TO(enc, 1, en_B); #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: encoderDiff += encoderDirection; break; case _E2: encoderDiff -= encoderDirection; break; } From f5b39d2dfbafee87170ef98dc74a9c8c13dbdff6 Mon Sep 17 00:00:00 2001 From: David Buezas Date: Wed, 7 Feb 2024 22:03:52 +0100 Subject: [PATCH 12/37] Test ignoring 2 steps jump. --- 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 fbbe81c45c00..7a8a1e31f21e 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1429,7 +1429,8 @@ void MarlinUI::init() { if (en_B != en_B_was) en_B_blocked_ms = now + (ENCODER_DEBOUNCE_MS); else if (ELAPSED(now, en_B_blocked_ms)) SET_BIT_TO(enc, 1, en_B); - #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: encoderDiff += encoderDirection; break; case _E2: encoderDiff -= encoderDirection; break; } + bool valid = true; + #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: encoderDiff += encoderDirection; break; case _E2: encoderDiff -= encoderDirection; break; default: valid = false; } if (enc != lastEncoderBits) { switch (enc) { case 0: ENCODER_SPIN(1, 2); break; @@ -1440,7 +1441,7 @@ void MarlinUI::init() { #if ALL(HAS_MARLINUI_MENU, AUTO_BED_LEVELING_UBL) external_encoder(); #endif - lastEncoderBits = enc; + if (valid) lastEncoderBits = enc; } #endif // HAS_ENCODER_WHEEL From 408aeba4027c4a37fb699c0bb25ee94e0a955860 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 7 Feb 2024 20:00:35 -0600 Subject: [PATCH 13/37] merge followup --- 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 09f99a845119..1a81d9b5104a 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1087,7 +1087,7 @@ void MarlinUI::init() { next_click_enable_ms = ms + BLOCK_CLICK_AFTER_MOVEMENT_MS; encoderDiff -= fullSteps * epps; if (can_encode() && !lcd_clicked) - encoderPosition += (fullSteps * encoderMultiplier); + encoderPosition += (fullSteps * encoder_multiplier); } } From 3d36ddcbb8fccab1fefb81033a605032157c97ef Mon Sep 17 00:00:00 2001 From: David Buezas Date: Thu, 8 Feb 2024 20:26:50 +0100 Subject: [PATCH 14/37] Add missed step detection, remove BLOCK_CLICK_AFTER_MOVEMENT_MS and RESET_ENCODER_AFTER_MS --- Marlin/src/lcd/marlinui.cpp | 44 +++++++++++++------------------------ 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 1a81d9b5104a..03a87e2788b0 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -67,9 +67,6 @@ MarlinUI ui; constexpr uint8_t epps = ENCODER_PULSES_PER_STEP; -#define BLOCK_CLICK_AFTER_MOVEMENT_MS 100 -#define RESET_ENCODER_AFTER_MS (2000UL / (ENCODER_PULSES_PER_STEP)) - #if HAS_STATUS_MESSAGE #if ENABLED(STATUS_MESSAGE_SCROLLING) && ANY(HAS_WIRED_LCD, DWIN_LCD_PROUI) uint8_t MarlinUI::status_scroll_offset; // = 0 @@ -939,7 +936,6 @@ void MarlinUI::init() { void MarlinUI::update() { static uint16_t max_display_update_time = 0; - static millis_t next_click_enable_ms = 0; const millis_t ms = millis(); #if LED_POWEROFF_TIMEOUT > 0 @@ -990,12 +986,7 @@ void MarlinUI::init() { if (!touch_buttons) { // Integrated LCD click handling via button_pressed if (!external_control && button_pressed()) { - if (!wait_for_unclick) { - if (ELAPSED(ms, next_click_enable_ms)) - do_click(); // Handle the click - else - wait_for_unclick = true; - } + if (!wait_for_unclick) do_click(); // Handle the click } else wait_for_unclick = false; @@ -1031,20 +1022,7 @@ void MarlinUI::init() { static int8_t lastEncoderDiff; if (lastEncoderDiff != encoderDiff) wake_display(); - - #if ENCODER_PULSES_PER_STEP > 1 - static millis_t encoder_reset_timeout_ms; - if (lastEncoderDiff != encoderDiff) { - encoder_reset_timeout_ms = ms + RESET_ENCODER_AFTER_MS; - } - else if (ELAPSED(ms, encoder_reset_timeout_ms)) { - // Reset encoder substeps after a while. - // This solves the issue of the haptic ticks of some encoders - // physically getting out of sync with the actual steps after a while. - encoderDiff = 0; - } - lastEncoderDiff = encoderDiff; - #endif + lastEncoderDiff = encoderDiff; const uint8_t abs_diff = ABS(encoderDiff); const bool encoderPastThreshold = (abs_diff >= epps); @@ -1084,7 +1062,6 @@ void MarlinUI::init() { int8_t fullSteps = encoderDiff / epps; if (fullSteps != 0) { - next_click_enable_ms = ms + BLOCK_CLICK_AFTER_MOVEMENT_MS; encoderDiff -= fullSteps * epps; if (can_encode() && !lcd_clicked) encoderPosition += (fullSteps * encoder_multiplier); @@ -1402,7 +1379,7 @@ void MarlinUI::init() { } // next_button_update_ms #if HAS_ENCODER_WHEEL - #define ENCODER_DEBOUNCE_MS 3 + #define ENCODER_DEBOUNCE_MS 2 static uint8_t lastEncoderBits, enc; static uint8_t buttons_was = buttons; static millis_t en_A_blocked_ms, en_B_blocked_ms; @@ -1419,8 +1396,9 @@ void MarlinUI::init() { if (en_B != en_B_was) en_B_blocked_ms = now + (ENCODER_DEBOUNCE_MS); else if (ELAPSED(now, en_B_blocked_ms)) SET_BIT_TO(enc, 1, en_B); - bool valid = true; - #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: encoderDiff += encoderDirection; break; case _E2: encoderDiff -= encoderDirection; break; default: valid = false; } + static int8_t last_dir; + int8_t dir = 0; + #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: dir = encoderDirection; break; case _E2: dir = -encoderDirection; break; } if (enc != lastEncoderBits) { switch (enc) { case 0: ENCODER_SPIN(1, 2); break; @@ -1428,10 +1406,18 @@ void MarlinUI::init() { case 3: ENCODER_SPIN(2, 1); break; case 1: ENCODER_SPIN(3, 0); break; } + if (dir == 0) { + // The encoder is 2 pulses away from last update, assume same direction. + // Without this, the "tick" in encoders with 4 pulses per step get of synch with encoderPosition + encoderDiff += last_dir * 2; + } else { + encoderDiff += dir; + last_dir = dir; + } + lastEncoderBits = enc; #if ALL(HAS_MARLINUI_MENU, AUTO_BED_LEVELING_UBL) external_encoder(); #endif - if (valid) lastEncoderBits = enc; } #endif // HAS_ENCODER_WHEEL From adff93953585a191a5e23064c344ace88bcc08ff Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 8 Feb 2024 21:24:51 -0600 Subject: [PATCH 15/37] prettify --- Marlin/src/lcd/marlinui.cpp | 62 ++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 03a87e2788b0..7c491b338e65 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1379,42 +1379,40 @@ void MarlinUI::init() { } // next_button_update_ms #if HAS_ENCODER_WHEEL - #define ENCODER_DEBOUNCE_MS 2 - static uint8_t lastEncoderBits, enc; - static uint8_t buttons_was = buttons; - static millis_t en_A_blocked_ms, en_B_blocked_ms; - - const bool en_A = (buttons & EN_A), - en_B = (buttons & EN_B), - en_A_was = (buttons_was & EN_A), - en_B_was = (buttons_was & EN_B); - - buttons_was = buttons; - - if (en_A != en_A_was) en_A_blocked_ms = now + (ENCODER_DEBOUNCE_MS); - else if (ELAPSED(now, en_A_blocked_ms)) SET_BIT_TO(enc, 0, en_A); - if (en_B != en_B_was) en_B_blocked_ms = now + (ENCODER_DEBOUNCE_MS); - else if (ELAPSED(now, en_B_blocked_ms)) SET_BIT_TO(enc, 1, en_B); - - static int8_t last_dir; - int8_t dir = 0; - #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: dir = encoderDirection; break; case _E2: dir = -encoderDirection; break; } - if (enc != lastEncoderBits) { - switch (enc) { - case 0: ENCODER_SPIN(1, 2); break; - case 2: ENCODER_SPIN(0, 3); break; - case 3: ENCODER_SPIN(2, 1); break; - case 1: ENCODER_SPIN(3, 0); break; + #define ENCODER_DEBOUNCE_MS 2 + static uint8_t old_buttons; + const uint8_t button_diff = buttons ^ old_buttons; + old_buttons = buttons; + + static uint8_t enc; + + static millis_t en_A_bounce_ms; + if (button_diff & EN_A) en_A_bounce_ms = now + (ENCODER_DEBOUNCE_MS); + else if (ELAPSED(now, en_A_bounce_ms)) SET_BIT_TO(enc, 0, buttons & EN_A); + + static millis_t en_B_bounce_ms; + if (button_diff & EN_B) en_B_bounce_ms = now + (ENCODER_DEBOUNCE_MS); + else if (ELAPSED(now, en_B_bounce_ms)) SET_BIT_TO(enc, 1, buttons & EN_B); + + static uint8_t old_enc; + if (enc != old_enc) { + int8_t dir = 0; + switch ((old_enc << 2) | enc) { + case 2: case 11: case 13: case 4: dir = encoderDirection; break; + case 8: case 14: case 7: case 1: dir = -encoderDirection; break; } - if (dir == 0) { - // The encoder is 2 pulses away from last update, assume same direction. - // Without this, the "tick" in encoders with 4 pulses per step get of synch with encoderPosition - encoderDiff += last_dir * 2; - } else { + old_enc = enc; + + static int8_t last_dir; + if (dir) { encoderDiff += dir; last_dir = dir; } - lastEncoderBits = enc; + else { + // The encoder is likely 2 pulses away from last update, assume same direction. + // Keeps encoders with 4 pulses-per-step in better sync, but for fast initial spin the dir may be wrong. + encoderDiff += last_dir * 2; + } #if ALL(HAS_MARLINUI_MENU, AUTO_BED_LEVELING_UBL) external_encoder(); #endif From 5a015dcca88b11cf8d47d780a5844e49b6789fce Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 8 Feb 2024 21:42:06 -0600 Subject: [PATCH 16/37] Adjust encoder multiplier --- Marlin/src/inc/Conditionals_adv.h | 18 ++++++++++++++++++ Marlin/src/lcd/e3v2/common/encoder.cpp | 11 ++++++----- Marlin/src/lcd/marlinui.cpp | 21 +++++++++++++-------- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/Marlin/src/inc/Conditionals_adv.h b/Marlin/src/inc/Conditionals_adv.h index a5481bb985dd..4f74fa006718 100644 --- a/Marlin/src/inc/Conditionals_adv.h +++ b/Marlin/src/inc/Conditionals_adv.h @@ -869,6 +869,24 @@ #define HAS_ENCODER_ACTION 1 #endif +#if ENABLED(ENCODER_RATE_MULTIPLIER) + #ifndef ENCODER_5X_STEPS_PER_SEC + #define ENCODER_5X_STEPS_PER_SEC 0 + #endif + #ifndef ENCODER_10X_STEPS_PER_SEC + #define ENCODER_10X_STEPS_PER_SEC 0 + #endif + #ifndef ENCODER_100X_STEPS_PER_SEC + #define ENCODER_100X_STEPS_PER_SEC 0 + #endif + #if !((HAS_MARLINUI_MENU || HAS_DWIN_E3V2) && (ENCODER_5X_STEPS_PER_SEC || ENCODER_10X_STEPS_PER_SEC || ENCODER_100X_STEPS_PER_SEC)) + #undef ENCODER_RATE_MULTIPLIER + #undef ENCODER_5X_STEPS_PER_SEC + #undef ENCODER_10X_STEPS_PER_SEC + #undef ENCODER_100X_STEPS_PER_SEC + #endif +#endif + #if STATUS_MESSAGE_TIMEOUT_SEC > 0 #define HAS_STATUS_MESSAGE_TIMEOUT 1 #endif diff --git a/Marlin/src/lcd/e3v2/common/encoder.cpp b/Marlin/src/lcd/e3v2/common/encoder.cpp index c406cc47d922..5825fb0f7753 100644 --- a/Marlin/src/lcd/e3v2/common/encoder.cpp +++ b/Marlin/src/lcd/e3v2/common/encoder.cpp @@ -137,11 +137,12 @@ EncoderState encoderReceiveAnalyze() { // Note that the rate is always calculated between two passes through the // loop and that the abs of the temp_diff value is tracked. const float encoderStepRate = encoderMovementSteps / float(ms - encoderRate.lastEncoderTime) * 1000; - if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoder_multiplier = 100; - else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoder_multiplier = 10; - #if ENCODER_5X_STEPS_PER_SEC - else if (encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) encoder_multiplier = 5; - #endif + if (ENCODER_100X_STEPS_PER_SEC > 0 && encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) + encoder_multiplier = 100; + else if (ENCODER_10X_STEPS_PER_SEC > 0 && encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) + encoder_multiplier = 10; + else if (ENCODER_5X_STEPS_PER_SEC > 0 && encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) + encoder_multiplier = 5; } encoderRate.lastEncoderTime = ms; } diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 7c491b338e65..573dfe4f206e 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1039,18 +1039,23 @@ void MarlinUI::init() { const float encoderStepRate = ((float(abs_diff) / float(epps)) * 1000.0f) / float(ms - encoder_mult_prev_ms); encoder_mult_prev_ms = ms; - if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoder_multiplier = 100; - else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoder_multiplier = 10; + if (ENCODER_100X_STEPS_PER_SEC > 0 && encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) + encoder_multiplier = 100; + else if (ENCODER_10X_STEPS_PER_SEC > 0 && encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) + encoder_multiplier = 10; + else if (ENCODER_5X_STEPS_PER_SEC > 0 && encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) + encoder_multiplier = 5; // 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: ", encoder_multiplier); - 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(); + SERIAL_ECHO_MSG( + "Enc Step Rate: ", encoderStepRate, + " Mult: ", encoder_multiplier, + " 5X Steps: ", ENCODER_5X_STEPS_PER_SEC, + " 10X Steps: ", ENCODER_10X_STEPS_PER_SEC, + " 100X Steps: ", ENCODER_100X_STEPS_PER_SEC + ); #endif } From e67b68e3b312200a2038916370ae2655b8772e32 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 8 Feb 2024 22:15:52 -0600 Subject: [PATCH 17/37] Move epps fallback --- Marlin/src/inc/Conditionals_LCD.h | 29 +++++++++++++++----------- Marlin/src/lcd/e3v2/common/encoder.cpp | 7 ++----- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Marlin/src/inc/Conditionals_LCD.h b/Marlin/src/inc/Conditionals_LCD.h index af2ea772c3db..b6ac3db0f9e0 100644 --- a/Marlin/src/inc/Conditionals_LCD.h +++ b/Marlin/src/inc/Conditionals_LCD.h @@ -976,6 +976,17 @@ #define DETECT_I2C_LCD_DEVICE 1 #endif +/** + * Ender-3 V2 DWIN with Encoder + */ +#if ANY(DWIN_CREALITY_LCD, DWIN_LCD_PROUI) + #define HAS_DWIN_E3V2_BASIC 1 +#endif +#if ANY(HAS_DWIN_E3V2_BASIC, DWIN_CREALITY_LCD_JYERSUI) + #define HAS_DWIN_E3V2 1 + #define STD_ENCODER_PULSES_PER_STEP 4 +#endif + // Encoder behavior #ifndef STD_ENCODER_PULSES_PER_STEP #if ENABLED(TOUCH_SCREEN) @@ -997,10 +1008,12 @@ #define ENCODER_FEEDRATE_DEADZONE 6 #endif -// Shift register panels -// --------------------- -// 2 wire Non-latching LCD SR from: -// https://github.com/fmalpartida/New-LiquidCrystal/wiki/schematics#user-content-ShiftRegister_connection +/** + * Shift register panels + * --------------------- + * 2 wire Non-latching LCD SR from: + * https://github.com/fmalpartida/New-LiquidCrystal/wiki/schematics#user-content-ShiftRegister_connection + */ #if ENABLED(FF_INTERFACEBOARD) #define SR_LCD_3W_NL // Non latching 3 wire shift register #define IS_ULTIPANEL 1 @@ -1040,14 +1053,6 @@ #define EXTENSIBLE_UI #endif -// Aliases for LCD features -#if ANY(DWIN_CREALITY_LCD, DWIN_LCD_PROUI) - #define HAS_DWIN_E3V2_BASIC 1 -#endif -#if ANY(HAS_DWIN_E3V2_BASIC, DWIN_CREALITY_LCD_JYERSUI) - #define HAS_DWIN_E3V2 1 -#endif - // E3V2 extras #if HAS_DWIN_E3V2 || IS_DWIN_MARLINUI #define SERIAL_CATCHALL 0 diff --git a/Marlin/src/lcd/e3v2/common/encoder.cpp b/Marlin/src/lcd/e3v2/common/encoder.cpp index 5825fb0f7753..76aa840de17d 100644 --- a/Marlin/src/lcd/e3v2/common/encoder.cpp +++ b/Marlin/src/lcd/e3v2/common/encoder.cpp @@ -42,10 +42,6 @@ #include -#ifndef ENCODER_PULSES_PER_STEP - #define ENCODER_PULSES_PER_STEP 4 -#endif - EncoderRate encoderRate; // TODO: Replace with ui.quick_feedback @@ -98,6 +94,7 @@ EncoderState encoderReceiveAnalyze() { } else return ENCODER_DIFF_NO; } + if (newbutton != lastEncoderBits) { switch (newbutton) { case 0: @@ -129,7 +126,7 @@ EncoderState encoderReceiveAnalyze() { millis_t ms = millis(); int32_t encoder_multiplier = 1; - // if must encoder rati multiplier + // Encoder rate multiplier if (encoderRate.enabled) { const float abs_diff = ABS(temp_diff), encoderMovementSteps = abs_diff / (ENCODER_PULSES_PER_STEP); From fa68359d54393d3eae0cd8c6d00351b00ef97706 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 8 Feb 2024 22:30:38 -0600 Subject: [PATCH 18/37] some E3V2 encoder --- Marlin/src/lcd/e3v2/common/encoder.cpp | 43 +++++++++----------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/Marlin/src/lcd/e3v2/common/encoder.cpp b/Marlin/src/lcd/e3v2/common/encoder.cpp index 76aa840de17d..874da453c328 100644 --- a/Marlin/src/lcd/e3v2/common/encoder.cpp +++ b/Marlin/src/lcd/e3v2/common/encoder.cpp @@ -68,13 +68,9 @@ void encoderConfiguration() { // Analyze encoder value and return state EncoderState encoderReceiveAnalyze() { const millis_t now = millis(); - static uint8_t lastEncoderBits; - uint8_t newbutton = 0; static signed char temp_diff = 0; EncoderState temp_diffState = ENCODER_DIFF_NO; - if (BUTTON_PRESSED(EN1)) newbutton |= EN_A; - if (BUTTON_PRESSED(EN2)) newbutton |= EN_B; if (BUTTON_PRESSED(ENC)) { static millis_t next_click_update_ms; if (ELAPSED(now, next_click_update_ms)) { @@ -95,35 +91,26 @@ EncoderState encoderReceiveAnalyze() { else return ENCODER_DIFF_NO; } - if (newbutton != lastEncoderBits) { - switch (newbutton) { - case 0: - if (lastEncoderBits == 1) temp_diff++; - else if (lastEncoderBits == 2) temp_diff--; - break; - case 2: - if (lastEncoderBits == 0) temp_diff++; - else if (lastEncoderBits == 3) temp_diff--; - break; - case 3: - if (lastEncoderBits == 2) temp_diff++; - else if (lastEncoderBits == 1) temp_diff--; - break; - case 1: - if (lastEncoderBits == 3) temp_diff++; - else if (lastEncoderBits == 0) temp_diff--; - break; + static uint8_t old_enc; + uint8_t enc = 0; + if (BUTTON_PRESSED(EN1)) enc |= EN_A; + if (BUTTON_PRESSED(EN2)) enc |= EN_B; + if (enc != old_enc) { + switch ((old_enc << 2) | enc) { + case 2: case 11: case 13: case 4: ++temp_diff; break; + case 8: case 14: case 7: case 1: --temp_diff; break; } - lastEncoderBits = newbutton; + old_enc = enc; } if (ABS(temp_diff) >= ENCODER_PULSES_PER_STEP) { - if (temp_diff > 0) temp_diffState = TERN(REVERSE_ENCODER_DIRECTION, ENCODER_DIFF_CCW, ENCODER_DIFF_CW); - else temp_diffState = TERN(REVERSE_ENCODER_DIRECTION, ENCODER_DIFF_CW, ENCODER_DIFF_CCW); + temp_diffState = temp_diff > 0 + ? TERN(REVERSE_ENCODER_DIRECTION, ENCODER_DIFF_CCW, ENCODER_DIFF_CW) + : TERN(REVERSE_ENCODER_DIRECTION, ENCODER_DIFF_CW, ENCODER_DIFF_CCW); #if ENABLED(ENCODER_RATE_MULTIPLIER) - millis_t ms = millis(); + const millis_t ms = millis(); int32_t encoder_multiplier = 1; // Encoder rate multiplier @@ -150,9 +137,7 @@ EncoderState encoderReceiveAnalyze() { #endif - // encoderRate.encoderMoveValue += (temp_diff * encoder_multiplier) / (ENCODER_PULSES_PER_STEP); - encoderRate.encoderMoveValue = (temp_diff * encoder_multiplier) / (ENCODER_PULSES_PER_STEP); - if (encoderRate.encoderMoveValue < 0) encoderRate.encoderMoveValue = -encoderRate.encoderMoveValue; + encoderRate.encoderMoveValue = ABS((temp_diff * encoder_multiplier) / (ENCODER_PULSES_PER_STEP)); temp_diff = 0; } From b928eec902642667460a6529c84682da0463bdb8 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 8 Feb 2024 22:38:41 -0600 Subject: [PATCH 19/37] more E3V2 --- Marlin/src/lcd/e3v2/common/encoder.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Marlin/src/lcd/e3v2/common/encoder.cpp b/Marlin/src/lcd/e3v2/common/encoder.cpp index 874da453c328..b54bbeeebd00 100644 --- a/Marlin/src/lcd/e3v2/common/encoder.cpp +++ b/Marlin/src/lcd/e3v2/common/encoder.cpp @@ -68,7 +68,7 @@ void encoderConfiguration() { // Analyze encoder value and return state EncoderState encoderReceiveAnalyze() { const millis_t now = millis(); - static signed char temp_diff = 0; + static int8_t temp_diff = 0; EncoderState temp_diffState = ENCODER_DIFF_NO; if (BUTTON_PRESSED(ENC)) { @@ -92,9 +92,7 @@ EncoderState encoderReceiveAnalyze() { } static uint8_t old_enc; - uint8_t enc = 0; - if (BUTTON_PRESSED(EN1)) enc |= EN_A; - if (BUTTON_PRESSED(EN2)) enc |= EN_B; + const uint8_t enc = (BUTTON_PRESSED(EN1) ? EN_A : 0) | (BUTTON_PRESSED(EN2) ? EN_B : 0); if (enc != old_enc) { switch ((old_enc << 2) | enc) { case 2: case 11: case 13: case 4: ++temp_diff; break; @@ -103,7 +101,8 @@ EncoderState encoderReceiveAnalyze() { old_enc = enc; } - if (ABS(temp_diff) >= ENCODER_PULSES_PER_STEP) { + const int8_t abs_diff = ABS(temp_diff); + if (abs_diff >= ENCODER_PULSES_PER_STEP) { temp_diffState = temp_diff > 0 ? TERN(REVERSE_ENCODER_DIRECTION, ENCODER_DIFF_CCW, ENCODER_DIFF_CW) : TERN(REVERSE_ENCODER_DIRECTION, ENCODER_DIFF_CW, ENCODER_DIFF_CCW); @@ -115,8 +114,7 @@ EncoderState encoderReceiveAnalyze() { // Encoder rate multiplier if (encoderRate.enabled) { - const float abs_diff = ABS(temp_diff), - encoderMovementSteps = abs_diff / (ENCODER_PULSES_PER_STEP); + const float encoderMovementSteps = float(abs_diff) / (ENCODER_PULSES_PER_STEP); if (encoderRate.lastEncoderTime) { // Note that the rate is always calculated between two passes through the // loop and that the abs of the temp_diff value is tracked. @@ -137,14 +135,16 @@ EncoderState encoderReceiveAnalyze() { #endif - encoderRate.encoderMoveValue = ABS((temp_diff * encoder_multiplier) / (ENCODER_PULSES_PER_STEP)); + encoderRate.encoderMoveValue = abs_diff * encoder_multiplier / (ENCODER_PULSES_PER_STEP); temp_diff = 0; } + if (temp_diffState != ENCODER_DIFF_NO) { TERN_(HAS_BACKLIGHT_TIMEOUT, ui.refresh_backlight_timeout()); if (!ui.backlight) ui.refresh_brightness(); } + return temp_diffState; } From b6bd7e339a3d3344941bc280709c9da8242eedfd Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 8 Feb 2024 22:43:46 -0600 Subject: [PATCH 20/37] more more --- Marlin/src/lcd/e3v2/common/encoder.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Marlin/src/lcd/e3v2/common/encoder.cpp b/Marlin/src/lcd/e3v2/common/encoder.cpp index b54bbeeebd00..5a885eb0d68f 100644 --- a/Marlin/src/lcd/e3v2/common/encoder.cpp +++ b/Marlin/src/lcd/e3v2/common/encoder.cpp @@ -114,19 +114,16 @@ EncoderState encoderReceiveAnalyze() { // Encoder rate multiplier if (encoderRate.enabled) { - const float encoderMovementSteps = float(abs_diff) / (ENCODER_PULSES_PER_STEP); - if (encoderRate.lastEncoderTime) { - // Note that the rate is always calculated between two passes through the - // loop and that the abs of the temp_diff value is tracked. - const float encoderStepRate = encoderMovementSteps / float(ms - encoderRate.lastEncoderTime) * 1000; - if (ENCODER_100X_STEPS_PER_SEC > 0 && encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) - encoder_multiplier = 100; - else if (ENCODER_10X_STEPS_PER_SEC > 0 && encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) - encoder_multiplier = 10; - else if (ENCODER_5X_STEPS_PER_SEC > 0 && encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) - encoder_multiplier = 5; - } + // Note that the rate is always calculated between two passes through the + // loop and that the abs of the temp_diff value is tracked. + const float encoderStepRate = ((float(abs_diff) / float(ENCODER_PULSES_PER_STEP)) * 1000.0f) / float(ms - encoderRate.lastEncoderTime); encoderRate.lastEncoderTime = ms; + if (ENCODER_100X_STEPS_PER_SEC > 0 && encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) + encoder_multiplier = 100; + else if (ENCODER_10X_STEPS_PER_SEC > 0 && encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) + encoder_multiplier = 10; + else if (ENCODER_5X_STEPS_PER_SEC > 0 && encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) + encoder_multiplier = 5; } #else From a78a4b14ea0508272a98f212656c3e55923d9794 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 9 Feb 2024 12:48:21 -0600 Subject: [PATCH 21/37] 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 573dfe4f206e..f536e9fd8834 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1065,7 +1065,7 @@ void MarlinUI::init() { #endif // ENCODER_RATE_MULTIPLIER - int8_t fullSteps = encoderDiff / epps; + const int8_t fullSteps = encoderDiff / epps; if (fullSteps != 0) { encoderDiff -= fullSteps * epps; if (can_encode() && !lcd_clicked) From 7657a2831d6bea61c52e393849edb6069bc6c855 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 9 Feb 2024 20:55:46 -0600 Subject: [PATCH 22/37] index encoder bits --- Marlin/src/lcd/marlinui.cpp | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index f536e9fd8834..d926021ba2be 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1399,32 +1399,26 @@ void MarlinUI::init() { if (button_diff & EN_B) en_B_bounce_ms = now + (ENCODER_DEBOUNCE_MS); else if (ELAPSED(now, en_B_bounce_ms)) SET_BIT_TO(enc, 1, buttons & EN_B); - static uint8_t old_enc; - if (enc != old_enc) { - int8_t dir = 0; - switch ((old_enc << 2) | enc) { - case 2: case 11: case 13: case 4: dir = encoderDirection; break; - case 8: case 14: case 7: case 1: dir = -encoderDirection; break; - } - old_enc = enc; + const uint8_t pos = en_A ^ en_B | (en_B << 1); // 0:00 1:01 2:11 3:10 + static uint8_t old_pos = pos; + if (pos != old_pos) { + uint8_t delta = (pos - old_pos + 4 + 1) % 4 - 1; + old_pos = pos; static int8_t last_dir; - if (dir) { - encoderDiff += dir; - last_dir = dir; - } - else { - // The encoder is likely 2 pulses away from last update, assume same direction. - // Keeps encoders with 4 pulses-per-step in better sync, but for fast initial spin the dir may be wrong. - encoderDiff += last_dir * 2; - } + if (delta == 2) delta = last_dir * 2; + else last_dir = delta; + + encoderDiff += delta * encoderDirection; + #if ALL(HAS_MARLINUI_MENU, AUTO_BED_LEVELING_UBL) external_encoder(); #endif } #endif // HAS_ENCODER_WHEEL - } + + } // update_buttons #endif // HAS_ENCODER_ACTION From f625a8f8c04cbf2b23acc939a17bd34bd7f4edf7 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 9 Feb 2024 23:33:23 -0600 Subject: [PATCH 23/37] en_A, B --- 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 d926021ba2be..025f5bcedb6b 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1393,14 +1393,14 @@ void MarlinUI::init() { static millis_t en_A_bounce_ms; if (button_diff & EN_A) en_A_bounce_ms = now + (ENCODER_DEBOUNCE_MS); - else if (ELAPSED(now, en_A_bounce_ms)) SET_BIT_TO(enc, 0, buttons & EN_A); + else if (ELAPSED(now, en_A_bounce_ms)) SET_BIT_TO(enc, 1, buttons & EN_A); static millis_t en_B_bounce_ms; if (button_diff & EN_B) en_B_bounce_ms = now + (ENCODER_DEBOUNCE_MS); - else if (ELAPSED(now, en_B_bounce_ms)) SET_BIT_TO(enc, 1, buttons & EN_B); + else if (ELAPSED(now, en_B_bounce_ms)) SET_BIT_TO(enc, 0, buttons & EN_B); - const uint8_t pos = en_A ^ en_B | (en_B << 1); // 0:00 1:01 2:11 3:10 - static uint8_t old_pos = pos; + static uint8_t old_pos; + const uint8_t pos = (enc & 1) ^ ((enc & 2) >> 1) | (enc & 2); // 0:00 1:01 2:11 3:10 if (pos != old_pos) { uint8_t delta = (pos - old_pos + 4 + 1) % 4 - 1; old_pos = pos; From 96f31fe5692ce4817429b0e7b4bb841b4b91f095 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Feb 2024 00:07:58 -0600 Subject: [PATCH 24/37] struct --- 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 025f5bcedb6b..e0273781bcfb 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1389,18 +1389,18 @@ void MarlinUI::init() { const uint8_t button_diff = buttons ^ old_buttons; old_buttons = buttons; - static uint8_t enc; + static struct { bool a:1, b:1; } enc; static millis_t en_A_bounce_ms; if (button_diff & EN_A) en_A_bounce_ms = now + (ENCODER_DEBOUNCE_MS); - else if (ELAPSED(now, en_A_bounce_ms)) SET_BIT_TO(enc, 1, buttons & EN_A); + else if (ELAPSED(now, en_A_bounce_ms)) enc.a = buttons & EN_A; static millis_t en_B_bounce_ms; if (button_diff & EN_B) en_B_bounce_ms = now + (ENCODER_DEBOUNCE_MS); - else if (ELAPSED(now, en_B_bounce_ms)) SET_BIT_TO(enc, 0, buttons & EN_B); + else if (ELAPSED(now, en_B_bounce_ms)) enc.b = buttons & EN_B; static uint8_t old_pos; - const uint8_t pos = (enc & 1) ^ ((enc & 2) >> 1) | (enc & 2); // 0:00 1:01 2:11 3:10 + const uint8_t pos = (enc.a ^ enc.b) | (enc.a << 1); // 0:00 1:10 2:11 3:01 if (pos != old_pos) { uint8_t delta = (pos - old_pos + 4 + 1) % 4 - 1; old_pos = pos; From 04c8a8649a4cf50184143be7be5744fc6fb90758 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Feb 2024 00:24:07 -0600 Subject: [PATCH 25/37] lvgl encoder --- Marlin/src/lcd/e3v2/common/encoder.cpp | 2 +- .../extui/mks_ui/tft_lvgl_configuration.cpp | 95 +++++++------------ 2 files changed, 33 insertions(+), 64 deletions(-) diff --git a/Marlin/src/lcd/e3v2/common/encoder.cpp b/Marlin/src/lcd/e3v2/common/encoder.cpp index 5a885eb0d68f..ad534869e009 100644 --- a/Marlin/src/lcd/e3v2/common/encoder.cpp +++ b/Marlin/src/lcd/e3v2/common/encoder.cpp @@ -92,7 +92,7 @@ EncoderState encoderReceiveAnalyze() { } static uint8_t old_enc; - const uint8_t enc = (BUTTON_PRESSED(EN1) ? EN_A : 0) | (BUTTON_PRESSED(EN2) ? EN_B : 0); + const uint8_t enc = (BUTTON_PRESSED(EN1) ? 1 : 0) | (BUTTON_PRESSED(EN2) ? 2 : 0); if (enc != old_enc) { switch ((old_enc << 2) | enc) { case 2: case 11: case 13: case 4: ++temp_diff; break; diff --git a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp index a10d0119f0e9..0873861199d7 100644 --- a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp +++ b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp @@ -475,72 +475,41 @@ void lv_encoder_pin_init() { #endif } -#if 1 // HAS_ENCODER_ACTION - - void lv_update_encoder() { - static uint32_t encoder_time1; - uint32_t tmpTime, diffTime = 0; - tmpTime = millis(); - diffTime = getTickDiff(tmpTime, encoder_time1); - if (diffTime > 50) { - - #if HAS_ENCODER_WHEEL - - #if ANY_BUTTON(EN1, EN2, ENC, BACK) - - uint8_t newbutton = 0; - if (BUTTON_PRESSED(EN1)) newbutton |= EN_A; - if (BUTTON_PRESSED(EN2)) newbutton |= EN_B; - if (BUTTON_PRESSED(ENC)) newbutton |= EN_C; - if (BUTTON_PRESSED(BACK)) newbutton |= EN_D; - - #else - - constexpr uint8_t newbutton = 0; - - #endif - - static uint8_t buttons = 0; - buttons = newbutton; - static uint8_t lastEncoderBits; - - #define encrot0 0 - #define encrot1 1 - #define encrot2 2 - - uint8_t enc = 0; - if (buttons & EN_A) enc |= B01; - if (buttons & EN_B) enc |= B10; - if (enc != lastEncoderBits) { - switch (enc) { - case encrot1: - if (lastEncoderBits == encrot0) { - enc_diff--; - encoder_time1 = tmpTime; - } - break; - case encrot2: - if (lastEncoderBits == encrot0) { - enc_diff++; - encoder_time1 = tmpTime; - } - break; - } - lastEncoderBits = enc; +void lv_update_encoder() { + + #if ANY_BUTTON(EN1, EN2, ENC, BACK) + + static millis_t last_encoder_ms; + const millis_t now = millis(), diffTime = getTickDiff(now, last_encoder_ms); + if (diffTime <= 50) return; + + uint8_t buttons = 0; + if (BUTTON_PRESSED(EN1)) buttons |= EN_A; + if (BUTTON_PRESSED(EN2)) buttons |= EN_B; + if (BUTTON_PRESSED(ENC)) buttons |= EN_C; + if (BUTTON_PRESSED(BACK)) buttons |= EN_D; + + static uint8_t old_enc; + const uint8_t enc = (buttons & EN_A ? B01 : 0) | (buttons & EN_B ? B10 : 0); + if (enc != old_enc) { + if (old_enc == 0) { + switch (enc) { + case 1: --enc_diff; last_encoder_ms = now; break; + case 2: ++enc_diff; last_encoder_ms = now; break; } - static uint8_t last_button_state = LV_INDEV_STATE_REL; - const uint8_t enc_c = (buttons & EN_C) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; - if (enc_c != last_button_state) { - state = enc_c ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; - last_button_state = enc_c; - } - - #endif // HAS_ENCODER_WHEEL + } + old_enc = enc; + } - } // encoder_time1 - } + static uint8_t old_buttons = LV_INDEV_STATE_REL; + const uint8_t enc_c = (buttons & EN_C) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; + if (enc_c != old_buttons) { + state = enc_c ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; + old_buttons = enc_c; + } -#endif // HAS_ENCODER_ACTION + #endif // ANY_BUTTON +} #ifdef __PLAT_NATIVE_SIM__ #include From 71a945969a30b0b56eb20c4f2afd38fbd63ce5fa Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Feb 2024 12:44:59 -0600 Subject: [PATCH 26/37] encoder style --- Marlin/src/lcd/e3v2/common/encoder.cpp | 6 +++--- Marlin/src/lcd/marlinui.cpp | 8 ++------ 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/Marlin/src/lcd/e3v2/common/encoder.cpp b/Marlin/src/lcd/e3v2/common/encoder.cpp index ad534869e009..eccef016bdf6 100644 --- a/Marlin/src/lcd/e3v2/common/encoder.cpp +++ b/Marlin/src/lcd/e3v2/common/encoder.cpp @@ -92,11 +92,11 @@ EncoderState encoderReceiveAnalyze() { } static uint8_t old_enc; - const uint8_t enc = (BUTTON_PRESSED(EN1) ? 1 : 0) | (BUTTON_PRESSED(EN2) ? 2 : 0); + const uint8_t enc = (BUTTON_PRESSED(EN1) ? B01 : 0) | (BUTTON_PRESSED(EN2) ? B10 : 0); if (enc != old_enc) { switch ((old_enc << 2) | enc) { - case 2: case 11: case 13: case 4: ++temp_diff; break; - case 8: case 14: case 7: case 1: --temp_diff; break; + case B0010: case B1011: case B1101: case B0100: ++temp_diff; break; // no change: 0000, 0101, 1010, 1111 + case B1000: case B1110: case B0111: case B0001: --temp_diff; break; // multiple: 0011, 0110, 1001, 1100 } old_enc = enc; } diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index e0273781bcfb..43fe89e76c81 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1028,9 +1028,9 @@ void MarlinUI::init() { const bool encoderPastThreshold = (abs_diff >= epps); if (encoderPastThreshold && TERN1(IS_TFTGLCD_PANEL, !external_control)) { - #if ALL(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLIER) + int32_t encoder_multiplier = 1; - int32_t encoder_multiplier = 1; + #if ALL(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLIER) if (encoder_multiplier_enabled) { // Note that the rate is always calculated between two passes through the @@ -1059,10 +1059,6 @@ void MarlinUI::init() { #endif } - #else - - constexpr int32_t encoder_multiplier = 1; - #endif // ENCODER_RATE_MULTIPLIER const int8_t fullSteps = encoderDiff / epps; From 3e4cbedcecf6b461dd23771bea4103175d0c3d96 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Feb 2024 12:59:04 -0600 Subject: [PATCH 27/37] simplify lv --- Marlin/src/lcd/e3v2/common/encoder.cpp | 9 +++---- .../extui/mks_ui/tft_lvgl_configuration.cpp | 24 ++++++++----------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/Marlin/src/lcd/e3v2/common/encoder.cpp b/Marlin/src/lcd/e3v2/common/encoder.cpp index eccef016bdf6..27fe471fb23f 100644 --- a/Marlin/src/lcd/e3v2/common/encoder.cpp +++ b/Marlin/src/lcd/e3v2/common/encoder.cpp @@ -68,7 +68,7 @@ void encoderConfiguration() { // Analyze encoder value and return state EncoderState encoderReceiveAnalyze() { const millis_t now = millis(); - static int8_t temp_diff = 0; + static int8_t temp_diff = 0; // Cleared on each full step, as configured EncoderState temp_diffState = ENCODER_DIFF_NO; if (BUTTON_PRESSED(ENC)) { @@ -107,10 +107,11 @@ EncoderState encoderReceiveAnalyze() { ? TERN(REVERSE_ENCODER_DIRECTION, ENCODER_DIFF_CCW, ENCODER_DIFF_CW) : TERN(REVERSE_ENCODER_DIRECTION, ENCODER_DIFF_CW, ENCODER_DIFF_CCW); + int32_t encoder_multiplier = 1; + #if ENABLED(ENCODER_RATE_MULTIPLIER) const millis_t ms = millis(); - int32_t encoder_multiplier = 1; // Encoder rate multiplier if (encoderRate.enabled) { @@ -126,10 +127,6 @@ EncoderState encoderReceiveAnalyze() { encoder_multiplier = 5; } - #else - - constexpr int32_t encoder_multiplier = 1; - #endif encoderRate.encoderMoveValue = abs_diff * encoder_multiplier / (ENCODER_PULSES_PER_STEP); diff --git a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp index 0873861199d7..be924305fd73 100644 --- a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp +++ b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp @@ -331,12 +331,12 @@ bool my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) { } int16_t enc_diff = 0; -lv_indev_state_t state = LV_INDEV_STATE_REL; +lv_indev_state_t lv_state = LV_INDEV_STATE_REL; // ENC button is pressed or released bool my_mousewheel_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data) { - (void) indev_drv; // Unused + UNUSED(indev_drv); - data->state = state; + data->state = lv_state; data->enc_diff = enc_diff; enc_diff = 0; @@ -483,14 +483,10 @@ void lv_update_encoder() { const millis_t now = millis(), diffTime = getTickDiff(now, last_encoder_ms); if (diffTime <= 50) return; - uint8_t buttons = 0; - if (BUTTON_PRESSED(EN1)) buttons |= EN_A; - if (BUTTON_PRESSED(EN2)) buttons |= EN_B; - if (BUTTON_PRESSED(ENC)) buttons |= EN_C; - if (BUTTON_PRESSED(BACK)) buttons |= EN_D; + //if (BUTTON_PRESSED(BACK)) {} static uint8_t old_enc; - const uint8_t enc = (buttons & EN_A ? B01 : 0) | (buttons & EN_B ? B10 : 0); + const uint8_t enc = (BUTTON_PRESSED(EN1) ? B01 : 0) | (BUTTON_PRESSED(EN2) ? B10 : 0); if (enc != old_enc) { if (old_enc == 0) { switch (enc) { @@ -501,11 +497,11 @@ void lv_update_encoder() { old_enc = enc; } - static uint8_t old_buttons = LV_INDEV_STATE_REL; - const uint8_t enc_c = (buttons & EN_C) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; - if (enc_c != old_buttons) { - state = enc_c ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; - old_buttons = enc_c; + static uint8_t old_button_enc = LV_INDEV_STATE_REL; + const uint8_t enc_c = BUTTON_PRESSED(ENC) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; + if (enc_c != old_button_enc) { + lv_state = enc_c ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; + old_button_enc = enc_c; } #endif // ANY_BUTTON From 8a5c1beb552708b2242a4f5e270c65c72298c242 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Feb 2024 13:18:57 -0600 Subject: [PATCH 28/37] clarify --- Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp index be924305fd73..5817eee05ede 100644 --- a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp +++ b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp @@ -331,12 +331,12 @@ bool my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) { } int16_t enc_diff = 0; -lv_indev_state_t lv_state = LV_INDEV_STATE_REL; // ENC button is pressed or released +lv_indev_state_t indev_enc_state = LV_INDEV_STATE_REL; // ENC button is pressed or released bool my_mousewheel_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data) { UNUSED(indev_drv); - data->state = lv_state; + data->state = indev_enc_state; data->enc_diff = enc_diff; enc_diff = 0; @@ -500,7 +500,7 @@ void lv_update_encoder() { static uint8_t old_button_enc = LV_INDEV_STATE_REL; const uint8_t enc_c = BUTTON_PRESSED(ENC) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; if (enc_c != old_button_enc) { - lv_state = enc_c ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; + indev_enc_state = enc_c ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; old_button_enc = enc_c; } From bd0a74e94d03720ffc1d0c31c3736cd42ffb0c26 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Feb 2024 18:35:48 -0600 Subject: [PATCH 29/37] share 'get_encoder_delta' --- Marlin/src/lcd/e3v2/common/encoder.cpp | 10 +--- Marlin/src/lcd/marlinui.cpp | 75 +++++++++++++++++--------- Marlin/src/lcd/marlinui.h | 4 ++ 3 files changed, 55 insertions(+), 34 deletions(-) diff --git a/Marlin/src/lcd/e3v2/common/encoder.cpp b/Marlin/src/lcd/e3v2/common/encoder.cpp index 27fe471fb23f..54fcee848274 100644 --- a/Marlin/src/lcd/e3v2/common/encoder.cpp +++ b/Marlin/src/lcd/e3v2/common/encoder.cpp @@ -91,15 +91,7 @@ EncoderState encoderReceiveAnalyze() { else return ENCODER_DIFF_NO; } - static uint8_t old_enc; - const uint8_t enc = (BUTTON_PRESSED(EN1) ? B01 : 0) | (BUTTON_PRESSED(EN2) ? B10 : 0); - if (enc != old_enc) { - switch ((old_enc << 2) | enc) { - case B0010: case B1011: case B1101: case B0100: ++temp_diff; break; // no change: 0000, 0101, 1010, 1111 - case B1000: case B1110: case B0111: case B0001: --temp_diff; break; // multiple: 0011, 0110, 1001, 1100 - } - old_enc = enc; - } + temp_diff += MarlinUI::get_encoder_delta(); const int8_t abs_diff = ABS(temp_diff); if (abs_diff >= ENCODER_PULSES_PER_STEP) { diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 43fe89e76c81..261577efb929 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1380,33 +1380,10 @@ void MarlinUI::init() { } // next_button_update_ms #if HAS_ENCODER_WHEEL - #define ENCODER_DEBOUNCE_MS 2 - static uint8_t old_buttons; - const uint8_t button_diff = buttons ^ old_buttons; - old_buttons = buttons; - - static struct { bool a:1, b:1; } enc; - - static millis_t en_A_bounce_ms; - if (button_diff & EN_A) en_A_bounce_ms = now + (ENCODER_DEBOUNCE_MS); - else if (ELAPSED(now, en_A_bounce_ms)) enc.a = buttons & EN_A; - - static millis_t en_B_bounce_ms; - if (button_diff & EN_B) en_B_bounce_ms = now + (ENCODER_DEBOUNCE_MS); - else if (ELAPSED(now, en_B_bounce_ms)) enc.b = buttons & EN_B; - - static uint8_t old_pos; - const uint8_t pos = (enc.a ^ enc.b) | (enc.a << 1); // 0:00 1:10 2:11 3:01 - if (pos != old_pos) { - uint8_t delta = (pos - old_pos + 4 + 1) % 4 - 1; - old_pos = pos; - - static int8_t last_dir; - if (delta == 2) delta = last_dir * 2; - else last_dir = delta; + const int8_t delta = get_encoder_delta(now); + if (delta) { encoderDiff += delta * encoderDirection; - #if ALL(HAS_MARLINUI_MENU, AUTO_BED_LEVELING_UBL) external_encoder(); #endif @@ -1420,6 +1397,54 @@ void MarlinUI::init() { #endif // HAS_WIRED_LCD +#if (HAS_WIRED_LCD && HAS_ENCODER_ACTION && HAS_ENCODER_WHEEL) || HAS_DWIN_E3V2 + + #define ENCODER_DEBOUNCE_MS 2 + + int8_t MarlinUI::get_encoder_delta(const millis_t now/*=millis()*/) { + + typedef struct { bool a:1, b:1; } enc_t; + + #if ENCODER_DEBOUNCE_MS + + static enc_t enc; + + static enc_t old_live; + const enc_t live_enc = { buttons & EN_A, buttons & EN_B }; + + static millis_t en_A_bounce_ms; + if (old_live.a != live_enc.a) en_A_bounce_ms = now + (ENCODER_DEBOUNCE_MS); + else if (ELAPSED(now, en_A_bounce_ms)) enc.a = live_enc.a; + + static millis_t en_B_bounce_ms; + if (old_live.b != live_enc.b) en_B_bounce_ms = now + (ENCODER_DEBOUNCE_MS); + else if (ELAPSED(now, en_B_bounce_ms)) enc.b = live_enc.b; + + old_live = live_enc; + + #else + + enc_t enc = { buttons & EN_A, buttons & EN_B }; + + #endif + + static uint8_t old_pos; + const uint8_t pos = (enc.a ^ enc.b) | (enc.a << 1); // 0:00 1:10 2:11 3:01 + int8_t delta = 0; + if (pos != old_pos) { + delta = (pos - old_pos + 4 + 1) % 4 - 1; + old_pos = pos; + + static int8_t last_dir; + if (delta == 2) delta = last_dir * 2; + else last_dir = delta; + } + return delta; + + } + +#endif + void MarlinUI::completion_feedback(const bool good/*=true*/) { wake_display(); // Wake the screen for all audio feedback #if HAS_SOUND diff --git a/Marlin/src/lcd/marlinui.h b/Marlin/src/lcd/marlinui.h index ff87852f119b..f3516dd00dfc 100644 --- a/Marlin/src/lcd/marlinui.h +++ b/Marlin/src/lcd/marlinui.h @@ -250,6 +250,10 @@ class MarlinUI { } #endif + #if (HAS_WIRED_LCD && HAS_ENCODER_ACTION && HAS_ENCODER_WHEEL) || HAS_DWIN_E3V2 + static int8_t get_encoder_delta(const millis_t now=millis()); + #endif + #if HAS_MEDIA #define MEDIA_MENU_GATEWAY TERN(PASSWORD_ON_SD_PRINT_MENU, password.media_gatekeeper, menu_media) static void media_changed(const uint8_t old_stat, const uint8_t stat); From 3ff61dc9dc1cf95b70f0d9a661446cde34064c32 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Feb 2024 19:51:53 -0600 Subject: [PATCH 30/37] no pause EN1/2 --- Marlin/src/lcd/e3v2/common/encoder.cpp | 3 +- Marlin/src/lcd/marlinui.cpp | 163 ++++++++++++------------- Marlin/src/lcd/marlinui.h | 3 +- 3 files changed, 80 insertions(+), 89 deletions(-) diff --git a/Marlin/src/lcd/e3v2/common/encoder.cpp b/Marlin/src/lcd/e3v2/common/encoder.cpp index 54fcee848274..0dfeeabf7bf7 100644 --- a/Marlin/src/lcd/e3v2/common/encoder.cpp +++ b/Marlin/src/lcd/e3v2/common/encoder.cpp @@ -91,7 +91,8 @@ EncoderState encoderReceiveAnalyze() { else return ENCODER_DIFF_NO; } - temp_diff += MarlinUI::get_encoder_delta(); + const MarlinUI::enc_t enc = { BUTTON_PRESSED(EN1), BUTTON_PRESSED(EN2) }; + temp_diff += ui.get_encoder_delta(enc); const int8_t abs_diff = ABS(temp_diff); if (abs_diff >= ENCODER_PULSES_PER_STEP) { diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 261577efb929..4b1f00cf1a9f 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1290,106 +1290,98 @@ void MarlinUI::init() { */ void MarlinUI::update_buttons() { const millis_t now = millis(); - if (ELAPSED(now, next_button_update_ms)) { - #if HAS_DIGITAL_BUTTONS - - #if ANY_BUTTON(EN1, EN2, ENC, BACK) - - uint8_t newbutton = 0; - if (BUTTON_PRESSED(EN1)) newbutton |= EN_A; - if (BUTTON_PRESSED(EN2)) newbutton |= EN_B; - if (can_encode() && BUTTON_PRESSED(ENC)) newbutton |= EN_C; - if (BUTTON_PRESSED(BACK)) newbutton |= EN_D; - - #else - - constexpr uint8_t newbutton = 0; + #if HAS_ENCODER_WHEEL + const enc_t enc = { BUTTON_PRESSED(EN1), BUTTON_PRESSED(EN2) }; + const int8_t delta = get_encoder_delta(enc, now); + if (delta) { + encoderDiff += delta * encoderDirection; + #if ALL(HAS_MARLINUI_MENU, AUTO_BED_LEVELING_UBL) + external_encoder(); #endif + } - // - // Directional buttons - // - #if ANY_BUTTON(UP, DOWN, LEFT, RIGHT) + #endif // HAS_ENCODER_WHEEL - const int8_t pulses = epps * encoderDirection; + if (PENDING(now, next_button_update_ms)) return; - if (BUTTON_PRESSED(UP)) { - encoderDiff = (ENCODER_STEPS_PER_MENU_ITEM) * pulses; - next_button_update_ms = now + 300; - } - else if (BUTTON_PRESSED(DOWN)) { - encoderDiff = -(ENCODER_STEPS_PER_MENU_ITEM) * pulses; - next_button_update_ms = now + 300; - } - else if (BUTTON_PRESSED(LEFT)) { - encoderDiff = -pulses; - next_button_update_ms = now + 300; - } - else if (BUTTON_PRESSED(RIGHT)) { - encoderDiff = pulses; - next_button_update_ms = now + 300; - } + #if HAS_DIGITAL_BUTTONS - #endif // UP || DOWN || LEFT || RIGHT + uint8_t newbuttons = 0; + #if ANY_BUTTON(ENC, BACK) + if (can_encode() && BUTTON_PRESSED(ENC)) newbuttons |= EN_C; + if (BUTTON_PRESSED(BACK)) newbuttons |= EN_D; + #endif - buttons = (newbutton | TERN0(HAS_SLOW_BUTTONS, slow_buttons) - #if ALL(HAS_TOUCH_BUTTONS, HAS_ENCODER_ACTION) - | (touch_buttons & TERN(HAS_ENCODER_WHEEL, ~(EN_A | EN_B), 0xFF)) - #endif - ); + // + // Directional buttons + // + #if ANY_BUTTON(UP, DOWN, LEFT, RIGHT) - #elif HAS_ADC_BUTTONS + const int8_t pulses = epps * encoderDirection; - buttons = 0; + if (BUTTON_PRESSED(UP)) { + encoderDiff = (ENCODER_STEPS_PER_MENU_ITEM) * pulses; + next_button_update_ms = now + 300; + } + else if (BUTTON_PRESSED(DOWN)) { + encoderDiff = -(ENCODER_STEPS_PER_MENU_ITEM) * pulses; + next_button_update_ms = now + 300; + } + else if (BUTTON_PRESSED(LEFT)) { + encoderDiff = -pulses; + next_button_update_ms = now + 300; + } + else if (BUTTON_PRESSED(RIGHT)) { + encoderDiff = pulses; + next_button_update_ms = now + 300; + } - #endif + #endif // UP || DOWN || LEFT || RIGHT - #if HAS_ADC_BUTTONS - if (keypad_buttons == 0) { - const uint8_t b = get_ADC_keyValue(); - if (WITHIN(b, 1, 8)) keypad_buttons = _BV(b - 1); - } - #endif + buttons = (newbuttons | TERN0(HAS_SLOW_BUTTONS, slow_buttons) + #if ALL(HAS_TOUCH_BUTTONS, HAS_ENCODER_ACTION) + | (touch_buttons & TERN(HAS_ENCODER_WHEEL, ~(EN_A | EN_B), 0xFF)) + #endif + ); - #if HAS_SHIFT_ENCODER - /** - * Set up Rotary Encoder bit values (for two pin encoders to indicate movement). - * These values are independent of which pins are used for EN_A / EN_B indications. - * The rotary encoder part is also independent of the LCD chipset. - */ - uint8_t val = 0; - WRITE(SHIFT_LD_PIN, LOW); - WRITE(SHIFT_LD_PIN, HIGH); - for (uint8_t i = 0; i < 8; ++i) { - val >>= 1; - if (READ(SHIFT_OUT_PIN)) SBI(val, 7); - WRITE(SHIFT_CLK_PIN, HIGH); - WRITE(SHIFT_CLK_PIN, LOW); - } - TERN(REPRAPWORLD_KEYPAD, keypad_buttons, buttons) = ~val; - #endif + #elif HAS_ADC_BUTTONS - #if IS_TFTGLCD_PANEL - next_button_update_ms = now + (LCD_UPDATE_INTERVAL / 2); - buttons = slow_buttons; - TERN_(AUTO_BED_LEVELING_UBL, external_encoder()); - #endif + buttons = 0; - } // next_button_update_ms + #endif - #if HAS_ENCODER_WHEEL + #if HAS_ADC_BUTTONS + if (keypad_buttons == 0) { + const uint8_t b = get_ADC_keyValue(); + if (WITHIN(b, 1, 8)) keypad_buttons = _BV(b - 1); + } + #endif - const int8_t delta = get_encoder_delta(now); - if (delta) { - encoderDiff += delta * encoderDirection; - #if ALL(HAS_MARLINUI_MENU, AUTO_BED_LEVELING_UBL) - external_encoder(); - #endif + #if HAS_SHIFT_ENCODER + /** + * Set up Rotary Encoder bit values (for two pin encoders to indicate movement). + * These values are independent of which pins are used for EN_A / EN_B indications. + * The rotary encoder part is also independent of the LCD chipset. + */ + uint8_t val = 0; + WRITE(SHIFT_LD_PIN, LOW); + WRITE(SHIFT_LD_PIN, HIGH); + for (uint8_t i = 0; i < 8; ++i) { + val >>= 1; + if (READ(SHIFT_OUT_PIN)) SBI(val, 7); + WRITE(SHIFT_CLK_PIN, HIGH); + WRITE(SHIFT_CLK_PIN, LOW); } + TERN(REPRAPWORLD_KEYPAD, keypad_buttons, buttons) = ~val; + #endif - #endif // HAS_ENCODER_WHEEL + #if IS_TFTGLCD_PANEL + next_button_update_ms = now + (LCD_UPDATE_INTERVAL / 2); + buttons = slow_buttons; + TERN_(AUTO_BED_LEVELING_UBL, external_encoder()); + #endif } // update_buttons @@ -1401,16 +1393,13 @@ void MarlinUI::init() { #define ENCODER_DEBOUNCE_MS 2 - int8_t MarlinUI::get_encoder_delta(const millis_t now/*=millis()*/) { - - typedef struct { bool a:1, b:1; } enc_t; + int8_t MarlinUI::get_encoder_delta(const enc_t &live_enc, const millis_t &now/*=millis()*/) { #if ENCODER_DEBOUNCE_MS static enc_t enc; static enc_t old_live; - const enc_t live_enc = { buttons & EN_A, buttons & EN_B }; static millis_t en_A_bounce_ms; if (old_live.a != live_enc.a) en_A_bounce_ms = now + (ENCODER_DEBOUNCE_MS); @@ -1424,7 +1413,7 @@ void MarlinUI::init() { #else - enc_t enc = { buttons & EN_A, buttons & EN_B }; + enc_t &enc = live_enc; #endif @@ -1441,7 +1430,7 @@ void MarlinUI::init() { } return delta; - } + } // get_encoder_delta #endif diff --git a/Marlin/src/lcd/marlinui.h b/Marlin/src/lcd/marlinui.h index f3516dd00dfc..c2b38b3a7ebf 100644 --- a/Marlin/src/lcd/marlinui.h +++ b/Marlin/src/lcd/marlinui.h @@ -251,7 +251,8 @@ class MarlinUI { #endif #if (HAS_WIRED_LCD && HAS_ENCODER_ACTION && HAS_ENCODER_WHEEL) || HAS_DWIN_E3V2 - static int8_t get_encoder_delta(const millis_t now=millis()); + typedef struct { bool a:1, b:1; } enc_t; + static int8_t get_encoder_delta(const enc_t &live_enc, const millis_t &now=millis()); #endif #if HAS_MEDIA From e329cb6646ece54dc0e5a2cd3f983032e095e38a Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Feb 2024 20:48:24 -0600 Subject: [PATCH 31/37] use buttons.h --- Marlin/src/lcd/e3v2/common/encoder.cpp | 3 +-- Marlin/src/lcd/marlinui.cpp | 10 ++++++---- Marlin/src/lcd/marlinui.h | 3 +-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Marlin/src/lcd/e3v2/common/encoder.cpp b/Marlin/src/lcd/e3v2/common/encoder.cpp index 0dfeeabf7bf7..4b3e1889aa62 100644 --- a/Marlin/src/lcd/e3v2/common/encoder.cpp +++ b/Marlin/src/lcd/e3v2/common/encoder.cpp @@ -91,8 +91,7 @@ EncoderState encoderReceiveAnalyze() { else return ENCODER_DIFF_NO; } - const MarlinUI::enc_t enc = { BUTTON_PRESSED(EN1), BUTTON_PRESSED(EN2) }; - temp_diff += ui.get_encoder_delta(enc); + temp_diff += ui.get_encoder_delta(); const int8_t abs_diff = ABS(temp_diff); if (abs_diff >= ENCODER_PULSES_PER_STEP) { diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 4b1f00cf1a9f..c4b4903d8885 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1293,8 +1293,7 @@ void MarlinUI::init() { #if HAS_ENCODER_WHEEL - const enc_t enc = { BUTTON_PRESSED(EN1), BUTTON_PRESSED(EN2) }; - const int8_t delta = get_encoder_delta(enc, now); + const int8_t delta = get_encoder_delta(now); if (delta) { encoderDiff += delta * encoderDirection; #if ALL(HAS_MARLINUI_MENU, AUTO_BED_LEVELING_UBL) @@ -1393,12 +1392,15 @@ void MarlinUI::init() { #define ENCODER_DEBOUNCE_MS 2 - int8_t MarlinUI::get_encoder_delta(const enc_t &live_enc, const millis_t &now/*=millis()*/) { + int8_t MarlinUI::get_encoder_delta(const millis_t &now/*=millis()*/) { + + typedef struct { bool a:1, b:1; } enc_t; + + const enc_t live_enc = { BUTTON_PRESSED(EN1), BUTTON_PRESSED(EN2) }; #if ENCODER_DEBOUNCE_MS static enc_t enc; - static enc_t old_live; static millis_t en_A_bounce_ms; diff --git a/Marlin/src/lcd/marlinui.h b/Marlin/src/lcd/marlinui.h index c2b38b3a7ebf..431763d67170 100644 --- a/Marlin/src/lcd/marlinui.h +++ b/Marlin/src/lcd/marlinui.h @@ -251,8 +251,7 @@ class MarlinUI { #endif #if (HAS_WIRED_LCD && HAS_ENCODER_ACTION && HAS_ENCODER_WHEEL) || HAS_DWIN_E3V2 - typedef struct { bool a:1, b:1; } enc_t; - static int8_t get_encoder_delta(const enc_t &live_enc, const millis_t &now=millis()); + static int8_t get_encoder_delta(const millis_t &now=millis()); #endif #if HAS_MEDIA From 1ed1e914b7d161928e9c973b8c5eaec3561a0c26 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Feb 2024 23:16:48 -0600 Subject: [PATCH 32/37] 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 c4b4903d8885..5e1f7f401f8c 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1415,7 +1415,7 @@ void MarlinUI::init() { #else - enc_t &enc = live_enc; + const enc_t &enc = live_enc; #endif From fb6b08eb1b4c0e9b96d925dcebeaacca598b8dbf Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Tue, 27 Feb 2024 20:09:38 -0600 Subject: [PATCH 33/37] Use ui.get_encoder_delta in lv_update_encoder --- Marlin/src/inc/Conditionals_LCD.h | 3 +- .../extui/mks_ui/tft_lvgl_configuration.cpp | 45 ++++++++++++------- Marlin/src/lcd/marlinui.cpp | 2 +- Marlin/src/lcd/marlinui.h | 2 +- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/Marlin/src/inc/Conditionals_LCD.h b/Marlin/src/inc/Conditionals_LCD.h index b6ac3db0f9e0..9fff6afe10d1 100644 --- a/Marlin/src/inc/Conditionals_LCD.h +++ b/Marlin/src/inc/Conditionals_LCD.h @@ -893,10 +893,11 @@ #endif #endif -// FSMC/SPI TFT Panels (LVGL) +// FSMC/SPI TFT Panels (LVGL) with encoder click wheel #if ENABLED(TFT_LVGL_UI) #define HAS_TFT_LVGL_UI 1 #define SERIAL_RUNTIME_HOOK 1 + #define STD_ENCODER_PULSES_PER_STEP 4 #endif // FSMC/SPI TFT Panels diff --git a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp index 5817eee05ede..d89afce811c8 100644 --- a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp +++ b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp @@ -460,7 +460,6 @@ void lv_encoder_pin_init() { #if BUTTON_EXISTS(BACK) SET_INPUT_PULLUP(BTN_BACK); #endif - #if BUTTON_EXISTS(UP) SET_INPUT(BTN_UP); #endif @@ -477,34 +476,46 @@ void lv_encoder_pin_init() { void lv_update_encoder() { - #if ANY_BUTTON(EN1, EN2, ENC, BACK) + #if ANY_BUTTON(EN1, EN2) + constexpr uint8_t epps = ENCODER_PULSES_PER_STEP; // We can fill in + static uint8_t pulse_count; + pulse_count += ui.get_encoder_delta(); + const int8_t fullSteps = pulse_count / epps; + pulse_count -= fullSteps * epps; + enc_diff += fullSteps; + #endif + #if ANY_BUTTON(ENC, BACK, UP, DOWN, LEFT, RIGHT) static millis_t last_encoder_ms; const millis_t now = millis(), diffTime = getTickDiff(now, last_encoder_ms); if (diffTime <= 50) return; + #endif - //if (BUTTON_PRESSED(BACK)) {} - - static uint8_t old_enc; - const uint8_t enc = (BUTTON_PRESSED(EN1) ? B01 : 0) | (BUTTON_PRESSED(EN2) ? B10 : 0); - if (enc != old_enc) { - if (old_enc == 0) { - switch (enc) { - case 1: --enc_diff; last_encoder_ms = now; break; - case 2: ++enc_diff; last_encoder_ms = now; break; - } - } - old_enc = enc; - } - + #if BUTTON_EXISTS(ENC) static uint8_t old_button_enc = LV_INDEV_STATE_REL; const uint8_t enc_c = BUTTON_PRESSED(ENC) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; if (enc_c != old_button_enc) { indev_enc_state = enc_c ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; old_button_enc = enc_c; } + #endif + + #if BUTTON_EXISTS(BACK) + if (BUTTON_PRESSED(BACK)) {} + #endif + #if BUTTON_EXISTS(UP) + if (BUTTON_PRESSED(UP)) {} + #endif + #if BUTTON_EXISTS(DOWN) + if (BUTTON_PRESSED(DOWN)) {} + #endif + #if BUTTON_EXISTS(LEFT) + if (BUTTON_PRESSED(LEFT)) {} + #endif + #if BUTTON_EXISTS(RIGHT) + if (BUTTON_PRESSED(RIGHT)) {} + #endif - #endif // ANY_BUTTON } #ifdef __PLAT_NATIVE_SIM__ diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 5e1f7f401f8c..872f7776ae11 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1388,7 +1388,7 @@ void MarlinUI::init() { #endif // HAS_WIRED_LCD -#if (HAS_WIRED_LCD && HAS_ENCODER_ACTION && HAS_ENCODER_WHEEL) || HAS_DWIN_E3V2 +#if (HAS_WIRED_LCD && HAS_ENCODER_ACTION && HAS_ENCODER_WHEEL) || HAS_DWIN_E3V2 || HAS_TFT_LVGL_UI #define ENCODER_DEBOUNCE_MS 2 diff --git a/Marlin/src/lcd/marlinui.h b/Marlin/src/lcd/marlinui.h index 431763d67170..4ab1c3e28c51 100644 --- a/Marlin/src/lcd/marlinui.h +++ b/Marlin/src/lcd/marlinui.h @@ -250,7 +250,7 @@ class MarlinUI { } #endif - #if (HAS_WIRED_LCD && HAS_ENCODER_ACTION && HAS_ENCODER_WHEEL) || HAS_DWIN_E3V2 + #if (HAS_WIRED_LCD && HAS_ENCODER_ACTION && HAS_ENCODER_WHEEL) || HAS_DWIN_E3V2 || HAS_TFT_LVGL_UI static int8_t get_encoder_delta(const millis_t &now=millis()); #endif From ffb1c82b99d4ef654e97bf4559652dcb898e38f8 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Tue, 27 Feb 2024 20:33:43 -0600 Subject: [PATCH 34/37] Clarify HAS_MARLINUI_ENCODER --- Marlin/src/lcd/buttons.h | 4 ++-- Marlin/src/lcd/marlinui.cpp | 14 +++++++++----- Marlin/src/lcd/marlinui.h | 3 ++- Marlin/src/lcd/menu/menu_main.cpp | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Marlin/src/lcd/buttons.h b/Marlin/src/lcd/buttons.h index 601e8a70ae05..2bd5fd7b35ef 100644 --- a/Marlin/src/lcd/buttons.h +++ b/Marlin/src/lcd/buttons.h @@ -24,9 +24,9 @@ #include "../inc/MarlinConfig.h" #if ((!HAS_ADC_BUTTONS && IS_NEWPANEL) || BUTTONS_EXIST(EN1, EN2)) && !IS_TFTGLCD_PANEL - #define HAS_ENCODER_WHEEL 1 + #define HAS_MARLINUI_ENCODER 1 #endif -#if (HAS_ENCODER_WHEEL || ANY_BUTTON(ENC, BACK, UP, DOWN, LEFT, RIGHT)) && DISABLED(TOUCH_UI_FTDI_EVE) +#if (HAS_MARLINUI_ENCODER || ANY_BUTTON(ENC, BACK, UP, DOWN, LEFT, RIGHT)) && DISABLED(TOUCH_UI_FTDI_EVE) #define HAS_DIGITAL_BUTTONS 1 #endif #if !HAS_ADC_BUTTONS && (IS_RRW_KEYPAD || (HAS_WIRED_LCD && !IS_NEWPANEL)) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 872f7776ae11..69d47119871a 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1291,7 +1291,7 @@ void MarlinUI::init() { void MarlinUI::update_buttons() { const millis_t now = millis(); - #if HAS_ENCODER_WHEEL + #if HAS_MARLINUI_ENCODER const int8_t delta = get_encoder_delta(now); if (delta) { @@ -1301,7 +1301,7 @@ void MarlinUI::init() { #endif } - #endif // HAS_ENCODER_WHEEL + #endif // HAS_MARLINUI_ENCODER if (PENDING(now, next_button_update_ms)) return; @@ -1341,7 +1341,7 @@ void MarlinUI::init() { buttons = (newbuttons | TERN0(HAS_SLOW_BUTTONS, slow_buttons) #if ALL(HAS_TOUCH_BUTTONS, HAS_ENCODER_ACTION) - | (touch_buttons & TERN(HAS_ENCODER_WHEEL, ~(EN_A | EN_B), 0xFF)) + | (touch_buttons & TERN(HAS_MARLINUI_ENCODER, ~(EN_A | EN_B), 0xFF)) #endif ); @@ -1388,10 +1388,14 @@ void MarlinUI::init() { #endif // HAS_WIRED_LCD -#if (HAS_WIRED_LCD && HAS_ENCODER_ACTION && HAS_ENCODER_WHEEL) || HAS_DWIN_E3V2 || HAS_TFT_LVGL_UI +#if MARLINUI_ENCODER_DELTA #define ENCODER_DEBOUNCE_MS 2 + /** + * Get the encoder delta (-2 -1 0 +1 +2) since the last call, reading the live encoder state. + * Pins may be debounced to filter noise. + */ int8_t MarlinUI::get_encoder_delta(const millis_t &now/*=millis()*/) { typedef struct { bool a:1, b:1; } enc_t; @@ -1434,7 +1438,7 @@ void MarlinUI::init() { } // get_encoder_delta -#endif +#endif // MARLINUI_ENCODER_DELTA void MarlinUI::completion_feedback(const bool good/*=true*/) { wake_display(); // Wake the screen for all audio feedback diff --git a/Marlin/src/lcd/marlinui.h b/Marlin/src/lcd/marlinui.h index 4ab1c3e28c51..62db78dab22d 100644 --- a/Marlin/src/lcd/marlinui.h +++ b/Marlin/src/lcd/marlinui.h @@ -250,7 +250,8 @@ class MarlinUI { } #endif - #if (HAS_WIRED_LCD && HAS_ENCODER_ACTION && HAS_ENCODER_WHEEL) || HAS_DWIN_E3V2 || HAS_TFT_LVGL_UI + #if (HAS_WIRED_LCD && HAS_ENCODER_ACTION && HAS_MARLINUI_ENCODER) || HAS_DWIN_E3V2 || HAS_TFT_LVGL_UI + #define MARLINUI_ENCODER_DELTA 1 static int8_t get_encoder_delta(const millis_t &now=millis()); #endif diff --git a/Marlin/src/lcd/menu/menu_main.cpp b/Marlin/src/lcd/menu/menu_main.cpp index 7277045baf6b..400dfe9ba0fa 100644 --- a/Marlin/src/lcd/menu/menu_main.cpp +++ b/Marlin/src/lcd/menu/menu_main.cpp @@ -247,7 +247,7 @@ void menu_main() { START_MENU(); BACK_ITEM(MSG_INFO_SCREEN); - #if HAS_MEDIA && !defined(MEDIA_MENU_AT_TOP) && !HAS_ENCODER_WHEEL + #if HAS_MEDIA && !defined(MEDIA_MENU_AT_TOP) && !HAS_MARLINUI_ENCODER #define MEDIA_MENU_AT_TOP #endif From 0467ea4acbcce9839f5ce85975ea429135786318 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Tue, 27 Feb 2024 20:58:35 -0600 Subject: [PATCH 35/37] unify button init --- Marlin/src/lcd/e3v2/common/encoder.cpp | 16 ------ Marlin/src/lcd/e3v2/common/encoder.h | 3 - Marlin/src/lcd/e3v2/creality/dwin.cpp | 1 - Marlin/src/lcd/e3v2/jyersui/dwin.cpp | 1 - Marlin/src/lcd/e3v2/proui/dwin.cpp | 1 - .../extui/mks_ui/tft_lvgl_configuration.cpp | 29 ---------- .../lcd/extui/mks_ui/tft_lvgl_configuration.h | 1 - Marlin/src/lcd/marlinui.cpp | 56 +++++++++---------- 8 files changed, 27 insertions(+), 81 deletions(-) diff --git a/Marlin/src/lcd/e3v2/common/encoder.cpp b/Marlin/src/lcd/e3v2/common/encoder.cpp index 4b3e1889aa62..889d1c61f3f5 100644 --- a/Marlin/src/lcd/e3v2/common/encoder.cpp +++ b/Marlin/src/lcd/e3v2/common/encoder.cpp @@ -49,22 +49,6 @@ void Encoder_tick() { TERN_(HAS_BEEPER, if (ui.sound_on) buzzer.click(10)); } -// Encoder initialization -void encoderConfiguration() { - #if BUTTON_EXISTS(EN1) - SET_INPUT_PULLUP(BTN_EN1); - #endif - #if BUTTON_EXISTS(EN2) - SET_INPUT_PULLUP(BTN_EN2); - #endif - #if BUTTON_EXISTS(ENC) - SET_INPUT_PULLUP(BTN_ENC); - #endif - #if HAS_BEEPER - SET_OUTPUT(BEEPER_PIN); // TODO: Use buzzer.h which already inits this - #endif -} - // Analyze encoder value and return state EncoderState encoderReceiveAnalyze() { const millis_t now = millis(); diff --git a/Marlin/src/lcd/e3v2/common/encoder.h b/Marlin/src/lcd/e3v2/common/encoder.h index ce431c9811b1..428193ca658c 100644 --- a/Marlin/src/lcd/e3v2/common/encoder.h +++ b/Marlin/src/lcd/e3v2/common/encoder.h @@ -47,9 +47,6 @@ typedef enum { #define ENCODER_WAIT_MS TERN(DWIN_LCD_PROUI, 10, 20) -// Encoder initialization -void encoderConfiguration(); - // Analyze encoder value and return state EncoderState encoderReceiveAnalyze(); diff --git a/Marlin/src/lcd/e3v2/creality/dwin.cpp b/Marlin/src/lcd/e3v2/creality/dwin.cpp index b2c08861d7d7..8da8e40b51a1 100644 --- a/Marlin/src/lcd/e3v2/creality/dwin.cpp +++ b/Marlin/src/lcd/e3v2/creality/dwin.cpp @@ -4080,7 +4080,6 @@ void hmiInit() { } void dwinInitScreen() { - encoderConfiguration(); hmiInit(); hmiSetLanguageCache(); hmiStartFrame(true); diff --git a/Marlin/src/lcd/e3v2/jyersui/dwin.cpp b/Marlin/src/lcd/e3v2/jyersui/dwin.cpp index 856229b0d845..3f9d9ad13fb2 100644 --- a/Marlin/src/lcd/e3v2/jyersui/dwin.cpp +++ b/Marlin/src/lcd/e3v2/jyersui/dwin.cpp @@ -5143,7 +5143,6 @@ void MarlinUI::init_lcd() { if (dwinHandshake()) SERIAL_ECHOLNPGM("ok."); else SERIAL_ECHOLNPGM("error."); dwinFrameSetDir(1); // Orientation 90° dwinUpdateLCD(); // Show bootscreen (first image) - encoderConfiguration(); for (uint16_t t = 0; t <= 100; t += 2) { dwinIconShow(ICON, ICON_Bar, 15, 260); dwinDrawRectangle(1, COLOR_BG_BLACK, 15 + t * 242 / 100, 260, 257, 280); diff --git a/Marlin/src/lcd/e3v2/proui/dwin.cpp b/Marlin/src/lcd/e3v2/proui/dwin.cpp index 888d1f74928e..0f2a31e28797 100644 --- a/Marlin/src/lcd/e3v2/proui/dwin.cpp +++ b/Marlin/src/lcd/e3v2/proui/dwin.cpp @@ -1867,7 +1867,6 @@ void MarlinUI::init_lcd() { const bool hs = dwinHandshake(); UNUSED(hs); dwinFrameSetDir(1); dwinJPGCacheTo1(Language_English); - encoderConfiguration(); } void dwinInitScreen() { diff --git a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp index d89afce811c8..f8dda5bd7b93 100644 --- a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp +++ b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp @@ -216,7 +216,6 @@ void tft_lvgl_init() { tft_style_init(); filament_pin_setup(); - lv_encoder_pin_init(); #if ENABLED(MKS_WIFI_MODULE) mks_esp_wifi_init(); @@ -446,34 +445,6 @@ lv_fs_res_t sd_tell_cb(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) { return LV_FS_RES_OK; } -void lv_encoder_pin_init() { - #if BUTTON_EXISTS(EN1) - SET_INPUT_PULLUP(BTN_EN1); - #endif - #if BUTTON_EXISTS(EN2) - SET_INPUT_PULLUP(BTN_EN2); - #endif - #if BUTTON_EXISTS(ENC) - SET_INPUT_PULLUP(BTN_ENC); - #endif - - #if BUTTON_EXISTS(BACK) - SET_INPUT_PULLUP(BTN_BACK); - #endif - #if BUTTON_EXISTS(UP) - SET_INPUT(BTN_UP); - #endif - #if BUTTON_EXISTS(DOWN) - SET_INPUT(BTN_DOWN); - #endif - #if BUTTON_EXISTS(LEFT) - SET_INPUT(BTN_LEFT); - #endif - #if BUTTON_EXISTS(RIGHT) - SET_INPUT(BTN_RIGHT); - #endif -} - void lv_update_encoder() { #if ANY_BUTTON(EN1, EN2) diff --git a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.h b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.h index d847cfb1933b..43e82bd34dd6 100644 --- a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.h +++ b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.h @@ -41,7 +41,6 @@ bool my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data); bool my_mousewheel_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data); void lcdClear(uint16_t color); -void lv_encoder_pin_init(); void lv_update_encoder(); lv_fs_res_t spi_flash_open_cb(lv_fs_drv_t * drv, void * file_p, const char * path, lv_fs_mode_t mode); diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 69d47119871a..ebedf999d8e6 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -228,34 +228,32 @@ void MarlinUI::init() { init_lcd(); - #if HAS_DIGITAL_BUTTONS - #if BUTTON_EXISTS(EN1) - SET_INPUT_PULLUP(BTN_EN1); - #endif - #if BUTTON_EXISTS(EN2) - SET_INPUT_PULLUP(BTN_EN2); - #endif - #if BUTTON_EXISTS(ENC) - SET_INPUT_PULLUP(BTN_ENC); - #endif - #if BUTTON_EXISTS(ENC_EN) - SET_INPUT_PULLUP(BTN_ENC_EN); - #endif - #if BUTTON_EXISTS(BACK) - SET_INPUT_PULLUP(BTN_BACK); - #endif - #if BUTTON_EXISTS(UP) - SET_INPUT(BTN_UP); - #endif - #if BUTTON_EXISTS(DOWN) - SET_INPUT(BTN_DOWN); - #endif - #if BUTTON_EXISTS(LFT) - SET_INPUT(BTN_LEFT); - #endif - #if BUTTON_EXISTS(RT) - SET_INPUT(BTN_RIGHT); - #endif + #if BUTTON_EXISTS(EN1) + SET_INPUT_PULLUP(BTN_EN1); + #endif + #if BUTTON_EXISTS(EN2) + SET_INPUT_PULLUP(BTN_EN2); + #endif + #if BUTTON_EXISTS(ENC) + SET_INPUT_PULLUP(BTN_ENC); + #endif + #if BUTTON_EXISTS(ENC_EN) + SET_INPUT_PULLUP(BTN_ENC_EN); + #endif + #if BUTTON_EXISTS(BACK) + SET_INPUT_PULLUP(BTN_BACK); + #endif + #if BUTTON_EXISTS(UP) + SET_INPUT(BTN_UP); + #endif + #if BUTTON_EXISTS(DOWN) + SET_INPUT(BTN_DOWN); + #endif + #if BUTTON_EXISTS(LFT) + SET_INPUT(BTN_LEFT); + #endif + #if BUTTON_EXISTS(RT) + SET_INPUT(BTN_RIGHT); #endif #if HAS_SHIFT_ENCODER @@ -1301,7 +1299,7 @@ void MarlinUI::init() { #endif } - #endif // HAS_MARLINUI_ENCODER + #endif if (PENDING(now, next_button_update_ms)) return; From 00b87330db88a64c89a7c9e4a716d3069ffcc547 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 9 Mar 2024 21:06:14 -0600 Subject: [PATCH 36/37] combine ab debounce --- Marlin/src/lcd/marlinui.cpp | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index ebedf999d8e6..1f83342cc7bd 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1402,18 +1402,19 @@ void MarlinUI::init() { #if ENCODER_DEBOUNCE_MS - static enc_t enc; - static enc_t old_live; + static enc_t enc, old_live; + static millis_t bounce_ms; - static millis_t en_A_bounce_ms; - if (old_live.a != live_enc.a) en_A_bounce_ms = now + (ENCODER_DEBOUNCE_MS); - else if (ELAPSED(now, en_A_bounce_ms)) enc.a = live_enc.a; - - static millis_t en_B_bounce_ms; - if (old_live.b != live_enc.b) en_B_bounce_ms = now + (ENCODER_DEBOUNCE_MS); - else if (ELAPSED(now, en_B_bounce_ms)) enc.b = live_enc.b; + if (old_live.a != live_enc.a || old_live.b != live_enc.b) { + bounce_ms = now + (ENCODER_DEBOUNCE_MS); // Wait for states to settle + old_live = live_enc; + return 0; + } - old_live = live_enc; + if (ELAPSED(now, bounce_ms)) { + enc.a = live_enc.a; + enc.b = live_enc.b; + } #else @@ -1421,12 +1422,12 @@ void MarlinUI::init() { #endif - static uint8_t old_pos; - const uint8_t pos = (enc.a ^ enc.b) | (enc.a << 1); // 0:00 1:10 2:11 3:01 + static uint8_t old_ab; + const uint8_t ab = (enc.a ^ enc.b) | (enc.a << 1); // 0:00 1:10 2:11 3:01 int8_t delta = 0; - if (pos != old_pos) { - delta = (pos - old_pos + 4 + 1) % 4 - 1; - old_pos = pos; + if (ab != old_ab) { + delta = (ab - old_ab + 4 + 1) % 4 - 1; + old_ab = ab; static int8_t last_dir; if (delta == 2) delta = last_dir * 2; From 49a49ac2f89f56f8021241cc362fcdb747251846 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 14 Apr 2024 16:57:17 -0500 Subject: [PATCH 37/37] Split up A-B debounce --- Marlin/src/lcd/marlinui.cpp | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 1f83342cc7bd..ebedf999d8e6 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1402,19 +1402,18 @@ void MarlinUI::init() { #if ENCODER_DEBOUNCE_MS - static enc_t enc, old_live; - static millis_t bounce_ms; + static enc_t enc; + static enc_t old_live; - if (old_live.a != live_enc.a || old_live.b != live_enc.b) { - bounce_ms = now + (ENCODER_DEBOUNCE_MS); // Wait for states to settle - old_live = live_enc; - return 0; - } + static millis_t en_A_bounce_ms; + if (old_live.a != live_enc.a) en_A_bounce_ms = now + (ENCODER_DEBOUNCE_MS); + else if (ELAPSED(now, en_A_bounce_ms)) enc.a = live_enc.a; - if (ELAPSED(now, bounce_ms)) { - enc.a = live_enc.a; - enc.b = live_enc.b; - } + static millis_t en_B_bounce_ms; + if (old_live.b != live_enc.b) en_B_bounce_ms = now + (ENCODER_DEBOUNCE_MS); + else if (ELAPSED(now, en_B_bounce_ms)) enc.b = live_enc.b; + + old_live = live_enc; #else @@ -1422,12 +1421,12 @@ void MarlinUI::init() { #endif - static uint8_t old_ab; - const uint8_t ab = (enc.a ^ enc.b) | (enc.a << 1); // 0:00 1:10 2:11 3:01 + static uint8_t old_pos; + const uint8_t pos = (enc.a ^ enc.b) | (enc.a << 1); // 0:00 1:10 2:11 3:01 int8_t delta = 0; - if (ab != old_ab) { - delta = (ab - old_ab + 4 + 1) % 4 - 1; - old_ab = ab; + if (pos != old_pos) { + delta = (pos - old_pos + 4 + 1) % 4 - 1; + old_pos = pos; static int8_t last_dir; if (delta == 2) delta = last_dir * 2;