浏览代码

Make CardReader class static (#12451)

* Make CardReader a static class
* Make CardReader flags into bitfields
Scott Lahteine 6 年前
父节点
当前提交
66580f32c2
没有帐户链接到提交者的电子邮件

+ 5
- 5
Marlin/src/HAL/HAL_DUE/usb/sd_mmc_spi_mem.cpp 查看文件

19
 }
19
 }
20
 
20
 
21
 Ctrl_status sd_mmc_spi_test_unit_ready(void) {
21
 Ctrl_status sd_mmc_spi_test_unit_ready(void) {
22
-  if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.cardOK)
22
+  if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.flag.cardOK)
23
     return CTRL_NO_PRESENT;
23
     return CTRL_NO_PRESENT;
24
   return CTRL_GOOD;
24
   return CTRL_GOOD;
25
 }
25
 }
27
 // NOTE: This function is defined as returning the address of the last block
27
 // NOTE: This function is defined as returning the address of the last block
28
 // in the card, which is cardSize() - 1
28
 // in the card, which is cardSize() - 1
29
 Ctrl_status sd_mmc_spi_read_capacity(uint32_t *nb_sector) {
29
 Ctrl_status sd_mmc_spi_read_capacity(uint32_t *nb_sector) {
30
-  if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.cardOK)
30
+  if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.flag.cardOK)
31
     return CTRL_NO_PRESENT;
31
     return CTRL_NO_PRESENT;
32
   *nb_sector = card.getSd2Card().cardSize() - 1;
32
   *nb_sector = card.getSd2Card().cardSize() - 1;
33
   return CTRL_GOOD;
33
   return CTRL_GOOD;
42
 }
42
 }
43
 
43
 
44
 bool sd_mmc_spi_removal(void) {
44
 bool sd_mmc_spi_removal(void) {
45
-  if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.cardOK)
45
+  if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.flag.cardOK)
46
     return true;
46
     return true;
47
   return false;
47
   return false;
48
 }
48
 }
61
 // #define DEBUG_MMC
61
 // #define DEBUG_MMC
62
 
62
 
63
 Ctrl_status sd_mmc_spi_usb_read_10(uint32_t addr, uint16_t nb_sector) {
63
 Ctrl_status sd_mmc_spi_usb_read_10(uint32_t addr, uint16_t nb_sector) {
64
-  if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.cardOK)
64
+  if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.flag.cardOK)
65
     return CTRL_NO_PRESENT;
65
     return CTRL_NO_PRESENT;
66
 
66
 
67
   #ifdef DEBUG_MMC
67
   #ifdef DEBUG_MMC
95
 }
95
 }
96
 
96
 
