From 810800df338052092005f93d58f19669f8b7f88e Mon Sep 17 00:00:00 2001 From: John Lewin Date: Tue, 16 Jan 2018 07:10:56 -0800 Subject: [PATCH 1/4] Revise Setup button for more consistent disabled styling - Issue MatterHackers/MCCentral#2478 Inconsistent Disabled styling --- .../View3D/PrinterBar/PauseResumeButton.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/PartPreviewWindow/View3D/PrinterBar/PauseResumeButton.cs b/PartPreviewWindow/View3D/PrinterBar/PauseResumeButton.cs index df1be89ee..6272be549 100644 --- a/PartPreviewWindow/View3D/PrinterBar/PauseResumeButton.cs +++ b/PartPreviewWindow/View3D/PrinterBar/PauseResumeButton.cs @@ -33,6 +33,7 @@ using MatterHackers.Agg.Platform; using MatterHackers.Agg.UI; using MatterHackers.Localizations; using MatterHackers.MatterControl.ConfigurationPage.PrintLeveling; +using MatterHackers.MatterControl.CustomWidgets; using MatterHackers.MatterControl.PrinterCommunication; using MatterHackers.MatterControl.SlicerConfiguration; @@ -51,10 +52,14 @@ namespace MatterHackers.MatterControl.PartPreviewWindow this.printer = printer; // add the finish setup button - finishSetupButton = theme.ButtonFactory.Generate("Setup...".Localize(), AggContext.StaticData.LoadIcon("icon_play_32x32.png", 14, 14, IconColor.Theme)); - finishSetupButton.Name = "Finish Setup Button"; - finishSetupButton.ToolTipText = "Run setup configuration for printer.".Localize(); - finishSetupButton.Margin = theme.ButtonSpacing; + finishSetupButton = new TextButton("Setup...".Localize(), theme) + { + Name = "Finish Setup Button", + ToolTipText = "Run setup configuration for printer.".Localize(), + Margin = theme.ButtonSpacing, + BackgroundColor = theme.ButtonFactory.Options.NormalFillColor, + HoverColor = theme.ButtonFactory.Options.HoverFillColor, + }; finishSetupButton.Click += (s, e) => { UiThread.RunOnIdle(async () => From a6d6fdaa3c76641250782320f828e2a3a00f3cb5 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Tue, 16 Jan 2018 08:14:04 -0800 Subject: [PATCH 2/4] Collapse DoneLoadingGCodeToPrint into StartPrint method --- PrinterCommunication/PrinterConnection.cs | 87 +++++++++++------------ 1 file changed, 42 insertions(+), 45 deletions(-) diff --git a/PrinterCommunication/PrinterConnection.cs b/PrinterCommunication/PrinterConnection.cs index 804cbc1a5..5fba227cb 100644 --- a/PrinterCommunication/PrinterConnection.cs +++ b/PrinterCommunication/PrinterConnection.cs @@ -1899,7 +1899,48 @@ namespace MatterHackers.MatterControl.PrinterCommunication { LoadGCodeToPrint(gcodeFilename); }); - DoneLoadingGCodeToPrint(); + + // DoneLoadingGCodeToPrint + switch (communicationState) + { + case CommunicationStates.Connected: + // This can happen if the printer is reset during the slicing of the part. + break; + + case CommunicationStates.PreparingToPrint: + { + var activePrintItem = printer.Bed.EditContext.printItem; + if (activePrintItem.PrintItem.Id == 0) + { + activePrintItem.PrintItem.Commit(); + } + + if (activePrintTask == null) + { + // TODO: Fix printerItemID int requirement + activePrintTask = new PrintTask + { + PrintStart = DateTime.Now, + PrinterId = this.printer.Settings.ID.GetHashCode(), + PrintName = activePrintItem.PrintItem.Name, + PrintItemId = activePrintItem.PrintItem.Id, + PrintingGCodeFileName = activePrintItem.GetGCodePathAndFileName(), + PrintComplete = false + }; + + activePrintTask.Commit(); + } + } + + CommunicationState = CommunicationStates.Printing; + break; + + default: +#if DEBUG + throw new Exception("We are not preparing to print so we should not be starting to print"); +#endif + break; + } } public bool StartSdCardPrint(string m23FileName) @@ -2148,50 +2189,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication CreateStreamProcessors(gcodeFilename, this.RecoveryIsEnabled); } - private void DoneLoadingGCodeToPrint() - { - switch (communicationState) - { - case CommunicationStates.Connected: - // This can happen if the printer is reset during the slicing of the part. - break; - - case CommunicationStates.PreparingToPrint: - { - var activePrintItem = printer.Bed.EditContext.printItem; - if (activePrintItem.PrintItem.Id == 0) - { - activePrintItem.PrintItem.Commit(); - } - - if (activePrintTask == null) - { - // TODO: Fix printerItemID int requirement - activePrintTask = new PrintTask - { - PrintStart = DateTime.Now, - PrinterId = this.printer.Settings.ID.GetHashCode(), - PrintName = activePrintItem.PrintItem.Name, - PrintItemId = activePrintItem.PrintItem.Id, - PrintingGCodeFileName = activePrintItem.GetGCodePathAndFileName(), - PrintComplete = false - }; - - activePrintTask.Commit(); - } - } - - CommunicationState = CommunicationStates.Printing; - break; - - default: -#if DEBUG - throw new Exception("We are not preparing to print so we should not be starting to print"); -#endif - break; - } - } - private void MovementWasSetToAbsoluteMode(object sender, EventArgs e) { movementMode = PrinterMachineInstruction.MovementTypes.Absolute; From 0962db3f3ed029588ae3d40260e7934af8c0f195 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Tue, 16 Jan 2018 08:17:24 -0800 Subject: [PATCH 3/4] Collapse LoadGCodeToPrint into StartPrint method --- PrinterCommunication/PrinterConnection.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/PrinterCommunication/PrinterConnection.cs b/PrinterCommunication/PrinterConnection.cs index 5fba227cb..a5736f4e9 100644 --- a/PrinterCommunication/PrinterConnection.cs +++ b/PrinterCommunication/PrinterConnection.cs @@ -1897,7 +1897,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication await Task.Run(() => { - LoadGCodeToPrint(gcodeFilename); + // LoadGCodeToPrint + CreateStreamProcessors(gcodeFilename, this.RecoveryIsEnabled); }); // DoneLoadingGCodeToPrint @@ -2184,11 +2185,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication ReadPosition(); } - private void LoadGCodeToPrint(string gcodeFilename) - { - CreateStreamProcessors(gcodeFilename, this.RecoveryIsEnabled); - } - private void MovementWasSetToAbsoluteMode(object sender, EventArgs e) { movementMode = PrinterMachineInstruction.MovementTypes.Absolute; From 241c9694e170d81f89f8a9fb2f68bee206832b89 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Tue, 16 Jan 2018 08:42:35 -0800 Subject: [PATCH 4/4] Extract DB sync to dedicated thread - Issue MatterHackers/MCCentral#2596 Consider using a single task to sync print progress to disk --- PrinterCommunication/PrinterConnection.cs | 83 ++++++++++++++--------- Submodules/agg-sharp | 2 +- 2 files changed, 53 insertions(+), 32 deletions(-) diff --git a/PrinterCommunication/PrinterConnection.cs b/PrinterCommunication/PrinterConnection.cs index a5736f4e9..de59daef3 100644 --- a/PrinterCommunication/PrinterConnection.cs +++ b/PrinterCommunication/PrinterConnection.cs @@ -1879,6 +1879,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication } } + private CancellationTokenSource printingCancellation; + public async void StartPrint(string gcodeFilename, PrintTask printTaskToUse = null) { if (!PrinterIsConnected || PrinterIsPrinting) @@ -1886,6 +1888,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication return; } + printingCancellation = new CancellationTokenSource(); + haveReportedError = false; PrintWasCanceled = false; @@ -1930,6 +1934,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication }; activePrintTask.Commit(); + + Task.Run(() => this.SyncProgressToDB(printingCancellation.Token)).ConfigureAwait(false); } } @@ -2017,6 +2023,9 @@ namespace MatterHackers.MatterControl.PrinterCommunication { lock (locker) { + // Flag as canceled, wait briefly for listening threads to catch up + printingCancellation.Cancel(); + Thread.Sleep(15); // get rid of all the gcode we have left to print ClearQueuedGCode(); @@ -2185,6 +2194,49 @@ namespace MatterHackers.MatterControl.PrinterCommunication ReadPosition(); } + private void SyncProgressToDB(CancellationToken cancellationToken) + { + //var timer = Stopwatch.StartNew(); + + while (!cancellationToken.IsCancellationRequested + && this.CommunicationState != CommunicationStates.FinishedPrint + && this.communicationState != CommunicationStates.Connected) + { + double secondsSinceStartedPrint = timeSinceStartedPrint.Elapsed.TotalSeconds; + + if (timeSinceStartedPrint.Elapsed.TotalSeconds > 0 + && gCodeFileStream0 != null + && (secondsSinceUpdateHistory > secondsSinceStartedPrint + || secondsSinceUpdateHistory + 1 < secondsSinceStartedPrint + || lineSinceUpdateHistory + 20 < gCodeFileStream0.LineIndex)) + { + double currentDone = gCodeFileStream0.GCodeFile.PercentComplete(gCodeFileStream0.LineIndex); + // Only update the amount done if it is greater than what is recorded. + // We don't want to mess up the resume before we actually resume it. + if (activePrintTask != null + && babyStepsStream7 != null + && activePrintTask.PercentDone < currentDone) + { + activePrintTask.PercentDone = currentDone; + activePrintTask.PrintingOffsetX = (float)babyStepsStream7.Offset.X; + activePrintTask.PrintingOffsetY = (float)babyStepsStream7.Offset.Y; + activePrintTask.PrintingOffsetZ = (float)babyStepsStream7.Offset.Z; + activePrintTask?.Commit(); + + // Interval looks to be ~10ms + //Console.WriteLine("DB write: {0}ms", timer.ElapsedMilliseconds); + //timer.Restart(); + } + secondsSinceUpdateHistory = secondsSinceStartedPrint; + lineSinceUpdateHistory = gCodeFileStream0.LineIndex; + } + + Thread.Sleep(5); + } + + // Console.WriteLine("Syncing print to db stopped"); + } + private void MovementWasSetToAbsoluteMode(object sender, EventArgs e) { movementMode = PrinterMachineInstruction.MovementTypes.Absolute; @@ -2349,37 +2401,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication waitingForPosition.Restart(); } - double secondsSinceStartedPrint = timeSinceStartedPrint.Elapsed.TotalSeconds; - if (timeSinceStartedPrint.Elapsed.TotalSeconds > 0 - && gCodeFileStream0 != null - && (secondsSinceUpdateHistory > secondsSinceStartedPrint - || secondsSinceUpdateHistory + 1 < secondsSinceStartedPrint - || lineSinceUpdateHistory + 20 < gCodeFileStream0.LineIndex)) - { - double currentDone = gCodeFileStream0.GCodeFile.PercentComplete(gCodeFileStream0.LineIndex); - // Only update the amount done if it is greater than what is recorded. - // We don't want to mess up the resume before we actually resume it. - if (activePrintTask != null - && babyStepsStream7 != null - && activePrintTask.PercentDone < currentDone) - { - activePrintTask.PercentDone = currentDone; - activePrintTask.PrintingOffsetX = (float)babyStepsStream7.Offset.X; - activePrintTask.PrintingOffsetY = (float)babyStepsStream7.Offset.Y; - activePrintTask.PrintingOffsetZ = (float)babyStepsStream7.Offset.Z; - try - { - Task.Run(() => activePrintTask?.Commit()); - } - catch - { - // Can't write for some reason, continue with the write. - } - } - secondsSinceUpdateHistory = secondsSinceStartedPrint; - lineSinceUpdateHistory = gCodeFileStream0.LineIndex; - } - currentSentLine = currentSentLine.Trim(); // Check if there is anything in front of the ;. if (currentSentLine.Split(';')[0].Trim().Length > 0) diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index 34251ebde..eb8c3919c 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit 34251ebdeb15fe6df98daf569770d09314f1d80f +Subproject commit eb8c3919cef843eabb7dfedb2a65c10398e9ddf0