Skip to content

Commit b925130

Browse files
authored
📺 Fix and optimize FTDI Eve Touch Interface (#22427)
1 parent 78be63b commit b925130

File tree

8 files changed

+66
-52
lines changed

8 files changed

+66
-52
lines changed

Marlin/src/lcd/extui/ftdi_eve_touch_ui/ftdi_eve_lib/basic/commands.cpp

+22-4
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,7 @@ bool CLCD::CommandFifo::has_fault() {
902902
}
903903

904904
#if FTDI_API_LEVEL == 800
905+
905906
void CLCD::CommandFifo::start() {
906907
if (command_write_ptr == 0xFFFFFFFFul) {
907908
command_write_ptr = mem_read_32(REG::CMD_WRITE) & 0x0FFF;
@@ -979,12 +980,13 @@ template <class T> bool CLCD::CommandFifo::_write_unaligned(T data, uint16_t len
979980

980981
template <class T> bool CLCD::CommandFifo::write(T data, uint16_t len) {
981982
const uint8_t padding = MULTIPLE_OF_4(len) - len;
982-
983-
uint8_t pad_bytes[] = {0, 0, 0, 0};
983+
const uint8_t pad_bytes[] = { 0, 0, 0, 0 };
984984
return _write_unaligned(data, len) &&
985985
_write_unaligned(pad_bytes, padding);
986986
}
987-
#else
987+
988+
#else // FTDI_API_LEVEL != 800 ...
989+
988990
void CLCD::CommandFifo::start() {
989991
}
990992

@@ -1042,13 +1044,29 @@ template <class T> bool CLCD::CommandFifo::write(T data, uint16_t len) {
10421044
mem_write_bulk(REG::CMDB_WRITE, data, len, padding);
10431045
return true;
10441046
}
1045-
#endif
1047+
1048+
#endif // ... FTDI_API_LEVEL != 800
10461049

10471050
template bool CLCD::CommandFifo::write(const void*, uint16_t);
10481051
template bool CLCD::CommandFifo::write(progmem_str, uint16_t);
10491052

10501053
// CO_PROCESSOR COMMANDS
10511054

1055+
void CLCD::CommandFifo::str(const char * data, size_t maxlen) {
1056+
// Write the string without the terminating '\0'
1057+
const size_t len = strnlen(data, maxlen);
1058+
write(data, len);
1059+
1060+
// If padding was added by the previous write, then
1061+
// the string is terminated. Otherwise write four
1062+
// more zeros.
1063+
const uint8_t padding = MULTIPLE_OF_4(len) - len;
1064+
if (padding == 0) {
1065+
const uint8_t pad_bytes[] = {0, 0, 0, 0};
1066+
write(pad_bytes, 4);
1067+
}
1068+
}
1069+
10521070
void CLCD::CommandFifo::str(const char * data) {
10531071
write(data, strlen(data)+1);
10541072
}

Marlin/src/lcd/extui/ftdi_eve_touch_ui/ftdi_eve_lib/basic/commands.h

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ class CLCD::CommandFifo {
248248
void keys (int16_t x, int16_t y, int16_t w, int16_t h, int16_t font, uint16_t options);
249249

250250
// Sends the string portion of text, button, toggle and keys.
251+
void str (const char * data, size_t maxlen);
251252
void str (const char * data);
252253
void str (progmem_str data);
253254

Marlin/src/lcd/extui/ftdi_eve_touch_ui/ftdi_eve_lib/extended/text_box.cpp

+24-26
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,31 @@ namespace FTDI {
2929
* be broken so that the display width is less than w. The line will also
3030
* be broken after a '\n'. Returns the display width of the line.
3131
*/
32-
static uint16_t find_line_break(const FontMetrics &utf8_fm, const CLCD::FontMetrics &clcd_fm, const uint16_t w, const char *str, const char *&end, bool use_utf8) {
33-
const char *p = str;
34-
end = str;
32+
static uint16_t find_line_break(const FontMetrics &utf8_fm, const CLCD::FontMetrics &clcd_fm, const uint16_t w, const char *start, const char *&end, bool use_utf8) {
33+
const char *p = start;
34+
end = start;
3535
uint16_t lw = 0, result = 0;
3636
for (;;) {
3737
const char *next = p;
38-
utf8_char_t c = get_utf8_char_and_inc(next);
38+
const utf8_char_t c = get_utf8_char_and_inc(next);
3939
// Decide whether to break the string at this location
4040
if (c == '\n' || c == '\0' || c == ' ') {
4141
end = p;
4242
result = lw;
4343
}
4444
if (c == '\n' || c == '\0') break;
45-
// Now add the length of the current character to the tally.
46-
lw += use_utf8 ? utf8_fm.get_char_width(c) : clcd_fm.char_widths[(uint8_t)c];
45+
// Measure the next character
46+
const uint16_t cw = use_utf8 ? utf8_fm.get_char_width(c) : clcd_fm.char_widths[(uint8_t)c];
4747
// Stop processing once string exceeds the display width
48-
if (lw >= w) {
49-
if (end == str) {
50-
end = p;
51-
result = lw;
52-
}
53-
break;
54-
}
48+
if (lw + cw > w) break;
49+
// Now add the length of the current character to the tally.
50+
lw += cw;
5551
p = next;
5652
}
53+
if (end == start) {
54+
end = p;
55+
result = lw;
56+
}
5757
return result;
5858
}
5959

@@ -66,12 +66,13 @@ namespace FTDI {
6666
const uint16_t wrap_width = width;
6767
width = height = 0;
6868
for (;;) {
69-
uint16_t line_width = find_line_break(utf8_fm, clcd_fm, wrap_width, line_start, line_end, use_utf8);
69+
const uint16_t line_width = find_line_break(utf8_fm, clcd_fm, wrap_width, line_start, line_end, use_utf8);
70+
if (line_end == line_start) break;
7071
width = max(width, line_width);
7172
height += utf8_fm.get_height();
7273
line_start = line_end;
73-
if (line_start[0] == '\n' || line_start[0] == ' ') line_start++;
74-
if (line_start[0] == '\0') break;
74+
if (*line_start == '\n' || *line_start == ' ') line_start++;
75+
if (*line_start == '\0') break;
7576
}
7677
}
7778

@@ -108,28 +109,25 @@ namespace FTDI {
108109
const char *line_start = str, *line_end;
109110
for (;;) {
110111
find_line_break(utf8_fm, clcd_fm, w, line_start, line_end, use_utf8);
112+
if (line_end == line_start) break;
111113

112114
const size_t line_len = line_end - line_start;
113115
if (line_len) {
114-
char line[line_len + 1];
115-
strncpy(line, line_start, line_len);
116-
line[line_len] = 0;
117-
118116
#if ENABLED(TOUCH_UI_USE_UTF8)
119-
if (use_utf8) {
120-
draw_utf8_text(cmd, x + dx, y + dy, line, utf8_fm.fs, options & ~(OPT_CENTERY | OPT_BOTTOMY));
121-
} else
117+
if (use_utf8)
118+
draw_utf8_text(cmd, x + dx, y + dy, line_start, utf8_fm.fs, options & ~(OPT_CENTERY | OPT_BOTTOMY), line_len);
119+
else
122120
#endif
123121
{
124122
cmd.CLCD::CommandFifo::text(x + dx, y + dy, font, options & ~(OPT_CENTERY | OPT_BOTTOMY));
125-
cmd.CLCD::CommandFifo::str(line);
123+
cmd.CLCD::CommandFifo::str(line_start, line_len);
126124
}
127125
}
128126
y += utf8_fm.get_height();
129127

130128
line_start = line_end;
131-
if (line_start[0] == '\n' || line_start[0] == ' ') line_start++;
132-
if (line_start[0] == '\0') break;
129+
if (*line_start == '\n' || *line_start == ' ') line_start++;
130+
if (*line_start == '\0') break;
133131
}
134132
}
135133

Marlin/src/lcd/extui/ftdi_eve_touch_ui/ftdi_eve_lib/extended/unicode/unicode.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@
9595
* fs - A scaling object used to specify the font size.
9696
*/
9797

98-
static uint16_t render_utf8_text(CommandProcessor* cmd, int x, int y, const char *str, font_size_t fs) {
98+
static uint16_t render_utf8_text(CommandProcessor* cmd, int x, int y, const char *str, font_size_t fs, size_t maxlen=SIZE_MAX) {
9999
const int start_x = x;
100-
while (*str) {
100+
while (*str && maxlen--) {
101101
const utf8_char_t c = get_utf8_char_and_inc(str);
102102
#ifdef TOUCH_UI_UTF8_CYRILLIC_CHARSET
103103
CyrillicCharSet::render_glyph(cmd, x, y, fs, c) ||
@@ -185,8 +185,8 @@
185185
* Returns: A width in pixels
186186
*/
187187

188-
uint16_t FTDI::get_utf8_text_width(const char *str, font_size_t fs) {
189-
return render_utf8_text(nullptr, 0, 0, str, fs);
188+
uint16_t FTDI::get_utf8_text_width(const char *str, font_size_t fs, size_t maxlen) {
189+
return render_utf8_text(nullptr, 0, 0, str, fs, maxlen);
190190
}
191191

192192
uint16_t FTDI::get_utf8_text_width(progmem_str pstr, font_size_t fs) {
@@ -210,24 +210,25 @@
210210
*
211211
* options - Text alignment options (i.e. OPT_CENTERX, OPT_CENTERY, OPT_CENTER or OPT_RIGHTX)
212212
*
213+
* maxlen - Maximum characters to draw
213214
*/
214215

215-
void FTDI::draw_utf8_text(CommandProcessor& cmd, int x, int y, const char *str, font_size_t fs, uint16_t options) {
216+
void FTDI::draw_utf8_text(CommandProcessor& cmd, int x, int y, const char *str, font_size_t fs, uint16_t options, size_t maxlen) {
216217
cmd.cmd(SAVE_CONTEXT());
217218
cmd.cmd(BITMAP_TRANSFORM_A(fs.get_coefficient()));
218219
cmd.cmd(BITMAP_TRANSFORM_E(fs.get_coefficient()));
219220
cmd.cmd(BEGIN(BITMAPS));
220221

221222
// Apply alignment options
222223
if (options & OPT_CENTERX)
223-
x -= get_utf8_text_width(str, fs) / 2;
224+
x -= get_utf8_text_width(str, fs, maxlen) / 2;
224225
else if (options & OPT_RIGHTX)
225-
x -= get_utf8_text_width(str, fs);
226+
x -= get_utf8_text_width(str, fs, maxlen);
226227
if (options & OPT_CENTERY)
227228
y -= fs.get_height()/2;
228229

229230
// Render the text
230-
render_utf8_text(&cmd, x, y, str, fs);
231+
render_utf8_text(&cmd, x, y, str, fs, maxlen);
231232
cmd.cmd(RESTORE_CONTEXT());
232233
}
233234

Marlin/src/lcd/extui/ftdi_eve_touch_ui/ftdi_eve_lib/extended/unicode/unicode.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ namespace FTDI {
6767

6868
uint16_t get_utf8_char_width(utf8_char_t, font_size_t);
6969
uint16_t get_utf8_text_width(progmem_str, font_size_t);
70-
uint16_t get_utf8_text_width(const char *, font_size_t);
70+
uint16_t get_utf8_text_width(const char *, font_size_t, size_t maxlen=SIZE_MAX);
7171

7272
void draw_utf8_text(CommandProcessor&, int x, int y, progmem_str, font_size_t, uint16_t options = 0);
73-
void draw_utf8_text(CommandProcessor&, int x, int y, const char *, font_size_t, uint16_t options = 0);
73+
void draw_utf8_text(CommandProcessor&, int x, int y, const char *, font_size_t, uint16_t options = 0, size_t maxlen=SIZE_MAX);
7474

7575
// Similar to CLCD::FontMetrics, but can be used with UTF8 encoded strings.
7676

Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/about_screen.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ void AboutScreen::onRedraw(draw_mode_t) {
7676
#endif
7777
, OPT_CENTER, font_xlarge
7878
);
79-
cmd.tag(3);
79+
#if BOTH(TOUCH_UI_DEVELOPER_MENU, FTDI_DEVELOPER_MENU)
80+
cmd.tag(3);
81+
#endif
8082
draw_text_box(cmd, FW_VERS_POS,
8183
#ifdef TOUCH_UI_VERSION
8284
F(TOUCH_UI_VERSION)
@@ -89,7 +91,7 @@ void AboutScreen::onRedraw(draw_mode_t) {
8991
draw_text_box(cmd, LICENSE_POS, GET_TEXT_F(MSG_LICENSE), OPT_CENTER, font_tiny);
9092

9193
cmd.font(font_medium);
92-
#if ENABLED(PRINTCOUNTER) && defined(FTDI_STATISTICS_SCREEN)
94+
#if BOTH(PRINTCOUNTER, FTDI_STATISTICS_SCREEN)
9395
cmd.colors(normal_btn)
9496
.tag(2).button(STATS_POS, GET_TEXT_F(MSG_INFO_STATS_MENU));
9597
#endif
@@ -100,10 +102,10 @@ void AboutScreen::onRedraw(draw_mode_t) {
100102
bool AboutScreen::onTouchEnd(uint8_t tag) {
101103
switch (tag) {
102104
case 1: GOTO_PREVIOUS(); break;
103-
#if ENABLED(PRINTCOUNTER) && defined(FTDI_STATISTICS_SCREEN)
105+
#if BOTH(PRINTCOUNTER, FTDI_STATISTICS_SCREEN)
104106
case 2: GOTO_SCREEN(StatisticsScreen); break;
105107
#endif
106-
#if ENABLED(TOUCH_UI_DEVELOPER_MENU) && defined(FTDI_DEVELOPER_MENU)
108+
#if BOTH(TOUCH_UI_DEVELOPER_MENU, FTDI_DEVELOPER_MENU)
107109
case 3: GOTO_SCREEN(DeveloperMenu); break;
108110
#endif
109111
default: return false;

Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/confirm_user_request_alert_box.cpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,12 @@ bool ConfirmUserRequestAlertBox::onTouchEnd(uint8_t tag) {
5353
}
5454
}
5555

56-
void ConfirmUserRequestAlertBox::onIdle() {
57-
if (!ExtUI::awaitingUserConfirm()) {
58-
hide();
59-
}
60-
}
61-
6256
void ConfirmUserRequestAlertBox::show(const char *msg) {
6357
drawMessage(msg);
6458
storeBackground();
6559
screen_data.AlertDialogBox.isError = false;
66-
GOTO_SCREEN(ConfirmUserRequestAlertBox);
60+
if (!AT_SCREEN(ConfirmUserRequestAlertBox))
61+
GOTO_SCREEN(ConfirmUserRequestAlertBox);
6762
}
6863

6964
void ConfirmUserRequestAlertBox::hide() {

Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/confirm_user_request_alert_box.h

-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,4 @@ class ConfirmUserRequestAlertBox : public AlertDialogBox {
3131
static bool onTouchEnd(uint8_t);
3232
static void hide();
3333
static void show(const char*);
34-
static void onIdle();
3534
};

0 commit comments

Comments
 (0)