Skip to content

Commit

Permalink
Add encoder reset mechanism to avoid off-by-one-step bug in Ender 3
Browse files Browse the repository at this point in the history
  • Loading branch information
dbuezas committed Jan 30, 2024
1 parent bb36767 commit 9db30f9
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 250

#if HAS_STATUS_MESSAGE
#if ENABLED(STATUS_MESSAGE_SCROLLING) && ANY(HAS_WIRED_LCD, DWIN_LCD_PROUI)
Expand Down Expand Up @@ -966,7 +967,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 last_encoder_movement_ms = 0;
const millis_t ms = millis();

#if LED_POWEROFF_TIMEOUT > 0
Expand Down Expand Up @@ -1018,7 +1019,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, last_encoder_movement_ms + BLOCK_CLICK_AFTER_MOVEMENT_MS))
do_click(); // Handle the click
else
wait_for_unclick = true;
Expand Down Expand Up @@ -1104,11 +1105,15 @@ void MarlinUI::init() {

int8_t fullSteps = encoderDiff / epps;
if (fullSteps != 0) {
next_encoder_enable_ms = ms + BLOCK_CLICK_AFTER_MOVEMENT_MS;
last_encoder_movement_ms = ms;
encoderDiff -= fullSteps * epps;
if (can_encode() && !lcd_clicked)
encoderPosition += (fullSteps * encoderMultiplier);
}
} else if (encoderDiff != 0 && ELAPSED(ms, last_encoder_movement_ms + RESET_ENCODER_AFTER_MS)){
// Reset encoder substeps after a while.
// This solves the issue of the haptic ticks of some encoders sliding a fraction of a fullStep after a while .
encoderDiff = 0;
}

if (encoderPastThreshold || lcd_clicked) {
Expand Down Expand Up @@ -1426,8 +1431,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);
Expand Down

0 comments on commit 9db30f9

Please sign in to comment.