Voinea Dragos 3 years ago
parent
commit
2129bcf315
3 changed files with 44 additions and 33 deletions
  1. 4 2
      Firmware/Marlin_main.cpp
  2. 26 27
      Firmware/cardreader.cpp
  3. 14 4
      Firmware/cardreader.h

+ 4 - 2
Firmware/Marlin_main.cpp

@@ -5744,11 +5744,13 @@ if(eSoundMode!=e_SOUND_MODE_SILENT)
     - `L` - Reports ling filenames instead of just short filenames. Requires host software parsing.
     */
     case 20:
+    {
       KEEPALIVE_STATE(NOT_BUSY); // do not send busy messages during listing. Inhibits the output of manage_heater()
       SERIAL_PROTOCOLLNRPGM(_N("Begin file list"));////MSG_BEGIN_FILE_LIST
-      card.ls(code_seen('L'));
+      struct CardReader::ls_param params = {.LFN = code_seen('L'), .timestamp = code_seen('T')};
+      card.ls(params);
       SERIAL_PROTOCOLLNRPGM(_N("End file list"));////MSG_END_FILE_LIST
-      break;
+    } break;
 
     /*!
 	### M21 - Init SD card <a href="https://reprap.org/wiki/G-code#M21:_Initialize_SD_card">M21: Initialize SD card</a>

+ 26 - 27
Firmware/cardreader.cpp

@@ -61,10 +61,9 @@ char *createFilename(char *buffer,const dir_t &p) //buffer>12characters
 +*   LS_Count           - Add +1 to nrFiles for every file within the parent
 +*   LS_GetFilename     - Get the filename of the file indexed by nrFiles
 +*   LS_SerialPrint     - Print the full path and size of each file to serial output
-+*   LS_SerialPrint_LFN - Print the full path, long filename and size of each file to serial output
 +*/
 
-void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) {
+void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/, LsAction lsAction, struct ls_param *lsParams) {
 	static uint8_t recursionCnt = 0;
 	// RAII incrementer for the recursionCnt
 	class _incrementer
@@ -85,7 +84,7 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
 			if (pn0 == DIR_NAME_DELETED || pn0 == '.') continue;
 			if (longFilename[0] == '.') continue;
 			if (!DIR_IS_FILE_OR_SUBDIR(&p) || (p.attributes & DIR_ATT_HIDDEN)) continue;
-			else if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) { // If the entry is a directory and the action is LS_SerialPrint
+			if (DIR_IS_SUBDIR(&p) && lsAction == LS_SerialPrint) { // If the entry is a directory and the action is LS_SerialPrint
 				// Get the short name for the item, which we know is a folder
 				char lfilename[FILENAME_LENGTH];
 				createFilename(lfilename, p);
@@ -103,21 +102,19 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
 				// Get a new directory object using the full path
 				// and dive recursively into it.
 				
-				if (lsAction == LS_SerialPrint_LFN)
+				if (lsParams->LFN)
 					printf_P(PSTR("DIR_ENTER: %s \"%s\"\n"), path, longFilename[0] ? longFilename : lfilename);
 				
 				SdFile dir;
 				if (!dir.open(parent, lfilename, O_READ)) {
-					if (lsAction == LS_SerialPrint || lsAction == LS_SerialPrint_LFN) {
-						//SERIAL_ECHO_START();
-						//SERIAL_ECHOPGM(_i("Cannot open subdir"));////MSG_SD_CANT_OPEN_SUBDIR
-						//SERIAL_ECHOLN(lfilename);
-					}
+					//SERIAL_ECHO_START();
+					//SERIAL_ECHOPGM(_i("Cannot open subdir"));////MSG_SD_CANT_OPEN_SUBDIR
+					//SERIAL_ECHOLN(lfilename);
 				}
-				lsDive(path, dir);
+				lsDive(path, dir, NULL, lsAction, lsParams);
 				// close() is done automatically by destructor of SdFile
 				
-				if (lsAction == LS_SerialPrint_LFN)
+				if (lsParams->LFN)
 					puts_P(PSTR("DIR_EXIT"));
 			}
 			else {
@@ -128,7 +125,6 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
 						nrFiles++;
 						break;
 					
-					case LS_SerialPrint_LFN:
 					case LS_SerialPrint:
 						createFilename(filename, p);
 						SERIAL_PROTOCOL(prepend);
@@ -137,7 +133,18 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
 						MYSERIAL.write(' ');
 						SERIAL_PROTOCOL(p.fileSize);
 						
-						if (lsAction == LS_SerialPrint_LFN)
+						if (lsParams->timestamp)
+						{
+							crmodDate = p.lastWriteDate;
+							crmodTime = p.lastWriteTime;
+							if( crmodDate < p.creationDate || ( crmodDate == p.creationDate && crmodTime < p.creationTime ) ){
+								crmodDate = p.creationDate;
+								crmodTime = p.creationTime;
+							}
+							printf_P(PSTR(" %#lx"), ((uint32_t)crmodDate << 16) | crmodTime);
+						}
+						
+						if (lsParams->LFN)
 							printf_P(PSTR(" \"%s\""), LONGEST_FILENAME);
 						
 						SERIAL_PROTOCOLLN();
@@ -182,14 +189,10 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
 		} // while readDir
 }
 
