Skip to content

Commit 3fb74f4

Browse files
marknn3albertogg
authored andcommitted
Simplify encoder handling (MarlinFirmware#18754)
1 parent b461c5e commit 3fb74f4

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

Marlin/src/lcd/ultralcd.cpp

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ MarlinUI ui;
4949
bool MarlinUI::wait_for_move; // = false
5050
#endif
5151

52+
constexpr uint8_t epps = ENCODER_PULSES_PER_STEP;
53+
5254
#if HAS_SPI_LCD
5355
#if ENABLED(STATUS_MESSAGE_SCROLLING)
5456
uint8_t MarlinUI::status_scroll_offset; // = 0
@@ -440,13 +442,13 @@ bool MarlinUI::get_blink() {
440442
#endif
441443
{
442444
#if HAS_LCD_MENU
443-
if (RRK(EN_KEYPAD_UP)) encoderPosition -= ENCODER_PULSES_PER_STEP;
444-
else if (RRK(EN_KEYPAD_DOWN)) encoderPosition += ENCODER_PULSES_PER_STEP;
445+
if (RRK(EN_KEYPAD_UP)) encoderPosition -= epps;
446+
else if (RRK(EN_KEYPAD_DOWN)) encoderPosition += epps;
445447
else if (RRK(EN_KEYPAD_LEFT)) { MenuItem_back::action(); quick_feedback(); }
446448
else if (RRK(EN_KEYPAD_RIGHT)) encoderPosition = 0;
447449
#else
448-
if (RRK(EN_KEYPAD_UP) || RRK(EN_KEYPAD_LEFT)) encoderPosition -= ENCODER_PULSES_PER_STEP;
449-
else if (RRK(EN_KEYPAD_DOWN) || RRK(EN_KEYPAD_RIGHT)) encoderPosition += ENCODER_PULSES_PER_STEP;
450+
if (RRK(EN_KEYPAD_UP) || RRK(EN_KEYPAD_LEFT)) encoderPosition -= epps;
451+
else if (RRK(EN_KEYPAD_DOWN) || RRK(EN_KEYPAD_RIGHT)) encoderPosition += epps;
450452
#endif
451453
}
452454
#endif
@@ -841,7 +843,7 @@ void MarlinUI::update() {
841843
RESET_STATUS_TIMEOUT();
842844
if (touch_buttons & (EN_A | EN_B)) { // Menu arrows, in priority
843845
if (ELAPSED(ms, next_button_update_ms)) {
844-
encoderDiff = (ENCODER_STEPS_PER_MENU_ITEM) * (ENCODER_PULSES_PER_STEP) * encoderDirection;
846+
encoderDiff = (ENCODER_STEPS_PER_MENU_ITEM) * epps * encoderDirection;
845847
if (touch_buttons & EN_A) encoderDiff *= -1;
846848
TERN_(AUTO_BED_LEVELING_UBL, external_encoder());
847849
next_button_update_ms = ms + repeat_delay; // Assume the repeat delay
@@ -901,26 +903,21 @@ void MarlinUI::update() {
901903
#if ENCODER_PULSES_PER_STEP > 1
902904
// When reversing the encoder direction, a movement step can be missed because
903905
// encoderDiff has a non-zero residual value, making the controller unresponsive.
904-
// The fix clears the residual value when the encoder is reversed.
906+
// The fix clears the residual value when the encoder is idle.
905907
// Also check if past half the threshold to compensate for missed single steps.
906908
static int8_t lastEncoderDiff;
907-
int8_t prevDiff = lastEncoderDiff;
908-
lastEncoderDiff = encoderDiff; // Store before updating encoderDiff to save actual steps
909-
910-
// When not past threshold, and reversing... or past half the threshold
911-
if (WITHIN(abs_diff, 1, (ENCODER_PULSES_PER_STEP) - 1) // Not past threshold
912-
&& (abs_diff > (ENCODER_PULSES_PER_STEP) / 2 // Passed half the threshold? Done! Call it a full step.
913-
|| (ABS(encoderDiff - prevDiff) >= (ENCODER_PULSES_PER_STEP) // A big change when abs_diff is small implies reverse
914-
&& ABS(prevDiff) < (ENCODER_PULSES_PER_STEP) // ...especially when starting from a partial or no step.
915-
)
916-
)
917-
) {
918-
abs_diff = ENCODER_PULSES_PER_STEP;
919-
encoderDiff = (encoderDiff < 0 ? -1 : 1) * abs_diff; // Treat as full step
909+
910+
// Timeout? No decoder change since last check. 10 or 20 times per second.
911+
if (encoderDiff == lastEncoderDiff && abs_diff <= epps / 2) // Same direction & size but not over a half-step?
912+
encoderDiff = 0; // Clear residual pulses.
913+
else if (WITHIN(abs_diff, epps / 2 + 1, epps - 1)) { // Past half of threshold?
914+
abs_diff = epps; // Treat as a full step size
915+
encoderDiff = (encoderDiff < 0 ? -1 : 1) * abs_diff; // ...in the spin direction.
920916
}
917+
lastEncoderDiff = encoderDiff;
921918
#endif
922919

923-
const bool encoderPastThreshold = (abs_diff >= (ENCODER_PULSES_PER_STEP));
920+
const bool encoderPastThreshold = (abs_diff >= epps);
924921
if (encoderPastThreshold || lcd_clicked) {
925922
if (encoderPastThreshold) {
926923

@@ -929,7 +926,7 @@ void MarlinUI::update() {
929926
int32_t encoderMultiplier = 1;
930927

931928
if (encoderRateMultiplierEnabled) {
932-
const float encoderMovementSteps = float(abs_diff) / (ENCODER_PULSES_PER_STEP);
929+
const float encoderMovementSteps = float(abs_diff) / epps;
933930

934931
if (lastEncoderMovementMillis) {
935932
// Note that the rate is always calculated between two passes through the
@@ -958,7 +955,7 @@ void MarlinUI::update() {
958955

959956
#endif // ENCODER_RATE_MULTIPLIER
960957

961-
encoderPosition += (encoderDiff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
958+
encoderPosition += (encoderDiff * encoderMultiplier) / epps;
962959
encoderDiff = 0;
963960
}
964961

@@ -1191,7 +1188,7 @@ void MarlinUI::update() {
11911188
//
11921189
#if ANY_BUTTON(UP, DWN, LFT, RT)
11931190

1194-
const int8_t pulses = (ENCODER_PULSES_PER_STEP) * encoderDirection;
1191+
const int8_t pulses = epps * encoderDirection;
11951192

11961193
if (false) {
11971194
// for the else-ifs below
@@ -1547,17 +1544,17 @@ void MarlinUI::update() {
15471544
const int8_t xdir = col < (LCD_WIDTH ) / 2 ? -1 : 1,
15481545
ydir = row < (LCD_HEIGHT) / 2 ? -1 : 1;
15491546
if (on_edit_screen)
1550-
encoderDiff = (ENCODER_PULSES_PER_STEP) * ydir;
1547+
encoderDiff = epps * ydir;
15511548
else if (screen_items > 0) {
15521549
// Last 3 cols act as a scroll :-)
15531550
if (col > (LCD_WIDTH) - 5)
15541551
// 2 * LCD_HEIGHT to scroll to bottom of next page. (LCD_HEIGHT would only go 1 item down.)
1555-
encoderDiff = (ENCODER_PULSES_PER_STEP) * (encoderLine - encoderTopLine + 2 * (LCD_HEIGHT)) * ydir;
1552+
encoderDiff = epps * (encoderLine - encoderTopLine + 2 * (LCD_HEIGHT)) * ydir;
15561553
else
1557-
encoderDiff = (ENCODER_PULSES_PER_STEP) * (row - encoderPosition + encoderTopLine);
1554+
encoderDiff = epps * (row - encoderPosition + encoderTopLine);
15581555
}
15591556
else if (!on_status_screen())
1560-
encoderDiff = (ENCODER_PULSES_PER_STEP) * xdir;
1557+
encoderDiff = epps * xdir;
15611558
}
15621559

15631560
#endif

0 commit comments

Comments
 (0)