Pārlūkot izejas kodu

Improvements, more SORT_USES_MORE_RAM

With this option, always keeps the dir in RAM, doubling as a cache for
getfilename. A board with only 8K of SRAM is cutting it very close.
Scott Lahteine 10 gadus atpakaļ
vecāks
revīzija
ae081d0fe0
3 mainītis faili ar 78 papildinājumiem un 81 dzēšanām
  1. 67
    70
      Marlin/cardreader.cpp
  2. 9
    9
      Marlin/cardreader.h
  3. 2
    2
      Marlin/ultralcd.cpp

+ 67
- 70
Marlin/cardreader.cpp Parādīt failu

11
 
11
 
12
 CardReader::CardReader()
12
 CardReader::CardReader()
13
 {
13
 {
14
-  #if defined(SDCARD_SORT_ALPHA) && SORT_USES_MORE_RAM
15
-   sortnames = NULL;
14
+  #ifdef SDCARD_SORT_ALPHA
16
    sort_count = 0;
15
    sort_count = 0;
17
   #endif
16
   #endif
18
    filesize = 0;
17
    filesize = 0;
37
   autostart_atmillis=millis()+5000;
36
   autostart_atmillis=millis()+5000;
38
 }
37
 }
39
 
38
 
40
-char *createFilename(char *buffer,const dir_t &p) //buffer>12characters
39
+char *createFilename(char *buffer, const dir_t &p) //buffer>12characters
41
 {
40
 {
42
   char *pos=buffer;
41
   char *pos=buffer;
43
-  for (uint8_t i = 0; i < 11; i++) 
44
-  {
45
-    if (p.name[i] == ' ')continue;
46
-    if (i == 8) 
47
-    {
48
-      *pos++='.';
49
-    }
50
-    *pos++=p.name[i];
42
+  for (uint8_t i = 0; i < 11; i++) {
43
+    if (p.name[i] == ' ') continue;
44
+    if (i == 8) *pos++ = '.';
45
+    *pos++ = p.name[i];
51
   }
46
   }
52
-  *pos++=0;
47
+  *pos++ = 0;
53
   return buffer;
48
   return buffer;
54
 }
49
 }
55
 
50
 
59
   dir_t p;
54
   dir_t p;
60
   uint8_t cnt=0;
55
   uint8_t cnt=0;
61
  
56
  