-void CardReader::ls(bool printLFN)
+void CardReader::ls(struct ls_param params)
 {
-  lsAction = printLFN ? LS_SerialPrint_LFN : LS_SerialPrint;
-  //if(lsAction==LS_Count)
-  //nrFiles=0;
-
   root.rewind();
-  lsDive("",root);
+  lsDive("",root, NULL, LS_SerialPrint, &params);
 }
 
 
@@ -696,38 +699,34 @@ void CardReader::closefile(bool store_location)
 void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/)
 {
   curDir=&workDir;
-  lsAction=LS_GetFilename;
   nrFiles=nr;
   curDir->rewind();
-  lsDive("",*curDir,match);
+  lsDive("",*curDir,match, LS_GetFilename);
   
 }
 
 void CardReader::getfilename_simple(uint32_t position, const char * const match/*=NULL*/)
 {
 	curDir = &workDir;
-	lsAction = LS_GetFilename;
 	nrFiles = 0;
 	curDir->seekSet(position);
-	lsDive("", *curDir, match);
+	lsDive("", *curDir, match, LS_GetFilename);
 }
 
 void CardReader::getfilename_next(uint32_t position, const char * const match/*=NULL*/)
 {
 	curDir = &workDir;
-	lsAction = LS_GetFilename;
 	nrFiles = 1;
 	curDir->seekSet(position);
-	lsDive("", *curDir, match);
+	lsDive("", *curDir, match, LS_GetFilename);
 }
 
 uint16_t CardReader::getnrfilenames()
 {
   curDir=&workDir;
-  lsAction=LS_Count;
   nrFiles=0;
   curDir->rewind();
-  lsDive("",*curDir);
+  lsDive("",*curDir, NULL, LS_Count);
   //SERIAL_ECHOLN(nrFiles);
   return nrFiles;
 }

+ 14 - 4
Firmware/cardreader.h

@@ -8,12 +8,23 @@
 #define MAX_DIR_DEPTH 6
 
 #include "SdFile.h"
-enum LsAction {LS_SerialPrint,LS_SerialPrint_LFN,LS_Count,LS_GetFilename};
 class CardReader
 {
 public:
   CardReader();
   
+  enum LsAction : uint8_t
+  {
+    LS_SerialPrint,
+    LS_Count,
+    LS_GetFilename,
+  };
+  struct ls_param
+  {
+    bool LFN : 1;
+    bool timestamp : 1;
+  } __attribute__((packed));
+  
   void initsd();
   void write_command(char *buf);
   void write_command_no_newline(char *buf);
@@ -43,7 +54,7 @@ public:
   uint16_t getWorkDirDepth();
   
 
-  void ls(bool printLFN);
+  void ls(struct ls_param params);
   bool chdir(const char * relpath, bool doPresort);
   void updir();
   void setroot(bool doPresort);
@@ -122,12 +133,11 @@ private:
 
   bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
   
-  LsAction lsAction; //stored for recursion.
   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.
   char* diveDirName;
 
   bool diveSubfolder (const char *&fileName);
-  void lsDive(const char *prepend, SdFile parent, const char * const match=NULL);
+  void lsDive(const char *prepend, SdFile parent, const char * const match=NULL, LsAction lsAction = LS_GetFilename, struct ls_param *lsParams = NULL);
 #ifdef SDCARD_SORT_ALPHA
   void flush_presort();
 #endif