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
 /**
74
 /**
75
  * Dive into a folder and recurse depth-first to perform a pre-set operation lsAction:
75
  * Dive into a folder and recurse depth-first to perform a pre-set operation lsAction:
76
  *   LS_Count       - Add +1 to nrFiles for every file within the parent
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
  *   LS_SerialPrint - Print the full path and size of each file to serial output
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
 void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) {
83
 void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) {
81
   dir_t p;
84
   dir_t p;
82
   uint8_t cnt = 0;
85
   uint8_t cnt = 0;
130
 
133
 
131
       if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue;
134
       if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue;
132
 
135
 
133
-      switch (lsAction) {
136
+      switch (lsAction) {  // 1 based file count
134
         case LS_Count:
137
         case LS_Count:
135
           nrFiles++;
138
           nrFiles++;
136
           break;
139
           break;
148
           if (match != NULL) {
151
           if (match != NULL) {
149
             if (strcasecmp(match, filename) == 0) return;
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
           cnt++;
155
           cnt++;
153
           break;
156
           break;
154
       }
157
       }
587
   #endif // SDSORT_CACHE_NAMES
590
   #endif // SDSORT_CACHE_NAMES
588
   curDir = &workDir;
591
   curDir = &workDir;
589
   lsAction = LS_GetFilename;
592
   lsAction = LS_GetFilename;
590
-  nrFiles = nr;
593
+  nrFile_index = nr;
591
   curDir->rewind();
594
   curDir->rewind();
592
   lsDive("", *curDir, match);
595
   lsDive("", *curDir, match);
593
 }
596
 }
688
             sortnames = new char*[fileCnt];
691
             sortnames = new char*[fileCnt];
689
           #endif
692
           #endif
690
         #elif ENABLED(SDSORT_USES_STACK)
693
         #elif ENABLED(SDSORT_USES_STACK)
691
-          char sortnames[fileCnt][LONG_FILENAME_LENGTH];
694
+          char sortnames[fileCnt][SORTED_LONGNAME_MAXLEN];
692
         #endif
695
         #endif
693
 
696
 
694
         // Folder sorting needs 1 bit per entry for flags.
697
         // Folder sorting needs 1 bit per entry for flags.
727
               #endif
730
               #endif
728
             #else
731
             #else
729
               // Copy filenames into the static array
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
               #if ENABLED(SDSORT_CACHE_NAMES)
739
               #if ENABLED(SDSORT_CACHE_NAMES)
732
                 strcpy(sortshort[i], filename);
740
                 strcpy(sortshort[i], filename);
733
               #endif
741
               #endif
818
           #if ENABLED(SDSORT_DYNAMIC_RAM)
826
           #if ENABLED(SDSORT_DYNAMIC_RAM)
819
             sortnames = new char*[1];
827
             sortnames = new char*[1];
820
             sortnames[0] = strdup(LONGEST_FILENAME); // malloc
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
             isDir = new uint8_t[1];
833
             isDir = new uint8_t[1];
824
           #else
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
           #endif
844
           #endif
828
           isDir[0] = filenameIsDir ? 0x01 : 0x00;
845
           isDir[0] = filenameIsDir ? 0x01 : 0x00;
829
         #endif
846
         #endif
852
 
869
 
853
 #endif // SDCARD_SORT_ALPHA
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
 void CardReader::printingHasFinished() {
882
 void CardReader::printingHasFinished() {
856
   stepper.synchronize();
883
   stepper.synchronize();
857
   file.close();
884
   file.close();

+ 10
- 2
Marlin/cardreader.h View File

70
   void updir();
70
   void updir();
71
   void setroot();
71
   void setroot();
72
 
72
 
73
+  uint16_t get_num_Files();
74
+
73
   #if ENABLED(SDCARD_SORT_ALPHA)
75
   #if ENABLED(SDCARD_SORT_ALPHA)
74
     void presort();
76
     void presort();
75
     void getfilename_sorted(const uint16_t nr);
77
     void getfilename_sorted(const uint16_t nr);
112
       uint8_t sort_order[SDSORT_LIMIT];
114
       uint8_t sort_order[SDSORT_LIMIT];
113
     #endif
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
     // Cache filenames to speed up SD menus.
123
     // Cache filenames to speed up SD menus.
116
     #if ENABLED(SDSORT_USES_RAM)
124
     #if ENABLED(SDSORT_USES_RAM)
117
 
125
 
121
           char **sortshort, **sortnames;
129
           char **sortshort, **sortnames;
122
         #else
130
         #else
123
           char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
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
         #endif
133
         #endif
126
       #elif DISABLED(SDSORT_USES_STACK)
134
       #elif DISABLED(SDSORT_USES_STACK)
127
-        char sortnames[SDSORT_LIMIT][LONG_FILENAME_LENGTH];
135
+        char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
128
       #endif
136
       #endif
129
 
137
 
130
       // Folder sorting uses an isDir array when caching items.
138
       // Folder sorting uses an isDir array when caching items.

+ 2
- 1
Marlin/ultralcd.cpp View File

3799
     void lcd_sdcard_menu() {
3799
     void lcd_sdcard_menu() {
3800
       ENCODER_DIRECTION_MENUS();
3800
       ENCODER_DIRECTION_MENUS();
3801
 
3801
 
3802
-      const uint16_t fileCnt = card.getnrfilenames();
3802
+      const uint16_t fileCnt = card.get_num_Files();
3803
+
3803
       START_MENU();
3804
       START_MENU();
3804
       MENU_BACK(MSG_MAIN);
3805
       MENU_BACK(MSG_MAIN);
3805
       card.getWorkDirName();
3806
       card.getWorkDirName();

Loading…
Cancel
Save