Skip to content

Commit

Permalink
Advance encoder after a while if it has crossed the half a step thres…
Browse files Browse the repository at this point in the history
…hold while changing direction
  • Loading branch information
dbuezas committed Feb 4, 2024
1 parent 92c4c7d commit 2100d45
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)) {

Expand Down Expand Up @@ -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
}
}
}

Expand Down

0 comments on commit 2100d45

Please sign in to comment.