Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/GCodes/GCodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3437,6 +3437,24 @@ bool GCodes::GetMacroRestarted() const noexcept
return ms.doingFileMacro && ms.GetPrevious() != nullptr && ms.GetPrevious()->firstCommandAfterRestart;
}

void GCodes::FileMacroReturnValue(GCodeBuffer& gb, const StringRef& varName, ExpressionValue& returnValue) noexcept
{
if (gb.IsDoingFileMacro() && gb.GetStackDepth() > 0)
{
GCodeMachineState * const ps = gb.CurrentFileMachineState().GetPrevious();
auto vset = WriteLockedPointer<VariableSet>(nullptr, &ps->variables);
Variable *_ecv_null v = vset->Lookup(varName.c_str(), false);
if (v == nullptr)
{
vset->InsertNew(varName.c_str(), returnValue, ps->GetBlockNesting());
}
else
{
v->Assign(returnValue);
}
}
}

void GCodes::FileMacroCyclesReturn(GCodeBuffer& gb) noexcept
{
//const int retValue = (gb.Seen('P')) ? gb.GetIValue() : 0; // for when we allow M99 to return 'result'
Expand Down
3 changes: 2 additions & 1 deletion src/GCodes/GCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ class GCodes
bool DoFileMacro(GCodeBuffer& gb, const char *_ecv_array fileName, bool reportMissing, int codeRunning) noexcept; // Run a GCode macro file, optionally report error if not found
bool DoFileMacroWithParameters(GCodeBuffer& gb, const char *_ecv_array fileName, bool reportMissing, int codeRunning) THROWS(GCodeException);

void FileMacroCyclesReturn(GCodeBuffer& gb) noexcept; // End a macro
void FileMacroCyclesReturn(GCodeBuffer& gb) noexcept; // End a macro
void FileMacroReturnValue(GCodeBuffer& gb, const StringRef& varName, ExpressionValue& returnValue) noexcept; // Return a value from a macro

bool ActOnCode(GCodeBuffer& gb, const StringRef& reply) noexcept; // Do a G, M or T Code
bool HandleGcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeException); // Do a G code
Expand Down
11 changes: 11 additions & 0 deletions src/GCodes/GCodes2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1675,8 +1675,19 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeEx
break;

case 99: // Return from Macro/Subprogram
{
String<MaxVariableNameLength> varName;
bool seenV = false;
gb.TryGetQuotedString('V', varName.GetRef(), seenV, false);

if(seenV) {
ExpressionValue returnVar = (gb.Seen('R')) ? gb.GetExpression() : ExpressionValue();
FileMacroReturnValue(gb, varName.GetRef(), returnVar);
}

FileMacroCyclesReturn(gb);
break;
}

case 101: // Un-retract, generated by S3D if "Include M101/101/103" is enabled
result = RetractFilament(gb, false);
Expand Down