From 2129bcf315dd6a8038c6d0eef0c4c7fa15b06112 Mon Sep 17 00:00:00 2001 From: Voinea Dragos Date: Mon, 19 Apr 2021 14:48:50 +0300 Subject: [PATCH] M20 T --- Firmware/Marlin_main.cpp | 6 +++-- Firmware/cardreader.cpp | 53 ++++++++++++++++++++-------------------- Firmware/cardreader.h | 18 +++++++++++--- 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 689330934..f46c6a53c 100755 --- a/Firmware/Marlin_main.cpp +++ b/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 M21: Initialize SD card diff --git a/Firmware/cardreader.cpp b/Firmware/cardreader.cpp index c5900a7c7..37c9ade97 100644 --- a/Firmware/cardreader.cpp +++ b/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, ¶ms); } @@ -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; } diff --git a/Firmware/cardreader.h b/Firmware/cardreader.h index 47343e238..7eba0f51d 100644 --- a/Firmware/cardreader.h +++ b/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