diff --git a/Marlin/cardreader.cpp b/Marlin/cardreader.cpp index 5c4ff5e7d..4f5fc0225 100644 --- a/Marlin/cardreader.cpp +++ b/Marlin/cardreader.cpp @@ -35,8 +35,6 @@ #include "power_loss_recovery.h" #endif -#define LONGEST_FILENAME (longFilename[0] ? longFilename : filename) - CardReader::CardReader() { #if ENABLED(SDCARD_SORT_ALPHA) sort_count = 0; @@ -731,7 +729,7 @@ void CardReader::setroot() { getfilename(i); #if ENABLED(SDSORT_DYNAMIC_RAM) // Use dynamic method to copy long filename - sortnames[i] = strdup(LONGEST_FILENAME); + sortnames[i] = strdup(longest_filename()); #if ENABLED(SDSORT_CACHE_NAMES) // When caching also store the short name, since // we're replacing the getfilename() behavior. @@ -740,10 +738,10 @@ void CardReader::setroot() { #else // Copy filenames into the static array #if SORTED_LONGNAME_MAXLEN != LONG_FILENAME_LENGTH - strncpy(sortnames[i], LONGEST_FILENAME, SORTED_LONGNAME_MAXLEN); + strncpy(sortnames[i], longest_filename(), SORTED_LONGNAME_MAXLEN); sortnames[i][SORTED_LONGNAME_MAXLEN - 1] = '\0'; #else - strncpy(sortnames[i], LONGEST_FILENAME, SORTED_LONGNAME_MAXLEN); + strncpy(sortnames[i], longest_filename(), SORTED_LONGNAME_MAXLEN); #endif #if ENABLED(SDSORT_CACHE_NAMES) strcpy(sortshort[i], filename); @@ -791,12 +789,12 @@ void CardReader::setroot() { // throughout the loop. Slow if there are many. #if DISABLED(SDSORT_USES_RAM) getfilename(o1); - strcpy(name1, LONGEST_FILENAME); // save (or getfilename below will trounce it) + strcpy(name1, longest_filename()); // save (or getfilename below will trounce it) #if HAS_FOLDER_SORTING bool dir1 = filenameIsDir; #endif getfilename(o2); - char *name2 = LONGEST_FILENAME; // use the string in-place + char *name2 = longest_filename(); // use the string in-place #endif // !SDSORT_USES_RAM // Sort the current pair according to settings. @@ -834,7 +832,7 @@ void CardReader::setroot() { getfilename(0); #if ENABLED(SDSORT_DYNAMIC_RAM) sortnames = new char*[1]; - sortnames[0] = strdup(LONGEST_FILENAME); // malloc + sortnames[0] = strdup(longest_filename()); // malloc #if ENABLED(SDSORT_CACHE_NAMES) sortshort = new char*[1]; sortshort[0] = strdup(filename); // malloc @@ -842,10 +840,10 @@ void CardReader::setroot() { isDir = new uint8_t[1]; #else #if SORTED_LONGNAME_MAXLEN != LONG_FILENAME_LENGTH - strncpy(sortnames[0], LONGEST_FILENAME, SORTED_LONGNAME_MAXLEN); + strncpy(sortnames[0], longest_filename(), SORTED_LONGNAME_MAXLEN); sortnames[0][SORTED_LONGNAME_MAXLEN - 1] = '\0'; #else - strncpy(sortnames[0], LONGEST_FILENAME, SORTED_LONGNAME_MAXLEN); + strncpy(sortnames[0], longest_filename(), SORTED_LONGNAME_MAXLEN); #endif #if ENABLED(SDSORT_CACHE_NAMES) strcpy(sortshort[0], filename); diff --git a/Marlin/cardreader.h b/Marlin/cardreader.h index 15275c072..bbc84d9db 100644 --- a/Marlin/cardreader.h +++ b/Marlin/cardreader.h @@ -114,6 +114,8 @@ public: } #endif + FORCE_INLINE char* longest_filename() { return longFilename[0] ? longFilename : filename; } + public: bool saving, logging, sdprinting, cardOK, filenameIsDir; char filename[FILENAME_LENGTH], longFilename[LONG_FILENAME_LENGTH]; diff --git a/Marlin/malyanlcd.cpp b/Marlin/malyanlcd.cpp index e8d6c2514..3c6ff481b 100644 --- a/Marlin/malyanlcd.cpp +++ b/Marlin/malyanlcd.cpp @@ -286,7 +286,7 @@ void process_lcd_p_command(const char* command) { } else { char message_buffer[MAX_CURLY_COMMAND]; - sprintf_P(message_buffer, PSTR("{PRINTFILE:%s}"), card.filename); + sprintf_P(message_buffer, PSTR("{PRINTFILE:%s}"), card.longest_filename()); write_to_lcd(message_buffer); write_to_lcd_P(PSTR("{SYS:BUILD}")); card.openAndPrintFile(card.filename); @@ -344,7 +344,7 @@ void process_lcd_s_command(const char* command) { uint16_t file_count = card.get_num_Files(); for (uint16_t i = 0; i < file_count; i++) { card.getfilename(i); - sprintf_P(message_buffer, card.filenameIsDir ? PSTR("{DIR:%s}") : PSTR("{FILE:%s}"), card.filename); + sprintf_P(message_buffer, card.filenameIsDir ? PSTR("{DIR:%s}") : PSTR("{FILE:%s}"), card.longest_filename()); write_to_lcd(message_buffer); } diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index 509cc5547..ac78b5bc1 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -265,8 +265,8 @@ uint16_t max_display_update_time = 0; #if ENABLED(SDSUPPORT) void lcd_sdcard_menu(); - void menu_action_sdfile(const char* filename, char* longFilename); - void menu_action_sddirectory(const char* filename, char* longFilename); + void menu_action_sdfile(CardReader& theCard); + void menu_action_sddirectory(CardReader& theCard); #endif //////////////////////////////////////////// @@ -756,7 +756,7 @@ void lcd_reset_status() { msg = paused; #if ENABLED(SDSUPPORT) else if (card.sdprinting) - return lcd_setstatus(card.longFilename[0] ? card.longFilename : card.filename, true); + return lcd_setstatus(card.longest_filename(), true); #endif else if (print_job_timer.isRunning()) msg = printing; @@ -4031,9 +4031,9 @@ void lcd_quick_feedback(const bool clear_buttons) { #endif if (card.filenameIsDir) - MENU_ITEM(sddirectory, MSG_CARD_MENU, card.filename, card.longFilename); + MENU_ITEM(sddirectory, MSG_CARD_MENU, card); else - MENU_ITEM(sdfile, MSG_CARD_MENU, card.filename, card.longFilename); + MENU_ITEM(sdfile, MSG_CARD_MENU, card); } else { MENU_ITEM_DUMMY(); @@ -4954,19 +4954,17 @@ void lcd_quick_feedback(const bool clear_buttons) { #if ENABLED(SDSUPPORT) - void menu_action_sdfile(const char* filename, char* longFilename) { + void menu_action_sdfile(CardReader& theCard) { #if ENABLED(SD_REPRINT_LAST_SELECTED_FILE) last_sdfile_encoderPosition = encoderPosition; // Save which file was selected for later use #endif - UNUSED(longFilename); - card.openAndPrintFile(filename); + card.openAndPrintFile(theCard.filename); lcd_return_to_status(); lcd_reset_status(); } - void menu_action_sddirectory(const char* filename, char* longFilename) { - UNUSED(longFilename); - card.chdir(filename); + void menu_action_sddirectory(CardReader& theCard) { + card.chdir(theCard.filename); encoderTopLine = 0; encoderPosition = 2 * ENCODER_STEPS_PER_MENU_ITEM; screen_changed = true; diff --git a/Marlin/ultralcd_impl_DOGM.h b/Marlin/ultralcd_impl_DOGM.h index ad1cc96d5..1c2844c72 100644 --- a/Marlin/ultralcd_impl_DOGM.h +++ b/Marlin/ultralcd_impl_DOGM.h @@ -586,7 +586,7 @@ void lcd_implementation_clear() { } // Automatically cleared by Picture Loop #if ENABLED(SDSUPPORT) - static void _drawmenu_sd(const bool isSelected, const uint8_t row, const char* const pstr, const char* filename, char* const longFilename, const bool isDir) { + static void _drawmenu_sd(const bool isSelected, const uint8_t row, const char* const pstr, CardReader& theCard, const bool isDir) { UNUSED(pstr); lcd_implementation_mark_as_selected(row, isSelected); @@ -594,23 +594,23 @@ void lcd_implementation_clear() { } // Automatically cleared by Picture Loop if (!PAGE_CONTAINS(row_y1, row_y2)) return; constexpr uint8_t maxlen = LCD_WIDTH - (START_COL) - 1; - const char *outstr = longFilename[0] ? longFilename : filename; - if (longFilename[0]) { + const char *outstr = theCard.longest_filename(); + if (theCard.longFilename[0]) { #if ENABLED(SCROLL_LONG_FILENAMES) if (isSelected) { uint8_t name_hash = row; for (uint8_t l = FILENAME_LENGTH; l--;) - name_hash = ((name_hash << 1) | (name_hash >> 7)) ^ filename[l]; // rotate, xor + name_hash = ((name_hash << 1) | (name_hash >> 7)) ^ theCard.filename[l]; // rotate, xor if (filename_scroll_hash != name_hash) { // If the hash changed... filename_scroll_hash = name_hash; // Save the new hash - filename_scroll_max = MAX(0, utf8_strlen(longFilename) - maxlen); // Update the scroll limit + filename_scroll_max = MAX(0, utf8_strlen(theCard.longFilename) - maxlen); // Update the scroll limit filename_scroll_pos = 0; // Reset scroll to the start lcd_status_update_delay = 8; // Don't scroll right away } outstr += filename_scroll_pos; } #else - longFilename[maxlen] = '\0'; // cutoff at screen edge + theCard.longFilename[maxlen] = '\0'; // cutoff at screen edge #endif } @@ -625,8 +625,8 @@ void lcd_implementation_clear() { } // Automatically cleared by Picture Loop while (n) { --n; u8g.print(' '); } } - #define lcd_implementation_drawmenu_sdfile(sel, row, pstr, filename, longFilename) _drawmenu_sd(sel, row, pstr, filename, longFilename, false) - #define lcd_implementation_drawmenu_sddirectory(sel, row, pstr, filename, longFilename) _drawmenu_sd(sel, row, pstr, filename, longFilename, true) + #define lcd_implementation_drawmenu_sdfile(sel, row, pstr, theCard) _drawmenu_sd(sel, row, pstr, theCard, false) + #define lcd_implementation_drawmenu_sddirectory(sel, row, pstr, theCard) _drawmenu_sd(sel, row, pstr, theCard, true) #endif // SDSUPPORT diff --git a/Marlin/ultralcd_impl_HD44780.h b/Marlin/ultralcd_impl_HD44780.h index e45bc3fd6..d229a8378 100644 --- a/Marlin/ultralcd_impl_HD44780.h +++ b/Marlin/ultralcd_impl_HD44780.h @@ -995,11 +995,8 @@ static void lcd_implementation_status_screen() { ++slen; } #endif - } - - #if ENABLED(ULTIPANEL) #if ENABLED(ADVANCED_PAUSE_FEATURE) @@ -1090,29 +1087,29 @@ static void lcd_implementation_status_screen() { #if ENABLED(SDSUPPORT) - static void lcd_implementation_drawmenu_sd(const bool sel, const uint8_t row, const char* const pstr, const char* filename, char* const longFilename, const uint8_t concat, const char post_char) { + static void lcd_implementation_drawmenu_sd(const bool sel, const uint8_t row, const char* const pstr, CardReader& theCard, const uint8_t concat, const char post_char) { UNUSED(pstr); lcd.setCursor(0, row); lcd.print(sel ? '>' : ' '); uint8_t n = LCD_WIDTH - concat; - const char *outstr = longFilename[0] ? longFilename : filename; - if (longFilename[0]) { + const char *outstr = theCard.longest_filename(); + if (theCard.longFilename[0]) { #if ENABLED(SCROLL_LONG_FILENAMES) if (sel) { uint8_t name_hash = row; for (uint8_t l = FILENAME_LENGTH; l--;) - name_hash = ((name_hash << 1) | (name_hash >> 7)) ^ filename[l]; // rotate, xor + name_hash = ((name_hash << 1) | (name_hash >> 7)) ^ theCard.filename[l]; // rotate, xor if (filename_scroll_hash != name_hash) { // If the hash changed... filename_scroll_hash = name_hash; // Save the new hash - filename_scroll_max = MAX(0, utf8_strlen(longFilename) - n); // Update the scroll limit + filename_scroll_max = MAX(0, utf8_strlen(theCard.longFilename) - n); // Update the scroll limit filename_scroll_pos = 0; // Reset scroll to the start lcd_status_update_delay = 8; // Don't scroll right away } outstr += filename_scroll_pos; } #else - longFilename[n] = '\0'; // cutoff at screen edge + theCard.longFilename[n] = '\0'; // cutoff at screen edge #endif } @@ -1126,12 +1123,12 @@ static void lcd_implementation_status_screen() { lcd.print(post_char); } - static void lcd_implementation_drawmenu_sdfile(const bool sel, const uint8_t row, const char* pstr, const char* filename, char* const longFilename) { - lcd_implementation_drawmenu_sd(sel, row, pstr, filename, longFilename, 2, ' '); + static void lcd_implementation_drawmenu_sdfile(const bool sel, const uint8_t row, const char* pstr, CardReader& theCard) { + lcd_implementation_drawmenu_sd(sel, row, pstr, theCard, 2, ' '); } - static void lcd_implementation_drawmenu_sddirectory(const bool sel, const uint8_t row, const char* pstr, const char* filename, char* const longFilename) { - lcd_implementation_drawmenu_sd(sel, row, pstr, filename, longFilename, 2, LCD_STR_FOLDER[0]); + static void lcd_implementation_drawmenu_sddirectory(const bool sel, const uint8_t row, const char* pstr, CardReader& theCard) { + lcd_implementation_drawmenu_sd(sel, row, pstr, theCard, 2, LCD_STR_FOLDER[0]); } #endif // SDSUPPORT