Sfoglia il codice sorgente

Merge pull request #3152 from thinkyhead/sd_alpha_sort

SD files alphabetical sort in LCD menus
Scott Lahteine 8 anni fa
parent
commit
20ef0a6e27

+ 9
- 0
.travis.yml Vedi File

@@ -298,6 +298,15 @@ script:
298 298
   - opt_enable G3D_PANEL SDSUPPORT
299 299
   - build_marlin
300 300
   #
301
+  # Add SDCARD_SORT_ALPHA, test G3D_PANEL again
302
+  #
303
+  - opt_enable_adv SDCARD_SORT_ALPHA
304
+  - opt_set_adv SDSORT_GCODE true
305
+  - opt_set_adv SDSORT_USES_RAM true
306
+  - opt_set_adv SDSORT_USES_STACK true
307
+  - opt_set_adv SDSORT_CACHE_NAMES true
308
+  - build_marlin
309
+  #
301 310
   # REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER
302 311
   #
303 312
   - restore_configs

+ 4
- 0
Marlin/Conditionals_post.h Vedi File

@@ -722,4 +722,8 @@
722 722
     #define DELTA_ENDSTOP_ADJ { 0 }
723 723
   #endif
724 724
 
725
+  #if ENABLED(SDCARD_SORT_ALPHA)
726
+    #define HAS_FOLDER_SORTING (FOLDER_SORTING || ENABLED(SDSORT_GCODE))
727
+  #endif
728
+
725 729
 #endif // CONDITIONALS_POST_H

+ 36
- 0
Marlin/Configuration_adv.h Vedi File

@@ -442,6 +442,42 @@
442 442
   // using:
443 443
   //#define MENU_ADDAUTOSTART
444 444
 
445
+  /**
446
+   * Sort SD file listings in alphabetical order.
447
+   *
448
+   * With this option enabled, items on SD cards will be sorted
449
+   * by name for easier navigation.
450
+   *
451
+   * By default...
452
+   *
453
+   *  - Use the slowest -but safest- method for sorting.
454
+   *  - Folders are sorted to the top.
455
+   *  - The sort key is statically allocated.
456
+   *  - No added G-code (M34) support.
457
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
458
+   *
459
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
460
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
461
+   * limit is exceeded.
462
+   *
463
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
464
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
465
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
466
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
467
+   */
468
+  //#define SDCARD_SORT_ALPHA
469
+
470
+  // SD Card Sorting options
471
+  #if ENABLED(SDCARD_SORT_ALPHA)
472
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
473
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
474
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
475
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
476
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
477
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
478
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
479
+  #endif
480
+
445 481
   // Show a progress bar on HD44780 LCDs for SD printing
446 482
   //#define LCD_PROGRESS_BAR
447 483
 

+ 20
- 0
Marlin/Marlin_main.cpp Vedi File

@@ -91,6 +91,7 @@
91 91
  *        Use P to run other files as sub-programs: "M32 P !filename#"
92 92
  *        The '#' is necessary when calling from within sd files, as it stops buffer prereading
93 93
  * M33  - Get the longname version of a path. (Requires LONG_FILENAME_HOST_SUPPORT)
94
+ * M34  - Set SD Card sorting options. (Requires SDCARD_SORT_ALPHA)
94 95
  * M42  - Change pin status via gcode: M42 P<pin> S<value>. LED pin assumed if P is omitted.
95 96
  * M43  - Monitor pins & report changes - report active pins
96 97
  * M48  - Measure Z Probe repeatability: M48 P<points> X<pos> Y<pos> V<level> E<engage> L<legs>. (Requires Z_MIN_PROBE_REPEATABILITY_TEST)
