From 01c90c940e8bde88acc514baee679ee6635ac57a Mon Sep 17 00:00:00 2001 From: Bjoern Kerler Date: Sat, 10 Feb 2024 19:07:21 +0100 Subject: [PATCH] Add gcode M27 auto report support --- lib/Marlin/Marlin/src/gcode/gcode.h | 7 ++++++ src/common/marlin_server.cpp | 13 +++++++++++ src/marlin_stubs/sdcard/M20-M30_M32-M34.cpp | 24 ++++++++++++++------- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/lib/Marlin/Marlin/src/gcode/gcode.h b/lib/Marlin/Marlin/src/gcode/gcode.h index c23a4a1870..e3cecd7244 100644 --- a/lib/Marlin/Marlin/src/gcode/gcode.h +++ b/lib/Marlin/Marlin/src/gcode/gcode.h @@ -308,6 +308,13 @@ enum AxisRelative : uint8_t { REL_X, REL_Y, REL_Z, REL_E, E_MODE_ABS, E_MODE_REL }; +#if ENABLED(SDSUPPORT) || ENABLED(SDCARD_GCODES) +namespace M27_handler { + extern uint32_t sd_auto_report_delay; + void print_sd_status(); +} // namespace M27_handler +#endif + class GcodeSuite { public: diff --git a/src/common/marlin_server.cpp b/src/common/marlin_server.cpp index 81d234234d..efaea7f339 100644 --- a/src/common/marlin_server.cpp +++ b/src/common/marlin_server.cpp @@ -566,6 +566,15 @@ void safely_unload_filament_from_nozzle_to_mmu() { } #endif +void print_sd_report() { + static uint32_t last_sd_report = 0; + uint32_t current_time = ticks_s(); + if (M27_handler::sd_auto_report_delay && (current_time - last_sd_report) >= M27_handler::sd_auto_report_delay) { + M27_handler::print_sd_status(); + last_sd_report = current_time; + } +} + #ifdef MINDA_BROKEN_CABLE_DETECTION static void print_Z_probe_cnt() { if (DEBUGGING(INFO)) { @@ -641,6 +650,10 @@ static void cycle() { print_fan_spd(); +#if ENABLED(SDSUPPORT) || ENABLED(SDCARD_GCODES) + print_sd_report(); +#endif + #ifdef MINDA_BROKEN_CABLE_DETECTION print_Z_probe_cnt(); #endif diff --git a/src/marlin_stubs/sdcard/M20-M30_M32-M34.cpp b/src/marlin_stubs/sdcard/M20-M30_M32-M34.cpp index fa525bc45b..4ecd8664d5 100644 --- a/src/marlin_stubs/sdcard/M20-M30_M32-M34.cpp +++ b/src/marlin_stubs/sdcard/M20-M30_M32-M34.cpp @@ -82,6 +82,19 @@ void GcodeSuite::M26() { } } +uint32_t M27_handler::sd_auto_report_delay = 0; + +void M27_handler::print_sd_status() { + if (media_print_get_state() != media_print_state_NONE) { + SERIAL_ECHOPGM(MSG_SD_PRINTING_BYTE); + SERIAL_ECHO(media_print_get_position()); + SERIAL_CHAR('/'); + SERIAL_ECHOLN(media_print_get_size()); + } else { + SERIAL_ECHOLNPGM(MSG_SD_NOT_PRINTING); + } +} + /** * M27 - Report SD print status on serial port * @@ -93,15 +106,10 @@ void GcodeSuite::M27() { if (parser.seen('C')) { SERIAL_ECHOPGM("Current file: "); SERIAL_ECHOLN(marlin_vars()->media_SFN_path.get_ptr()); + } else if (parser.seen('S')) { + M27_handler::sd_auto_report_delay = parser.byteval('S'); } else { - if (media_print_get_state() != media_print_state_NONE) { - SERIAL_ECHOPGM(MSG_SD_PRINTING_BYTE); - SERIAL_ECHO(media_print_get_position()); - SERIAL_CHAR('/'); - SERIAL_ECHOLN(media_print_get_size()); - } else { - SERIAL_ECHOLNPGM(MSG_SD_NOT_PRINTING); - } + M27_handler::print_sd_status(); } }