From 548ed0eb31b4b371eef4c3eeead3e1d8a88fc325 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 24 Apr 2023 10:42:09 +0200 Subject: [PATCH 1/6] Convert to native binary literals --- Firmware/lcd.cpp | 206 +++++++++++++++++++++++------------------------ 1 file changed, 103 insertions(+), 103 deletions(-) diff --git a/Firmware/lcd.cpp b/Firmware/lcd.cpp index b1b15176d..cc640a2ca 100644 --- a/Firmware/lcd.cpp +++ b/Firmware/lcd.cpp @@ -810,114 +810,114 @@ void lcd_buttons_update(void) // Custom character data const uint8_t lcd_chardata_bedTemp[8] PROGMEM = { - B00000, - B11111, - B10101, - B10001, - B10101, - B11111, - B00000, - B00000}; //thanks Sonny Mounicou + 0b00000, + 0b11111, + 0b10101, + 0b10001, + 0b10101, + 0b11111, + 0b00000, + 0b00000}; //thanks Sonny Mounicou const uint8_t lcd_chardata_degree[8] PROGMEM = { - B01100, - B10010, - B10010, - B01100, - B00000, - B00000, - B00000, - B00000}; + 0b01100, + 0b10010, + 0b10010, + 0b01100, + 0b00000, + 0b00000, + 0b00000, + 0b00000}; const uint8_t lcd_chardata_thermometer[8] PROGMEM = { - B00100, - B01010, - B01010, - B01010, - B01010, - B10001, - B10001, - B01110}; + 0b00100, + 0b01010, + 0b01010, + 0b01010, + 0b01010, + 0b10001, + 0b10001, + 0b01110}; const uint8_t lcd_chardata_uplevel[8] PROGMEM = { - B00100, - B01110, - B11111, - B00100, - B11100, - B00000, - B00000, - B00000}; //thanks joris + 0b00100, + 0b01110, + 0b11111, + 0b00100, + 0b11100, + 0b00000, + 0b00000, + 0b00000}; //thanks joris const uint8_t lcd_chardata_refresh[8] PROGMEM = { - B00000, - B00110, - B11001, - B11000, - B00011, - B10011, - B01100, - B00000}; //thanks joris + 0b00000, + 0b00110, + 0b11001, + 0b11000, + 0b00011, + 0b10011, + 0b01100, + 0b00000}; //thanks joris const uint8_t lcd_chardata_folder[8] PROGMEM = { - B00000, - B11100, - B11111, - B10001, - B10001, - B11111, - B00000, - B00000}; //thanks joris + 0b00000, + 0b11100, + 0b11111, + 0b10001, + 0b10001, + 0b11111, + 0b00000, + 0b00000}; //thanks joris /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = { - B11100, - B10000, - B11000, - B10111, - B00101, - B00110, - B00101, - B00000};*/ //thanks Sonny Mounicou + 0b11100, + 0b10000, + 0b11000, + 0b10111, + 0b00101, + 0b00110, + 0b00101, + 0b00000};*/ //thanks Sonny Mounicou /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = { - B11100, - B10100, - B11000, - B10100, - B00000, - B00111, - B00010, - B00010};*/ + 0b11100, + 0b10100, + 0b11000, + 0b10100, + 0b00000, + 0b00111, + 0b00010, + 0b00010};*/ /*const uint8_t lcd_chardata_feedrate[8] PROGMEM = { - B01100, - B10011, - B00000, - B01100, - B10011, - B00000, - B01100, - B10011};*/ + 0b01100, + 0b10011, + 0b00000, + 0b01100, + 0b10011, + 0b00000, + 0b01100, + 0b10011};*/ const uint8_t lcd_chardata_feedrate[8] PROGMEM = { - B00000, - B00100, - B10010, - B01001, - B10010, - B00100, - B00000, - B00000}; + 0b00000, + 0b00100, + 0b10010, + 0b01001, + 0b10010, + 0b00100, + 0b00000, + 0b00000}; const uint8_t lcd_chardata_clock[8] PROGMEM = { - B00000, - B01110, - B10011, - B10101, - B10001, - B01110, - B00000, - B00000}; //thanks Sonny Mounicou + 0b00000, + 0b01110, + 0b10011, + 0b10101, + 0b10001, + 0b01110, + 0b00000, + 0b00000}; //thanks Sonny Mounicou void lcd_set_custom_characters(void) { @@ -932,23 +932,23 @@ void lcd_set_custom_characters(void) } const uint8_t lcd_chardata_arr2down[8] PROGMEM = { - B00000, - B00000, - B10001, - B01010, - B00100, - B10001, - B01010, - B00100}; + 0b00000, + 0b00000, + 0b10001, + 0b01010, + 0b00100, + 0b10001, + 0b01010, + 0b00100}; const uint8_t lcd_chardata_confirm[8] PROGMEM = { - B00000, - B00001, - B00011, - B10110, - B11100, - B01000, - B00000}; + 0b00000, + 0b00001, + 0b00011, + 0b10110, + 0b11100, + 0b01000, + 0b00000}; void lcd_set_custom_characters_nextpage(void) { From 17e085040c977d54423c20724b4aaec6f0f832b8 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 24 Apr 2023 10:44:48 +0200 Subject: [PATCH 2/6] Add missing line to custom character Shouldn't make a difference since the array is already defined as 8B long, but I just found it weird and surprising when I noticed only 7 lines were defined --- Firmware/lcd.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Firmware/lcd.cpp b/Firmware/lcd.cpp index cc640a2ca..2027b84da 100644 --- a/Firmware/lcd.cpp +++ b/Firmware/lcd.cpp @@ -948,6 +948,7 @@ const uint8_t lcd_chardata_confirm[8] PROGMEM = { 0b10110, 0b11100, 0b01000, + 0b00000, 0b00000}; void lcd_set_custom_characters_nextpage(void) From dd16cf45259981bd8f93513de5ad877099681e65 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 24 Apr 2023 09:13:17 +0200 Subject: [PATCH 3/6] Remove Binary.h include --- Firmware/lcd.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Firmware/lcd.cpp b/Firmware/lcd.cpp index 2027b84da..4a88f7ea6 100644 --- a/Firmware/lcd.cpp +++ b/Firmware/lcd.cpp @@ -10,7 +10,6 @@ #include "Configuration.h" #include "pins.h" -#include #include #include "Marlin.h" #include "fastio.h" From ea101f49d4aa3c66da90dc630fde1a7f642d78bc Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Sun, 23 Apr 2023 22:44:38 +0200 Subject: [PATCH 4/6] `lcd_buttons` is actually `lcd_click_trigger` Flash: -4B SRAM: 0B --- Firmware/lcd.cpp | 4 ++-- Firmware/lcd.h | 22 ++++------------------ 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/Firmware/lcd.cpp b/Firmware/lcd.cpp index b1b15176d..36e58eb59 100644 --- a/Firmware/lcd.cpp +++ b/Firmware/lcd.cpp @@ -635,7 +635,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 @@ -776,7 +776,7 @@ 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; diff --git a/Firmware/lcd.h b/Firmware/lcd.h index aedfc38d3..13377da70 100644 --- a/Firmware/lcd.h +++ b/Firmware/lcd.h @@ -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; @@ -156,20 +155,7 @@ private: 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< Date: Sun, 23 Apr 2023 23:21:24 +0200 Subject: [PATCH 5/6] 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 From c339711025af7c16719da30242f00ef9c7a63ecd Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 24 Apr 2023 09:15:31 +0200 Subject: [PATCH 6/6] Make the encrot table balanced It won't change much. It is not possible to determine in which direction the encoder spun when two steps are made, so just use + or - 2. --- Firmware/lcd.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Firmware/lcd.cpp b/Firmware/lcd.cpp index e6a2d4c40..13d8c7106 100644 --- a/Firmware/lcd.cpp +++ b/Firmware/lcd.cpp @@ -786,8 +786,8 @@ void lcd_buttons_update(void) static const int8_t encrot_table[] PROGMEM = { 0, -1, 1, 2, 1, 0, 2, -1, - -1, 2, 0, 1, - 2, 1, -1, 0, + -1, -2, 0, 1, + -2, 1, -1, 0, }; static uint8_t enc_bits_old = 0;