Skip to content

Commit 9e7e7b1

Browse files
committed
Allow M99 to return a named variable and value
Implements #1038 Signed-off-by: Ben Agricola <[email protected]>
1 parent f2893c0 commit 9e7e7b1

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/GCodes/GCodes.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,6 +3437,24 @@ bool GCodes::GetMacroRestarted() const noexcept
34373437
return ms.doingFileMacro && ms.GetPrevious() != nullptr && ms.GetPrevious()->firstCommandAfterRestart;
34383438
}
34393439

3440+
void GCodes::FileMacroReturnValue(GCodeBuffer& gb, const StringRef& varName, ExpressionValue& returnValue) noexcept
3441+
{
3442+
if (gb.IsDoingFileMacro() && gb.GetStackDepth() > 0)
3443+
{
3444+
GCodeMachineState * const ps = gb.CurrentFileMachineState().GetPrevious();
3445+
auto vset = WriteLockedPointer<VariableSet>(nullptr, &ps->variables);
3446+
Variable *_ecv_null v = vset->Lookup(varName.c_str(), false);
3447+
if (v == nullptr)
3448+
{
3449+
vset->InsertNew(varName.c_str(), returnValue, ps->GetBlockNesting());
3450+
}
3451+
else
3452+
{
3453+
v->Assign(returnValue);
3454+
}
3455+
}
3456+
}
3457+
34403458
void GCodes::FileMacroCyclesReturn(GCodeBuffer& gb) noexcept
34413459
{
34423460
//const int retValue = (gb.Seen('P')) ? gb.GetIValue() : 0; // for when we allow M99 to return 'result'

src/GCodes/GCodes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ class GCodes
385385
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
386386
bool DoFileMacroWithParameters(GCodeBuffer& gb, const char *_ecv_array fileName, bool reportMissing, int codeRunning) THROWS(GCodeException);
387387

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

390391
bool ActOnCode(GCodeBuffer& gb, const StringRef& reply) noexcept; // Do a G, M or T Code
391392
bool HandleGcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeException); // Do a G code

src/GCodes/GCodes2.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,8 +1675,19 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeEx
16751675
break;
16761676

16771677
case 99: // Return from Macro/Subprogram
1678+
{
1679+
String<MaxVariableNameLength> varName;
1680+
bool seenV = false;
1681+
gb.TryGetQuotedString('V', varName.GetRef(), seenV, false);
1682+
1683+
if(seenV) {
1684+
ExpressionValue returnVar = (gb.Seen('R')) ? gb.GetExpression() : ExpressionValue();
1685+
FileMacroReturnValue(gb, varName.GetRef(), returnVar);
1686+
}
1687+
16781688
FileMacroCyclesReturn(gb);
16791689
break;
1690+
}
16801691

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

0 commit comments

Comments
 (0)