Skip to content

Commit

Permalink
🎨 Apply F() to UTF-8/MMU2 string put
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Dec 25, 2021
1 parent c3ae221 commit 9cf1c3c
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 115 deletions.
2 changes: 1 addition & 1 deletion Marlin/src/HAL/shared/progmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class __FlashStringHelper;
#define strrchr_P(str, c) strrchr((str), (c))
#endif
#ifndef strsep_P
#define strsep_P(strp, delim) strsep((strp), (delim))
#define strsep_P(pstr, delim) strsep((pstr), (delim))
#endif
#ifndef strspn_P
#define strspn_P(str, chrs) strspn((str), (chrs))
Expand Down
48 changes: 25 additions & 23 deletions Marlin/src/feature/mmu/mmu2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ MMU2 mmu2;
#define MMU_CMD_TIMEOUT 45000UL // 45s timeout for mmu commands (except P0)
#define MMU_P0_TIMEOUT 3000UL // Timeout for P0 command: 3seconds

#define MMU2_COMMAND(S) tx_str_P(PSTR(S "\n"))
#define MMU2_COMMAND(S) tx_str(F(S "\n"))

#if ENABLED(MMU_EXTRUDER_SENSOR)
uint8_t mmu_idl_sens = 0;
Expand Down Expand Up @@ -229,17 +229,17 @@ void MMU2::mmu_loop() {
if (cmd) {
if (WITHIN(cmd, MMU_CMD_T0, MMU_CMD_T0 + EXTRUDERS - 1)) {
// tool change
int filament = cmd - MMU_CMD_T0;
const int filament = cmd - MMU_CMD_T0;
DEBUG_ECHOLNPGM("MMU <= T", filament);
tx_printf_P(PSTR("T%d\n"), filament);
tx_printf(F("T%d\n"), filament);
TERN_(MMU_EXTRUDER_SENSOR, mmu_idl_sens = 1); // enable idler sensor, if any
state = 3; // wait for response
}
else if (WITHIN(cmd, MMU_CMD_L0, MMU_CMD_L0 + EXTRUDERS - 1)) {
// load
int filament = cmd - MMU_CMD_L0;
const int filament = cmd - MMU_CMD_L0;
DEBUG_ECHOLNPGM("MMU <= L", filament);
tx_printf_P(PSTR("L%d\n"), filament);
tx_printf(F("L%d\n"), filament);
state = 3; // wait for response
}
else if (cmd == MMU_CMD_C0) {
Expand All @@ -257,9 +257,9 @@ void MMU2::mmu_loop() {
}
else if (WITHIN(cmd, MMU_CMD_E0, MMU_CMD_E0 + EXTRUDERS - 1)) {
// eject filament
int filament = cmd - MMU_CMD_E0;
const int filament = cmd - MMU_CMD_E0;
DEBUG_ECHOLNPGM("MMU <= E", filament);
tx_printf_P(PSTR("E%d\n"), filament);
tx_printf(F("E%d\n"), filament);
state = 3; // wait for response
}
else if (cmd == MMU_CMD_R0) {
Expand All @@ -270,9 +270,9 @@ void MMU2::mmu_loop() {
}
else if (WITHIN(cmd, MMU_CMD_F0, MMU_CMD_F0 + EXTRUDERS - 1)) {
// filament type
int filament = cmd - MMU_CMD_F0;
const int filament = cmd - MMU_CMD_F0;
DEBUG_ECHOLNPGM("MMU <= F", filament, " ", cmd_arg);
tx_printf_P(PSTR("F%d %d\n"), filament, cmd_arg);
tx_printf(F("F%d %d\n"), filament, cmd_arg);
state = 3; // wait for response
}

Expand Down Expand Up @@ -356,13 +356,15 @@ void MMU2::mmu_loop() {
*/
bool MMU2::rx_start() {
// check for start message
return rx_str_P(PSTR("start\n"));
return rx_str(F("start\n"));
}

/**
* Check if the data received ends with the given string.
*/
bool MMU2::rx_str_P(const char *str) {
bool MMU2::rx_str(FSTR_P fstr) {
PGM_P pstr = FTOP(fstr);

uint8_t i = strlen(rx_buffer);

while (MMU2_SERIAL.available()) {
Expand All @@ -375,14 +377,14 @@ bool MMU2::rx_str_P(const char *str) {
}
rx_buffer[i] = '\0';

uint8_t len = strlen_P(str);
uint8_t len = strlen_P(pstr);

if (i < len) return false;

str += len;
pstr += len;

while (len--) {
char c0 = pgm_read_byte(str--), c1 = rx_buffer[i--];
char c0 = pgm_read_byte(pstr--), c1 = rx_buffer[i--];
if (c0 == c1) continue;
if (c0 == '\r' && c1 == '\n') continue; // match cr as lf
if (c0 == '\n' && c1 == '\r') continue; // match lf as cr
Expand All @@ -394,29 +396,29 @@ bool MMU2::rx_str_P(const char *str) {
/**
* Transfer data to MMU, no argument
*/
void MMU2::tx_str_P(const char *str) {
void MMU2::tx_str(FSTR_P fstr) {
clear_rx_buffer();
uint8_t len = strlen_P(str);
LOOP_L_N(i, len) MMU2_SERIAL.write(pgm_read_byte(str++));
PGM_P pstr = FTOP(fstr);
while (const char c = pgm_read_byte(pstr)) { MMU2_SERIAL.write(c); pstr++; }
prev_request = millis();
}

/**
* Transfer data to MMU, single argument
*/
void MMU2::tx_printf_P(const char *format, int argument = -1) {
void MMU2::tx_printf(FSTR_P format, int argument = -1) {
clear_rx_buffer();
uint8_t len = sprintf_P(tx_buffer, format, argument);
const uint8_t len = sprintf_P(tx_buffer, FTOP(format), argument);
LOOP_L_N(i, len) MMU2_SERIAL.write(tx_buffer[i]);
prev_request = millis();
}

/**
* Transfer data to MMU, two arguments
*/
void MMU2::tx_printf_P(const char *format, int argument1, int argument2) {
void MMU2::tx_printf(FSTR_P format, int argument1, int argument2) {
clear_rx_buffer();
uint8_t len = sprintf_P(tx_buffer, format, argument1, argument2);
const uint8_t len = sprintf_P(tx_buffer, FTOP(format), argument1, argument2);
LOOP_L_N(i, len) MMU2_SERIAL.write(tx_buffer[i]);
prev_request = millis();
}
Expand All @@ -433,7 +435,7 @@ void MMU2::clear_rx_buffer() {
* Check if we received 'ok' from MMU
*/
bool MMU2::rx_ok() {
if (rx_str_P(PSTR("ok\n"))) {
if (rx_str(F("ok\n"))) {
prev_P0_request = millis();
return true;
}
Expand Down Expand Up @@ -853,7 +855,7 @@ void MMU2::filament_runout() {
if (cmd == MMU_CMD_NONE && last_cmd == MMU_CMD_C0) {
if (present && !mmu2s_triggered) {
DEBUG_ECHOLNPGM("MMU <= 'A'");
tx_str_P(PSTR("A\n"));
tx_str(F("A\n"));
}
// Slowly spin the extruder during C0
else {
Expand Down
8 changes: 4 additions & 4 deletions Marlin/src/feature/mmu/mmu2.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ class MMU2 {
static bool eject_filament(const uint8_t index, const bool recover);

private:
static bool rx_str_P(const char *str);
static void tx_str_P(const char *str);
static void tx_printf_P(const char *format, const int argument);
static void tx_printf_P(const char *format, const int argument1, const int argument2);
static inline bool rx_str(FSTR_P fstr);
static inline void tx_str(FSTR_P fstr);
static inline void tx_printf(FSTR_P ffmt, const int argument);
static inline void tx_printf(FSTR_P ffmt, const int argument1, const int argument2);
static void clear_rx_buffer();

static bool rx_ok();
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/lcd/HD44780/lcdprint_hd44780.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1063,8 +1063,8 @@ int lcd_put_u8str_max(const char * utf8_str, pixel_len_t max_length) {
return lcd_put_u8str_max_cb(utf8_str, read_byte_ram, max_length);
}

int lcd_put_u8str_max_P(PGM_P utf8_str_P, pixel_len_t max_length) {
return lcd_put_u8str_max_cb(utf8_str_P, read_byte_rom, max_length);
int lcd_put_u8str_max_P(PGM_P utf8_pstr, pixel_len_t max_length) {
return lcd_put_u8str_max_cb(utf8_pstr, read_byte_rom, max_length);
}

#if ENABLED(DEBUG_LCDPRINT)
Expand Down
45 changes: 23 additions & 22 deletions Marlin/src/lcd/HD44780/marlinui_HD44780.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,15 @@ void MarlinUI::clear_lcd() { lcd.clear(); }
}

// Scroll the PSTR 'text' in a 'len' wide field for 'time' milliseconds at position col,line
void lcd_scroll(const lcd_uint_t col, const lcd_uint_t line, PGM_P const text, const uint8_t len, const int16_t time) {
uint8_t slen = utf8_strlen_P(text);
void lcd_scroll(const lcd_uint_t col, const lcd_uint_t line, FSTR_P const ftxt, const uint8_t len, const int16_t time) {
uint8_t slen = utf8_strlen_P(FTOP(ftxt));
if (slen < len) {
lcd_put_u8str_max_P(col, line, text, len);
lcd_put_u8str_max(col, line, ftxt, len);
for (; slen < len; ++slen) lcd_put_wchar(' ');
safe_delay(time);
}
else {
PGM_P p = text;
PGM_P p = FTOP(ftxt);
int dly = time / _MAX(slen, 1);
LOOP_LE_N(i, slen) {

Expand All @@ -439,9 +439,9 @@ void MarlinUI::clear_lcd() { lcd.clear(); }

static void logo_lines(PGM_P const extra) {
int16_t indent = (LCD_WIDTH - 8 - utf8_strlen_P(extra)) / 2;
lcd_put_wchar(indent, 0, '\x00'); lcd_put_u8str_P(PSTR( "------" )); lcd_put_wchar('\x01');
lcd_put_u8str_P(indent, 1, PSTR("|Marlin|")); lcd_put_u8str_P(extra);
lcd_put_wchar(indent, 2, '\x02'); lcd_put_u8str_P(PSTR( "------" )); lcd_put_wchar('\x03');
lcd_put_wchar(indent, 0, '\x00'); lcd_put_u8str(F( "------" )); lcd_put_wchar('\x01');
lcd_put_u8str(indent, 1, F("|Marlin|")); lcd_put_u8str_P(extra);
lcd_put_wchar(indent, 2, '\x02'); lcd_put_u8str(F( "------" )); lcd_put_wchar('\x03');
}

void MarlinUI::show_bootscreen() {
Expand All @@ -450,15 +450,16 @@ void MarlinUI::clear_lcd() { lcd.clear(); }

#define LCD_EXTRA_SPACE (LCD_WIDTH-8)

#define CENTER_OR_SCROLL(STRING,DELAY) \
#define CENTER_OR_SCROLL(STRING,DELAY) { \
lcd_erase_line(3); \
if (utf8_strlen(STRING) <= LCD_WIDTH) { \
lcd_put_u8str_P((LCD_WIDTH - utf8_strlen_P(PSTR(STRING))) / 2, 3, PSTR(STRING)); \
const int len = utf8_strlen(STRING); \
if (len <= LCD_WIDTH) { \
lcd_put_u8str((LCD_WIDTH - len) / 2, 3, F(STRING)); \
safe_delay(DELAY); \
} \
else { \
lcd_scroll(0, 3, PSTR(STRING), LCD_WIDTH, DELAY); \
}
else \
lcd_scroll(0, 3, F(STRING), LCD_WIDTH, DELAY); \
}

//
// Show the Marlin logo with splash line 1
Expand Down Expand Up @@ -497,9 +498,9 @@ void MarlinUI::draw_kill_screen() {
lcd_put_u8str(0, 0, status_message);
lcd_uint_t y = 2;
#if LCD_HEIGHT >= 4
lcd_put_u8str_P(0, y++, GET_TEXT(MSG_HALTED));
lcd_put_u8str(0, y++, GET_TEXT_F(MSG_HALTED));
#endif
lcd_put_u8str_P(0, y, GET_TEXT(MSG_PLEASE_RESET));
lcd_put_u8str(0, y, GET_TEXT_F(MSG_PLEASE_RESET));
}

//
Expand All @@ -514,7 +515,7 @@ FORCE_INLINE void _draw_axis_value(const AxisEnum axis, const char *value, const
else if (axis_should_home(axis))
while (const char c = *value++) lcd_put_wchar(c <= '.' ? c : '?');
else if (NONE(HOME_AFTER_DEACTIVATE, DISABLE_REDUCED_ACCURACY_WARNING) && !axis_is_trusted(axis))
lcd_put_u8str_P(axis == Z_AXIS ? PSTR(" ") : PSTR(" "));
lcd_put_u8str(axis == Z_AXIS ? F(" ") : F(" "));
else
lcd_put_u8str(value);
}
Expand Down Expand Up @@ -613,11 +614,11 @@ FORCE_INLINE void _draw_bed_status(const bool blink) {

FORCE_INLINE void _draw_print_progress() {
const uint8_t progress = ui.get_progress_percent();
lcd_put_u8str_P(PSTR(TERN(SDSUPPORT, "SD", "P:")));
lcd_put_u8str(F(TERN(SDSUPPORT, "SD", "P:")));
if (progress)
lcd_put_u8str(ui8tostr3rj(progress));
else
lcd_put_u8str_P(PSTR("---"));
lcd_put_u8str(F("---"));
lcd_put_wchar('%');
}

Expand Down Expand Up @@ -661,9 +662,9 @@ void MarlinUI::draw_status_message(const bool blink) {

// Alternate Status message and Filament display
if (ELAPSED(millis(), next_filament_display)) {
lcd_put_u8str_P(PSTR("Dia "));
lcd_put_u8str(F("Dia "));
lcd_put_u8str(ftostr12ns(filwidth.measured_mm));
lcd_put_u8str_P(PSTR(" V"));
lcd_put_u8str(F(" V"));
lcd_put_u8str(i16tostr3rj(planner.volumetric_percent(parser.volumetric_enabled)));
lcd_put_wchar('%');
return;
Expand Down Expand Up @@ -1473,7 +1474,7 @@ void MarlinUI::draw_status_screen() {
if (!isnan(ubl.z_values[x_plot][y_plot]))
lcd_put_u8str(ftostr43sign(ubl.z_values[x_plot][y_plot]));
else
lcd_put_u8str_P(PSTR(" -----"));
lcd_put_u8str(F(" -----"));

#else // 16x4 or 20x4 display

Expand All @@ -1492,7 +1493,7 @@ void MarlinUI::draw_status_screen() {
if (!isnan(ubl.z_values[x_plot][y_plot]))
lcd_put_u8str(ftostr43sign(ubl.z_values[x_plot][y_plot]));
else
lcd_put_u8str_P(PSTR(" -----"));
lcd_put_u8str(F(" -----"));

#endif // LCD_HEIGHT > 3
}
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/lcd/TFTGLCD/lcdprint_TFTGLCD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1061,8 +1061,8 @@ int lcd_put_u8str_max(const char * utf8_str, pixel_len_t max_length) {
return lcd_put_u8str_max_cb(utf8_str, read_byte_ram, max_length);
}

int lcd_put_u8str_max_P(PGM_P utf8_str_P, pixel_len_t max_length) {
return lcd_put_u8str_max_cb(utf8_str_P, read_byte_rom, max_length);
int lcd_put_u8str_max_P(PGM_P utf8_pstr, pixel_len_t max_length) {
return lcd_put_u8str_max_cb(utf8_pstr, read_byte_rom, max_length);
}

#if ENABLED(DEBUG_LCDPRINT)
Expand Down
Loading

0 comments on commit 9cf1c3c

Please sign in to comment.