Skip to content

Commit 4aa48be

Browse files
committed
🚸 Adjust encoder multiplier
1 parent 371fb5a commit 4aa48be

File tree

6 files changed

+43
-56
lines changed

6 files changed

+43
-56
lines changed

Marlin/src/lcd/e3v2/common/encoder.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ EncoderState encoderReceiveAnalyze() {
127127
#if ENABLED(ENCODER_RATE_MULTIPLIER)
128128

129129
millis_t ms = millis();
130-
int32_t encoderMultiplier = 1;
130+
int32_t encoder_multiplier = 1;
131131

132132
// if must encoder rati multiplier
133133
if (encoderRate.enabled) {
@@ -137,23 +137,23 @@ EncoderState encoderReceiveAnalyze() {
137137
// Note that the rate is always calculated between two passes through the
138138
// loop and that the abs of the temp_diff value is tracked.
139139
const float encoderStepRate = encoderMovementSteps / float(ms - encoderRate.lastEncoderTime) * 1000;
140-
if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
141-
else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;
140+
if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoder_multiplier = 100;
141+
else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoder_multiplier = 10;
142142
#if ENCODER_5X_STEPS_PER_SEC
143-
else if (encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) encoderMultiplier = 5;
143+
else if (encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) encoder_multiplier = 5;
144144
#endif
145145
}
146146
encoderRate.lastEncoderTime = ms;
147147
}
148148

149149
#else
150150

151-
constexpr int32_t encoderMultiplier = 1;
151+
constexpr int32_t encoder_multiplier = 1;
152152

153153
#endif
154154

155-
// encoderRate.encoderMoveValue += (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
156-
encoderRate.encoderMoveValue = (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
155+
// encoderRate.encoderMoveValue += (temp_diff * encoder_multiplier) / (ENCODER_PULSES_PER_STEP);
156+
encoderRate.encoderMoveValue = (temp_diff * encoder_multiplier) / (ENCODER_PULSES_PER_STEP);
157157
if (encoderRate.encoderMoveValue < 0) encoderRate.encoderMoveValue = -encoderRate.encoderMoveValue;
158158

159159
temp_diff = 0;

Marlin/src/lcd/marlinui.cpp

+26-37
Original file line numberDiff line numberDiff line change
@@ -398,12 +398,7 @@ void MarlinUI::init() {
398398
bool MarlinUI::screen_changed;
399399

400400
#if ENABLED(ENCODER_RATE_MULTIPLIER)
401-
bool MarlinUI::encoderRateMultiplierEnabled;
402-
millis_t MarlinUI::lastEncoderMovementMillis = 0;
403-
void MarlinUI::enable_encoder_multiplier(const bool onoff) {
404-
encoderRateMultiplierEnabled = onoff;
405-
lastEncoderMovementMillis = 0;
406-
}
401+
bool MarlinUI::encoder_multiplier_enabled;
407402
#endif
408403

409404
#if ANY(REVERSE_MENU_DIRECTION, REVERSE_SELECT_DIRECTION)
@@ -614,8 +609,6 @@ void MarlinUI::init() {
614609

615610
void MarlinUI::status_screen() {
616611

617-
TERN_(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLY(false));
618-
619612
#if BASIC_PROGRESS_BAR
620613

621614
//
@@ -1053,41 +1046,37 @@ void MarlinUI::init() {
10531046

10541047
#if ALL(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLIER)
10551048

1056-
int32_t encoderMultiplier = 1;
1057-
1058-
if (encoderRateMultiplierEnabled) {
1059-
const float encoderMovementSteps = float(abs_diff) / epps;
1060-
1061-
if (lastEncoderMovementMillis) {
1062-
// Note that the rate is always calculated between two passes through the
1063-
// loop and that the abs of the encoderDiff value is tracked.
1064-
const float encoderStepRate = encoderMovementSteps / float(ms - lastEncoderMovementMillis) * 1000;
1065-
1066-
if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
1067-
else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;
1068-
1069-
// Enable to output the encoder steps per second value
1070-
//#define ENCODER_RATE_MULTIPLIER_DEBUG
1071-
#if ENABLED(ENCODER_RATE_MULTIPLIER_DEBUG)
1072-
SERIAL_ECHO_START();
1073-
SERIAL_ECHOPGM("Enc Step Rate: ", encoderStepRate);
1074-
SERIAL_ECHOPGM(" Multiplier: ", encoderMultiplier);
1075-
SERIAL_ECHOPGM(" ENCODER_10X_STEPS_PER_SEC: ", ENCODER_10X_STEPS_PER_SEC);
1076-
SERIAL_ECHOPGM(" ENCODER_100X_STEPS_PER_SEC: ", ENCODER_100X_STEPS_PER_SEC);
1077-
SERIAL_EOL();
1078-
#endif
1079-
}
1080-
1081-
lastEncoderMovementMillis = ms;
1082-
} // encoderRateMultiplierEnabled
1049+
int32_t encoder_multiplier = 1;
1050+
1051+
if (encoder_multiplier_enabled) {
1052+
// Note that the rate is always calculated between two passes through the
1053+
// loop and that the abs of the encoderDiff value is tracked.
1054+
static millis_t encoder_mult_prev_ms = 0;
1055+
const float encoderStepRate = ((float(abs_diff) / float(epps)) * 1000.0f) / float(ms - encoder_mult_prev_ms);
1056+
encoder_mult_prev_ms = ms;
1057+
1058+
if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoder_multiplier = 100;
1059+
else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoder_multiplier = 10;
1060+
1061+
// Enable to output the encoder steps per second value
1062+
//#define ENCODER_RATE_MULTIPLIER_DEBUG
1063+
#if ENABLED(ENCODER_RATE_MULTIPLIER_DEBUG)
1064+
SERIAL_ECHO_START();
1065+
SERIAL_ECHOPGM("Enc Step Rate: ", encoderStepRate);
1066+
SERIAL_ECHOPGM(" Multiplier: ", encoder_multiplier);
1067+
SERIAL_ECHOPGM(" ENCODER_10X_STEPS_PER_SEC: ", ENCODER_10X_STEPS_PER_SEC);
1068+
SERIAL_ECHOPGM(" ENCODER_100X_STEPS_PER_SEC: ", ENCODER_100X_STEPS_PER_SEC);
1069+
SERIAL_EOL();
1070+
#endif
1071+
}
10831072

10841073
#else
10851074

1086-
constexpr int32_t encoderMultiplier = 1;
1075+
constexpr int32_t encoder_multiplier = 1;
10871076

10881077
#endif // ENCODER_RATE_MULTIPLIER
10891078

1090-
if (can_encode()) encoderPosition += (encoderDiff * encoderMultiplier) / epps;
1079+
if (can_encode()) encoderPosition += (encoderDiff * encoder_multiplier) / epps;
10911080

10921081
encoderDiff = 0;
10931082
}

Marlin/src/lcd/marlinui.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,13 @@ class MarlinUI {
658658
TERN(HAS_SCREEN_TIMEOUT, return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS, UNUSED(ms));
659659
}
660660

661+
#if ALL(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLIER)
662+
static bool encoder_multiplier_enabled;
663+
static void enable_encoder_multiplier(const bool onoff) { encoder_multiplier_enabled = onoff; }
664+
#else
665+
static void enable_encoder_multiplier(const bool) {}
666+
#endif
667+
661668
#if HAS_MARLINUI_MENU
662669

663670
#if HAS_TOUCH_BUTTONS
@@ -667,15 +674,6 @@ class MarlinUI {
667674
static constexpr uint8_t touch_buttons = 0;
668675
#endif
669676

670-
#if ENABLED(ENCODER_RATE_MULTIPLIER)
671-
static bool encoderRateMultiplierEnabled;
672-
static millis_t lastEncoderMovementMillis;
673-
static void enable_encoder_multiplier(const bool onoff);
674-
#define ENCODER_RATE_MULTIPLY(F) (ui.encoderRateMultiplierEnabled = F)
675-
#else
676-
#define ENCODER_RATE_MULTIPLY(F) NOOP
677-
#endif
678-
679677
// Manual Movement
680678
static ManualMove manual_move;
681679
static bool can_show_slider() { return !external_control && currentScreen != manual_move.screen_ptr; }

Marlin/src/lcd/menu/menu.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ void MarlinUI::goto_screen(screenFunc_t screen, const uint16_t encoder/*=0*/, co
222222
TERN_(HAS_MARLINUI_U8GLIB, drawing_screen = false);
223223

224224
TERN_(HAS_MARLINUI_MENU, encoder_direction_normal());
225+
enable_encoder_multiplier(false);
225226

226227
set_selection(false);
227228
}
@@ -255,7 +256,6 @@ void MarlinUI::synchronize(FSTR_P const fmsg/*=nullptr*/) {
255256
*/
256257
void scroll_screen(const uint8_t limit, const bool is_menu) {
257258
ui.encoder_direction_menus();
258-
ENCODER_RATE_MULTIPLY(false);
259259
if (int32_t(ui.encoderPosition) < 0) ui.encoderPosition = 0;
260260
if (ui.first_page) {
261261
encoderLine = ui.encoderPosition / (ENCODER_STEPS_PER_MENU_ITEM);

Marlin/src/lcd/menu/menu_item.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ class MenuItem_bool : public MenuEditItemBase {
287287
#define _MENU_INNER_F(TYPE, USE_MULTIPLIER, FLABEL, V...) do { \
288288
FSTR_P const flabel = FLABEL; \
289289
if (CLICKED()) { \
290-
_MENU_ITEM_MULTIPLIER_CHECK(USE_MULTIPLIER); \
291290
MenuItem_##TYPE::action(flabel, ##V); \
291+
_MENU_ITEM_MULTIPLIER_CHECK(USE_MULTIPLIER); \
292292
if (ui.screen_changed) return; \
293293
} \
294294
if (ui.should_draw()) \

Marlin/src/lcd/menu/menu_mixer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
void _lcd_mixer_gradient_z_edit(const bool isend) {
4545
ui.defer_status_screen();
46-
ENCODER_RATE_MULTIPLY(true);
46+
ui.enable_encoder_multiplier(true);
4747

4848
float &zvar = isend ? mixer.gradient.end_z : mixer.gradient.start_z;
4949

0 commit comments

Comments
 (0)