From 32e68c33b476042c7dc52fefc3a685b7cc90b429 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Sun, 23 Apr 2023 23:21:24 +0200 Subject: [PATCH] lcd encoder: use lookup table Also handle the scenario where the encoder moves two steps in a single lcd_buttons_update cycle. Flash: -46B SRAM: 0B --- Firmware/lcd.cpp | 30 ++++++++++++++++-------------- Firmware/lcd.h | 8 -------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/Firmware/lcd.cpp b/Firmware/lcd.cpp index 36e58eb59..e6a2d4c40 100644 --- a/Firmware/lcd.cpp +++ b/Firmware/lcd.cpp @@ -751,7 +751,6 @@ void lcd_buttons_update(void) { static uint8_t lcd_long_press_active = 0; static uint8_t lcd_button_pressed = 0; - static uint8_t lcd_encoder_bits = 0; if (READ(BTN_ENC) == 0) { //button is pressed if (buttonBlanking.expired_cont(BUTTON_BLANKING_TIME)) { @@ -784,24 +783,27 @@ void lcd_buttons_update(void) } //manage encoder rotation - #define ENCODER_SPIN(_E1, _E2) switch (lcd_encoder_bits) { case _E1: lcd_encoder_diff++; break; case _E2: lcd_encoder_diff--; } - uint8_t enc = 0; - if (READ(BTN_EN1) == 0) enc |= B01; - if (READ(BTN_EN2) == 0) enc |= B10; - if (enc != lcd_encoder_bits) + static const int8_t encrot_table[] PROGMEM = { + 0, -1, 1, 2, + 1, 0, 2, -1, + -1, 2, 0, 1, + 2, 1, -1, 0, + }; + + static uint8_t enc_bits_old = 0; + uint8_t enc_bits = 0; + if (!READ(BTN_EN1)) enc_bits |= _BV(0); + if (!READ(BTN_EN2)) enc_bits |= _BV(1); + + if (enc_bits != enc_bits_old) { - switch (enc) - { - case encrot0: ENCODER_SPIN(encrot3, encrot1); break; - case encrot1: ENCODER_SPIN(encrot0, encrot2); break; - case encrot2: ENCODER_SPIN(encrot1, encrot3); break; - case encrot3: ENCODER_SPIN(encrot2, encrot0); break; - } + int8_t newDiff = pgm_read_byte(&encrot_table[(enc_bits_old << 2) | enc_bits]); + lcd_encoder_diff += newDiff; if (abs(lcd_encoder_diff) >= ENCODER_PULSES_PER_STEP) { lcd_backlight_wake_trigger = true; // flag event, knob rotated } - lcd_encoder_bits = enc; + enc_bits_old = enc_bits; } } diff --git a/Firmware/lcd.h b/Firmware/lcd.h index 13377da70..2a4682739 100644 --- a/Firmware/lcd.h +++ b/Firmware/lcd.h @@ -168,14 +168,6 @@ private: //! @retval 1 button was clicked #define LCD_CLICKED (lcd_click_trigger) -// Setup Rotary Encoder Bit Values (for two pin encoders to indicate movement) -// These values are independent of which pins are used for EN_A and EN_B indications -// The rotary encoder part is also independent to the chipset used for the LCD -#define encrot0 0 -#define encrot1 2 -#define encrot2 3 -#define encrot3 1 - //////////////////////////////////// //Custom characters defined in the first 8 characters of the LCD