From a6ee4a0468390c8afcd3eac7ea04e278c9055c19 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 15 Nov 2017 00:34:54 -0600 Subject: [PATCH] Apply SDCARD_SORT_ALPHA changes from 2.0.x --- Marlin/cardreader.cpp | 47 ++++++++++++++++++++++++++++++++++--------- Marlin/cardreader.h | 12 +++++++++-- Marlin/ultralcd.cpp | 3 ++- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/Marlin/cardreader.cpp b/Marlin/cardreader.cpp index 9728961e7..40a5d5d42 100644 --- a/Marlin/cardreader.cpp +++ b/Marlin/cardreader.cpp @@ -74,9 +74,12 @@ char *createFilename(char *buffer, const dir_t &p) { //buffer > 12characters /** * Dive into a folder and recurse depth-first to perform a pre-set operation lsAction: * LS_Count - Add +1 to nrFiles for every file within the parent - * LS_GetFilename - Get the filename of the file indexed by nrFiles + * LS_GetFilename - Get the filename of the file indexed by nrFile_index * LS_SerialPrint - Print the full path and size of each file to serial output */ + +uint16_t nrFile_index; + void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) { dir_t p; uint8_t cnt = 0; @@ -130,7 +133,7 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue; - switch (lsAction) { + switch (lsAction) { // 1 based file count case LS_Count: nrFiles++; break; @@ -148,7 +151,7 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m if (match != NULL) { if (strcasecmp(match, filename) == 0) return; } - else if (cnt == nrFiles) return; + else if (cnt == nrFile_index) return; // 0 based index cnt++; break; } @@ -587,7 +590,7 @@ void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) { #endif // SDSORT_CACHE_NAMES curDir = &workDir; lsAction = LS_GetFilename; - nrFiles = nr; + nrFile_index = nr; curDir->rewind(); lsDive("", *curDir, match); } @@ -688,7 +691,7 @@ void CardReader::updir() { sortnames = new char*[fileCnt]; #endif #elif ENABLED(SDSORT_USES_STACK) - char sortnames[fileCnt][LONG_FILENAME_LENGTH]; + char sortnames[fileCnt][SORTED_LONGNAME_MAXLEN]; #endif // Folder sorting needs 1 bit per entry for flags. @@ -727,7 +730,12 @@ void CardReader::updir() { #endif #else // Copy filenames into the static array - strcpy(sortnames[i], LONGEST_FILENAME); + #if SORTED_LONGNAME_MAXLEN != LONG_FILENAME_LENGTH + strncpy(sortnames[i], LONGEST_FILENAME, SORTED_LONGNAME_MAXLEN); + sortnames[i][SORTED_LONGNAME_MAXLEN - 1] = '\0'; + #else + strncpy(sortnames[i], LONGEST_FILENAME, SORTED_LONGNAME_MAXLEN); + #endif #if ENABLED(SDSORT_CACHE_NAMES) strcpy(sortshort[i], filename); #endif @@ -818,12 +826,21 @@ void CardReader::updir() { #if ENABLED(SDSORT_DYNAMIC_RAM) sortnames = new char*[1]; sortnames[0] = strdup(LONGEST_FILENAME); // malloc - sortshort = new char*[1]; - sortshort[0] = strdup(filename); // malloc + #if ENABLED(SDSORT_CACHE_NAMES) + sortshort = new char*[1]; + sortshort[0] = strdup(filename); // malloc + #endif isDir = new uint8_t[1]; #else - strcpy(sortnames[0], LONGEST_FILENAME); - strcpy(sortshort[0], filename); + #if SORTED_LONGNAME_MAXLEN != LONG_FILENAME_LENGTH + strncpy(sortnames[0], LONGEST_FILENAME, SORTED_LONGNAME_MAXLEN); + sortnames[0][SORTED_LONGNAME_MAXLEN - 1] = '\0'; + #else + strncpy(sortnames[0], LONGEST_FILENAME, SORTED_LONGNAME_MAXLEN); + #endif + #if ENABLED(SDSORT_CACHE_NAMES) + strcpy(sortshort[0], filename); + #endif #endif isDir[0] = filenameIsDir ? 0x01 : 0x00; #endif @@ -852,6 +869,16 @@ void CardReader::updir() { #endif // SDCARD_SORT_ALPHA +uint16_t CardReader::get_num_Files() { + return + #if ENABLED(SDCARD_SORT_ALPHA) && SDSORT_USES_RAM && SDSORT_CACHE_NAMES + nrFiles // no need to access the SD card for filenames + #else + getnrfilenames() + #endif + ; +} + void CardReader::printingHasFinished() { stepper.synchronize(); file.close(); diff --git a/Marlin/cardreader.h b/Marlin/cardreader.h index 3feb5d55f..5bdfa7f47 100644 --- a/Marlin/cardreader.h +++ b/Marlin/cardreader.h @@ -70,6 +70,8 @@ public: void updir(); void setroot(); + uint16_t get_num_Files(); + #if ENABLED(SDCARD_SORT_ALPHA) void presort(); void getfilename_sorted(const uint16_t nr); @@ -112,6 +114,12 @@ private: uint8_t sort_order[SDSORT_LIMIT]; #endif + #if ENABLED(SDSORT_USES_RAM) && ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM) + #define SORTED_LONGNAME_MAXLEN ((SDSORT_CACHE_VFATS) * (FILENAME_LENGTH) + 1) + #else + #define SORTED_LONGNAME_MAXLEN LONG_FILENAME_LENGTH + #endif + // Cache filenames to speed up SD menus. #if ENABLED(SDSORT_USES_RAM) @@ -121,10 +129,10 @@ private: char **sortshort, **sortnames; #else char sortshort[SDSORT_LIMIT][FILENAME_LENGTH]; - char sortnames[SDSORT_LIMIT][SDSORT_CACHE_VFATS * FILENAME_LENGTH + 1]; + char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN]; #endif #elif DISABLED(SDSORT_USES_STACK) - char sortnames[SDSORT_LIMIT][LONG_FILENAME_LENGTH]; + char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN]; #endif // Folder sorting uses an isDir array when caching items. diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index 9533916de..3b4082cd2 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -3799,7 +3799,8 @@ void kill_screen(const char* lcd_msg) { void lcd_sdcard_menu() { ENCODER_DIRECTION_MENUS(); - const uint16_t fileCnt = card.getnrfilenames(); + const uint16_t fileCnt = card.get_num_Files(); + START_MENU(); MENU_BACK(MSG_MAIN); card.getWorkDirName();