62
-  while (parent.readDir(p, diveFilename) > 0)
57
+  while (parent.readDir(p, longFilename) > 0)
63
   {
58
   {
64
     if( DIR_IS_SUBDIR(&p) && lsAction!=LS_Count && lsAction!=LS_GetFilename) // hence LS_SerialPrint
59
     if( DIR_IS_SUBDIR(&p) && lsAction!=LS_Count && lsAction!=LS_GetFilename) // hence LS_SerialPrint
65
     {
60
     {
96
     {
91
     {
97
       if (p.name[0] == DIR_NAME_FREE) break;
92
       if (p.name[0] == DIR_NAME_FREE) break;
98
       if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
93
       if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
99
-      if (diveFilename[0] != '\0' &&
100
-          (diveFilename[0] == '.' || diveFilename[0] == '_')) continue;
94
+      if (longFilename[0] != '\0' &&
95
+          (longFilename[0] == '.' || longFilename[0] == '_')) continue;
101
       if ( p.name[0] == '.')
96
       if ( p.name[0] == '.')
102
       {
97
       {
103
         if ( p.name[1] != '.')
98
         if ( p.name[1] != '.')
556
   
551
   
557
 }
552
 }
558
 
553
 
559
-void CardReader::getfilename(const uint8_t nr)
554
+void CardReader::getfilename(const uint16_t nr)
560
 {
555
 {
561
-  #if defined(SDCARD_SORT_ALPHA) && SORT_USES_MORE_RAM
556
+  #if defined(SDCARD_SORT_ALPHA) && SORT_USES_RAM && SORT_USES_MORE_RAM
562
     if (nr < sort_count) {
557
     if (nr < sort_count) {
563
-      strcpy(diveFilename, sortnames[nr]);
558
+      strcpy(longFilename, sortnames[nr]);
559
+      filenameIsDir = isDir[nr];
564
       return;
560
       return;
565
     }
561
     }
566
   #endif
562
   #endif
567
-
568
   curDir=&workDir;
563
   curDir=&workDir;
569
   lsAction=LS_GetFilename;
564
   lsAction=LS_GetFilename;
570
   nrFiles=nr;
565
   nrFiles=nr;
571
   curDir->rewind();
566
   curDir->rewind();
572
   lsDive("",*curDir);
567
   lsDive("",*curDir);
573
-  
574
 }
568
 }
575
 
569
 
576
 uint16_t CardReader::getnrfilenames()
570
 uint16_t CardReader::getnrfilenames()
631
 /**
625
 /**
632
  * Get the name of a file in the current directory by sort-index
626
  * Get the name of a file in the current directory by sort-index
633
  */
627
  */
634
-void CardReader::getfilename_sorted(const uint8_t nr) {
635
-  #if SORT_USES_MORE_RAM
636
-    getfilename(nr < sort_count ? sort_order[nr] : nr);
637
-  #else
638
-    getfilename(nr < SORT_LIMIT ? sort_order[nr] : nr);
639
-  #endif
628
+void CardReader::getfilename_sorted(const uint16_t nr) {
629
+  getfilename(nr < sort_count ? sort_order[nr] : nr);
640
 }
630
 }
641
 
631
 
642
 /**
632
 /**
656
 
646
 
657
     if (fileCnt > SORT_LIMIT) fileCnt = SORT_LIMIT;
647
     if (fileCnt > SORT_LIMIT) fileCnt = SORT_LIMIT;
658
 
648
 
659
-    #if SORT_USES_MORE_RAM
660
-      sortnames = (char**)malloc(fileCnt * sizeof(char*));
661
-      sort_count = fileCnt;
662
-    #elif SORT_USES_RAM
663
-      char *sortnames[fileCnt];
664
-      #if FOLDER_SORTING != 0
665
-        uint8_t isdir[fileCnt];
649
+    #if SORT_USES_RAM
650
+      #if SORT_USES_MORE_RAM
651
+        sortnames = (char**)calloc(fileCnt, sizeof(char*));
652
+      #else
653
+        char *sortnames[fileCnt];
666
       #endif
654
       #endif
667
     #else
655
     #else
668
-      char sortname[LONG_FILENAME_LENGTH+1];
656
+      char name1[LONG_FILENAME_LENGTH+1];
657
+    #endif
658
+
659
+    #if FOLDER_SORTING != 0
660
+      #if SORT_USES_RAM && SORT_USES_MORE_RAM
661
+        isDir = (uint8_t*)calloc(fileCnt, sizeof(uint8_t));
662
+      #else
663
+        uint8_t isDir[fileCnt];
664
+      #endif
669
     #endif
665
     #endif
670
 
666
 
667
+    sort_count = fileCnt;
668
+    sort_order = new uint8_t[fileCnt];
669
+
671
     if (fileCnt > 1) {
670
     if (fileCnt > 1) {
672
 
671
 
673
-      // Init sort order [and get filenames]
674
-      for (int i=0; i<fileCnt; i++) {
675
-        int byte=i/8, bit=1<<(i%8);
672
+      // Init sort order. If using RAM then read all filenames now.
673
+      for (uint16_t i=0; i<fileCnt; i++) {
676
         sort_order[i] = i;
674
         sort_order[i] = i;
677
         #if SORT_USES_RAM
675
         #if SORT_USES_RAM
678
           getfilename(i);
676
           getfilename(i);
679
-          char *name = diveFilename[0] ? diveFilename : filename;
680
-          // SERIAL_ECHOPGM("--- ");
681
-          // SERIAL_ECHOLN(name);
682
-          sortnames[i] = (char*)malloc(strlen(name) + 1);
683
-          strcpy(sortnames[i], name);
677
+          sortnames[i] = strdup(longFilename[0] ? longFilename : filename);
678
+          // char out[30];
679
+          // sprintf_P(out, PSTR("---- %i %s %s"), i, filenameIsDir ? "D" : " ", sortnames[i]);
680
+          // SERIAL_ECHOLN(out);
684
           #if FOLDER_SORTING != 0
681
           #if FOLDER_SORTING != 0
685
-            isdir[i] = filenameIsDir;
682
+            isDir[i] = filenameIsDir;
686
           #endif
683
           #endif
687
         #endif
684
         #endif
688
       }
685
       }
689
 
686
 
690
       // Bubble Sort
687
       // Bubble Sort
691
-      for (uint8_t i=fileCnt; --i;) {
688
+      for (uint16_t i=fileCnt; --i;) {
692
         bool cmp, didSwap = false;
689
         bool cmp, didSwap = false;
693
-        for (uint8_t j=0; j<i; ++j) {
694
-          int s1 = j, s2 = j+1, o1 = sort_order[s1], o2 = sort_order[s2];
690
+        for (uint16_t j=0; j<i; ++j) {
691
+          uint16_t s1 = j, s2 = j+1, o1 = sort_order[s1], o2 = sort_order[s2];
695
           #if SORT_USES_RAM
692
           #if SORT_USES_RAM
696
             #if FOLDER_SORTING != 0
693
             #if FOLDER_SORTING != 0
697
-              cmp = (isdir[o1] == isdir[o2]) ? (strcasecmp(sortnames[o1], sortnames[o2]) > 0) : isdir[FOLDER_SORTING > 0 ? o1 : o2];
694
+              cmp = (isDir[o1] == isDir[o2]) ? (strcasecmp(sortnames[o1], sortnames[o2]) > 0) : isDir[FOLDER_SORTING > 0 ? o1 : o2];
698
             #else
695
             #else
699
-              cmp = strcasecmp(sortnames[o1], sortnames[o2]) > 0);
696
+              cmp = strcasecmp(sortnames[o1], sortnames[o2]) > 0;
700
             #endif
697
             #endif
701
           #else
698
           #else
702
             getfilename(o1);
699
             getfilename(o1);
700
+            strcpy(name1, longFilename[0] ? longFilename : filename);
703
             #if FOLDER_SORTING != 0
701
             #if FOLDER_SORTING != 0
704
               bool dir1 = filenameIsDir;
702
               bool dir1 = filenameIsDir;
705
             #endif
703
             #endif
706
-            char *name = diveFilename[0] ? diveFilename : filename;
707
-            strcpy(sortname, name);
708
             getfilename(o2);
704
             getfilename(o2);
709
-            name = diveFilename[0] ? diveFilename : filename;
705
+            char *name2 = longFilename[0] ? longFilename : filename;
710
             #if FOLDER_SORTING != 0
706
             #if FOLDER_SORTING != 0
711
-              cmp = (dir1 == filenameIsDir) ? (strcasecmp(sortname, name) > 0) : (FOLDER_SORTING > 0 ? dir1 : !dir1);
707
+              cmp = (dir1 == filenameIsDir) ? (strcasecmp(name1, name2) > 0) : (FOLDER_SORTING > 0 ? dir1 : !dir1);
712
             #else
708
             #else
713
-              cmp = strcasecmp(sortname, name) > 0);
709
+              cmp = strcasecmp(name1, name2) > 0;
714
             #endif
710
             #endif
715
           #endif
711
           #endif
716
           if (cmp) {
712
           if (cmp) {
717
-            // SERIAL_ECHOPGM("Swap ");
718
-            // SERIAL_ECHOLN(sortnames[o1]);
719
-            // SERIAL_ECHOPGM(" for ");
720
-            // SERIAL_ECHOLN(sortnames[o2]);
713
+            // char out[LONG_FILENAME_LENGTH*2+20];
714
+            // sprintf_P(out, PSTR("Swap %i %s for %i %s"), o1, sortnames[o1], o2, sortnames[o2]);
715
+            // SERIAL_ECHOLN(out);
721
             sort_order[s1] = o2;
716
             sort_order[s1] = o2;
722
             sort_order[s2] = o1;
717
             sort_order[s2] = o1;
723
             didSwap = true;
718
             didSwap = true;
727
       }
722
       }
728
 
723
 
729
       #if SORT_USES_RAM && !SORT_USES_MORE_RAM
724
       #if SORT_USES_RAM && !SORT_USES_MORE_RAM
730
-        for (int i=0; i < fileCnt; ++i) free(sortnames[i]);
725
+        for (uint16_t i=0; i<fileCnt; ++i) free(sortnames[i]);
731
       #endif
726
       #endif
732
     }
727
     }
733
     else {
728
     else {
734
       sort_order[0] = 0;
729
       sort_order[0] = 0;
730
+      #if SORT_USES_RAM && SORT_USES_MORE_RAM
731
+        sortnames = (char**)malloc(sizeof(char*));
732
+        isDir = (uint8_t*)malloc(sizeof(uint8_t));
733
+        getfilename(0);
734
+        sortnames[0] = strdup(longFilename[0] ? longFilename : filename);
735
+        isDir[0] = filenameIsDir;
736
+      #endif
735
     }
737
     }
736
 
738
 
737
   }
739
   }
738
 }
740
 }
739
 
741
 
740
 void CardReader::flush_presort() {
742
 void CardReader::flush_presort() {
741
-  #if SORT_USES_MORE_RAM
742
-    if (sort_count > 0) {
743
-      for (int i=0; i < sort_count; ++i) {
744
-        free(sortnames[i]);
745
-        sort_order[i] = i;
746
-      }
743
+  if (sort_count > 0) {
744
+    #if SORT_USES_RAM && SORT_USES_MORE_RAM
745
+      for (uint8_t i=0; i<sort_count; ++i) free(sortnames[i]);
747
       free(sortnames);
746
       free(sortnames);
748
-      sortnames = NULL;
749
-      sort_count = 0;
750
-    }
751
-  #else
752
-    for (int i=SORT_LIMIT; --i;) sort_order[i] = i;
753
-  #endif
747
+    #endif
748
+    delete sort_order;
749
+    sort_count = 0;
750
+  }
754
 }
751
 }
755
 
752
 
756
 #endif // SDCARD_SORT_ALPHA
753
 #endif // SDCARD_SORT_ALPHA

+ 9
- 9
Marlin/cardreader.h Parādīt failu

6
 #define MAX_DIR_DEPTH 10          // Maximum folder depth
6
 #define MAX_DIR_DEPTH 10          // Maximum folder depth
7
 #define SORT_USES_RAM false       // Buffer while sorting, else re-read from SD
7
 #define SORT_USES_RAM false       // Buffer while sorting, else re-read from SD
8
 #define SORT_USES_MORE_RAM false  // Always keep the directory in RAM
8
 #define SORT_USES_MORE_RAM false  // Always keep the directory in RAM
9
-#define SORT_LIMIT 256            // Maximum number of sorted items
9
+#define SORT_LIMIT 64             // Maximum number of sorted items
10
 #define FOLDER_SORTING -1         // -1=above  0=none  1=below
10
 #define FOLDER_SORTING -1         // -1=above  0=none  1=below
11
 
11
 
12
 #include "SdFile.h"
12
 #include "SdFile.h"
32
   void getStatus();
32
   void getStatus();
33
   void printingHasFinished();
33
   void printingHasFinished();
34
 
34
 
35
-  void getfilename(const uint8_t nr);
35
+  void getfilename(const uint16_t nr);
36
   uint16_t getnrfilenames();
36
   uint16_t getnrfilenames();
37
   
37
   
38
   void getAbsFilename(char *t);
38
   void getAbsFilename(char *t);
46
 #ifdef SDCARD_SORT_ALPHA
46
 #ifdef SDCARD_SORT_ALPHA
47
   void presort();
47
   void presort();
48
   void flush_presort();
48
   void flush_presort();
49
-  void getfilename_sorted(const uint8_t nr);
49
+  void getfilename_sorted(const uint16_t nr);
50
 #endif
50
 #endif
51
 
51
 
52
 
52
 
60
 public:
60
 public:
61
   bool saving;
61
   bool saving;
62
   bool logging;
62
   bool logging;
63
-  bool sdprinting ;  
63
+  bool sdprinting;
64
   bool cardOK;
64
   bool cardOK;
65
   char filename[FILENAME_LENGTH];
65
   char filename[FILENAME_LENGTH];
66
-  char diveFilename[LONG_FILENAME_LENGTH];
66
+  char longFilename[LONG_FILENAME_LENGTH];
67
   bool filenameIsDir;
67
   bool filenameIsDir;
68
   int lastnr; //last number of the autostart;
68
   int lastnr; //last number of the autostart;
69
 private:
69
 private:
70
   SdFile root,*curDir,workDir,workDirParents[MAX_DIR_DEPTH];
70
   SdFile root,*curDir,workDir,workDirParents[MAX_DIR_DEPTH];
71
   uint16_t workDirDepth;
71
   uint16_t workDirDepth;
72
 #ifdef SDCARD_SORT_ALPHA
72
 #ifdef SDCARD_SORT_ALPHA
73
+  uint16_t sort_count;
74
+  uint8_t *sort_order;
73
   #if SORT_USES_MORE_RAM
75
   #if SORT_USES_MORE_RAM
74
-    uint16_t sort_count;
75
     char **sortnames;
76
     char **sortnames;
76
-  #else
77
-    uint8_t sort_order[SORT_LIMIT];
77
+    uint8_t *isDir;
78
   #endif
78
   #endif
79
 #endif
79
 #endif
80
   Sd2Card card;
80
   Sd2Card card;
93
   bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
93
   bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
94
   
94
   
95
   LsAction lsAction; //stored for recursion.
95
   LsAction lsAction; //stored for recursion.
96
-  int16_t nrFiles; //counter for the files in the current directory and recycled as position counter for getting the nrFiles'th name in the directory.
96
+  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.
97
   char* diveDirName;
97
   char* diveDirName;
98
   void lsDive(const char *prepend,SdFile parent);
98
   void lsDive(const char *prepend,SdFile parent);
99
 };
99
 };

+ 2
- 2
Marlin/ultralcd.cpp Parādīt failu

1017
             #endif
1017
             #endif
1018
 
1018
 
1019
             if (card.filenameIsDir) {
1019
             if (card.filenameIsDir) {
1020
-              MENU_ITEM(sddirectory, MSG_CARD_MENU, card.filename, card.diveFilename);
1020
+              MENU_ITEM(sddirectory, MSG_CARD_MENU, card.filename, card.longFilename);
1021
             }
1021
             }
1022
             else {
1022
             else {
1023
-              MENU_ITEM(sdfile, MSG_CARD_MENU, card.filename, card.diveFilename);
1023
+              MENU_ITEM(sdfile, MSG_CARD_MENU, card.filename, card.longFilename);
1024
             }
1024
             }
1025
         }else{
1025
         }else{
1026
             MENU_ITEM_DUMMY();
1026
             MENU_ITEM_DUMMY();

Notiek ielāde…
Atcelt
Saglabāt