Skip to content

Commit

Permalink
combine ab debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Mar 10, 2024
1 parent 0467ea4 commit 00b8733
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1402,31 +1402,32 @@ void MarlinUI::init() {

#if ENCODER_DEBOUNCE_MS

static enc_t enc;
static enc_t old_live;
static enc_t enc, old_live;
static millis_t bounce_ms;

static millis_t en_A_bounce_ms;
if (old_live.a != live_enc.a) en_A_bounce_ms = now + (ENCODER_DEBOUNCE_MS);
else if (ELAPSED(now, en_A_bounce_ms)) enc.a = live_enc.a;

static millis_t en_B_bounce_ms;
if (old_live.b != live_enc.b) en_B_bounce_ms = now + (ENCODER_DEBOUNCE_MS);
else if (ELAPSED(now, en_B_bounce_ms)) enc.b = live_enc.b;
if (old_live.a != live_enc.a || old_live.b != live_enc.b) {
bounce_ms = now + (ENCODER_DEBOUNCE_MS); // Wait for states to settle
old_live = live_enc;
return 0;
}

old_live = live_enc;
if (ELAPSED(now, bounce_ms)) {
enc.a = live_enc.a;
enc.b = live_enc.b;
}

#else

const enc_t &enc = live_enc;

#endif

static uint8_t old_pos;
const uint8_t pos = (enc.a ^ enc.b) | (enc.a << 1); // 0:00 1:10 2:11 3:01
static uint8_t old_ab;
const uint8_t ab = (enc.a ^ enc.b) | (enc.a << 1); // 0:00 1:10 2:11 3:01
int8_t delta = 0;
if (pos != old_pos) {
delta = (pos - old_pos + 4 + 1) % 4 - 1;
old_pos = pos;
if (ab != old_ab) {
delta = (ab - old_ab + 4 + 1) % 4 - 1;
old_ab = ab;

static int8_t last_dir;
if (delta == 2) delta = last_dir * 2;
Expand Down

0 comments on commit 00b8733

Please sign in to comment.