From 4aa48beb378fec6a6e7de0c8c3c7fe47f6551c24 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 7 Feb 2024 18:25:13 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=20Adjust=20encoder=20multiplier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Marlin/src/lcd/e3v2/common/encoder.cpp | 14 +++--- Marlin/src/lcd/marlinui.cpp | 63 +++++++++++--------------- Marlin/src/lcd/marlinui.h | 16 +++---- Marlin/src/lcd/menu/menu.cpp | 2 +- Marlin/src/lcd/menu/menu_item.h | 2 +- Marlin/src/lcd/menu/menu_mixer.cpp | 2 +- 6 files changed, 43 insertions(+), 56 deletions(-) diff --git a/Marlin/src/lcd/e3v2/common/encoder.cpp b/Marlin/src/lcd/e3v2/common/encoder.cpp index cb14596849a5..c406cc47d922 100644 --- a/Marlin/src/lcd/e3v2/common/encoder.cpp +++ b/Marlin/src/lcd/e3v2/common/encoder.cpp @@ -127,7 +127,7 @@ EncoderState encoderReceiveAnalyze() { #if ENABLED(ENCODER_RATE_MULTIPLIER) millis_t ms = millis(); - int32_t encoderMultiplier = 1; + int32_t encoder_multiplier = 1; // if must encoder rati multiplier if (encoderRate.enabled) { @@ -137,10 +137,10 @@ 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) encoderMultiplier = 100; - else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10; + 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) encoderMultiplier = 5; + else if (encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) encoder_multiplier = 5; #endif } encoderRate.lastEncoderTime = ms; @@ -148,12 +148,12 @@ EncoderState encoderReceiveAnalyze() { #else - constexpr int32_t encoderMultiplier = 1; + constexpr int32_t encoder_multiplier = 1; #endif - // encoderRate.encoderMoveValue += (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP); - encoderRate.encoderMoveValue = (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP); + // 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; temp_diff = 0; diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 313131d87aba..9a8152ba9909 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -398,12 +398,7 @@ void MarlinUI::init() { bool MarlinUI::screen_changed; #if ENABLED(ENCODER_RATE_MULTIPLIER) - bool MarlinUI::encoderRateMultiplierEnabled; - millis_t MarlinUI::lastEncoderMovementMillis = 0; - void MarlinUI::enable_encoder_multiplier(const bool onoff) { - encoderRateMultiplierEnabled = onoff; - lastEncoderMovementMillis = 0; - } + bool MarlinUI::encoder_multiplier_enabled; #endif #if ANY(REVERSE_MENU_DIRECTION, REVERSE_SELECT_DIRECTION) @@ -614,8 +609,6 @@ void MarlinUI::init() { void MarlinUI::status_screen() { - TERN_(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLY(false)); - #if BASIC_PROGRESS_BAR // @@ -1053,41 +1046,37 @@ void MarlinUI::init() { #if ALL(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLIER) - int32_t encoderMultiplier = 1; - - if (encoderRateMultiplierEnabled) { - const float encoderMovementSteps = float(abs_diff) / epps; - - 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 + int32_t encoder_multiplier = 1; + + if (encoder_multiplier_enabled) { + // Note that the rate is always calculated between two passes through the + // loop and that the abs of the encoderDiff value is tracked. + static millis_t encoder_mult_prev_ms = 0; + 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; + + // 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(); + #endif + } #else - constexpr int32_t encoderMultiplier = 1; + constexpr int32_t encoder_multiplier = 1; #endif // ENCODER_RATE_MULTIPLIER - if (can_encode()) encoderPosition += (encoderDiff * encoderMultiplier) / epps; + if (can_encode()) encoderPosition += (encoderDiff * encoder_multiplier) / epps; encoderDiff = 0; } diff --git a/Marlin/src/lcd/marlinui.h b/Marlin/src/lcd/marlinui.h index 04f4aea24ff7..ff87852f119b 100644 --- a/Marlin/src/lcd/marlinui.h +++ b/Marlin/src/lcd/marlinui.h @@ -658,6 +658,13 @@ class MarlinUI { TERN(HAS_SCREEN_TIMEOUT, return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS, UNUSED(ms)); } + #if ALL(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLIER) + static bool encoder_multiplier_enabled; + static void enable_encoder_multiplier(const bool onoff) { encoder_multiplier_enabled = onoff; } + #else + static void enable_encoder_multiplier(const bool) {} + #endif + #if HAS_MARLINUI_MENU #if HAS_TOUCH_BUTTONS @@ -667,15 +674,6 @@ class MarlinUI { static constexpr uint8_t touch_buttons = 0; #endif - #if ENABLED(ENCODER_RATE_MULTIPLIER) - static bool encoderRateMultiplierEnabled; - static millis_t lastEncoderMovementMillis; - static void enable_encoder_multiplier(const bool onoff); - #define ENCODER_RATE_MULTIPLY(F) (ui.encoderRateMultiplierEnabled = F) - #else - #define ENCODER_RATE_MULTIPLY(F) NOOP - #endif - // Manual Movement static ManualMove manual_move; static bool can_show_slider() { return !external_control && currentScreen != manual_move.screen_ptr; } diff --git a/Marlin/src/lcd/menu/menu.cpp b/Marlin/src/lcd/menu/menu.cpp index e98c9b48c04a..892af5dbabd8 100644 --- a/Marlin/src/lcd/menu/menu.cpp +++ b/Marlin/src/lcd/menu/menu.cpp @@ -222,6 +222,7 @@ void MarlinUI::goto_screen(screenFunc_t screen, const uint16_t encoder/*=0*/, co TERN_(HAS_MARLINUI_U8GLIB, drawing_screen = false); TERN_(HAS_MARLINUI_MENU, encoder_direction_normal()); + enable_encoder_multiplier(false); set_selection(false); } @@ -255,7 +256,6 @@ void MarlinUI::synchronize(FSTR_P const fmsg/*=nullptr*/) { */ void scroll_screen(const uint8_t limit, const bool is_menu) { ui.encoder_direction_menus(); - ENCODER_RATE_MULTIPLY(false); if (int32_t(ui.encoderPosition) < 0) ui.encoderPosition = 0; if (ui.first_page) { encoderLine = ui.encoderPosition / (ENCODER_STEPS_PER_MENU_ITEM); diff --git a/Marlin/src/lcd/menu/menu_item.h b/Marlin/src/lcd/menu/menu_item.h index 823d2a4a25e7..47cd7d5cf3a5 100644 --- a/Marlin/src/lcd/menu/menu_item.h +++ b/Marlin/src/lcd/menu/menu_item.h @@ -287,8 +287,8 @@ class MenuItem_bool : public MenuEditItemBase { #define _MENU_INNER_F(TYPE, USE_MULTIPLIER, FLABEL, V...) do { \ FSTR_P const flabel = FLABEL; \ if (CLICKED()) { \ - _MENU_ITEM_MULTIPLIER_CHECK(USE_MULTIPLIER); \ MenuItem_##TYPE::action(flabel, ##V); \ + _MENU_ITEM_MULTIPLIER_CHECK(USE_MULTIPLIER); \ if (ui.screen_changed) return; \ } \ if (ui.should_draw()) \ diff --git a/Marlin/src/lcd/menu/menu_mixer.cpp b/Marlin/src/lcd/menu/menu_mixer.cpp index 21c18c820989..917164c0dba3 100644 --- a/Marlin/src/lcd/menu/menu_mixer.cpp +++ b/Marlin/src/lcd/menu/menu_mixer.cpp @@ -43,7 +43,7 @@ void _lcd_mixer_gradient_z_edit(const bool isend) { ui.defer_status_screen(); - ENCODER_RATE_MULTIPLY(true); + ui.enable_encoder_multiplier(true); float &zvar = isend ? mixer.gradient.end_z : mixer.gradient.start_z;