String interpolation for 2-digit numbers (#18998)

This commit is contained in:
Scott Lahteine 2020-08-12 16:46:51 -05:00 committed by GitHub
parent ca4eaf92b4
commit ee28a10795
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 22 deletions

View file

@ -353,7 +353,7 @@
* *
*/ */
#if ENABLED(NUMBER_TOOLS_FROM_0) #if ENABLED(NUMBER_TOOLS_FROM_0)
#define LCD_FIRST_TOOL '0' #define LCD_FIRST_TOOL 0
#define LCD_STR_N0 "0" #define LCD_STR_N0 "0"
#define LCD_STR_N1 "1" #define LCD_STR_N1 "1"
#define LCD_STR_N2 "2" #define LCD_STR_N2 "2"
@ -363,7 +363,7 @@
#define LCD_STR_N6 "6" #define LCD_STR_N6 "6"
#define LCD_STR_N7 "7" #define LCD_STR_N7 "7"
#else #else
#define LCD_FIRST_TOOL '1' #define LCD_FIRST_TOOL 1
#define LCD_STR_N0 "1" #define LCD_STR_N0 "1"
#define LCD_STR_N1 "2" #define LCD_STR_N1 "2"
#define LCD_STR_N2 "3" #define LCD_STR_N2 "3"

View file

@ -44,23 +44,29 @@ lcd_uint_t lcd_put_u8str_ind_P(PGM_P const pstr, const int8_t ind, PGM_P const i
if (ch == '=' || ch == '~' || ch == '*') { if (ch == '=' || ch == '~' || ch == '*') {
if (ind >= 0) { if (ind >= 0) {
if (ch == '*') { lcd_put_wchar('E'); n--; } if (ch == '*') { lcd_put_wchar('E'); n--; }
if (n) { lcd_put_wchar(ind + ((ch == '=') ? '0' : LCD_FIRST_TOOL)); n--; } if (n) {
int8_t inum = ind + ((ch == '=') ? 0 : LCD_FIRST_TOOL);
if (inum >= 10) {
lcd_put_wchar('0' + (inum / 10)); n--;
inum %= 10;
}
if (n) { lcd_put_wchar('0' + inum); n--; }
}
} }
else { else {
PGM_P const b = ind == -2 ? GET_TEXT(MSG_CHAMBER) : GET_TEXT(MSG_BED); PGM_P const b = ind == -2 ? GET_TEXT(MSG_CHAMBER) : GET_TEXT(MSG_BED);
n -= lcd_put_u8str_max_P(b, n * (MENU_FONT_WIDTH)) / (MENU_FONT_WIDTH); n -= lcd_put_u8str_max_P(b, n * (MENU_FONT_WIDTH)) / (MENU_FONT_WIDTH);
} }
if (n) n -= lcd_put_u8str_max_P((PGM_P)p, n * (MENU_FONT_WIDTH)) / (MENU_FONT_WIDTH); if (n) n -= lcd_put_u8str_max_P((PGM_P)p, n * (MENU_FONT_WIDTH)) / (MENU_FONT_WIDTH);
continue;
} }
else if (ch == '$' && inStr) { else if (ch == '$' && inStr) {
n -= lcd_put_u8str_max_P(inStr, n * (MENU_FONT_WIDTH)) / (MENU_FONT_WIDTH); n -= lcd_put_u8str_max_P(inStr, n * (MENU_FONT_WIDTH)) / (MENU_FONT_WIDTH);
continue;
} }
else {
lcd_put_wchar(ch); lcd_put_wchar(ch);
n--; n--;
} }
}
return n; return n;
} }

View file

@ -86,33 +86,32 @@ void TFT_String::set() {
uint8_t read_byte(uint8_t *byte) { return *byte; } uint8_t read_byte(uint8_t *byte) { return *byte; }
void TFT_String::add(uint8_t *string, uint8_t index, uint8_t *itemString) { void TFT_String::add(uint8_t *string, int8_t index, uint8_t *itemString) {
uint8_t character;
wchar_t wchar; wchar_t wchar;
while (*string) { while (*string) {
string = get_utf8_value_cb(string, read_byte, &wchar); string = get_utf8_value_cb(string, read_byte, &wchar);
if (wchar > 255) if (wchar > 255) wchar |= 0x0080;
wchar |= 0x0080; uint8_t ch = uint8_t(wchar & 0x00FF);
character = (uint8_t) (wchar & 0x00FF);
if (character == '=' || character == '~' || character == '*') { if (ch == '=' || ch == '~' || ch == '*') {
if (index >= 0) { if (index >= 0) {
if (character == '*') int8_t inum = index + ((ch == '=') ? 0 : LCD_FIRST_TOOL);
add_character('E'); if (ch == '*') add_character('E');
add_character(index + ((character == '=') ? '0' : LCD_FIRST_TOOL)); if (inum >= 10) { add_character('0' + (inum / 10)); inum %= 10; }
add_character('0' + inum);
} }
else { else {
add(index == -2 ? GET_TEXT(MSG_CHAMBER) : GET_TEXT(MSG_BED)); add(index == -2 ? GET_TEXT(MSG_CHAMBER) : GET_TEXT(MSG_BED));
} }
continue; continue;
} }
else if (character == '$' && itemString) { else if (ch == '$' && itemString) {
add(itemString); add(itemString);
continue; continue;
} }
add_character(character); add_character(ch);
} }
eol(); eol();
} }

View file

@ -86,11 +86,11 @@ class TFT_String {
static void set(); static void set();
static void add(uint8_t character) { add_character(character); eol(); } static void add(uint8_t character) { add_character(character); eol(); }
static void add(uint8_t *string) { while (*string) { add_character(*string++); } eol(); } static void add(uint8_t *string) { while (*string) { add_character(*string++); } eol(); }
static void add(uint8_t *string, uint8_t index, uint8_t *itemString = NULL); static void add(uint8_t *string, int8_t index, uint8_t *itemString = NULL);
static void set(uint8_t *string) { set(); add(string); }; static void set(uint8_t *string) { set(); add(string); };
static void set(uint8_t *string, uint8_t index, const char *itemString = NULL) { set(); add(string, index, (uint8_t *)itemString); }; static void set(uint8_t *string, int8_t index, const char *itemString = NULL) { set(); add(string, index, (uint8_t *)itemString); };
static inline void set(const char *string) { set((uint8_t *)string); } static inline void set(const char *string) { set((uint8_t *)string); }
static inline void set(const char *string, uint8_t index, const char *itemString = NULL) { set((uint8_t *)string, index, itemString); } static inline void set(const char *string, int8_t index, const char *itemString = NULL) { set((uint8_t *)string, index, itemString); }
static inline void add(const char *string) { add((uint8_t *)string); } static inline void add(const char *string) { add((uint8_t *)string); }
static void trim(uint8_t character = 0x20); static void trim(uint8_t character = 0x20);