From e90bbb37d4f956ee4050ff02a4f9b1e0275a87cd Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 16 Jul 2020 22:20:55 -0500 Subject: [PATCH] Tweak and use SD_ORDER --- Marlin/src/lcd/dwin/dwin.cpp | 18 ++++++++++-------- Marlin/src/lcd/menu/menu_media.cpp | 2 +- Marlin/src/sd/cardreader.cpp | 2 +- Marlin/src/sd/cardreader.h | 6 +++++- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Marlin/src/lcd/dwin/dwin.cpp b/Marlin/src/lcd/dwin/dwin.cpp index eb804b5e04..db7bfc6dd7 100644 --- a/Marlin/src/lcd/dwin/dwin.cpp +++ b/Marlin/src/lcd/dwin/dwin.cpp @@ -1547,8 +1547,9 @@ millis_t shift_ms; // = 0 inline void Init_Shift_Name() { const bool is_subdir = !card.flag.workDirIsRoot; const int8_t filenum = select_file.now - 1 - is_subdir; // Skip "Back" and ".." - if (WITHIN(filenum, 0, card.get_num_Files() - 1)) { - card.getfilename_sorted(filenum); + const uint16_t fileCnt = card.get_num_Files(); + if (WITHIN(filenum, 0, fileCnt - 1)) { + card.getfilename_sorted(SD_ORDER(filenum, fileCnt)); char * const name = card.longest_filename(); make_name_without_ext(shift_name, name, 100); } @@ -1561,8 +1562,8 @@ inline void Init_SDItem_Shift() { } /** -* Display an SD item, adding a CDUP for subfolders. -*/ + * Display an SD item, adding a CDUP for subfolders. + */ inline void Draw_SDItem(const uint16_t item, int16_t row=-1) { if (row < 0) row = item + 1 + MROWS - index_file; const bool is_subdir = !card.flag.workDirIsRoot; @@ -1617,7 +1618,7 @@ inline void Redraw_SD_List() { // As many files as will fit LOOP_L_N(i, _MIN(nr_sd_menu_items(), MROWS)) - Draw_SDItem(i, i + 1); + Draw_SDItem(i, i + 1); Init_SDItem_Shift(); } @@ -1898,7 +1899,7 @@ void HMI_SelectFile(void) { } else { const uint16_t filenum = select_file.now - 1 - hasUpDir; - card.getfilename_sorted(filenum); + card.getfilename_sorted(SD_ORDER(filenum, card.get_num_Files())); // Enter that folder! if (card.flag.filenameIsDir) { @@ -3415,10 +3416,11 @@ void EachMomentUpdate(void) { DWIN_Draw_Rectangle(0, c2, 144, 305, 247, 346); }; - LOOP_L_N(i, card.get_num_Files()) { + const uint16_t fileCnt = card.get_num_Files(); + for (uint16_t i = 0; i < fileCnt; i++) { // TODO: Resume print via M1000 then update the UI // with the active filename which can come from CardReader. - card.getfilename_sorted(i); + card.getfilename_sorted(SD_ORDER(i, fileCnt)); if (!strcmp(card.filename, &recovery.info.sd_filename[1])) { // Resume print before power failure while have the same file recovery_flag = 1; HMI_flag.select_flag = 1; diff --git a/Marlin/src/lcd/menu/menu_media.cpp b/Marlin/src/lcd/menu/menu_media.cpp index d2514f61d2..7b3d1e549c 100644 --- a/Marlin/src/lcd/menu/menu_media.cpp +++ b/Marlin/src/lcd/menu/menu_media.cpp @@ -126,7 +126,7 @@ void menu_media() { if (ui.should_draw()) for (uint16_t i = 0; i < fileCnt; i++) { if (_menuLineNr == _thisItemNr) { - card.getfilename_sorted(i); + card.getfilename_sorted(SD_ORDER(i, fileCnt)); if (card.flag.filenameIsDir) MENU_ITEM(sdfolder, MSG_MEDIA_MENU, card); else diff --git a/Marlin/src/sd/cardreader.cpp b/Marlin/src/sd/cardreader.cpp index beac76531c..5b4054f21c 100644 --- a/Marlin/src/sd/cardreader.cpp +++ b/Marlin/src/sd/cardreader.cpp @@ -947,7 +947,7 @@ void CardReader::cdroot() { // Init sort order. for (uint16_t i = 0; i < fileCnt; i++) { - sort_order[i] = SD_ORDER(i, fileCnt); + sort_order[i] = i; // If using RAM then read all filenames now. #if ENABLED(SDSORT_USES_RAM) selectFileByIndex(i); diff --git a/Marlin/src/sd/cardreader.h b/Marlin/src/sd/cardreader.h index 36b81910a8..8efa6c720e 100644 --- a/Marlin/src/sd/cardreader.h +++ b/Marlin/src/sd/cardreader.h @@ -31,7 +31,11 @@ #define SD_RESORT 1 #endif -#define SD_ORDER(N,C) (TERN(SDCARD_RATHERRECENTFIRST, C - 1 - (N), N)) +#if ENABLED(SDCARD_RATHERRECENTFIRST) && DISABLED(SDCARD_SORT_ALPHA) + #define SD_ORDER(N,C) ((C) - 1 - (N)) +#else + #define SD_ORDER(N,C) N +#endif #define MAX_DIR_DEPTH 10 // Maximum folder depth #define MAXDIRNAMELENGTH 8 // DOS folder name size