Skip to content

Commit

Permalink
Merge pull request MarlinFirmware#4167 from leptun/lcd_encoder_improv…
Browse files Browse the repository at this point in the history
…ements

Lcd encoder improvements
  • Loading branch information
leptun authored Apr 24, 2023
2 parents 3692a24 + c339711 commit a354aad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 42 deletions.
34 changes: 18 additions & 16 deletions Firmware/lcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ uint8_t lcd_draw_update = 2;
int16_t lcd_encoder = 0;
static int8_t lcd_encoder_diff = 0;

uint8_t lcd_buttons = 0;
uint8_t lcd_click_trigger = 0;
uint8_t lcd_update_enabled = 1;
static bool lcd_backlight_wake_trigger; // Flag set by interrupt when the knob is pressed or rotated

Expand Down Expand Up @@ -750,7 +750,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)) {
Expand All @@ -775,32 +774,35 @@ void lcd_buttons_update(void)
lcd_button_pressed = 0; // Reset to prevent double triggering
if (!lcd_long_press_active)
{ //button released before long press gets activated
lcd_buttons |= EN_C; // This flag is reset when the event is consumed
lcd_click_trigger = 1; // This flag is reset when the event is consumed
}
lcd_backlight_wake_trigger = true; // flag event, knob pressed
lcd_long_press_active = 0;
}
}

//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;
}
}

Expand Down
30 changes: 4 additions & 26 deletions Firmware/lcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ extern uint8_t lcd_draw_update;

extern int16_t lcd_encoder;

//the last checked lcd_buttons in a bit array.
extern uint8_t lcd_buttons;
extern uint8_t lcd_click_trigger;

extern uint8_t lcd_update_enabled;

Expand Down Expand Up @@ -156,20 +155,7 @@ class LcdUpdateDisabler
bool m_updateEnabled;
};


////////////////////////////////////
// Setup button and encode mappings for each panel (into 'lcd_buttons' variable
//
// This is just to map common functions (across different panels) onto the same
// macro name. The mapping is independent of whether the button is directly connected or
// via a shift/i2c register.

#define BLEN_B 1
#define BLEN_A 0
#define EN_B (1<<BLEN_B) // The two encoder pins are connected through BTN_EN1 and BTN_EN2
#define EN_A (1<<BLEN_A)
#define BLEN_C 2
#define EN_C (1<<BLEN_C)

//! @brief Was button clicked?
//!
Expand All @@ -180,17 +166,9 @@ class LcdUpdateDisabler
//!
//! @retval 0 button was not clicked
//! @retval 1 button was clicked
#define LCD_CLICKED (lcd_buttons&EN_C)

////////////////////////
// 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
#define LCD_CLICKED (lcd_click_trigger)

////////////////////////////////////

//Custom characters defined in the first 8 characters of the LCD
#define LCD_STR_BEDTEMP "\x00"
Expand All @@ -212,7 +190,7 @@ extern void lcd_set_custom_characters_nextpage(void);
//! @brief Consume click and longpress event
inline void lcd_consume_click()
{
lcd_buttons = 0;
lcd_click_trigger = 0;
lcd_longpress_trigger = 0;
}

Expand Down

0 comments on commit a354aad

Please sign in to comment.