Skip to content

Commit 0c1de66

Browse files
committed
Changes to fix M99 (WIP)
1 parent 925e548 commit 0c1de66

File tree

2 files changed

+33
-43
lines changed

2 files changed

+33
-43
lines changed

src/DuetControlServer/SPI/Channel/Processor.cs

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ public State Push(CodeFile? file = null)
121121
}
122122

123123
// Done
124-
CurrentState.SetBusy(true);
125124
Stack.Push(state);
126125
CurrentState = state;
127126
return state;
@@ -526,14 +525,19 @@ public async Task SetMacroPausable(bool isPausable)
526525
}
527526

528527
/// <summary>
529-
/// Called when the last or all files have been aborted
528+
/// Called when the last or all files have been aborted by the firmware
530529
/// </summary>
531530
/// <param name="abortAll">Whether to abort all files</param>
532-
/// <param name="fromFirmware">Whether the request came from the firmware</param>
533-
public void FilesAborted(bool abortAll, bool fromFirmware)
531+
public void FilesAborted(bool abortAll)
534532
{
535533
bool macroAborted = false;
536534

535+
// If only the last macro is aborted, we may have a pending reply for e.g. M99
536+
if (!abortAll)
537+
{
538+
ResolvePendingReplies();
539+
}
540+
537541
// Clean up the stack
538542
Code? startCode = null;
539543
while (CurrentState.WaitingForAcknowledgement || CurrentState.File is MacroFile)
@@ -548,9 +552,9 @@ public void FilesAborted(bool abortAll, bool fromFirmware)
548552
{
549553
using (macro.Lock())
550554
{
551-
// Resolve potential start codes when the macro file finishes
552-
if (startCode is not null)
555+
if (startCode is not null && abortAll)
553556
{
557+
// Wait for the macro to be fully cancelled and then cancel the code that started it
554558
_ = macro.WaitForFinishAsync().ContinueWith(async task =>
555559
{
556560
try
@@ -571,14 +575,14 @@ public void FilesAborted(bool abortAll, bool fromFirmware)
571575
}
572576
else if (startCode is not null)
573577
{
574-
// Cancel the code that started the blocking message prompt
578+
// This is a message prompt. Cancel the code that started it
575579
Codes.Processor.CancelCode(startCode);
576580
startCode = null;
577581
}
578582

579583
// Pop the stack
580584
Pop();
581-
if (startCode is not null)
585+
if (startCode is not null && abortAll)
582586
{
583587
_logger.Debug("==> Unfinished starting code: {0}", startCode);
584588
}
@@ -593,22 +597,32 @@ public void FilesAborted(bool abortAll, bool fromFirmware)
593597
if (abortAll)
594598
{
595599
// Cancel pending codes and requests
596-
_allFilesAborted = !fromFirmware && (DataTransfer.ProtocolVersion >= 3);
597600
InvalidateRegular();
598601
}
599602
else
600603
{
601604
// Invalidate remaining buffered codes from the last macro file
602605
foreach (Code bufferedCode in BufferedCodes)
603606
{
604-
Codes.Processor.CancelCode(bufferedCode);
607+
if (bufferedCode != startCode)
608+
{
609+
Codes.Processor.CancelCode(bufferedCode);
610+
}
605611
}
606612
BufferedCodes.Clear();
607613
BytesBuffered = 0;
614+
615+
// If only the last file was closed (e.g. from M99), carry on with the execution of the code that started it
616+
if (startCode is not null)
617+
{
618+
BytesBuffered += startCode.BinarySize;
619+
BufferedCodes.Insert(0, startCode);
620+
_logger.Debug("==> Resuming unfinished starting code: {0}", startCode);
621+
}
608622
}
609623

610624
// Abort the file print if necessary
611-
if ((Channel == CodeChannel.File || Channel == CodeChannel.File2) && (abortAll || !macroAborted))
625+
if ((Channel is CodeChannel.File or CodeChannel.File2) && (abortAll || !macroAborted))
612626
{
613627
using (JobProcessor.Lock())
614628
{
@@ -618,15 +632,11 @@ public void FilesAborted(bool abortAll, bool fromFirmware)
618632
}
619633

620634
/// <summary>
621-
/// Abort the last or all files asynchronously
635+
/// Abort all files asynchronously
622636
/// </summary>
623-
/// <param name="abortAll">Whether to abort all files</param>
624-
/// <param name="fromFirmware">Whether the request came from the firmware</param>
625637
/// <returns>Asynchronous task</returns>
626-
public async Task AbortFilesAsync(bool abortAll, bool fromFirmware)
638+
public async Task AbortAllFilesAsync()
627639
{
628-
bool macroAborted = false;
629-
630640
// Clean up the stack
631641
Code? startCode = null;
632642
while (CurrentState.WaitingForAcknowledgement || CurrentState.File is MacroFile)
@@ -660,7 +670,6 @@ public async Task AbortFilesAsync(bool abortAll, bool fromFirmware)
660670
// Abort the macro file
661671
macro.Abort();
662672
}
663-
macroAborted = true;
664673
}
665674
else if (startCode is not null)
666675
{
@@ -675,33 +684,14 @@ public async Task AbortFilesAsync(bool abortAll, bool fromFirmware)
675684
{
676685
_logger.Debug("==> Unfinished starting code: {0}", startCode);
677686
}
678-
679-
// Stop if only a single file is supposed to be aborted
680-
if (!abortAll && macroAborted)
681-
{
682-
break;
683-
}
684687
}
685688

686-
if (abortAll)
687-
{
688-
// Cancel pending codes and requests
689-
_allFilesAborted = !fromFirmware && (DataTransfer.ProtocolVersion >= 3);
690-
InvalidateRegular();
691-
}
692-
else
693-
{
694-
// Invalidate remaining buffered codes from the last macro file
695-
foreach (Code bufferedCode in BufferedCodes)
696-
{
697-
Codes.Processor.CancelCode(bufferedCode);
698-
}
699-
BufferedCodes.Clear();
700-
BytesBuffered = 0;
701-
}
689+
// Cancel pending codes and requests
690+
_allFilesAborted = (DataTransfer.ProtocolVersion >= 3);
691+
InvalidateRegular();
702692

703693
// Abort the job files if necessary
704-
if ((Channel == CodeChannel.File || Channel == CodeChannel.File2) && (abortAll || !macroAborted))
694+
if (Channel is CodeChannel.File or CodeChannel.File2)
705695
{
706696
using (await JobProcessor.LockAsync())
707697
{

src/DuetControlServer/SPI/Interface.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ public static async Task AbortAllAsync(CodeChannel channel)
689689

690690
using (await _channels[channel].LockAsync())
691691
{
692-
await _channels[channel].AbortFilesAsync(true, false);
692+
await _channels[channel].AbortAllFilesAsync();
693693
}
694694
}
695695

@@ -1188,7 +1188,7 @@ private static void HandleAbortFileRequest()
11881188

11891189
using (_channels[channel].Lock())
11901190
{
1191-
_channels[channel].FilesAborted(abortAll, true);
1191+
_channels[channel].FilesAborted(abortAll);
11921192
}
11931193
}
11941194

0 commit comments

Comments
 (0)