Просмотр исходного кода

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 лет назад
Родитель
Сommit
ae081d0fe0
3 измененных файлов: 78 добавлений и 81 удалений
  1. 67
    70
      Marlin/cardreader.cpp
  2. 9
    9
      Marlin/cardreader.h
  3. 2
    2
      Marlin/ultralcd.cpp

+ 67
- 70
Marlin/cardreader.cpp Просмотреть файл

@@ -11,8 +11,7 @@
11 11
 
12 12
 CardReader::CardReader()
13 13
 {
14
-  #if defined(SDCARD_SORT_ALPHA) && SORT_USES_MORE_RAM
15
-   sortnames = NULL;
14
+  #ifdef SDCARD_SORT_ALPHA
16 15
    sort_count = 0;
17 16
   #endif
18 17
    filesize = 0;
@@ -37,19 +36,15 @@ CardReader::CardReader()
37 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 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 48
   return buffer;
54 49
 }
55 50
 
@@ -59,7 +54,7 @@ void  CardReader::lsDive(const char *prepend,SdFile parent)
59 54
   dir_t p;
60 55
   uint8_t cnt=0;
61 56
  
62
-  while (parent.readDir(p, diveFilename) > 0)
57
+  while (parent.readDir(p, longFilename) > 0)
63 58
   {
64 59
     if( DIR_IS_SUBDIR(&p) && lsAction!=LS_Count && lsAction!=LS_GetFilename) // hence LS_SerialPrint
65 60
     {
@@ -96,8 +91,8 @@ void  CardReader::lsDive(const char *prepend,SdFile parent)
96 91
     {
97 92
       if (p.name[0] == DIR_NAME_FREE) break;
98 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 96
       if ( p.name[0] == '.')
102 97
       {
103 98
         if ( p.name[1] != '.')
@@ -556,21 +551,20 @@ void CardReader::closefile(bool store_location)
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 557
     if (nr < sort_count) {
563
-      strcpy(diveFilename, sortnames[nr]);
558
+      strcpy(longFilename, sortnames[nr]);
559
+      filenameIsDir = isDir[nr];
564 560
       return;
565 561
     }
566 562
   #endif
567
-
568 563
   curDir=&workDir;
569 564
   lsAction=LS_GetFilename;
570 565
   nrFiles=nr;
571 566
   curDir->rewind();
572 567
   lsDive("",*curDir);
573
-  
574 568
 }
575 569
 
576 570
 uint16_t CardReader::getnrfilenames()
@@ -631,12 +625,8 @@ void CardReader::updir()
631 625
 /**
632 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,68 +646,73 @@ void CardReader::presort()
656 646
 
657 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 654
       #endif
667 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 665
     #endif
670 666
 
667
+    sort_count = fileCnt;
668
+    sort_order = new uint8_t[fileCnt];
669
+
671 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 674
         sort_order[i] = i;
677 675
         #if SORT_USES_RAM
678 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 681
           #if FOLDER_SORTING != 0
685
-            isdir[i] = filenameIsDir;
682
+            isDir[i] = filenameIsDir;
686 683
           #endif
687 684
         #endif
688 685
       }
689 686
 
690 687
       // Bubble Sort
691
-      for (uint8_t i=fileCnt; --i;) {
688
+      for (uint16_t i=fileCnt; --i;) {
692 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 692
           #if SORT_USES_RAM
696 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 695
             #else
699
-              cmp = strcasecmp(sortnames[o1], sortnames[o2]) > 0);
696
+              cmp = strcasecmp(sortnames[o1], sortnames[o2]) > 0;
700 697
             #endif
701 698
           #else
702 699
             getfilename(o1);
700
+            strcpy(name1, longFilename[0] ? longFilename : filename);
703 701
             #if FOLDER_SORTING != 0
704 702
               bool dir1 = filenameIsDir;
705 703
             #endif
706
-            char *name = diveFilename[0] ? diveFilename : filename;
707
-            strcpy(sortname, name);
708 704
             getfilename(o2);
709
-            name = diveFilename[0] ? diveFilename : filename;
705
+            char *name2 = longFilename[0] ? longFilename : filename;
710 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 708
             #else
713
-              cmp = strcasecmp(sortname, name) > 0);
709
+              cmp = strcasecmp(name1, name2) > 0;
714 710
             #endif
715 711
           #endif
716 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 716
             sort_order[s1] = o2;
722 717
             sort_order[s2] = o1;
723 718
             didSwap = true;
@@ -727,30 +722,32 @@ void CardReader::presort()
727 722
       }
728 723
 
729 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 726
       #endif
732 727
     }
733 728
     else {
734 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 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 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 753
 #endif // SDCARD_SORT_ALPHA

+ 9
- 9
Marlin/cardreader.h Просмотреть файл

@@ -6,7 +6,7 @@
6 6
 #define MAX_DIR_DEPTH 10          // Maximum folder depth
7 7
 #define SORT_USES_RAM false       // Buffer while sorting, else re-read from SD
8 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 10
 #define FOLDER_SORTING -1         // -1=above  0=none  1=below
11 11
 
12 12
 #include "SdFile.h"
@@ -32,7 +32,7 @@ public:
32 32
   void getStatus();
33 33
   void printingHasFinished();
34 34
 
35
-  void getfilename(const uint8_t nr);
35
+  void getfilename(const uint16_t nr);
36 36
   uint16_t getnrfilenames();
37 37
   
38 38
   void getAbsFilename(char *t);
@@ -46,7 +46,7 @@ public:
46 46
 #ifdef SDCARD_SORT_ALPHA
47 47
   void presort();
48 48
   void flush_presort();
49
-  void getfilename_sorted(const uint8_t nr);
49
+  void getfilename_sorted(const uint16_t nr);
50 50
 #endif
51 51
 
52 52
 
@@ -60,21 +60,21 @@ public:
60 60
 public:
61 61
   bool saving;
62 62
   bool logging;
63
-  bool sdprinting ;  
63
+  bool sdprinting;
64 64
   bool cardOK;
65 65
   char filename[FILENAME_LENGTH];
66
-  char diveFilename[LONG_FILENAME_LENGTH];
66
+  char longFilename[LONG_FILENAME_LENGTH];
67 67
   bool filenameIsDir;
68 68
   int lastnr; //last number of the autostart;
69 69
 private:
70 70
   SdFile root,*curDir,workDir,workDirParents[MAX_DIR_DEPTH];
71 71
   uint16_t workDirDepth;
72 72
 #ifdef SDCARD_SORT_ALPHA
73
+  uint16_t sort_count;
74
+  uint8_t *sort_order;
73 75
   #if SORT_USES_MORE_RAM
74
-    uint16_t sort_count;
75 76
     char **sortnames;
76
-  #else
77
-    uint8_t sort_order[SORT_LIMIT];
77
+    uint8_t *isDir;
78 78
   #endif
79 79
 #endif
80 80
   Sd2Card card;
@@ -93,7 +93,7 @@ private:
93 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 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 97
   char* diveDirName;
98 98
   void lsDive(const char *prepend,SdFile parent);
99 99
 };

+ 2
- 2
Marlin/ultralcd.cpp Просмотреть файл

@@ -1017,10 +1017,10 @@ void lcd_sdcard_menu()
1017 1017
             #endif
1018 1018
 
1019 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 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 1025
         }else{
1026 1026
             MENU_ITEM_DUMMY();

Загрузка…
Отмена
Сохранить