From 2e43438e0c3f2c5e0ec7b701c6f33a71136a601a Mon Sep 17 00:00:00 2001
From: TheSFReader <TheSFReader@gmail.com>
Date: Sun, 11 Mar 2018 11:57:31 +0100
Subject: [PATCH] Add C parameter to M27 to include the long filename

In answer to #10001

Add an option to retrieve the currently open file name (long filename if possible).
---
 Marlin/src/gcode/gcode.h                      |  4 ++-
 .../src/gcode/sdcard/M20-M30_M32-M34_M928.cpp | 31 +++++++++++++------
 Marlin/src/sd/cardreader.cpp                  | 30 ++++++++++++++++++
 Marlin/src/sd/cardreader.h                    |  5 +++
 4 files changed, 59 insertions(+), 11 deletions(-)

diff --git a/Marlin/src/gcode/gcode.h b/Marlin/src/gcode/gcode.h
index 548987c96f..3fd16a95fc 100644
--- a/Marlin/src/gcode/gcode.h
+++ b/Marlin/src/gcode/gcode.h
@@ -85,7 +85,9 @@
  * M24  - Start/resume SD print. (Requires SDSUPPORT)
  * M25  - Pause SD print. (Requires SDSUPPORT)
  * M26  - Set SD position in bytes: "M26 S12345". (Requires SDSUPPORT)
- * M27  - Report SD print status. (Requires SDSUPPORT) Or, with 'S<seconds>' set the SD status auto-report interval. (Requires AUTO_REPORT_SD_STATUS)
+ * M27  - Report SD print status. (Requires SDSUPPORT)
+ *        OR, with 'S<seconds>' set the SD status auto-report interval. (Requires AUTO_REPORT_SD_STATUS)
+ *        OR, with 'C' get the current filename.
  * M28  - Start SD write: "M28 /path/file.gco". (Requires SDSUPPORT)
  * M29  - Stop SD write. (Requires SDSUPPORT)
  * M30  - Delete file from SD: "M30 /path/file.gco"
diff --git a/Marlin/src/gcode/sdcard/M20-M30_M32-M34_M928.cpp b/Marlin/src/gcode/sdcard/M20-M30_M32-M34_M928.cpp
index 0901d6359a..59d45492a6 100644
--- a/Marlin/src/gcode/sdcard/M20-M30_M32-M34_M928.cpp
+++ b/Marlin/src/gcode/sdcard/M20-M30_M32-M34_M928.cpp
@@ -107,24 +107,35 @@ void GcodeSuite::M26() {
 }
 
 /**
- * M27: Get SD Card status or set the SD status auto-report interval.
+ * M27: Get SD Card status
+ *      OR, with 'S<seconds>' set the SD status auto-report interval. (Requires AUTO_REPORT_SD_STATUS)
+ *      OR, with 'C' get the current filename.
  */
 void GcodeSuite::M27() {
+  #if NUM_SERIAL > 1
+    const int16_t port = command_queue_port[cmd_queue_index_r];
+  #endif
+
+  if (parser.seen('C')) {
+    SERIAL_ECHOPGM_P(port, "Current file: ");
+    card.printFilename();
+  }
+
   #if ENABLED(AUTO_REPORT_SD_STATUS)
-    if (parser.seenval('S')) {
+    else if (parser.seenval('S'))
       card.set_auto_report_interval(parser.value_byte()
         #if NUM_SERIAL > 1
-          , command_queue_port[cmd_queue_index_r]
+          , port
         #endif
       );
-    }
-    else
   #endif
-      card.getStatus(
-        #if NUM_SERIAL > 1
-          command_queue_port[cmd_queue_index_r]
-        #endif
-      );
+
+  else
+    card.getStatus(
+      #if NUM_SERIAL > 1
+        port
+      #endif
+    );
 }
 
 /**
diff --git a/Marlin/src/sd/cardreader.cpp b/Marlin/src/sd/cardreader.cpp
index 10e4396824..5aba4c62f5 100644
--- a/Marlin/src/sd/cardreader.cpp
+++ b/Marlin/src/sd/cardreader.cpp
@@ -262,6 +262,32 @@ void CardReader::ls(
 
 #endif // LONG_FILENAME_HOST_SUPPORT
 
+/**
+ * Echo the DOS 8.3 filename (and long filename, if any)
+ */
+void CardReader::printFilename(
+  #if NUM_SERIAL > 1
+    const int8_t port/*= -1*/
+  #endif
+) {
+  if (file.isOpen()) {
+    char lfilename[FILENAME_LENGTH];
+    file.getFilename(lfilename);
+    SERIAL_ECHO_P(port, lfilename);
+    #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
+      getfilename(0, lfilename);
+      if (longFilename[0]) {
+        SERIAL_ECHO_P(port, ' ');
+        SERIAL_ECHO_P(port, longFilename);
+      }
+    #endif
+  }
+  else
+    SERIAL_ECHOPGM_P(port, "(no file)");
+
+  SERIAL_EOL_P(port);
+}
+
 void CardReader::initsd() {
   cardOK = false;
   if (root.isOpen()) root.close();
@@ -460,8 +486,12 @@ void CardReader::openFile(char* name, const bool read, const bool subcall/*=fals
       SERIAL_PROTOCOLPAIR(MSG_SD_FILE_OPENED, fname);
       SERIAL_PROTOCOLLNPAIR(MSG_SD_SIZE, filesize);
       SERIAL_PROTOCOLLNPGM(MSG_SD_FILE_SELECTED);
+
       getfilename(0, fname);
       lcd_setstatus(longFilename[0] ? longFilename : fname);
+      //if (longFilename[0]) {
+      //  SERIAL_PROTOCOLPAIR(MSG_SD_FILE_LONG_NAME, longFilename);
+      //}
     }
     else {
       SERIAL_PROTOCOLPAIR(MSG_SD_OPEN_FILE_FAIL, fname);
diff --git a/Marlin/src/sd/cardreader.h b/Marlin/src/sd/cardreader.h
index db4e7f216a..66a23d47c5 100644
--- a/Marlin/src/sd/cardreader.h
+++ b/Marlin/src/sd/cardreader.h
@@ -63,6 +63,11 @@ public:
     #endif
   );
   void printingHasFinished();
+  void printFilename(
+    #if NUM_SERIAL > 1
+      const int8_t port = -1
+    #endif
+  );
 
   #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
     void printLongPath(char *path
-- 
GitLab