@@ -4843,6 +4844,20 @@ inline void gcode_M31() {
4843 4844
 
4844 4845
   #endif
4845 4846
 
4847
+  #if ENABLED(SDCARD_SORT_ALPHA) && ENABLED(SDSORT_GCODE)
4848
+    /**
4849
+     * M34: Set SD Card Sorting Options
4850
+     */
4851
+    inline void gcode_M34() {
4852
+      if (code_seen('S')) card.setSortOn(code_value_bool());
4853
+      if (code_seen('F')) {
4854
+        int v = code_value_long();
4855
+        card.setSortFolders(v < 0 ? -1 : v > 0 ? 1 : 0);
4856
+      }
4857
+      //if (code_seen('R')) card.setSortReverse(code_value_bool());
4858
+    }
4859
+  #endif // SDCARD_SORT_ALPHA && SDSORT_GCODE
4860
+
4846 4861
   /**
4847 4862
    * M928: Start SD Write
4848 4863
    */
@@ -8289,6 +8304,11 @@ void process_next_command() {
8289 8304
             gcode_M33(); break;
8290 8305
         #endif
8291 8306
 
8307
+        #if ENABLED(SDCARD_SORT_ALPHA) && ENABLED(SDSORT_GCODE)
8308
+          case 34: //M34 - Set SD card sorting options
8309
+            gcode_M34(); break;
8310
+        #endif // SDCARD_SORT_ALPHA && SDSORT_GCODE
8311
+
8292 8312
         case 928: // M928: Start SD write
8293 8313
           gcode_M928(); break;
8294 8314
       #endif //SDSUPPORT

+ 17
- 0
Marlin/SanityCheck.h Vedi File

@@ -206,6 +206,23 @@
206 206
 #endif
207 207
 
208 208
 /**
209
+ * SD File Sorting
210
+ */
211
+#if ENABLED(SDCARD_SORT_ALPHA)
212
+  #if SDSORT_LIMIT > 256
213
+    #error "SDSORT_LIMIT must be 256 or smaller."
214
+  #elif SDSORT_LIMIT < 10
215
+    #error "SDSORT_LIMIT should be greater than 9 to be useful."
216
+  #elif DISABLED(SDSORT_USES_RAM)
217
+    #if ENABLED(SDSORT_DYNAMIC_RAM)
218
+      #error "SDSORT_DYNAMIC_RAM requires SDSORT_USES_RAM (which reads the directory into RAM)."
219
+    #elif ENABLED(SDSORT_CACHE_NAMES)
220
+      #error "SDSORT_CACHE_NAMES requires SDSORT_USES_RAM (which reads the directory into RAM)."
221
+    #endif
222
+  #endif
223
+#endif
224
+
225
+/**
209 226
  * Delta requirements
210 227
  */
211 228
 #if ENABLED(DELTA)

+ 270
- 2
Marlin/cardreader.cpp Vedi File

@@ -30,7 +30,17 @@
30 30
 
31 31
 #if ENABLED(SDSUPPORT)
32 32
 
33
+#define LONGEST_FILENAME (longFilename[0] ? longFilename : filename)
34
+
33 35
 CardReader::CardReader() {
36
+  #if ENABLED(SDCARD_SORT_ALPHA)
37
+    sort_count = 0;
38
+    #if ENABLED(SDSORT_GCODE)
39
+      sort_alpha = true;
40
+      sort_folders = FOLDER_SORTING;
41
+      //sort_reverse = false;
42
+    #endif
43
+  #endif
34 44
   sdprinting = cardOK = saving = logging = false;
35 45
   filesize = 0;
36 46
   sdpos = 0;
@@ -243,6 +253,9 @@ void CardReader::initsd() {
243 253
   }
244 254
   workDir = root;
245 255
   curDir = &root;
256
+  #if ENABLED(SDCARD_SORT_ALPHA)
257
+    presort();
258
+  #endif
246 259
   /**
247 260
   if (!workDir.openRoot(&volume)) {
248 261
     SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
@@ -256,6 +269,9 @@ void CardReader::setroot() {
256 269
   }*/
257 270
   workDir = root;
258 271
   curDir = &workDir;
272
+  #if ENABLED(SDCARD_SORT_ALPHA)
273
+    presort();
274
+  #endif
259 275
 }
260 276
 
261 277
 void CardReader::release() {
@@ -272,7 +288,12 @@ void CardReader::openAndPrintFile(const char *name) {
272 288
 }
273 289
 
274 290
 void CardReader::startFileprint() {
275
-  if (cardOK) sdprinting = true;
291
+  if (cardOK) {
292
+    sdprinting = true;
293
+    #if ENABLED(SDCARD_SORT_ALPHA)
294
+      flush_presort();
295
+    #endif
296
+  }
276 297
 }
277 298
 
278 299
 void CardReader::stopSDPrint() {
@@ -463,6 +484,9 @@ void CardReader::removeFile(char* name) {
463 484
     SERIAL_PROTOCOLPGM("File deleted:");
464 485
     SERIAL_PROTOCOLLN(fname);
465 486
     sdpos = 0;
487
+    #if ENABLED(SDCARD_SORT_ALPHA)
488
+      presort();
489
+    #endif
466 490
   }
467 491
   else {
468 492
     SERIAL_PROTOCOLPGM("Deletion failed, File: ");
@@ -551,6 +575,20 @@ void CardReader::closefile(bool store_location) {
551 575
  * Get the name of a file in the current directory by index
552 576
  */
553 577
 void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) {
578
+  #if ENABLED(SDSORT_CACHE_NAMES)
579
+    if (match != NULL) {
580
+      while (nr < sort_count) {
581
+        if (strcasecmp(match, sortshort[nr]) == 0) break;
582
+        nr++;
583
+      }
584
+    }
585
+    if (nr < sort_count) {
586
+      strcpy(filename, sortshort[nr]);
587
+      strcpy(longFilename, sortnames[nr]);
588
+      filenameIsDir = TEST(isDir[nr>>3], nr & 0x07);
589
+      return;
590
+    }
591
+  #endif // SDSORT_CACHE_NAMES
554 592
   curDir = &workDir;
555 593
   lsAction = LS_GetFilename;
556 594
   nrFiles = nr;
@@ -583,14 +621,241 @@ void CardReader::chdir(const char * relpath) {
583 621
     if (workDirDepth < MAX_DIR_DEPTH)
584 622
       workDirParents[workDirDepth++] = *parent;
585 623
     workDir = newfile;
624
+    #if ENABLED(SDCARD_SORT_ALPHA)
625
+      presort();
626
+    #endif
586 627
   }
587 628
 }
588 629
 
589 630
 void CardReader::updir() {
590
-  if (workDirDepth > 0)
631
+  if (workDirDepth > 0) {
591 632
     workDir = workDirParents[--workDirDepth];
633
+    #if ENABLED(SDCARD_SORT_ALPHA)
634
+      presort();
635
+    #endif
636
+  }
592 637
 }
593 638
 
639
+#if ENABLED(SDCARD_SORT_ALPHA)
640
+
641
+  /**
642
+   * Get the name of a file in the current directory by sort-index
643
+   */
644
+  void CardReader::getfilename_sorted(const uint16_t nr) {
645
+    getfilename(
646
+      #if ENABLED(SDSORT_GCODE)
647
+        sort_alpha &&
648
+      #endif
649
+      (nr < sort_count) ? sort_order[nr] : nr
650
+    );
651
+  }
652
+
653
+  /**
654
+   * Read all the files and produce a sort key
655
+   *
656
+   * We can do this in 3 ways...
657
+   *  - Minimal RAM: Read two filenames at a time sorting along...
658
+   *  - Some RAM: Buffer the directory just for this sort
659
+   *  - Most RAM: Buffer the directory and return filenames from RAM
660
+   */
661
+  void CardReader::presort() {
662
+
663
+    // Sorting may be turned off
664
+    #if ENABLED(SDSORT_GCODE)
665
+      if (!sort_alpha) return;
666
+    #endif
667
+
668
+    // Throw away old sort index
669
+    flush_presort();
670
+
671
+    // If there are files, sort up to the limit
672
+    uint16_t fileCnt = getnrfilenames();
673
+    if (fileCnt > 0) {
674
+
675
+      // Never sort more than the max allowed
676
+      // If you use folders to organize, 20 may be enough
677
+      if (fileCnt > SDSORT_LIMIT) fileCnt = SDSORT_LIMIT;
678
+
679
+      // Sort order is always needed. May be static or dynamic.
680
+      #if ENABLED(SDSORT_DYNAMIC_RAM)
681
+        sort_order = new uint8_t[fileCnt];
682
+      #endif
683
+
684
+      // Use RAM to store the entire directory during pre-sort.
685
+      // SDSORT_LIMIT should be set to prevent over-allocation.
686
+      #if ENABLED(SDSORT_USES_RAM)
687
+
688
+        // If using dynamic ram for names, allocate on the heap.
689
+        #if ENABLED(SDSORT_CACHE_NAMES)
690
+          #if ENABLED(SDSORT_DYNAMIC_RAM)
691
+            sortshort = new char*[fileCnt];
692
+            sortnames = new char*[fileCnt];
693
+          #endif
694
+        #elif ENABLED(SDSORT_USES_STACK)
695
+          char sortnames[fileCnt][LONG_FILENAME_LENGTH];
696
+        #endif
697
+
698
+        // Folder sorting needs 1 bit per entry for flags.
699
+        #if HAS_FOLDER_SORTING
700
+          #if ENABLED(SDSORT_DYNAMIC_RAM)
701
+            isDir = new uint8_t[(fileCnt + 7) >> 3];
702
+          #elif ENABLED(SDSORT_USES_STACK)
703
+            uint8_t isDir[(fileCnt + 7) >> 3];
704
+          #endif
705
+        #endif
706
+
707
+      #else // !SDSORT_USES_RAM
708
+
709
+        // By default re-read the names from SD for every compare
710
+        // retaining only two filenames at a time. This is very
711
+        // slow but is safest and uses minimal RAM.
712
+        char name1[LONG_FILENAME_LENGTH + 1];
713
+
714
+      #endif
715
+
716
+      if (fileCnt > 1) {
717
+
718
+        // Init sort order.
719
+        for (uint16_t i = 0; i < fileCnt; i++) {
720
+          sort_order[i] = i;
721
+          // If using RAM then read all filenames now.
722
+          #if ENABLED(SDSORT_USES_RAM)
723
+            getfilename(i);
724
+            #if ENABLED(SDSORT_DYNAMIC_RAM)
725
+              // Use dynamic method to copy long filename
726
+              sortnames[i] = strdup(LONGEST_FILENAME);
727
+              #if ENABLED(SDSORT_CACHE_NAMES)
728
+                // When caching also store the short name, since
729
+                // we're replacing the getfilename() behavior.
730
+                sortshort[i] = strdup(filename);
731
+              #endif
732
+            #else
733
+              // Copy filenames into the static array
734
+              strcpy(sortnames[i], LONGEST_FILENAME);
735
+              #if ENABLED(SDSORT_CACHE_NAMES)
736
+                strcpy(sortshort[i], filename);
737
+              #endif
738
+            #endif
739
+            // char out[30];
740
+            // sprintf_P(out, PSTR("---- %i %s %s"), i, filenameIsDir ? "D" : " ", sortnames[i]);
741
+            // SERIAL_ECHOLN(out);
742
+            #if HAS_FOLDER_SORTING
743
+              const uint16_t bit = i & 0x07, ind = i >> 3;
744
+              if (bit == 0) isDir[ind] = 0x00;
745
+              if (filenameIsDir) isDir[ind] |= _BV(bit);
746
+            #endif
747
+          #endif
748
+        }
749
+
750
+        // Bubble Sort
751
+        for (uint16_t i = fileCnt; --i;) {
752
+          bool didSwap = false;
753
+          for (uint16_t j = 0; j < i; ++j) {
754
+            const uint16_t o1 = sort_order[j], o2 = sort_order[j + 1];
755
+
756
+            // Compare names from the array or just the two buffered names
757
+            #if ENABLED(SDSORT_USES_RAM)
758
+              #define _SORT_CMP_NODIR() (strcasecmp(sortnames[o1], sortnames[o2]) > 0)
759
+            #else
760
+              #define _SORT_CMP_NODIR() (strcasecmp(name1, name2) > 0)
761
+            #endif
762
+
763
+            #if HAS_FOLDER_SORTING
764
+              #if ENABLED(SDSORT_USES_RAM)
765
+                // Folder sorting needs an index and bit to test for folder-ness.
766
+                const uint8_t ind1 = o1 >> 3, bit1 = o1 & 0x07,
767
+                              ind2 = o2 >> 3, bit2 = o2 & 0x07;
768
+                #define _SORT_CMP_DIR(fs) \
769
+                  (((isDir[ind1] & _BV(bit1)) != 0) == ((isDir[ind2] & _BV(bit2)) != 0) \
770
+                    ? _SORT_CMP_NODIR() \
771
+                    : (isDir[fs > 0 ? ind1 : ind2] & (fs > 0 ? _BV(bit1) : _BV(bit2))) != 0)
772
+              #else
773
+                #define _SORT_CMP_DIR(fs) ((dir1 == filenameIsDir) ? _SORT_CMP_NODIR() : (fs > 0 ? dir1 : !dir1))
774
+              #endif
775
+            #endif
776
+
777
+            // The most economical method reads names as-needed
778
+            // throughout the loop. Slow if there are many.
779
+            #if DISABLED(SDSORT_USES_RAM)
780
+              getfilename(o1);
781
+              strcpy(name1, LONGEST_FILENAME); // save (or getfilename below will trounce it)
782
+              #if HAS_FOLDER_SORTING
783
+                bool dir1 = filenameIsDir;
784
+              #endif
785
+              getfilename(o2);
786
+              char *name2 = LONGEST_FILENAME; // use the string in-place
787
+            #endif // !SDSORT_USES_RAM
788
+
789
+            // Sort the current pair according to settings.
790
+            if (
791
+              #if HAS_FOLDER_SORTING
792
+                #if ENABLED(SDSORT_GCODE)
793
+                  sort_folders ? _SORT_CMP_DIR(sort_folders) : _SORT_CMP_NODIR()
794
+                #else
795
+                  _SORT_CMP_DIR(FOLDER_SORTING)
796
+                #endif
797
+              #else
798
+                _SORT_CMP_NODIR()
799
+              #endif
800
+            ) {
801
+              sort_order[j] = o2;
802
+              sort_order[j + 1] = o1;
803
+              didSwap = true;
804
+            }
805
+          }
806
+          if (!didSwap) break;
807
+        }
808
+        // Using RAM but not keeping names around
809
+        #if ENABLED(SDSORT_USES_RAM) && DISABLED(SDSORT_CACHE_NAMES)
810
+          #if ENABLED(SDSORT_DYNAMIC_RAM)
811
+            for (uint16_t i = 0; i < fileCnt; ++i) free(sortnames[i]);
812
+            #if HAS_FOLDER_SORTING
813
+              free(isDir);
814
+            #endif
815
+          #endif
816
+        #endif
817
+      }
818
+      else {
819
+        sort_order[0] = 0;
820
+        #if ENABLED(SDSORT_USES_RAM) && ENABLED(SDSORT_CACHE_NAMES)
821
+          getfilename(0);
822
+          #if ENABLED(SDSORT_DYNAMIC_RAM)
823
+            sortnames = new char*[1];
824
+            sortnames[0] = strdup(LONGEST_FILENAME); // malloc
825
+            sortshort = new char*[1];
826
+            sortshort[0] = strdup(filename);         // malloc
827
+            isDir = new uint8_t[1];
828
+          #else
829
+            strcpy(sortnames[0], LONGEST_FILENAME);
830
+            strcpy(sortshort[0], filename);
831
+          #endif
832
+          isDir[0] = filenameIsDir ? 0x01 : 0x00;
833
+        #endif
834
+      }
835
+
836
+      sort_count = fileCnt;
837
+    }
838
+  }
839
+
840
+  void CardReader::flush_presort() {
841
+    if (sort_count > 0) {
842
+      #if ENABLED(SDSORT_DYNAMIC_RAM)
843
+        delete sort_order;
844
+        #if ENABLED(SDSORT_CACHE_NAMES)
845
+          for (uint8_t i = 0; i < sort_count; ++i) {
846
+            free(sortshort[i]); // strdup
847
+            free(sortnames[i]); // strdup
848
+          }
849
+          delete sortshort;
850
+          delete sortnames;
851
+        #endif
852
+      #endif
853
+      sort_count = 0;
854
+    }
855
+  }
856
+
857
+#endif // SDCARD_SORT_ALPHA
858
+
594 859
 void CardReader::printingHasFinished() {
595 860
   stepper.synchronize();
596 861
   file.close();
@@ -607,6 +872,9 @@ void CardReader::printingHasFinished() {
607 872
     print_job_timer.stop();
608 873
     if (print_job_timer.duration() > 60)
609 874
       enqueue_and_echo_commands_P(PSTR("M31"));
875
+    #if ENABLED(SDCARD_SORT_ALPHA)
876
+      presort();
877
+    #endif
610 878
   }
611 879
 }
612 880
 

+ 59
- 0
Marlin/cardreader.h Vedi File

@@ -69,6 +69,16 @@ public:
69 69
   void updir();
70 70
   void setroot();
71 71
 
72
+  #if ENABLED(SDCARD_SORT_ALPHA)
73
+    void presort();
74
+    void getfilename_sorted(const uint16_t nr);
75
+    #if ENABLED(SDSORT_GCODE)
76
+      FORCE_INLINE void setSortOn(bool b) { sort_alpha = b; presort(); }
77
+      FORCE_INLINE void setSortFolders(int i) { sort_folders = i; presort(); }
78
+      //FORCE_INLINE void setSortReverse(bool b) { sort_reverse = b; }
79
+    #endif
80
+  #endif
81
+
72 82
   FORCE_INLINE void pauseSDPrint() { sdprinting = false; }
73 83
   FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
74 84
   FORCE_INLINE bool eof() { return sdpos >= filesize; }
@@ -84,6 +94,51 @@ public:
84 94
 private:
85 95
   SdFile root, *curDir, workDir, workDirParents[MAX_DIR_DEPTH];
86 96
   uint8_t workDirDepth;
97
+
98
+  // Sort files and folders alphabetically.
99
+  #if ENABLED(SDCARD_SORT_ALPHA)
100
+    uint16_t sort_count;        // Count of sorted items in the current directory
101
+    #if ENABLED(SDSORT_GCODE)
102
+      bool sort_alpha;          // Flag to enable / disable the feature
103
+      int sort_folders;         // Flag to enable / disable folder sorting
104
+      //bool sort_reverse;      // Flag to enable / disable reverse sorting
105
+    #endif
106
+
107
+    // By default the sort index is static
108
+    #if ENABLED(SDSORT_DYNAMIC_RAM)
109
+      uint8_t *sort_order;
110
+    #else
111
+      uint8_t sort_order[SDSORT_LIMIT];
112
+    #endif
113
+
114
+    // Cache filenames to speed up SD menus.
115
+    #if ENABLED(SDSORT_USES_RAM)
116
+
117
+      // If using dynamic ram for names, allocate on the heap.
118
+      #if ENABLED(SDSORT_CACHE_NAMES)
119
+        #if ENABLED(SDSORT_DYNAMIC_RAM)
120
+          char **sortshort, **sortnames;
121
+        #else
122
+          char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
123
+          char sortnames[SDSORT_LIMIT][FILENAME_LENGTH];
124
+        #endif
125
+      #elif DISABLED(SDSORT_USES_STACK)
126
+        char sortnames[SDSORT_LIMIT][FILENAME_LENGTH];
127
+      #endif
128
+
129
+      // Folder sorting uses an isDir array when caching items.
130
+      #if HAS_FOLDER_SORTING
131
+        #if ENABLED(SDSORT_DYNAMIC_RAM)
132
+          uint8_t *isDir;
133
+        #elif ENABLED(SDSORT_CACHE_NAMES) || DISABLED(SDSORT_USES_STACK)
134
+          uint8_t isDir[(SDSORT_LIMIT+7)>>3];
135
+        #endif
136
+      #endif
137
+
138
+    #endif // SDSORT_USES_RAM
139
+
140
+  #endif // SDCARD_SORT_ALPHA
141
+
87 142
   Sd2Card card;
88 143
   SdVolume volume;
89 144
   SdFile file;
@@ -103,6 +158,10 @@ private:
103 158
   uint16_t nrFiles; //counter for the files in the current directory and recycled as position counter for getting the nrFiles'th name in the directory.
104 159
   char* diveDirName;
105 160
   void lsDive(const char *prepend, SdFile parent, const char * const match=NULL);
161
+
162
+  #if ENABLED(SDCARD_SORT_ALPHA)
163
+    void flush_presort();
164
+  #endif
106 165
 };
107 166
 
108 167
 extern CardReader card;

+ 36
- 0
Marlin/example_configurations/Cartesio/Configuration_adv.h Vedi File

@@ -442,6 +442,42 @@
442 442
   // using:
443 443
   //#define MENU_ADDAUTOSTART
444 444
 
445
+  /**
446
+   * Sort SD file listings in alphabetical order.
447
+   *
448
+   * With this option enabled, items on SD cards will be sorted
449
+   * by name for easier navigation.
450
+   *
451
+   * By default...
452
+   *
453
+   *  - Use the slowest -but safest- method for sorting.
454
+   *  - Folders are sorted to the top.
455
+   *  - The sort key is statically allocated.
456
+   *  - No added G-code (M34) support.
457
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
458
+   *
459
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
460
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
461
+   * limit is exceeded.
462
+   *
463
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
464
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
465
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
466
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
467
+   */
468
+  //#define SDCARD_SORT_ALPHA
469
+
470
+  // SD Card Sorting options
471
+  #if ENABLED(SDCARD_SORT_ALPHA)
472
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
473
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
474
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
475
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
476
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
477
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
478
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
479
+  #endif
480
+
445 481
   // Show a progress bar on HD44780 LCDs for SD printing
446 482
   //#define LCD_PROGRESS_BAR
447 483
 

+ 36
- 0
Marlin/example_configurations/Felix/Configuration_adv.h Vedi File

@@ -442,6 +442,42 @@
442 442
   // using:
443 443
   //#define MENU_ADDAUTOSTART
444 444
 
445
+  /**
446
+   * Sort SD file listings in alphabetical order.
447
+   *
448
+   * With this option enabled, items on SD cards will be sorted
449
+   * by name for easier navigation.
450
+   *
451
+   * By default...
452
+   *
453
+   *  - Use the slowest -but safest- method for sorting.
454
+   *  - Folders are sorted to the top.
455
+   *  - The sort key is statically allocated.
456
+   *  - No added G-code (M34) support.
457
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
458
+   *
459
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
460
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
461
+   * limit is exceeded.
462
+   *
463
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
464
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
465
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
466
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
467
+   */
468
+  //#define SDCARD_SORT_ALPHA
469
+
470
+  // SD Card Sorting options
471
+  #if ENABLED(SDCARD_SORT_ALPHA)
472
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
473
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
474
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
475
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
476
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
477
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
478
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
479
+  #endif
480
+
445 481
   // Show a progress bar on HD44780 LCDs for SD printing
446 482
   //#define LCD_PROGRESS_BAR
447 483
 

+ 36
- 0
Marlin/example_configurations/Hephestos/Configuration_adv.h Vedi File

@@ -442,6 +442,42 @@
442 442
   // using:
443 443
   //#define MENU_ADDAUTOSTART
444 444
 
445
+  /**
446
+   * Sort SD file listings in alphabetical order.
447
+   *
448
+   * With this option enabled, items on SD cards will be sorted
449
+   * by name for easier navigation.
450
+   *
451
+   * By default...
452
+   *
453
+   *  - Use the slowest -but safest- method for sorting.
454
+   *  - Folders are sorted to the top.
455
+   *  - The sort key is statically allocated.
456
+   *  - No added G-code (M34) support.
457
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
458
+   *
459
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
460
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
461
+   * limit is exceeded.
462
+   *
463
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
464
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
465
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
466
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
467
+   */
468
+  //#define SDCARD_SORT_ALPHA
469
+
470
+  // SD Card Sorting options
471
+  #if ENABLED(SDCARD_SORT_ALPHA)
472
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
473
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
474
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
475
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
476
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
477
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
478
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
479
+  #endif
480
+
445 481
   // Show a progress bar on HD44780 LCDs for SD printing
446 482
   //#define LCD_PROGRESS_BAR
447 483
 

+ 36
- 0
Marlin/example_configurations/Hephestos_2/Configuration_adv.h Vedi File

@@ -442,6 +442,42 @@
442 442
   // using:
443 443
   //#define MENU_ADDAUTOSTART
444 444
 
445
+  /**
446
+   * Sort SD file listings in alphabetical order.
447
+   *
448
+   * With this option enabled, items on SD cards will be sorted
449
+   * by name for easier navigation.
450
+   *
451
+   * By default...
452
+   *
453
+   *  - Use the slowest -but safest- method for sorting.
454
+   *  - Folders are sorted to the top.
455
+   *  - The sort key is statically allocated.
456
+   *  - No added G-code (M34) support.
457
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
458
+   *
459
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
460
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
461
+   * limit is exceeded.
462
+   *
463
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
464
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
465
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
466
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
467
+   */
468
+  //#define SDCARD_SORT_ALPHA
469
+
470
+  // SD Card Sorting options
471
+  #if ENABLED(SDCARD_SORT_ALPHA)
472
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
473
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
474
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
475
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
476
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
477
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
478
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
479
+  #endif
480
+
445 481
   // Show a progress bar on HD44780 LCDs for SD printing
446 482
   //#define LCD_PROGRESS_BAR
447 483
 

+ 36
- 0
Marlin/example_configurations/K8200/Configuration_adv.h Vedi File

@@ -455,6 +455,42 @@
455 455
   // using:
456 456
   #define MENU_ADDAUTOSTART
457 457
 
458
+  /**
459
+   * Sort SD file listings in alphabetical order.
460
+   *
461
+   * With this option enabled, items on SD cards will be sorted
462
+   * by name for easier navigation.
463
+   *
464
+   * By default...
465
+   *
466
+   *  - Use the slowest -but safest- method for sorting.
467
+   *  - Folders are sorted to the top.
468
+   *  - The sort key is statically allocated.
469
+   *  - No added G-code (M34) support.
470
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
471
+   *
472
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
473
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
474
+   * limit is exceeded.
475
+   *
476
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
477
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
478
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
479
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
480
+   */
481
+  //#define SDCARD_SORT_ALPHA
482
+
483
+  // SD Card Sorting options
484
+  #if ENABLED(SDCARD_SORT_ALPHA)
485
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
486
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
487
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
488
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
489
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
490
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
491
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
492
+  #endif
493
+
458 494
   // Show a progress bar on HD44780 LCDs for SD printing
459 495
   #define LCD_PROGRESS_BAR
460 496
 

+ 36
- 0
Marlin/example_configurations/K8400/Configuration_adv.h Vedi File

@@ -442,6 +442,42 @@
442 442
   // using:
443 443
   //#define MENU_ADDAUTOSTART
444 444
 
445
+  /**
446
+   * Sort SD file listings in alphabetical order.
447
+   *
448
+   * With this option enabled, items on SD cards will be sorted
449
+   * by name for easier navigation.
450
+   *
451
+   * By default...
452
+   *
453
+   *  - Use the slowest -but safest- method for sorting.
454
+   *  - Folders are sorted to the top.
455
+   *  - The sort key is statically allocated.
456
+   *  - No added G-code (M34) support.
457
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
458
+   *
459
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
460
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
461
+   * limit is exceeded.
462
+   *
463
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
464
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
465
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
466
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
467
+   */
468
+  //#define SDCARD_SORT_ALPHA
469
+
470
+  // SD Card Sorting options
471
+  #if ENABLED(SDCARD_SORT_ALPHA)
472
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
473
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
474
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
475
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
476
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
477
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
478
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
479
+  #endif
480
+
445 481
   // Show a progress bar on HD44780 LCDs for SD printing
446 482
   //#define LCD_PROGRESS_BAR
447 483
 

+ 36
- 0
Marlin/example_configurations/RigidBot/Configuration_adv.h Vedi File

@@ -442,6 +442,42 @@
442 442
   // using:
443 443
   //#define MENU_ADDAUTOSTART
444 444
 
445
+  /**
446
+   * Sort SD file listings in alphabetical order.
447
+   *
448
+   * With this option enabled, items on SD cards will be sorted
449
+   * by name for easier navigation.
450
+   *
451
+   * By default...
452
+   *
453
+   *  - Use the slowest -but safest- method for sorting.
454
+   *  - Folders are sorted to the top.
455
+   *  - The sort key is statically allocated.
456
+   *  - No added G-code (M34) support.
457
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
458
+   *
459
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
460
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
461
+   * limit is exceeded.
462
+   *
463
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
464
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
465
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
466
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
467
+   */
468
+  //#define SDCARD_SORT_ALPHA
469
+
470
+  // SD Card Sorting options
471
+  #if ENABLED(SDCARD_SORT_ALPHA)
472
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
473
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
474
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
475
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
476
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
477
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
478
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
479
+  #endif
480
+
445 481
   // Show a progress bar on HD44780 LCDs for SD printing
446 482
   //#define LCD_PROGRESS_BAR
447 483
 

+ 36
- 0
Marlin/example_configurations/SCARA/Configuration_adv.h Vedi File

@@ -442,6 +442,42 @@
442 442
   // using:
443 443
   //#define MENU_ADDAUTOSTART
444 444
 
445
+  /**
446
+   * Sort SD file listings in alphabetical order.
447
+   *
448
+   * With this option enabled, items on SD cards will be sorted
449
+   * by name for easier navigation.
450
+   *
451
+   * By default...
452
+   *
453
+   *  - Use the slowest -but safest- method for sorting.
454
+   *  - Folders are sorted to the top.
455
+   *  - The sort key is statically allocated.
456
+   *  - No added G-code (M34) support.
457
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
458
+   *
459
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
460
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
461
+   * limit is exceeded.
462
+   *
463
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
464
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
465
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
466
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
467
+   */
468
+  //#define SDCARD_SORT_ALPHA
469
+
470
+  // SD Card Sorting options
471
+  #if ENABLED(SDCARD_SORT_ALPHA)
472
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
473
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
474
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
475
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
476
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
477
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
478
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
479
+  #endif
480
+
445 481
   // Show a progress bar on HD44780 LCDs for SD printing
446 482
   //#define LCD_PROGRESS_BAR
447 483
 

+ 36
- 0
Marlin/example_configurations/TAZ4/Configuration_adv.h Vedi File

@@ -450,6 +450,42 @@
450 450
   // using:
451 451
   //#define MENU_ADDAUTOSTART
452 452
 
453
+  /**
454
+   * Sort SD file listings in alphabetical order.
455
+   *
456
+   * With this option enabled, items on SD cards will be sorted
457
+   * by name for easier navigation.
458
+   *
459
+   * By default...
460
+   *
461
+   *  - Use the slowest -but safest- method for sorting.
462
+   *  - Folders are sorted to the top.
463
+   *  - The sort key is statically allocated.
464
+   *  - No added G-code (M34) support.
465
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
466
+   *
467
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
468
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
469
+   * limit is exceeded.
470
+   *
471
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
472
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
473
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
474
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
475
+   */
476
+  //#define SDCARD_SORT_ALPHA
477
+
478
+  // SD Card Sorting options
479
+  #if ENABLED(SDCARD_SORT_ALPHA)
480
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
481
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
482
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
483
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
484
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
485
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
486
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
487
+  #endif
488
+
453 489
   // Show a progress bar on HD44780 LCDs for SD printing
454 490
   //#define LCD_PROGRESS_BAR
455 491
 

+ 36
- 0
Marlin/example_configurations/WITBOX/Configuration_adv.h Vedi File

@@ -442,6 +442,42 @@
442 442
   // using:
443 443
   //#define MENU_ADDAUTOSTART
444 444
 
445
+  /**
446
+   * Sort SD file listings in alphabetical order.
447
+   *
448
+   * With this option enabled, items on SD cards will be sorted
449
+   * by name for easier navigation.
450
+   *
451
+   * By default...
452
+   *
453
+   *  - Use the slowest -but safest- method for sorting.
454
+   *  - Folders are sorted to the top.
455
+   *  - The sort key is statically allocated.
456
+   *  - No added G-code (M34) support.
457
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
458
+   *
459
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
460
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
461
+   * limit is exceeded.
462
+   *
463
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
464
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
465
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
466
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
467
+   */
468
+  //#define SDCARD_SORT_ALPHA
469
+
470
+  // SD Card Sorting options
471
+  #if ENABLED(SDCARD_SORT_ALPHA)
472
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
473
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
474
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
475
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
476
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
477
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
478
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
479
+  #endif
480
+
445 481
   // Show a progress bar on HD44780 LCDs for SD printing
446 482
   //#define LCD_PROGRESS_BAR
447 483
 

+ 36
- 0
Marlin/example_configurations/delta/generic/Configuration_adv.h Vedi File

@@ -444,6 +444,42 @@
444 444
   // using:
445 445
   //#define MENU_ADDAUTOSTART
446 446
 
447
+  /**
448
+   * Sort SD file listings in alphabetical order.
449
+   *
450
+   * With this option enabled, items on SD cards will be sorted
451
+   * by name for easier navigation.
452
+   *
453
+   * By default...
454
+   *
455
+   *  - Use the slowest -but safest- method for sorting.
456
+   *  - Folders are sorted to the top.
457
+   *  - The sort key is statically allocated.
458
+   *  - No added G-code (M34) support.
459
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
460
+   *
461
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
462
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
463
+   * limit is exceeded.
464
+   *
465
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
466
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
467
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
468
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
469
+   */
470
+  //#define SDCARD_SORT_ALPHA
471
+
472
+  // SD Card Sorting options
473
+  #if ENABLED(SDCARD_SORT_ALPHA)
474
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
475
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
476
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
477
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
478
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
479
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
480
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
481
+  #endif
482
+
447 483
   // Show a progress bar on HD44780 LCDs for SD printing
448 484
   //#define LCD_PROGRESS_BAR
449 485
 

+ 36
- 0
Marlin/example_configurations/delta/kossel_mini/Configuration_adv.h Vedi File

@@ -444,6 +444,42 @@
444 444
   // using:
445 445
   //#define MENU_ADDAUTOSTART
446 446
 
447
+  /**
448
+   * Sort SD file listings in alphabetical order.
449
+   *
450
+   * With this option enabled, items on SD cards will be sorted
451
+   * by name for easier navigation.
452
+   *
453
+   * By default...
454
+   *
455
+   *  - Use the slowest -but safest- method for sorting.
456
+   *  - Folders are sorted to the top.
457
+   *  - The sort key is statically allocated.
458
+   *  - No added G-code (M34) support.
459
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
460
+   *
461
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
462
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
463
+   * limit is exceeded.
464
+   *
465
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
466
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
467
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
468
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
469
+   */
470
+  //#define SDCARD_SORT_ALPHA
471
+
472
+  // SD Card Sorting options
473
+  #if ENABLED(SDCARD_SORT_ALPHA)
474
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
475
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
476
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
477
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
478
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
479
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
480
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
481
+  #endif
482
+
447 483
   // Show a progress bar on HD44780 LCDs for SD printing
448 484
   //#define LCD_PROGRESS_BAR
449 485
 

+ 36
- 0
Marlin/example_configurations/delta/kossel_pro/Configuration_adv.h Vedi File

@@ -449,6 +449,42 @@
449 449
   // using:
450 450
   //#define MENU_ADDAUTOSTART
451 451
 
452
+  /**
453
+   * Sort SD file listings in alphabetical order.
454
+   *
455
+   * With this option enabled, items on SD cards will be sorted
456
+   * by name for easier navigation.
457
+   *
458
+   * By default...
459
+   *
460
+   *  - Use the slowest -but safest- method for sorting.
461
+   *  - Folders are sorted to the top.
462
+   *  - The sort key is statically allocated.
463
+   *  - No added G-code (M34) support.
464
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
465
+   *
466
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
467
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
468
+   * limit is exceeded.
469
+   *
470
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
471
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
472
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
473
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
474
+   */
475
+  //#define SDCARD_SORT_ALPHA
476
+
477
+  // SD Card Sorting options
478
+  #if ENABLED(SDCARD_SORT_ALPHA)
479
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
480
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
481
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
482
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
483
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
484
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
485
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
486
+  #endif
487
+
452 488
   // Show a progress bar on HD44780 LCDs for SD printing
453 489
   //#define LCD_PROGRESS_BAR
454 490
 

+ 36
- 0
Marlin/example_configurations/delta/kossel_xl/Configuration_adv.h Vedi File

@@ -444,6 +444,42 @@
444 444
   // using:
445 445
   //#define MENU_ADDAUTOSTART
446 446
 
447
+  /**
448
+   * Sort SD file listings in alphabetical order.
449
+   *
450
+   * With this option enabled, items on SD cards will be sorted
451
+   * by name for easier navigation.
452
+   *
453
+   * By default...
454
+   *
455
+   *  - Use the slowest -but safest- method for sorting.
456
+   *  - Folders are sorted to the top.
457
+   *  - The sort key is statically allocated.
458
+   *  - No added G-code (M34) support.
459
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
460
+   *
461
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
462
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
463
+   * limit is exceeded.
464
+   *
465
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
466
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
467
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
468
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
469
+   */
470
+  //#define SDCARD_SORT_ALPHA
471
+
472
+  // SD Card Sorting options
473
+  #if ENABLED(SDCARD_SORT_ALPHA)
474
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
475
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
476
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
477
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
478
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
479
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
480
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
481
+  #endif
482
+
447 483
   // Show a progress bar on HD44780 LCDs for SD printing
448 484
   //#define LCD_PROGRESS_BAR
449 485
 

+ 36
- 0
Marlin/example_configurations/makibox/Configuration_adv.h Vedi File

@@ -442,6 +442,42 @@
442 442
   // using:
443 443
   //#define MENU_ADDAUTOSTART
444 444
 
445
+  /**
446
+   * Sort SD file listings in alphabetical order.
447
+   *
448
+   * With this option enabled, items on SD cards will be sorted
449
+   * by name for easier navigation.
450
+   *
451
+   * By default...
452
+   *
453
+   *  - Use the slowest -but safest- method for sorting.
454
+   *  - Folders are sorted to the top.
455
+   *  - The sort key is statically allocated.
456
+   *  - No added G-code (M34) support.
457
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
458
+   *
459
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
460
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
461
+   * limit is exceeded.
462
+   *
463
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
464
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
465
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
466
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
467
+   */
468
+  //#define SDCARD_SORT_ALPHA
469
+
470
+  // SD Card Sorting options
471
+  #if ENABLED(SDCARD_SORT_ALPHA)
472
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
473
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
474
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
475
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
476
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
477
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
478
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
479
+  #endif
480
+
445 481
   // Show a progress bar on HD44780 LCDs for SD printing
446 482
   //#define LCD_PROGRESS_BAR
447 483
 

+ 36
- 0
Marlin/example_configurations/tvrrug/Round2/Configuration_adv.h Vedi File

@@ -442,6 +442,42 @@
442 442
   // using:
443 443
   //#define MENU_ADDAUTOSTART
444 444
 
445
+  /**
446
+   * Sort SD file listings in alphabetical order.
447
+   *
448
+   * With this option enabled, items on SD cards will be sorted
449
+   * by name for easier navigation.
450
+   *
451
+   * By default...
452
+   *
453
+   *  - Use the slowest -but safest- method for sorting.
454
+   *  - Folders are sorted to the top.
455
+   *  - The sort key is statically allocated.
456
+   *  - No added G-code (M34) support.
457
+   *  - 40 item sorting limit. (Items after the first 40 are unsorted.)
458
+   *
459
+   * SD sorting uses static allocation (as set by SDSORT_LIMIT), allowing the
460
+   * compiler to calculate the worst-case usage and throw an error if the SRAM
461
+   * limit is exceeded.
462
+   *
463
+   *  - SDSORT_USES_RAM provides faster sorting via a static directory buffer.
464
+   *  - SDSORT_USES_STACK does the same, but uses a local stack-based buffer.
465
+   *  - SDSORT_CACHE_NAMES will retain the sorted file listing in RAM. (Expensive!)
466
+   *  - SDSORT_DYNAMIC_RAM only uses RAM when the SD menu is visible. (Use with caution!)
467
+   */
468
+  //#define SDCARD_SORT_ALPHA
469
+
470
+  // SD Card Sorting options
471
+  #if ENABLED(SDCARD_SORT_ALPHA)
472
+    #define SDSORT_LIMIT       40     // Maximum number of sorted items (10-256).
473
+    #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
474
+    #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
475
+    #define SDSORT_USES_RAM    false  // Pre-allocate a static array for faster pre-sorting.
476
+    #define SDSORT_USES_STACK  false  // Prefer the stack for pre-sorting to give back some SRAM. (Negated by next 2 options.)
477
+    #define SDSORT_CACHE_NAMES false  // Keep sorted items in RAM longer for speedy performance. Most expensive option.
478
+    #define SDSORT_DYNAMIC_RAM false  // Use dynamic allocation (within SD menus). Least expensive option. Set SDSORT_LIMIT before use!
479
+  #endif
480
+
445 481
   // Show a progress bar on HD44780 LCDs for SD printing
446 482
   //#define LCD_PROGRESS_BAR
447 483
 

+ 11
- 6
Marlin/ultralcd.cpp Vedi File

@@ -2277,12 +2277,17 @@ KeepDrawing:
2277 2277
 
2278 2278
       for (uint16_t i = 0; i < fileCnt; i++) {
2279 2279
         if (_menuLineNr == _thisItemNr) {
2280
-          card.getfilename(
2281
-             #if ENABLED(SDCARD_RATHERRECENTFIRST)
2282
-               fileCnt-1 -
2283
-             #endif
2284
-             i
2285
-          );
2280
+          #if ENABLED(SDCARD_RATHERRECENTFIRST) && DISABLED(SDCARD_SORT_ALPHA)
2281
+            int nr = fileCnt - 1 - i;
2282
+          #else
2283
+            int nr = i;
2284
+          #endif
2285
+
2286
+          #if ENABLED(SDCARD_SORT_ALPHA)
2287
+            card.getfilename_sorted(nr);
2288
+          #else
2289
+            card.getfilename(nr);
2290
+          #endif
2286 2291
 
2287 2292
           if (card.filenameIsDir)
2288 2293
             MENU_ITEM(sddirectory, MSG_CARD_MENU, card.filename, card.longFilename);

Loading…
Annulla
Salva