97
 Ctrl_status sd_mmc_spi_usb_write_10(uint32_t addr, uint16_t nb_sector) {
97
 Ctrl_status sd_mmc_spi_usb_write_10(uint32_t addr, uint16_t nb_sector) {
98
-  if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.cardOK)
98
+  if (!IS_SD_INSERTED() || IS_SD_PRINTING() || IS_SD_FILE_OPEN() || !card.flag.cardOK)
99
     return CTRL_NO_PRESENT;
99
     return CTRL_NO_PRESENT;
100
 
100
 
101
   #ifdef DEBUG_MMC
101
   #ifdef DEBUG_MMC

+ 1
- 1
Marlin/src/HAL/HAL_LPC1768/main.cpp 查看文件

106
     // the disk if Marlin has it mounted. Unfortuately there is currently no way
106
     // the disk if Marlin has it mounted. Unfortuately there is currently no way
107
     // to unmount the disk from the LCD menu.
107
     // to unmount the disk from the LCD menu.
108
     // if (IS_SD_PRINTING() || IS_SD_FILE_OPEN())
108
     // if (IS_SD_PRINTING() || IS_SD_FILE_OPEN())
109
-    if (card.cardOK)
109
+    if (card.flag.cardOK)
110
       MSC_Aquire_Lock();
110
       MSC_Aquire_Lock();
111
     else
111
     else
112
       MSC_Release_Lock();
112
       MSC_Release_Lock();

+ 2
- 2
Marlin/src/HAL/HAL_STM32F1/persistent_store_sdcard.cpp 查看文件

41
 char eeprom_filename[] = "eeprom.dat";
41
 char eeprom_filename[] = "eeprom.dat";
42
 
42
 
43
 bool PersistentStore::access_start() {
43
 bool PersistentStore::access_start() {
44
-  if (!card.cardOK) return false;
44
+  if (!card.flag.cardOK) return false;
45
   int16_t bytes_read = 0;
45
   int16_t bytes_read = 0;
46
   constexpr char eeprom_zero = 0xFF;
46
   constexpr char eeprom_zero = 0xFF;
47
   card.openFile(eeprom_filename, true);
47
   card.openFile(eeprom_filename, true);
54
 }
54
 }
55
 
55
 
56
 bool PersistentStore::access_finish() {
56
 bool PersistentStore::access_finish() {
57
-  if (!card.cardOK) return false;
57
+  if (!card.flag.cardOK) return false;
58
   card.openFile(eeprom_filename, true);
58
   card.openFile(eeprom_filename, true);
59
   int16_t bytes_written = card.write(HAL_STM32F1_eeprom_content, HAL_STM32F1_EEPROM_SIZE);
59
   int16_t bytes_written = card.write(HAL_STM32F1_eeprom_content, HAL_STM32F1_EEPROM_SIZE);
60
   card.closefile();
60
   card.closefile();

+ 1
- 1
Marlin/src/Marlin.cpp 查看文件

967
     #if ENABLED(SDSUPPORT)
967
     #if ENABLED(SDSUPPORT)
968
       card.checkautostart();
968
       card.checkautostart();
969
 
969
 
970
-      if (card.abort_sd_printing) {
970
+      if (card.flag.abort_sd_printing) {
971
         card.stopSDPrint(
971
         card.stopSDPrint(
972
           #if SD_RESORT
972
           #if SD_RESORT
973
             true
973
             true

+ 2
- 2
Marlin/src/feature/power_loss_recovery.cpp 查看文件

84
  */
84
  */
85
 void PrintJobRecovery::check() {
85
 void PrintJobRecovery::check() {
86
   if (enabled) {
86
   if (enabled) {
87
-    if (!card.cardOK) card.initsd();
88
-    if (card.cardOK) {
87
+    if (!card.flag.cardOK) card.initsd();
88
+    if (card.flag.cardOK) {
89
       load();
89
       load();
90
       if (!valid()) return purge();
90
       if (!valid()) return purge();
91
       enqueue_and_echo_commands_P(PSTR("M1000 S"));
91
       enqueue_and_echo_commands_P(PSTR("M1000 S"));

+ 1
- 1
Marlin/src/feature/power_loss_recovery.h 查看文件

68
   char command_queue[BUFSIZE][MAX_CMD_SIZE];
68
   char command_queue[BUFSIZE][MAX_CMD_SIZE];
69
 
69
 
70
   // SD Filename and position
70
   // SD Filename and position
71
-  char sd_filename[MAXPATHNAMELENGTH];
71
+  char sd_filename[MAXPATHNAMELENGTH + 1];
72
   uint32_t sdpos;
72
   uint32_t sdpos;
73
 
73
 
74
   // Job elapsed time
74
   // Job elapsed time

+ 6
- 6
Marlin/src/gcode/queue.cpp 查看文件

503
             break;
503
             break;
504
           case StreamState::STREAM_COMPLETE:
504
           case StreamState::STREAM_COMPLETE:
505
             stream_state = StreamState::STREAM_RESET;
505
             stream_state = StreamState::STREAM_RESET;
506
-            card.binary_mode = false;
506
+            card.flag.binary_mode = false;
507
             card.closefile();
507
             card.closefile();
508
             CARD_ECHO_P("echo: ");
508
             CARD_ECHO_P("echo: ");
509
             CARD_ECHO_P(card.filename);
509
             CARD_ECHO_P(card.filename);
514
             return;
514
             return;
515
           case StreamState::STREAM_FAILED:
515
           case StreamState::STREAM_FAILED:
516
             stream_state = StreamState::STREAM_RESET;
516
             stream_state = StreamState::STREAM_RESET;
517
-            card.binary_mode = false;
517
+            card.flag.binary_mode = false;
518
             card.closefile();
518
             card.closefile();
519
             card.removeFile(card.filename);
519
             card.removeFile(card.filename);
520
             CARD_ECHOLN_P("echo: File transfer failed");
520
             CARD_ECHOLN_P("echo: File transfer failed");
549
             ;
549
             ;
550
 
550
 
551
   #if ENABLED(FAST_FILE_TRANSFER)
551
   #if ENABLED(FAST_FILE_TRANSFER)
552
-    if (card.saving && card.binary_mode) {
552
+    if (card.flag.saving && card.flag.binary_mode) {
553
       /**
553
       /**
554
        * For binary stream file transfer, use serial_line_buffer as the working
554
        * For binary stream file transfer, use serial_line_buffer as the working
555
        * receive buffer (which limits the packet size to MAX_CMD_SIZE).
555
        * receive buffer (which limits the packet size to MAX_CMD_SIZE).
630
           gcode_LastN = gcode_N;
630
           gcode_LastN = gcode_N;
631
         }
631
         }
632
         #if ENABLED(SDSUPPORT)
632
         #if ENABLED(SDSUPPORT)
633
-          else if (card.saving && strcmp(command, "M29") != 0) // No line number with M29 in Pronterface
633
+          else if (card.flag.saving && strcmp(command, "M29") != 0) // No line number with M29 in Pronterface
634
             return gcode_line_error(PSTR(MSG_ERR_NO_CHECKSUM), i);
634
             return gcode_line_error(PSTR(MSG_ERR_NO_CHECKSUM), i);
635
         #endif
635
         #endif
636
 
636
 
838
 
838
 
839
   #if ENABLED(SDSUPPORT)
839
   #if ENABLED(SDSUPPORT)
840
 
840
 
841
-    if (card.saving) {
841
+    if (card.flag.saving) {
842
       char* command = command_queue[cmd_queue_index_r];
842
       char* command = command_queue[cmd_queue_index_r];
843
       if (strstr_P(command, PSTR("M29"))) {
843
       if (strstr_P(command, PSTR("M29"))) {
844
         // M29 closes the file
844
         // M29 closes the file
860
       else {
860
       else {
861
         // Write the string from the read buffer to SD
861
         // Write the string from the read buffer to SD
862
         card.write_command(command);
862
         card.write_command(command);
863
-        if (card.logging)
863
+        if (card.flag.logging)
864
           gcode.process_next_command(); // The card is saving because it's logging
864
           gcode.process_next_command(); // The card is saving because it's logging
865
         else
865
         else
866
           ok_to_send();
866
           ok_to_send();

+ 6
- 6
Marlin/src/gcode/sdcard/M20-M30_M32-M34_M524_M928.cpp 查看文件

118
  * M26: Set SD Card file index
118
  * M26: Set SD Card file index
119
  */
119
  */
120
 void GcodeSuite::M26() {
120
 void GcodeSuite::M26() {
121
-  if (card.cardOK && parser.seenval('S'))
121
+  if (card.flag.cardOK && parser.seenval('S'))
122
     card.setIndex(parser.value_long());
122
     card.setIndex(parser.value_long());
123
 }
123
 }
124
 
124
 
178
     }
178
     }
179
 
179
 
180
     // Binary transfer mode
180
     // Binary transfer mode
181
-    if ((card.binary_mode = binary_mode)) {
181
+    if ((card.flag.binary_mode = binary_mode)) {
182
       SERIAL_ECHO_START_P(port);
182
       SERIAL_ECHO_START_P(port);
183
       SERIAL_ECHO_P(port, " preparing to receive: ");
183
       SERIAL_ECHO_P(port, " preparing to receive: ");
184
       SERIAL_ECHOLN_P(port, p);
184
       SERIAL_ECHOLN_P(port, p);
202
  * Processed in write to file routine
202
  * Processed in write to file routine
203
  */
203
  */
204
 void GcodeSuite::M29() {
204
 void GcodeSuite::M29() {
205
-  // card.saving = false;
205
+  // card.flag.saving = false;
206
 }
206
 }
207
 
207
 
208
 /**
208
 /**
209
  * M30 <filename>: Delete SD Card file
209
  * M30 <filename>: Delete SD Card file
210
  */
210
  */
211
 void GcodeSuite::M30() {
211
 void GcodeSuite::M30() {
212
-  if (card.cardOK) {
212
+  if (card.flag.cardOK) {
213
     card.closefile();
213
     card.closefile();
214
     card.removeFile(parser.string_arg);
214
     card.removeFile(parser.string_arg);
215
   }
215
   }
228
 void GcodeSuite::M32() {
228
 void GcodeSuite::M32() {
229
   if (IS_SD_PRINTING()) planner.synchronize();
229
   if (IS_SD_PRINTING()) planner.synchronize();
230
 
230
 
231
-  if (card.cardOK) {
231
+  if (card.flag.cardOK) {
232
     const bool call_procedure = parser.boolval('P');
232
     const bool call_procedure = parser.boolval('P');
233
 
233
 
234
     card.openFile(parser.string_arg, true, call_procedure);
234
     card.openFile(parser.string_arg, true, call_procedure);
286
  * M524: Abort the current SD print job (started with M24)
286
  * M524: Abort the current SD print job (started with M24)
287
  */
287
  */
288
 void GcodeSuite::M524() {
288
 void GcodeSuite::M524() {
289
-  if (IS_SD_PRINTING()) card.abort_sd_printing = true;
289
+  if (IS_SD_PRINTING()) card.flag.abort_sd_printing = true;
290
 }
290
 }
291
 
291
 
292
 /**
292
 /**

+ 6
- 6
Marlin/src/lcd/extensible_ui/ui_api.cpp 查看文件

575
   }
575
   }
576
 
576
 
577
   bool isPrintingFromMedia() {
577
   bool isPrintingFromMedia() {
578
-    return IFSD(card.cardOK && card.isFileOpen(), false);
578
+    return IFSD(card.flag.cardOK && card.isFileOpen(), false);
579
   }
579
   }
580
 
580
 
581
   bool isPrinting() {
581
   bool isPrinting() {
583
   }
583
   }
584
 
584
 
585
   bool isMediaInserted() {
585
   bool isMediaInserted() {
586
-    return IFSD(IS_SD_INSERTED() && card.cardOK, false);
586
+    return IFSD(IS_SD_INSERTED() && card.flag.cardOK, false);
587
   }
587
   }
588
 
588
 
589
   void pausePrint() {
589
   void pausePrint() {
612
   void stopPrint() {
612
   void stopPrint() {
613
     #if ENABLED(SDSUPPORT)
613
     #if ENABLED(SDSUPPORT)
614
       wait_for_heatup = wait_for_user = false;
614
       wait_for_heatup = wait_for_user = false;
615
-      card.abort_sd_printing = true;
615
+      card.flag.abort_sd_printing = true;
616
       ExtUI::onStatusChanged(PSTR(MSG_PRINT_ABORTED));
616
       ExtUI::onStatusChanged(PSTR(MSG_PRINT_ABORTED));
617
     #endif
617
     #endif
618
   }
618
   }
648
   }
648
   }
649
 
649
 
650
   bool FileList::isDir() {
650
   bool FileList::isDir() {
651
-    return IFSD(card.filenameIsDir, false);
651
+    return IFSD(card.flag.filenameIsDir, false);
652
   }
652
   }
653
 
653
 
654
   uint16_t FileList::count() {
654
   uint16_t FileList::count() {
697
       last_sd_status = sd_status;
697
       last_sd_status = sd_status;
698
       if (sd_status) {
698
       if (sd_status) {
699
         card.initsd();
699
         card.initsd();
700
-        if (card.cardOK)
700
+        if (card.flag.cardOK)
701
           ExtUI::onMediaInserted();
701
           ExtUI::onMediaInserted();
702
         else
702
         else
703
           ExtUI::onMediaError();
703
           ExtUI::onMediaError();
704
       }
704
       }
705
       else {
705
       else {
706
-        const bool ok = card.cardOK;
706
+        const bool ok = card.flag.cardOK;
707
         card.release();
707
         card.release();
708
         if (ok) ExtUI::onMediaRemoved();
708
         if (ok) ExtUI::onMediaRemoved();
709
       }
709
       }

+ 3
- 3
Marlin/src/lcd/malyanlcd.cpp 查看文件

278
         // There may be a difference in how V1 and V2 LCDs handle subdirectory
278
         // There may be a difference in how V1 and V2 LCDs handle subdirectory
279
         // prints. Investigate more. This matches the V1 motion controller actions
279
         // prints. Investigate more. This matches the V1 motion controller actions
280
         // but the V2 LCD switches to "print" mode on {SYS:DIR} response.
280
         // but the V2 LCD switches to "print" mode on {SYS:DIR} response.
281
-        if (card.filenameIsDir) {
281
+        if (card.flag.filenameIsDir) {
282
           card.chdir(card.filename);
282
           card.chdir(card.filename);
283
           write_to_lcd_P(PSTR("{SYS:DIR}"));
283
           write_to_lcd_P(PSTR("{SYS:DIR}"));
284
         }
284
         }
330
 
330
 
331
     case 'L': {
331
     case 'L': {
332
       #if ENABLED(SDSUPPORT)
332
       #if ENABLED(SDSUPPORT)
333
-        if (!card.cardOK) card.initsd();
333
+        if (!card.flag.cardOK) card.initsd();
334
 
334
 
335
         // A more efficient way to do this would be to
335
         // A more efficient way to do this would be to
336
         // implement a callback in the ls_SerialPrint code, but
336
         // implement a callback in the ls_SerialPrint code, but
342
         uint16_t file_count = card.get_num_Files();
342
         uint16_t file_count = card.get_num_Files();
343
         for (uint16_t i = 0; i < file_count; i++) {
343
         for (uint16_t i = 0; i < file_count; i++) {
344
           card.getfilename(i);
344
           card.getfilename(i);
345
-          sprintf_P(message_buffer, card.filenameIsDir ? PSTR("{DIR:%s}") : PSTR("{FILE:%s}"), card.longest_filename());
345
+          sprintf_P(message_buffer, card.flag.filenameIsDir ? PSTR("{DIR:%s}") : PSTR("{FILE:%s}"), card.longest_filename());
346
           write_to_lcd(message_buffer);
346
           write_to_lcd(message_buffer);
347
         }
347
         }
348
 
348
 

+ 2
- 2
Marlin/src/lcd/menu/menu_main.cpp 查看文件

58
 
58
 
59
   void lcd_sdcard_stop() {
59
   void lcd_sdcard_stop() {
60
     wait_for_heatup = wait_for_user = false;
60
     wait_for_heatup = wait_for_user = false;
61
-    card.abort_sd_printing = true;
61
+    card.flag.abort_sd_printing = true;
62
     ui.setstatusPGM(PSTR(MSG_PRINT_ABORTED), -1);
62
     ui.setstatusPGM(PSTR(MSG_PRINT_ABORTED), -1);
63
     ui.return_to_status();
63
     ui.return_to_status();
64
   }
64
   }
86
   MENU_BACK(MSG_WATCH);
86
   MENU_BACK(MSG_WATCH);
87
 
87
 
88
   #if ENABLED(SDSUPPORT)
88
   #if ENABLED(SDSUPPORT)
89
-    if (card.cardOK) {
89
+    if (card.flag.cardOK) {
90
       if (card.isFileOpen()) {
90
       if (card.isFileOpen()) {
91
         if (IS_SD_PRINTING())
91
         if (IS_SD_PRINTING())
92
           MENU_ITEM(function, MSG_PAUSE_PRINT, lcd_sdcard_pause);
92
           MENU_ITEM(function, MSG_PAUSE_PRINT, lcd_sdcard_pause);

+ 1
- 1
Marlin/src/lcd/menu/menu_sdcard.cpp 查看文件

125
 
125
 
126
       card.getfilename_sorted(nr);
126
       card.getfilename_sorted(nr);
127
 
127
 
128
-      if (card.filenameIsDir)
128
+      if (card.flag.filenameIsDir)
129
         MENU_ITEM(sdfolder, MSG_CARD_MENU, card);
129
         MENU_ITEM(sdfolder, MSG_CARD_MENU, card);
130
       else
130
       else
131
         MENU_ITEM(sdfile, MSG_CARD_MENU, card);
131
         MENU_ITEM(sdfile, MSG_CARD_MENU, card);

+ 2
- 2
Marlin/src/sd/SdFatConfig.h 查看文件

106
  * Defines for 8.3 and long (vfat) filenames
106
  * Defines for 8.3 and long (vfat) filenames
107
  */
107
  */
108
 
108
 
109
-#define FILENAME_LENGTH 13 // Number of UTF-16 characters per entry
109
+#define FILENAME_LENGTH 12 // Number of UTF-16 characters per entry
110
 
110
 
111
 // Total bytes needed to store a single long filename
111
 // Total bytes needed to store a single long filename
112
-#define LONG_FILENAME_LENGTH (FILENAME_LENGTH * MAX_VFAT_ENTRIES + 1)
112
+#define LONG_FILENAME_LENGTH ((FILENAME_LENGTH) * (MAX_VFAT_ENTRIES))

+ 143
- 69
Marlin/src/sd/cardreader.cpp 查看文件

45
   #include "../feature/pause.h"
45
   #include "../feature/pause.h"
46
 #endif
46
 #endif
47
 
47
 
48
-#include <ctype.h>
48
+// public:
49
+
50
+card_flags_t CardReader::flag;
51
+char CardReader::filename[FILENAME_LENGTH + 1], CardReader::longFilename[LONG_FILENAME_LENGTH + 1];
52
+int8_t CardReader::autostart_index;
53
+
54
+#if ENABLED(FAST_FILE_TRANSFER)
55
+  #if NUM_SERIAL > 1
56
+    uint8_t CardReader::transfer_port;
57
+  #endif
58
+#endif
59
+
60
+// private:
61
+
62
+SdFile CardReader::root, CardReader::workDir, CardReader::workDirParents[MAX_DIR_DEPTH];
63
+uint8_t CardReader::workDirDepth;
64
+
65
+#if ENABLED(SDCARD_SORT_ALPHA)
66
+  uint16_t CardReader::sort_count;
67
+  #if ENABLED(SDSORT_GCODE)
68
+    bool CardReader::sort_alpha;
69
+    int CardReader::sort_folders;
70
+    //bool CardReader::sort_reverse;
71
+  #endif
72
+
73
+  #if ENABLED(SDSORT_DYNAMIC_RAM)
74
+    uint8_t *CardReader::sort_order;
75
+  #else
76
+    uint8_t CardReader::sort_order[SDSORT_LIMIT];
77
+  #endif
78
+
79
+  #if ENABLED(SDSORT_USES_RAM)
80
+
81
+    #if ENABLED(SDSORT_CACHE_NAMES)
82
+      #if ENABLED(SDSORT_DYNAMIC_RAM)
83
+        char **CardReader::sortshort, **CardReader::sortnames;
84
+      #else
85
+        char CardReader::sortshort[SDSORT_LIMIT][FILENAME_LENGTH + 1];
86
+        char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN + 1];
87
+      #endif
88
+    #elif DISABLED(SDSORT_USES_STACK)
89
+      char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN + 1];
90
+    #endif
91
+
92
+    #if HAS_FOLDER_SORTING
93
+      #if ENABLED(SDSORT_DYNAMIC_RAM)
94
+        uint8_t *CardReader::isDir;
95
+      #elif ENABLED(SDSORT_CACHE_NAMES) || DISABLED(SDSORT_USES_STACK)
96
+        uint8_t CardReader::isDir[(SDSORT_LIMIT+7)>>3];
97
+      #endif
98
+    #endif
99
+
100
+  #endif // SDSORT_USES_RAM
101
+
102
+#endif // SDCARD_SORT_ALPHA
103
+
104
+Sd2Card CardReader::sd2card;
105
+SdVolume CardReader::volume;
106
+SdFile CardReader::file;
107
+
108
+uint8_t CardReader::file_subcall_ctr;
109
+uint32_t CardReader::filespos[SD_PROCEDURE_DEPTH];
110
+char CardReader::proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH + 1];
111
+
112
+uint32_t CardReader::filesize, CardReader::sdpos;
113
+
114
+LsAction CardReader::lsAction; //stored for recursion.
115
+uint16_t CardReader::nrFiles; //counter for the files in the current directory and recycled as position counter for getting the nrFiles'th name in the directory.
116
+char *CardReader::diveDirName;
117
+
118
+#if ENABLED(AUTO_REPORT_SD_STATUS)
119
+  uint8_t CardReader::auto_report_sd_interval;
120
+  millis_t CardReader::next_sd_report_ms;
121
+  #if NUM_SERIAL > 1
122
+    int8_t CardReader::serialport;
123
+  #endif
124
+#endif
49
 
125
 
50
 CardReader::CardReader() {
126
 CardReader::CardReader() {
51
   #if ENABLED(SDCARD_SORT_ALPHA)
127
   #if ENABLED(SDCARD_SORT_ALPHA)
56
       //sort_reverse = false;
132
       //sort_reverse = false;
57
     #endif
133
     #endif
58
   #endif
134
   #endif
59
-  sdprinting = cardOK = saving = logging = false;
60
-  filesize = 0;
61
-  sdpos = 0;
135
+  flag.sdprinting = flag.cardOK = flag.saving = flag.logging = false;
136
+  filesize = sdpos = 0;
62
   file_subcall_ctr = 0;
137
   file_subcall_ctr = 0;
63
 
138
 
64
   workDirDepth = 0;
139
   workDirDepth = 0;
73
   #endif
148
   #endif
74
 }
149
 }
75
 
150
 
76
-char *createFilename(char *buffer, const dir_t &p) { //buffer > 12characters
151
+char *createFilename(char *buffer, const dir_t &p) {
77
   char *pos = buffer;
152
   char *pos = buffer;
78
   for (uint8_t i = 0; i < 11; i++) {
153
   for (uint8_t i = 0; i < 11; i++) {
79
     if (p.name[i] == ' ') continue;
154
     if (p.name[i] == ' ') continue;
108
     if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) {
183
     if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) {
109
 
184
 
110
       // Get the short name for the item, which we know is a folder
185
       // Get the short name for the item, which we know is a folder
111
-      char dosFilename[FILENAME_LENGTH];
186
+      char dosFilename[FILENAME_LENGTH + 1];
112
       createFilename(dosFilename, p);
187
       createFilename(dosFilename, p);
113
 
188
 
114
       // Allocate enough stack space for the full path to a folder, trailing slash, and nul
189
       // Allocate enough stack space for the full path to a folder, trailing slash, and nul
120
       // It contains the full path to the "parent" argument.
195
       // It contains the full path to the "parent" argument.
121
       // We now have the full path to the item in this folder.
196
       // We now have the full path to the item in this folder.
122
       strcpy(path, prepend_is_empty ? "/" : prepend); // root slash if prepend is empty
197
       strcpy(path, prepend_is_empty ? "/" : prepend); // root slash if prepend is empty
123
-      strcat(path, dosFilename); // FILENAME_LENGTH-1 characters maximum
124
-      strcat(path, "/");       // 1 character
198
+      strcat(path, dosFilename);                      // FILENAME_LENGTH characters maximum
199
+      strcat(path, "/");                              // 1 character
125
 
200
 
126
       // Serial.print(path);
201
       // Serial.print(path);
127
 
202
 
150
 
225
 
151
       if (!DIR_IS_FILE_OR_SUBDIR(&p) || (p.attributes & DIR_ATT_HIDDEN)) continue;
226
       if (!DIR_IS_FILE_OR_SUBDIR(&p) || (p.attributes & DIR_ATT_HIDDEN)) continue;
152
 
227
 
153
-      filenameIsDir = DIR_IS_SUBDIR(&p);
228
+      flag.filenameIsDir = DIR_IS_SUBDIR(&p);
154
 
229
 
155
-      if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue;
230
+      if (!flag.filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue;
156
 
231
 
157
       switch (lsAction) {  // 1 based file count
232
       switch (lsAction) {  // 1 based file count
158
         case LS_Count:
233
         case LS_Count:
242
       SERIAL_PROTOCOL_P(port, longFilename[0] ? longFilename : "???");
317
       SERIAL_PROTOCOL_P(port, longFilename[0] ? longFilename : "???");
243
 
318
 
244
       // If the filename was printed then that's it
319
       // If the filename was printed then that's it
245
-      if (!filenameIsDir) break;
320
+      if (!flag.filenameIsDir) break;
246
 
321
 
247
       // SERIAL_ECHOPGM_P(port, "Opening dir: "); SERIAL_ECHOLN_P(port, segment);
322
       // SERIAL_ECHOPGM_P(port, "Opening dir: "); SERIAL_ECHOLN_P(port, segment);
248
 
323
 
275
   #endif
350
   #endif
276
 ) {
351
 ) {
277
   if (file.isOpen()) {
352
   if (file.isOpen()) {
278
-    char dosFilename[FILENAME_LENGTH];
353
+    char dosFilename[FILENAME_LENGTH + 1];
279
     file.getFilename(dosFilename);
354
     file.getFilename(dosFilename);
280
     SERIAL_ECHO_P(port, dosFilename);
355
     SERIAL_ECHO_P(port, dosFilename);
281
     #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
356
     #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
293
 }
368
 }
294
 
369
 
295
 void CardReader::initsd() {
370
 void CardReader::initsd() {
296
-  cardOK = false;
371
+  flag.cardOK = false;
297
   if (root.isOpen()) root.close();
372
   if (root.isOpen()) root.close();
298
 
373
 
299
   #ifndef SPI_SPEED
374
   #ifndef SPI_SPEED
318
     SERIAL_ERRORLNPGM(MSG_SD_OPENROOT_FAIL);
393
     SERIAL_ERRORLNPGM(MSG_SD_OPENROOT_FAIL);
319
   }
394
   }
320
   else {
395
   else {
321
-    cardOK = true;
396
+    flag.cardOK = true;
322
     SERIAL_ECHO_START();
397
     SERIAL_ECHO_START();
323
     SERIAL_ECHOLNPGM(MSG_SD_CARD_OK);
398
     SERIAL_ECHOLNPGM(MSG_SD_CARD_OK);
324
   }
399
   }
327
 
402
 
328
 void CardReader::release() {
403
 void CardReader::release() {
329
   stopSDPrint();
404
   stopSDPrint();
330
-  cardOK = false;
405
+  flag.cardOK = false;
331
 }
406
 }
332
 
407
 
333
 void CardReader::openAndPrintFile(const char *name) {
408
 void CardReader::openAndPrintFile(const char *name) {
339
 }
414
 }
340
 
415
 
341
 void CardReader::startFileprint() {
416
 void CardReader::startFileprint() {
342
-  if (cardOK) {
343
-    sdprinting = true;
417
+  if (flag.cardOK) {
418
+    flag.sdprinting = true;
344
     #if SD_RESORT
419
     #if SD_RESORT
345
       flush_presort();
420
       flush_presort();
346
     #endif
421
     #endif
355
   #if ENABLED(ADVANCED_PAUSE_FEATURE)
430
   #if ENABLED(ADVANCED_PAUSE_FEATURE)
356
     did_pause_print = 0;
431
     did_pause_print = 0;
357
   #endif
432
   #endif
358
-  sdprinting = abort_sd_printing = false;
433
+  flag.sdprinting = flag.abort_sd_printing = false;
359
   if (isFileOpen()) file.close();
434
   if (isFileOpen()) file.close();
360
   #if SD_RESORT
435
   #if SD_RESORT
361
     if (re_sort) presort();
436
     if (re_sort) presort();
363
 }
438
 }
364
 
439
 
365
 void CardReader::openLogFile(char * const path) {
440
 void CardReader::openLogFile(char * const path) {
366
-  logging = true;
441
+  flag.logging = true;
367
   openFile(path, false);
442
   openFile(path, false);
368
 }
443
 }
369
 
444
 
380
   for (uint8_t i = 0; i < workDirDepth; i++)                // Loop to current work dir
455
   for (uint8_t i = 0; i < workDirDepth; i++)                // Loop to current work dir
381
     appendAtom(workDirParents[i], t, cnt);
456
     appendAtom(workDirParents[i], t, cnt);
382
 
457
 
383
-  if (cnt < MAXPATHNAMELENGTH - (FILENAME_LENGTH)) {
458
+  if (cnt < MAXPATHNAMELENGTH - (FILENAME_LENGTH) - 1) {    // Leave room for filename and nul
384
     appendAtom(file, t, cnt);
459
     appendAtom(file, t, cnt);
385
     --t;
460
     --t;
386
   }
461
   }
389
 
464
 
390
 void CardReader::openFile(char * const path, const bool read, const bool subcall/*=false*/) {
465
 void CardReader::openFile(char * const path, const bool read, const bool subcall/*=false*/) {
391
 
466
 
392
-  if (!cardOK) return;
467
+  if (!flag.cardOK) return;
393
 
468
 
394
   uint8_t doing = 0;
469
   uint8_t doing = 0;
395
   if (isFileOpen()) {                     // Replacing current file or doing a subroutine
470
   if (isFileOpen()) {                     // Replacing current file or doing a subroutine
464
       SERIAL_EOL();
539
       SERIAL_EOL();
465
     }
540
     }
466
     else {
541
     else {
467
-      saving = true;
542
+      flag.saving = true;
468
       getfilename(0, fname);
543
       getfilename(0, fname);
469
       #if ENABLED(EMERGENCY_PARSER)
544
       #if ENABLED(EMERGENCY_PARSER)
470
         emergency_parser.disable();
545
         emergency_parser.disable();
476
 }
551
 }
477
 
552
 
478
 void CardReader::removeFile(const char * const name) {
553
 void CardReader::removeFile(const char * const name) {
479
-  if (!cardOK) return;
554
+  if (!flag.cardOK) return;
480
 
555
 
481
   //stopSDPrint();
556
   //stopSDPrint();
482
 
557
 
504
     const int8_t port/*= -1*/
579
     const int8_t port/*= -1*/
505
   #endif
580
   #endif
506
 ) {
581
 ) {
507
-  if (cardOK && sdprinting) {
582
+  if (flag.cardOK && flag.sdprinting) {
508
     SERIAL_PROTOCOLPGM_P(port, MSG_SD_PRINTING_BYTE);
583
     SERIAL_PROTOCOLPGM_P(port, MSG_SD_PRINTING_BYTE);
509
     SERIAL_PROTOCOL_P(port, sdpos);
584
     SERIAL_PROTOCOL_P(port, sdpos);
510
     SERIAL_PROTOCOLCHAR_P(port, '/');
585
     SERIAL_PROTOCOLCHAR_P(port, '/');
543
 
618
 
544
 void CardReader::checkautostart() {
619
 void CardReader::checkautostart() {
545
 
620
 
546
-  if (autostart_index < 0 || sdprinting) return;
621
+  if (autostart_index < 0 || flag.sdprinting) return;
547
 
622
 
548
-  if (!cardOK) initsd();
623
+  if (!flag.cardOK) initsd();
549
 
624
 
550
-  if (cardOK
625
+  if (flag.cardOK
551
     #if ENABLED(POWER_LOSS_RECOVERY)
626
     #if ENABLED(POWER_LOSS_RECOVERY)
552
       && !recovery.valid() // Don't run auto#.g when a resume file exists
627
       && !recovery.valid() // Don't run auto#.g when a resume file exists
553
     #endif
628
     #endif
576
 void CardReader::closefile(const bool store_location) {
651
 void CardReader::closefile(const bool store_location) {
577
   file.sync();
652
   file.sync();
578
   file.close();
653
   file.close();
579
-  saving = logging = false;
654
+  flag.saving = flag.logging = false;
580
   sdpos = 0;
655
   sdpos = 0;
581
   #if ENABLED(EMERGENCY_PARSER)
656
   #if ENABLED(EMERGENCY_PARSER)
582
     emergency_parser.enable();
657
     emergency_parser.enable();
603
     if (nr < sort_count) {
678
     if (nr < sort_count) {
604
       strcpy(filename, sortshort[nr]);
679
       strcpy(filename, sortshort[nr]);
605
       strcpy(longFilename, sortnames[nr]);
680
       strcpy(longFilename, sortnames[nr]);
606
-      filenameIsDir = TEST(isDir[nr>>3], nr & 0x07);
681
+      flag.filenameIsDir = TEST(isDir[nr>>3], nr & 0x07);
607
       return;
682
       return;
608
     }
683
     }
609
   #endif // SDSORT_CACHE_NAMES
684
   #endif // SDSORT_CACHE_NAMES
712
     );
787
     );
713
   }
788
   }
714
 
789
 
790
+  #if ENABLED(SDSORT_USES_RAM)
791
+    #if ENABLED(SDSORT_DYNAMIC_RAM)
792
+      // Use dynamic method to copy long filename
793
+      #define SET_SORTNAME(I) (sortnames[I] = strdup(longest_filename()))
794
+      #if ENABLED(SDSORT_CACHE_NAMES)
795
+        // When caching also store the short name, since
796
+        // we're replacing the getfilename() behavior.
797
+        #define SET_SORTSHORT(I) (sortshort[I] = strdup(filename))
798
+      #else
799
+        #define SET_SORTSHORT(I) NOOP
800
+      #endif
801
+    #else
802
+      // Copy filenames into the static array
803
+      #if SORTED_LONGNAME_MAXLEN != LONG_FILENAME_LENGTH
804
+        #define SET_SORTNAME(I) do{ strncpy(sortnames[I], longest_filename(), SORTED_LONGNAME_MAXLEN); \
805
+                                    sortnames[I][SORTED_LONGNAME_MAXLEN] = '\0'; }while(0)
806
+      #else
807
+        #define SET_SORTNAME(I) strncpy(sortnames[I], longest_filename(), SORTED_LONGNAME_MAXLEN)
808
+      #endif
809
+      #if ENABLED(SDSORT_CACHE_NAMES)
810
+        #define SET_SORTSHORT(I) strcpy(sortshort[I], filename)
811
+      #else
812
+        #define SET_SORTSHORT(I) NOOP
813
+      #endif
814
+    #endif
815
+  #endif
816
+
715
   /**
817
   /**
716
    * Read all the files and produce a sort key
818
    * Read all the files and produce a sort key
717
    *
819
    *
754
             sortnames = new char*[fileCnt];
856
             sortnames = new char*[fileCnt];
755
           #endif
857
           #endif
756
         #elif ENABLED(SDSORT_USES_STACK)
858
         #elif ENABLED(SDSORT_USES_STACK)
757
-          char sortnames[fileCnt][SORTED_LONGNAME_MAXLEN];
859
+          char sortnames[fileCnt][SORTED_LONGNAME_MAXLEN + 1];
758
         #endif
860
         #endif
759
 
861
 
760
         // Folder sorting needs 1 bit per entry for flags.
862
         // Folder sorting needs 1 bit per entry for flags.
783
           // If using RAM then read all filenames now.
885
           // If using RAM then read all filenames now.
784
           #if ENABLED(SDSORT_USES_RAM)
886
           #if ENABLED(SDSORT_USES_RAM)
785
             getfilename(i);
887
             getfilename(i);
786
-            #if ENABLED(SDSORT_DYNAMIC_RAM)
787
-              // Use dynamic method to copy long filename
788
-              sortnames[i] = strdup(longest_filename());
789
-              #if ENABLED(SDSORT_CACHE_NAMES)
790
-                // When caching also store the short name, since
791
-                // we're replacing the getfilename() behavior.
792
-                sortshort[i] = strdup(filename);
793
-              #endif
794
-            #else
795
-              // Copy filenames into the static array
796
-              #if SORTED_LONGNAME_MAXLEN != LONG_FILENAME_LENGTH
797
-                strncpy(sortnames[i], longest_filename(), SORTED_LONGNAME_MAXLEN);
798
-                sortnames[i][SORTED_LONGNAME_MAXLEN - 1] = '\0';
799
-              #else
800
-                strncpy(sortnames[i], longest_filename(), SORTED_LONGNAME_MAXLEN);
801
-              #endif
802
-              #if ENABLED(SDSORT_CACHE_NAMES)
803
-                strcpy(sortshort[i], filename);
804
-              #endif
805
-            #endif
888
+            SET_SORTNAME(i);
889
+            SET_SORTSHORT(i);
806
             // char out[30];
890
             // char out[30];
807
-            // sprintf_P(out, PSTR("---- %i %s %s"), i, filenameIsDir ? "D" : " ", sortnames[i]);
891
+            // sprintf_P(out, PSTR("---- %i %s %s"), i, flag.filenameIsDir ? "D" : " ", sortnames[i]);
808
             // SERIAL_ECHOLN(out);
892
             // SERIAL_ECHOLN(out);
809
             #if HAS_FOLDER_SORTING
893
             #if HAS_FOLDER_SORTING
810
               const uint16_t bit = i & 0x07, ind = i >> 3;
894
               const uint16_t bit = i & 0x07, ind = i >> 3;
811
               if (bit == 0) isDir[ind] = 0x00;
895
               if (bit == 0) isDir[ind] = 0x00;
812
-              if (filenameIsDir) isDir[ind] |= _BV(bit);
896
+              if (flag.filenameIsDir) isDir[ind] |= _BV(bit);
813
             #endif
897
             #endif
814
           #endif
898
           #endif
815
         }
899
         }
837
                     ? _SORT_CMP_NODIR() \
921
                     ? _SORT_CMP_NODIR() \
838
                     : (isDir[fs > 0 ? ind1 : ind2] & (fs > 0 ? _BV(bit1) : _BV(bit2))) != 0)
922
                     : (isDir[fs > 0 ? ind1 : ind2] & (fs > 0 ? _BV(bit1) : _BV(bit2))) != 0)
839
               #else
923
               #else
840
-                #define _SORT_CMP_DIR(fs) ((dir1 == filenameIsDir) ? _SORT_CMP_NODIR() : (fs > 0 ? dir1 : !dir1))
924
+                #define _SORT_CMP_DIR(fs) ((dir1 == flag.filenameIsDir) ? _SORT_CMP_NODIR() : (fs > 0 ? dir1 : !dir1))
841
               #endif
925
               #endif
842
             #endif
926
             #endif
843
 
927
 
847
               getfilename(o1);
931
               getfilename(o1);
848
               strcpy(name1, longest_filename()); // save (or getfilename below will trounce it)
932
               strcpy(name1, longest_filename()); // save (or getfilename below will trounce it)
849
               #if HAS_FOLDER_SORTING
933
               #if HAS_FOLDER_SORTING
850
-                bool dir1 = filenameIsDir;
934
+                bool dir1 = flag.filenameIsDir;
851
               #endif
935
               #endif
852
               getfilename(o2);
936
               getfilename(o2);
853
               char *name2 = longest_filename(); // use the string in-place
937
               char *name2 = longest_filename(); // use the string in-place
885
       else {
969
       else {
886
         sort_order[0] = 0;
970
         sort_order[0] = 0;
887
         #if ENABLED(SDSORT_USES_RAM) && ENABLED(SDSORT_CACHE_NAMES)
971
         #if ENABLED(SDSORT_USES_RAM) && ENABLED(SDSORT_CACHE_NAMES)
888
-          getfilename(0);
889
           #if ENABLED(SDSORT_DYNAMIC_RAM)
972
           #if ENABLED(SDSORT_DYNAMIC_RAM)
890
             sortnames = new char*[1];
973
             sortnames = new char*[1];
891
-            sortnames[0] = strdup(longest_filename()); // malloc
892
             #if ENABLED(SDSORT_CACHE_NAMES)
974
             #if ENABLED(SDSORT_CACHE_NAMES)
893
               sortshort = new char*[1];
975
               sortshort = new char*[1];
894
-              sortshort[0] = strdup(filename);       // malloc
895
             #endif
976
             #endif
896
             isDir = new uint8_t[1];
977
             isDir = new uint8_t[1];
897
-          #else
898
-            #if SORTED_LONGNAME_MAXLEN != LONG_FILENAME_LENGTH
899
-              strncpy(sortnames[0], longest_filename(), SORTED_LONGNAME_MAXLEN);
900
-              sortnames[0][SORTED_LONGNAME_MAXLEN - 1] = '\0';
901
-            #else
902
-              strncpy(sortnames[0], longest_filename(), SORTED_LONGNAME_MAXLEN);
903
-            #endif
904
-            #if ENABLED(SDSORT_CACHE_NAMES)
905
-              strcpy(sortshort[0], filename);
906
-            #endif
907
           #endif
978
           #endif
908
-          isDir[0] = filenameIsDir ? 0x01 : 0x00;
979
+          getfilename(0);
980
+          SET_SORTNAME(0);
981
+          SET_SORTSHORT(0);
982
+          isDir[0] = flag.filenameIsDir ? 0x01 : 0x00;
909
         #endif
983
         #endif
910
       }
984
       }
911
 
985
 
1007
   }
1081
   }
1008
 
1082
 
1009
   void CardReader::openJobRecoveryFile(const bool read) {
1083
   void CardReader::openJobRecoveryFile(const bool read) {
1010
-    if (!cardOK) return;
1084
+    if (!flag.cardOK) return;
1011
     if (recovery.file.isOpen()) return;
1085
     if (recovery.file.isOpen()) return;
1012
     if (!recovery.file.open(&root, job_recovery_file_name, read ? O_READ : O_CREAT | O_WRITE | O_TRUNC | O_SYNC)) {
1086
     if (!recovery.file.open(&root, job_recovery_file_name, read ? O_READ : O_CREAT | O_WRITE | O_TRUNC | O_SYNC)) {
1013
       SERIAL_PROTOCOLPAIR(MSG_SD_OPEN_FILE_FAIL, job_recovery_file_name);
1087
       SERIAL_PROTOCOLPAIR(MSG_SD_OPEN_FILE_FAIL, job_recovery_file_name);

+ 102
- 85
Marlin/src/sd/cardreader.h 查看文件

27
 
27
 
28
 #define SD_RESORT ENABLED(SDCARD_SORT_ALPHA) && ENABLED(SDSORT_DYNAMIC_RAM)
28
 #define SD_RESORT ENABLED(SDCARD_SORT_ALPHA) && ENABLED(SDSORT_DYNAMIC_RAM)
29
 
29
 
30
-#define MAX_DIR_DEPTH 10          // Maximum folder depth
30
+#define MAX_DIR_DEPTH     10       // Maximum folder depth
31
+#define MAXDIRNAMELENGTH   8       // DOS folder name size
32
+#define MAXPATHNAMELENGTH  (1 + (MAXDIRNAMELENGTH + 1) * (MAX_DIR_DEPTH) + 1 + FILENAME_LENGTH) // "/" + N * ("ADIRNAME/") + "filename.ext"
31
 
33
 
32
 #include "SdFile.h"
34
 #include "SdFile.h"
33
 
35
 
34
 enum LsAction : uint8_t { LS_SerialPrint, LS_Count, LS_GetFilename };
36
 enum LsAction : uint8_t { LS_SerialPrint, LS_Count, LS_GetFilename };
35
 
37
 
38
+typedef struct {
39
+  bool saving:1,
40
+       logging:1,
41
+       sdprinting:1,
42
+       cardOK:1,
43
+       filenameIsDir:1,
44
+       abort_sd_printing:1
45
+       #if ENABLED(FAST_FILE_TRANSFER)
46
+         , binary_mode:1
47
+       #endif
48
+    ;
49
+} card_flags_t;
50
+
36
 class CardReader {
51
 class CardReader {
37
 public:
52
 public:
38
   CardReader();
53
   CardReader();
39
 
54
 
40
-  void initsd();
41
-  void write_command(char *buf);
55
+  static void initsd();
56
+  static void write_command(char *buf);
42
 
57
 
43
-  void beginautostart();
44
-  void checkautostart();
58
+  static void beginautostart();
59
+  static void checkautostart();
45
 
60
 
46
-  void openFile(char * const path, const bool read, const bool subcall=false);
47
-  void openLogFile(char * const path);
48
-  void removeFile(const char * const name);
49
-  void closefile(const bool store_location=false);
50
-  void release();
51
-  void openAndPrintFile(const char *name);
52
-  void startFileprint();
53
-  void stopSDPrint(
61
+  static void openFile(char * const path, const bool read, const bool subcall=false);
62
+  static void openLogFile(char * const path);
63
+  static void removeFile(const char * const name);
64
+  static void closefile(const bool store_location=false);
65
+  static void release();
66
+  static void openAndPrintFile(const char *name);
67
+  static void startFileprint();
68
+  static void stopSDPrint(
54
     #if SD_RESORT
69
     #if SD_RESORT
55
       const bool re_sort=false
70
       const bool re_sort=false
56
     #endif
71
     #endif
57
   );
72
   );
58
-  void getStatus(
73
+  static void getStatus(
59
     #if NUM_SERIAL > 1
74
     #if NUM_SERIAL > 1
60
       const int8_t port = -1
75
       const int8_t port = -1
61
     #endif
76
     #endif
62
   );
77
   );
63
-  void printingHasFinished();
64
-  void printFilename(
78
+  static void printingHasFinished();
79
+  static void printFilename(
65
     #if NUM_SERIAL > 1
80
     #if NUM_SERIAL > 1
66
       const int8_t port = -1
81
       const int8_t port = -1
67
     #endif
82
     #endif
68
   );
83
   );
69
 
84
 
70
   #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
85
   #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
71
-    void printLongPath(char *path
86
+    static void printLongPath(char *path
72
       #if NUM_SERIAL > 1
87
       #if NUM_SERIAL > 1
73
         , const int8_t port = -1
88
         , const int8_t port = -1
74
       #endif
89
       #endif
75
     );
90
     );
76
   #endif
91
   #endif
77
 
92
 
78
-  void getfilename(uint16_t nr, const char* const match=NULL);
79
-  uint16_t getnrfilenames();
93
+  static void getfilename(uint16_t nr, const char* const match=NULL);
94
+  static uint16_t getnrfilenames();
80
 
95
 
81
-  void getAbsFilename(char *t);
96
+  static void getAbsFilename(char *t);
82
 
97
 
83
-  void ls(
98
+  static void ls(
84
     #if NUM_SERIAL > 1
99
     #if NUM_SERIAL > 1
85
       const int8_t port = -1
100
       const int8_t port = -1
86
     #endif
101
     #endif
87
   );
102
   );
88
-  void chdir(const char *relpath);
89
-  int8_t updir();
90
-  void setroot();
103
+  static void chdir(const char *relpath);
104
+  static int8_t updir();
105
+  static void setroot();
91
 
106
 
92
-  const char* diveToFile(SdFile*& curDir, const char * const path, const bool echo);
107
+  static const char* diveToFile(SdFile*& curDir, const char * const path, const bool echo);
93
 
108
 
94
-  uint16_t get_num_Files();
109
+  static uint16_t get_num_Files();
95
 
110
 
96
   #if ENABLED(SDCARD_SORT_ALPHA)
111
   #if ENABLED(SDCARD_SORT_ALPHA)
97
-    void presort();
98
-    void getfilename_sorted(const uint16_t nr);
112
+    static void presort();
113
+    static void getfilename_sorted(const uint16_t nr);
99
     #if ENABLED(SDSORT_GCODE)
114
     #if ENABLED(SDSORT_GCODE)
100
-      FORCE_INLINE void setSortOn(bool b) { sort_alpha = b; presort(); }
101
-      FORCE_INLINE void setSortFolders(int i) { sort_folders = i; presort(); }
102
-      //FORCE_INLINE void setSortReverse(bool b) { sort_reverse = b; }
115
+      FORCE_INLINE static void setSortOn(bool b) { sort_alpha = b; presort(); }
116
+      FORCE_INLINE static void setSortFolders(int i) { sort_folders = i; presort(); }
117
+      //FORCE_INLINE static void setSortReverse(bool b) { sort_reverse = b; }
103
     #endif
118
     #endif
104
   #else
119
   #else
105
-    FORCE_INLINE void getfilename_sorted(const uint16_t nr) { getfilename(nr); }
120
+    FORCE_INLINE static void getfilename_sorted(const uint16_t nr) { getfilename(nr); }
106
   #endif
121
   #endif
107
 
122
 
108
   #if ENABLED(POWER_LOSS_RECOVERY)
123
   #if ENABLED(POWER_LOSS_RECOVERY)
109
-    bool jobRecoverFileExists();
110
-    void openJobRecoveryFile(const bool read);
111
-    void removeJobRecoveryFile();
124
+    static bool jobRecoverFileExists();
125
+    static void openJobRecoveryFile(const bool read);
126
+    static void removeJobRecoveryFile();
112
   #endif
127
   #endif
113
 
128
 
114
-  inline void pauseSDPrint() { sdprinting = false; }
115
-  inline bool isFileOpen() { return file.isOpen(); }
116
-  inline bool eof() { return sdpos >= filesize; }
117
-  inline int16_t get() { sdpos = file.curPosition(); return (int16_t)file.read(); }
118
-  inline void setIndex(const uint32_t index) { sdpos = index; file.seekSet(index); }
119
-  inline uint32_t getIndex() { return sdpos; }
120
-  inline uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
121
-  inline char* getWorkDirName() { workDir.getFilename(filename); return filename; }
122
-  inline int16_t read(void* buf, uint16_t nbyte) { return file.isOpen() ? file.read(buf, nbyte) : -1; }
123
-  inline int16_t write(void* buf, uint16_t nbyte) { return file.isOpen() ? file.write(buf, nbyte) : -1; }
129
+  static inline void pauseSDPrint() { flag.sdprinting = false; }
130
+  static inline bool isFileOpen() { return file.isOpen(); }
131
+  static inline bool eof() { return sdpos >= filesize; }
132
+  static inline int16_t get() { sdpos = file.curPosition(); return (int16_t)file.read(); }
133
+  static inline void setIndex(const uint32_t index) { sdpos = index; file.seekSet(index); }
134
+  static inline uint32_t getIndex() { return sdpos; }
135
+  static inline uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
136
+  static inline char* getWorkDirName() { workDir.getFilename(filename); return filename; }
137
+  static inline int16_t read(void* buf, uint16_t nbyte) { return file.isOpen() ? file.read(buf, nbyte) : -1; }
138
+  static inline int16_t write(void* buf, uint16_t nbyte) { return file.isOpen() ? file.write(buf, nbyte) : -1; }
124
 
139
 
125
-  Sd2Card& getSd2Card() { return sd2card; }
140
+  static Sd2Card& getSd2Card() { return sd2card; }
126
 
141
 
127
   #if ENABLED(AUTO_REPORT_SD_STATUS)
142
   #if ENABLED(AUTO_REPORT_SD_STATUS)
128
-    void auto_report_sd_status(void);
129
-    inline void set_auto_report_interval(uint8_t v
143
+    static void auto_report_sd_status(void);
144
+    static inline void set_auto_report_interval(uint8_t v
130
       #if NUM_SERIAL > 1
145
       #if NUM_SERIAL > 1
131
         , int8_t port
146
         , int8_t port
132
       #endif
147
       #endif
140
     }
155
     }
141
   #endif
156
   #endif
142
 
157
 
143
-  inline char* longest_filename() { return longFilename[0] ? longFilename : filename; }
158
+  static inline char* longest_filename() { return longFilename[0] ? longFilename : filename; }
144
 
159
 
145
 public:
160
 public:
146
-  bool saving, logging, sdprinting, cardOK, filenameIsDir, abort_sd_printing;
147
-  char filename[FILENAME_LENGTH], longFilename[LONG_FILENAME_LENGTH];
148
-  int8_t autostart_index;
161
+  static card_flags_t flag;
162
+  static char filename[FILENAME_LENGTH + 1], longFilename[LONG_FILENAME_LENGTH + 1];
163
+  static int8_t autostart_index;
149
 
164
 
150
   #if ENABLED(FAST_FILE_TRANSFER)
165
   #if ENABLED(FAST_FILE_TRANSFER)
151
-    bool binary_mode;
152
     #if NUM_SERIAL > 1
166
     #if NUM_SERIAL > 1
153
-      uint8_t transfer_port;
167
+      static uint8_t transfer_port;
154
     #else
168
     #else
155
       static constexpr uint8_t transfer_port = 0;
169
       static constexpr uint8_t transfer_port = 0;
156
     #endif
170
     #endif
157
   #endif
171
   #endif
158
 
172
 
159
 private:
173
 private:
160
-  SdFile root, workDir, workDirParents[MAX_DIR_DEPTH];
161
-  uint8_t workDirDepth;
174
+  static SdFile root, workDir, workDirParents[MAX_DIR_DEPTH];
175
+  static uint8_t workDirDepth;
162
 
176
 
163
   // Sort files and folders alphabetically.
177
   // Sort files and folders alphabetically.
164
   #if ENABLED(SDCARD_SORT_ALPHA)
178
   #if ENABLED(SDCARD_SORT_ALPHA)
165
-    uint16_t sort_count;        // Count of sorted items in the current directory
179
+    static uint16_t sort_count;   // Count of sorted items in the current directory
166
     #if ENABLED(SDSORT_GCODE)
180
     #if ENABLED(SDSORT_GCODE)
167
-      bool sort_alpha;          // Flag to enable / disable the feature
168
-      int sort_folders;         // Flag to enable / disable folder sorting
169
-      //bool sort_reverse;      // Flag to enable / disable reverse sorting
181
+      static bool sort_alpha;     // Flag to enable / disable the feature
182
+      static int sort_folders;    // Folder sorting before/none/after
183
+      //static bool sort_reverse; // Flag to enable / disable reverse sorting
170
     #endif
184
     #endif
171
 
185
 
172
     // By default the sort index is static
186
     // By default the sort index is static
173
     #if ENABLED(SDSORT_DYNAMIC_RAM)
187
     #if ENABLED(SDSORT_DYNAMIC_RAM)
174
-      uint8_t *sort_order;
188
+      static uint8_t *sort_order;
175
     #else
189
     #else
176
-      uint8_t sort_order[SDSORT_LIMIT];
190
+      static uint8_t sort_order[SDSORT_LIMIT];
177
     #endif
191
     #endif
178
 
192
 
179
     #if ENABLED(SDSORT_USES_RAM) && ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)
193
     #if ENABLED(SDSORT_USES_RAM) && ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)
188
       // If using dynamic ram for names, allocate on the heap.
202
       // If using dynamic ram for names, allocate on the heap.
189
       #if ENABLED(SDSORT_CACHE_NAMES)
203
       #if ENABLED(SDSORT_CACHE_NAMES)
190
         #if ENABLED(SDSORT_DYNAMIC_RAM)
204
         #if ENABLED(SDSORT_DYNAMIC_RAM)
191
-          char **sortshort, **sortnames;
205
+          static char **sortshort, **sortnames;
192
         #else
206
         #else
193
-          char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
194
-          char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
207
+          static char sortshort[SDSORT_LIMIT][FILENAME_LENGTH + 1];
208
+          static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN + 1];
195
         #endif
209
         #endif
196
       #elif DISABLED(SDSORT_USES_STACK)
210
       #elif DISABLED(SDSORT_USES_STACK)
197
-        char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
211
+        static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN + 1];
198
       #endif
212
       #endif
199
 
213
 
200
       // Folder sorting uses an isDir array when caching items.
214
       // Folder sorting uses an isDir array when caching items.
201
       #if HAS_FOLDER_SORTING
215
       #if HAS_FOLDER_SORTING
202
         #if ENABLED(SDSORT_DYNAMIC_RAM)
216
         #if ENABLED(SDSORT_DYNAMIC_RAM)
203
-          uint8_t *isDir;
217
+          static uint8_t *isDir;
204
         #elif ENABLED(SDSORT_CACHE_NAMES) || DISABLED(SDSORT_USES_STACK)
218
         #elif ENABLED(SDSORT_CACHE_NAMES) || DISABLED(SDSORT_USES_STACK)
205
-          uint8_t isDir[(SDSORT_LIMIT+7)>>3];
219
+          static uint8_t isDir[(SDSORT_LIMIT+7)>>3];
206
         #endif
220
         #endif
207
       #endif
221
       #endif
208
 
222
 
210
 
224
 
211
   #endif // SDCARD_SORT_ALPHA
225
   #endif // SDCARD_SORT_ALPHA
212
 
226
 
213
-  Sd2Card sd2card;
214
-  SdVolume volume;
215
-  SdFile file;
216
-
217
-  #define SD_PROCEDURE_DEPTH 1
218
-  #define MAXPATHNAMELENGTH (FILENAME_LENGTH*MAX_DIR_DEPTH + MAX_DIR_DEPTH + 1)
219
-  uint8_t file_subcall_ctr;
220
-  uint32_t filespos[SD_PROCEDURE_DEPTH];
221
-  char proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
222
-  uint32_t filesize, sdpos;
223
-
224
-  LsAction lsAction; //stored for recursion.
225
-  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.
226
-  char* diveDirName;
227
-  void lsDive(const char *prepend, SdFile parent, const char * const match=NULL
227
+  static Sd2Card sd2card;
228
+  static SdVolume volume;
229
+  static SdFile file;
230
+
231
+  #ifndef SD_PROCEDURE_DEPTH
232
+    #define SD_PROCEDURE_DEPTH 1
233
+  #endif
234
+
235
+  static uint8_t file_subcall_ctr;
236
+  static uint32_t filespos[SD_PROCEDURE_DEPTH];
237
+  static char proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH + 1];
238
+
239
+  static uint32_t filesize, sdpos;
240
+
241
+  static LsAction lsAction; //stored for recursion.
242
+  static 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.
243
+  static char *diveDirName;
244
+  static void lsDive(const char *prepend, SdFile parent, const char * const match=NULL
228
     #if NUM_SERIAL > 1
245
     #if NUM_SERIAL > 1
229
       , const int8_t port = -1
246
       , const int8_t port = -1
230
     #endif
247
     #endif
231
   );
248
   );
232
 
249
 
233
   #if ENABLED(SDCARD_SORT_ALPHA)
250
   #if ENABLED(SDCARD_SORT_ALPHA)
234
-    void flush_presort();
251
+    static void flush_presort();
235
   #endif
252
   #endif
236
 
253
 
237
   #if ENABLED(AUTO_REPORT_SD_STATUS)
254
   #if ENABLED(AUTO_REPORT_SD_STATUS)
256
   #define IS_SD_INSERTED() true
273
   #define IS_SD_INSERTED() true
257
 #endif
274
 #endif
258
 
275
 
259
-#define IS_SD_PRINTING()  card.sdprinting
276
+#define IS_SD_PRINTING()  card.flag.sdprinting
260
 #define IS_SD_FILE_OPEN() card.isFileOpen()
277
 #define IS_SD_FILE_OPEN() card.isFileOpen()
261
 
278
 
262
 extern CardReader card;
279
 extern CardReader card;

正在加载...
取消
保存