Browse Source

Apply SDCARD_SORT_ALPHA changes from 2.0.x

Scott Lahteine 6 years ago
parent
commit
a6ee4a0468
3 changed files with 49 additions and 13 deletions
  1. 37
    10
      Marlin/cardreader.cpp
  2. 10
    2
      Marlin/cardreader.h
  3. 2
    1
      Marlin/ultralcd.cpp

+ 37
- 10
Marlin/cardreader.cpp View File

@@ -74,9 +74,12 @@ char *createFilename(char *buffer, const dir_t &p) { //buffer > 12characters
74 74
 /**
75 75
  * Dive into a folder and recurse depth-first to perform a pre-set operation lsAction:
76 76
  *   LS_Count       - Add +1 to nrFiles for every file within the parent
77
- *   LS_GetFilename - Get the filename of the file indexed by nrFiles
77
+ *   LS_GetFilename - Get the filename of the file indexed by nrFile_index
78 78
  *   LS_SerialPrint - Print the full path and size of each file to serial output
79 79
  */
80
+
81
+uint16_t nrFile_index;
82
+
80 83
 void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) {
81 84
   dir_t p;
82 85
   uint8_t cnt = 0;
@@ -130,7 +133,7 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
130 133
 
131 134
       if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue;
132 135
 
133
-      switch (lsAction) {
136
+      switch (lsAction) {  // 1 based file count
134 137
         case LS_Count:
135 138
           nrFiles++;
136 139
           break;
@@ -148,7 +151,7 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
148 151
           if (match != NULL) {
149 152
             if (strcasecmp(match, filename) == 0) return;
150 153
           }
151
-          else if (cnt == nrFiles) return;
154
+          else if (cnt == nrFile_index) return;  // 0 based index
152 155
           cnt++;
153 156
           break;
154 157
       }
@@ -587,7 +590,7 @@ void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) {
587 590
   #endif // SDSORT_CACHE_NAMES
588 591
   curDir = &workDir;
589 592
   lsAction = LS_GetFilename;
590
-  nrFiles = nr;
593
+  nrFile_index = nr;
591 594
   curDir->rewind();
592 595
   lsDive("", *curDir, match);
593 596
 }
@@ -688,7 +691,7 @@ void CardReader::updir() {
688 691
             sortnames = new char*[fileCnt];
689 692
           #endif
690 693
         #elif ENABLED(SDSORT_USES_STACK)
691
-          char sortnames[fileCnt][LONG_FILENAME_LENGTH];
694
+          char sortnames[fileCnt][SORTED_LONGNAME_MAXLEN];
692 695
         #endif
693 696
 
694 697
         // Folder sorting needs 1 bit per entry for flags.
@@ -727,7 +730,12 @@ void CardReader::updir() {
727 730
               #endif
728 731
             #else
729 732
               // Copy filenames into the static array
730
-              strcpy(sortnames[i], LONGEST_FILENAME);
733
+              #if SORTED_LONGNAME_MAXLEN != LONG_FILENAME_LENGTH
734
+                strncpy(sortnames[i], LONGEST_FILENAME, SORTED_LONGNAME_MAXLEN);
735
+                sortnames[i][SORTED_LONGNAME_MAXLEN - 1] = '\0';
736
+              #else
737
+                strncpy(sortnames[i], LONGEST_FILENAME, SORTED_LONGNAME_MAXLEN);
738
+              #endif
731 739
               #if ENABLED(SDSORT_CACHE_NAMES)
732 740
                 strcpy(sortshort[i], filename);
733 741
               #endif
@@ -818,12 +826,21 @@ void CardReader::updir() {
818 826
           #if ENABLED(SDSORT_DYNAMIC_RAM)
819 827
             sortnames = new char*[1];
820 828
             sortnames[0] = strdup(LONGEST_FILENAME); // malloc
821
-            sortshort = new char*[1];
822
-            sortshort[0] = strdup(filename);         // malloc
829
+            #if ENABLED(SDSORT_CACHE_NAMES)
830
+              sortshort = new char*[1];
831
+              sortshort[0] = strdup(filename);       // malloc
832
+            #endif
823 833
             isDir = new uint8_t[1];
824 834
           #else
825
-            strcpy(sortnames[0], LONGEST_FILENAME);
826
-            strcpy(sortshort[0], filename);
835
+            #if SORTED_LONGNAME_MAXLEN != LONG_FILENAME_LENGTH
836
+              strncpy(sortnames[0], LONGEST_FILENAME, SORTED_LONGNAME_MAXLEN);
837
+              sortnames[0][SORTED_LONGNAME_MAXLEN - 1] = '\0';
838
+            #else
839
+              strncpy(sortnames[0], LONGEST_FILENAME, SORTED_LONGNAME_MAXLEN);
840
+            #endif
841
+            #if ENABLED(SDSORT_CACHE_NAMES)
842
+              strcpy(sortshort[0], filename);
843
+            #endif
827 844
           #endif
828 845
           isDir[0] = filenameIsDir ? 0x01 : 0x00;
829 846
         #endif
@@ -852,6 +869,16 @@ void CardReader::updir() {
852 869
 
853 870
 #endif // SDCARD_SORT_ALPHA
854 871
 
872
+uint16_t CardReader::get_num_Files() {
873
+  return
874
+    #if ENABLED(SDCARD_SORT_ALPHA) && SDSORT_USES_RAM && SDSORT_CACHE_NAMES
875
+      nrFiles // no need to access the SD card for filenames
876
+    #else
877
+      getnrfilenames()
878
+    #endif
879
+  ;
880
+}
881
+
855 882
 void CardReader::printingHasFinished() {
856 883
   stepper.synchronize();
857 884
   file.close();

+ 10
- 2
Marlin/cardreader.h View File

@@ -70,6 +70,8 @@ public:
70 70
   void updir();
71 71
   void setroot();
72 72
 
73
+  uint16_t get_num_Files();
74
+
73 75
   #if ENABLED(SDCARD_SORT_ALPHA)
74 76
     void presort();
75 77
     void getfilename_sorted(const uint16_t nr);
@@ -112,6 +114,12 @@ private:
112 114
       uint8_t sort_order[SDSORT_LIMIT];
113 115
     #endif
114 116
 
117
+    #if ENABLED(SDSORT_USES_RAM) && ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)
118
+      #define SORTED_LONGNAME_MAXLEN ((SDSORT_CACHE_VFATS) * (FILENAME_LENGTH) + 1)
119
+    #else
120
+      #define SORTED_LONGNAME_MAXLEN LONG_FILENAME_LENGTH
121
+    #endif
122
+
115 123
     // Cache filenames to speed up SD menus.
116 124
     #if ENABLED(SDSORT_USES_RAM)
117 125
 
@@ -121,10 +129,10 @@ private:
121 129
           char **sortshort, **sortnames;
122 130
         #else
123 131
           char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
124
-          char sortnames[SDSORT_LIMIT][SDSORT_CACHE_VFATS * FILENAME_LENGTH + 1];
132
+          char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
125 133
         #endif
126 134
       #elif DISABLED(SDSORT_USES_STACK)
127
-        char sortnames[SDSORT_LIMIT][LONG_FILENAME_LENGTH];
135
+        char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
128 136
       #endif
129 137
 
130 138
       // Folder sorting uses an isDir array when caching items.

+ 2
- 1
Marlin/ultralcd.cpp View File

@@ -3799,7 +3799,8 @@ void kill_screen(const char* lcd_msg) {
3799 3799
     void lcd_sdcard_menu() {
3800 3800
       ENCODER_DIRECTION_MENUS();
3801 3801
 
3802
-      const uint16_t fileCnt = card.getnrfilenames();
3802
+      const uint16_t fileCnt = card.get_num_Files();
3803
+
3803 3804
       START_MENU();
3804 3805
       MENU_BACK(MSG_MAIN);
3805 3806
       card.getWorkDirName();

Loading…
Cancel
Save