optimisation: refactor menu_draw_P()

Add a way to only change the LCD column

Change in memory:
Flash: -90 bytes
SRAM: 0 bytes
This commit is contained in:
Guðni Már Gilbert 2023-01-14 14:58:04 +00:00 committed by DRracer
parent 730bb3708e
commit eeb5f3d50c
3 changed files with 42 additions and 12 deletions

View File

@ -329,13 +329,31 @@ void lcd_no_autoscroll(void)
}
#endif
void lcd_set_cursor(uint8_t col, uint8_t row)
/// @brief set the current LCD row
/// @param row LCD row number, ranges from 0 to LCD_HEIGHT - 1
static void FORCE_INLINE lcd_set_current_row(uint8_t row)
{
lcd_currline = min(row, LCD_HEIGHT - 1);
}
/// @brief Calculate the LCD row offset
/// @param row LCD row number, ranges from 0 to LCD_HEIGHT - 1
/// @return row offset which the LCD register understands
static uint8_t __attribute__((noinline)) lcd_get_row_offset(uint8_t row)
{
uint8_t row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
if (row >= LCD_HEIGHT)
row = LCD_HEIGHT - 1; // we count rows starting w/0
lcd_currline = row;
lcd_command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
return row_offsets[min(row, LCD_HEIGHT - 1)];
}
void lcd_set_cursor(uint8_t col, uint8_t row)
{
lcd_set_current_row(row);
lcd_command(LCD_SETDDRAMADDR | (col + lcd_get_row_offset(lcd_currline)));
}
void lcd_set_cursor_column(uint8_t col)
{
lcd_command(LCD_SETDDRAMADDR | (col + lcd_get_row_offset(lcd_currline)));
}
// Allows us to fill the first 8 CGRAM locations

View File

@ -37,6 +37,10 @@ extern void lcd_no_autoscroll(void);*/
extern void lcd_set_cursor(uint8_t col, uint8_t row);
/// @brief Change the cursor column position while preserving the current row position
/// @param col column number, ranges from 0 to LCD_WIDTH - 1
void lcd_set_cursor_column(uint8_t col);
extern void lcd_createChar_P(uint8_t, const uint8_t*);

View File

@ -435,13 +435,21 @@ static void menu_draw_P(char chr, const char* str, int16_t val);
template<>
void menu_draw_P<int16_t*>(char chr, const char* str, int16_t val)
{
int text_len = strlen_P(str);
if (text_len > 15) text_len = 15;
char spaces[LCD_WIDTH + 1] = {0};
memset(spaces,' ', LCD_WIDTH);
if (val <= -100) spaces[15 - text_len - 1] = 0;
else spaces[15 - text_len] = 0;
lcd_printf_P(menu_fmt_int3, chr, str, spaces, val);
// The LCD row position is controlled externally. We may only modify the column here
lcd_putc(chr);
uint8_t len = lcd_print_pad_P(str, LCD_WIDTH - 1);
lcd_set_cursor_column((LCD_WIDTH - 1) - len + 1);
lcd_putc(':');
// The value is right adjusted, set the cursor then render the value
if (val < 10) { // 1 digit
lcd_set_cursor_column(LCD_WIDTH - 1);
} else if (val < 100) { // 2 digits
lcd_set_cursor_column(LCD_WIDTH - 2);
} else { // 3 digits
lcd_set_cursor_column(LCD_WIDTH - 3);
}
lcd_print(val);
}
template<>