Skip to content

Commit

Permalink
Export M503 setting to file - serial redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
vovodroid committed Apr 5, 2024
1 parent 17dfe8e commit 80cc6fb
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Marlin/src/core/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include "../feature/ethernet.h"
#endif

#include <src/sd/SdFile.h>
#include <src/sd/cardreader.h>

// Echo commands to the terminal by default in dev mode
uint8_t marlin_debug_flags = TERN(MARLIN_DEV_MODE, MARLIN_DEBUG_ECHO, MARLIN_DEBUG_NONE);

Expand Down Expand Up @@ -144,3 +147,26 @@ void print_xyze(LOGICAL_AXIS_ARGS_(const_float_t) FSTR_P const prefix/*=nullptr*
#endif
if (suffix) SERIAL_ECHO(suffix); else SERIAL_EOL();
}

#if ENABLED(SERIAL_2_FILE)
MediaFile sr_dump_file;
size_t sr_write_res;

bool sr_file_open(const char * filename)
{
sr_write_res = 0;
MediaFile root = card.getroot();
return sr_dump_file.open(&root, filename, O_CREAT | O_WRITE | O_TRUNC);
}

void serial2file(uint8_t c)
{
if (sr_dump_file.isOpen() && sr_write_res != -1)
sr_write_res = sr_dump_file.write(c);
}

bool sr_file_close()
{
return sr_dump_file.close();
}
#endif
4 changes: 4 additions & 0 deletions Marlin/src/core/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ inline void print_xyze(const xyze_pos_t &xyze, FSTR_P const prefix=nullptr, FSTR
print_xyze(LOGICAL_AXIS_ELEM_(xyze) prefix, suffix);
}

bool sr_file_open(const char * filename);
bool sr_file_close();
extern size_t sr_write_res;

#define SERIAL_POS(SUFFIX,VAR) do { print_xyz(VAR, F(" " STRINGIFY(VAR) "="), F(" : " SUFFIX "\n")); }while(0)
#define SERIAL_XYZ(PREFIX,V...) do { print_xyz(V, F(PREFIX)); }while(0)

Expand Down
5 changes: 5 additions & 0 deletions Marlin/src/core/serial_hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ struct RuntimeSerial : public SerialBase< RuntimeSerial<SerialT> >, public Seria
#define _S_CLASS(N) class Serial##N##T,
#define _S_NAME(N) Serial##N##T,

void serial2file(uint8_t c);

template < REPEAT(NUM_SERIAL, _S_CLASS) const uint8_t offset=0, const uint8_t step=1 >
struct MultiSerial : public SerialBase< MultiSerial< REPEAT(NUM_SERIAL, _S_NAME) offset, step > > {
typedef SerialBase< MultiSerial< REPEAT(NUM_SERIAL, _S_NAME) offset, step > > BaseClassT;
Expand Down Expand Up @@ -227,6 +229,9 @@ struct MultiSerial : public SerialBase< MultiSerial< REPEAT(NUM_SERIAL, _S_NAME)
#define _S_WRITE(N) if (portMask.enabled(output[N])) serial##N.write(c);
REPEAT(NUM_SERIAL, _S_WRITE);
#undef _S_WRITE
#if ENABLED(SERIAL_2_FILE)
serial2file(c);
#endif
}
NO_INLINE void msgDone() {
#define _S_DONE(N) if (portMask.enabled(output[N])) serial##N.msgDone();
Expand Down
5 changes: 5 additions & 0 deletions Marlin/src/inc/Conditionals_LCD.h
Original file line number Diff line number Diff line change
Expand Up @@ -1888,3 +1888,8 @@
#if ALL(SPI_FLASH, HAS_MEDIA, MARLIN_DEV_MODE)
#define SPI_FLASH_BACKUP 1
#endif

#if ALL(HAS_MEDIA, HAS_MARLINUI_MENU)
#define EXPORT_SETTINGS // Export memory settings to file M503.gc in SD card root for replay
#define SERIAL_2_FILE // Dump serial output to file, needed for export settings
#endif
2 changes: 2 additions & 0 deletions Marlin/src/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -2612,6 +2612,8 @@ static_assert(NUM_SERVOS <= NUM_SERVO_PLUGS, "NUM_SERVOS (or some servo index) i
#error "Either disable SDCARD_READONLY or disable BINARY_FILE_TRANSFER."
#elif ENABLED(SDCARD_EEPROM_EMULATION)
#error "Either disable SDCARD_READONLY or disable SDCARD_EEPROM_EMULATION."
#elif ENABLED(EXPORT_SETTINGS)
#error "Either disable SDCARD_READONLY or disable EXPORT_SETTINGS."
#endif
#endif

Expand Down
1 change: 1 addition & 0 deletions Marlin/src/lcd/language/language_en.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ namespace LanguageNarrow_en {
LSTR MSG_LOAD_EEPROM = _UxGT("Load Settings");
LSTR MSG_RESTORE_DEFAULTS = _UxGT("Restore Defaults");
LSTR MSG_INIT_EEPROM = _UxGT("Initialize EEPROM");
LSTR MSG_EXPORT_SETTINGS = _UxGT("Export Settings");
LSTR MSG_ERR_EEPROM_CRC = _UxGT("Err: EEPROM CRC");
LSTR MSG_ERR_EEPROM_SIZE = _UxGT("Err: EEPROM Size");
LSTR MSG_ERR_EEPROM_VERSION = _UxGT("Err: EEPROM Version");
Expand Down
10 changes: 10 additions & 0 deletions Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,16 @@ void MarlinUI::host_notify(const char * const cstr) {
}
#endif

#if ENABLED(EXPORT_SETTINGS)
void MarlinUI::export_settings() {
if (sr_file_open("M503.gc")) {
settings.report(true);
completion_feedback(sr_file_close() && sr_write_res != -1);
} else
completion_feedback(false);
}
#endif

#endif

#if ENABLED(EEPROM_SETTINGS)
Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/lcd/marlinui.h
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,10 @@ class MarlinUI {
static void eeprom_alert(const EEPROM_Error) TERN_(EEPROM_AUTO_INIT, {});
#endif

#if ENABLED(EXPORT_SETTINGS)
static void export_settings();
#endif

//
// Special handling if a move is underway
//
Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/lcd/menu/menu_advanced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,10 @@ void menu_advanced_settings() {
);
#endif

#if ENABLED(EXPORT_SETTINGS)
ACTION_ITEM(MSG_EXPORT_SETTINGS, ui.export_settings);
#endif

END_MENU();
}

Expand Down

0 comments on commit 80cc6fb

Please sign in to comment.