From 2ab8002655772b2402b509455b5f99051c965be3 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Sat, 6 Jan 2018 22:03:06 -0800 Subject: [PATCH 1/6] Rename sent/received events - Issue MatterHackers/MCCentral#2597 Rename ReadLine event to resolve ambiguity in terms --- ApplicationView/PrinterModels.cs | 9 ++++----- ConfigurationPage/PrintLeveling/PrintLevelPages.cs | 10 +++++----- ConfigurationPage/RunningMacroPage.cs | 2 +- Library/Providers/SDCard/SDCardContainer.cs | 6 +++--- PrinterCommunication/Io/PauseHandlingStream.cs | 2 +- PrinterCommunication/PrinterConnection.cs | 8 ++++---- Queue/QueueData.cs | 4 ++-- 7 files changed, 20 insertions(+), 21 deletions(-) diff --git a/ApplicationView/PrinterModels.cs b/ApplicationView/PrinterModels.cs index 4b8e49b93..4b56a1f28 100644 --- a/ApplicationView/PrinterModels.cs +++ b/ApplicationView/PrinterModels.cs @@ -41,20 +41,19 @@ using Newtonsoft.Json; namespace MatterHackers.MatterControl { + using System.Collections.Generic; using System.Threading; using MatterHackers.Agg; using MatterHackers.DataConverters3D; using MatterHackers.GCodeVisualizer; + using MatterHackers.Localizations; using MatterHackers.MatterControl.Library; + using MatterHackers.MatterControl.PartPreviewWindow; using MatterHackers.MatterControl.PrinterCommunication; + using MatterHackers.MatterControl.PrintLibrary; using MatterHackers.MeshVisualizer; using MatterHackers.PolygonMesh; using MatterHackers.VectorMath; - using MatterHackers.MatterControl.PartPreviewWindow; - using System.Collections.Generic; - using MatterHackers.MatterControl.PrintLibrary; - using MatterHackers.Localizations; - using System.Text.RegularExpressions; public class BedConfig { diff --git a/ConfigurationPage/PrintLeveling/PrintLevelPages.cs b/ConfigurationPage/PrintLeveling/PrintLevelPages.cs index dec659bdd..debe2ffd6 100644 --- a/ConfigurationPage/PrintLeveling/PrintLevelPages.cs +++ b/ConfigurationPage/PrintLeveling/PrintLevelPages.cs @@ -239,14 +239,14 @@ namespace MatterHackers.MatterControl.ConfigurationPage.PrintLeveling public override void PageIsBecomingActive() { // first make sure there is no leftover FinishedProbe event - printer.Connection.ReadLine.UnregisterEvent(FinishedProbe, ref unregisterEvents); + printer.Connection.LineReceived.UnregisterEvent(FinishedProbe, ref unregisterEvents); var feedRates = printer.Settings.Helpers.ManualMovementSpeeds(); printer.Connection.MoveAbsolute(PrinterConnection.Axis.Z, probeStartPosition.Z, feedRates.Z); printer.Connection.MoveAbsolute(probeStartPosition, feedRates.X); printer.Connection.QueueLine("G30"); - printer.Connection.ReadLine.RegisterEvent(FinishedProbe, ref unregisterEvents); + printer.Connection.LineReceived.RegisterEvent(FinishedProbe, ref unregisterEvents); base.PageIsBecomingActive(); @@ -261,7 +261,7 @@ namespace MatterHackers.MatterControl.ConfigurationPage.PrintLeveling { if (currentEvent.Data.Contains("endstops hit")) { - printer.Connection.ReadLine.UnregisterEvent(FinishedProbe, ref unregisterEvents); + printer.Connection.LineReceived.UnregisterEvent(FinishedProbe, ref unregisterEvents); int zStringPos = currentEvent.Data.LastIndexOf("Z:"); string zProbeHeight = currentEvent.Data.Substring(zStringPos + 2); probePosition.position = new Vector3(probeStartPosition.X, probeStartPosition.Y, double.Parse(zProbeHeight)); @@ -578,13 +578,13 @@ namespace MatterHackers.MatterControl.ConfigurationPage.PrintLeveling && !(printer.Connection.PrinterIsPrinting || printer.Connection.PrinterIsPaused)) { - printer.Connection.ReadLine.RegisterEvent(GetZProbeHeight, ref unregisterEvents); + printer.Connection.LineReceived.RegisterEvent(GetZProbeHeight, ref unregisterEvents); } } public override void PageIsBecomingInactive() { - printer.Connection.ReadLine.UnregisterEvent(GetZProbeHeight, ref unregisterEvents); + printer.Connection.LineReceived.UnregisterEvent(GetZProbeHeight, ref unregisterEvents); base.PageIsBecomingInactive(); } } diff --git a/ConfigurationPage/RunningMacroPage.cs b/ConfigurationPage/RunningMacroPage.cs index d17450604..ef33cd4b0 100644 --- a/ConfigurationPage/RunningMacroPage.cs +++ b/ConfigurationPage/RunningMacroPage.cs @@ -66,7 +66,7 @@ namespace MatterHackers.MatterControl.PrinterControls contentRow.AddChild(materialSelector); } - printer.Connection.WroteLine.RegisterEvent(LookForTempRequest, ref unregisterEvents); + printer.Connection.LineSent.RegisterEvent(LookForTempRequest, ref unregisterEvents); if (macroData.waitOk | macroData.expireTime > 0) { diff --git a/Library/Providers/SDCard/SDCardContainer.cs b/Library/Providers/SDCard/SDCardContainer.cs index d9f771030..8ce332d83 100644 --- a/Library/Providers/SDCard/SDCardContainer.cs +++ b/Library/Providers/SDCard/SDCardContainer.cs @@ -66,7 +66,7 @@ namespace MatterHackers.MatterControl.Library { autoResetEvent = new AutoResetEvent(false); - printer.Connection.ReadLine.RegisterEvent(Printer_LineRead, ref unregisterEvents); + printer.Connection.LineReceived.RegisterEvent(Printer_LineRead, ref unregisterEvents); gotBeginFileList = false; printer.Connection.QueueLine("M21\r\nM20"); @@ -99,7 +99,7 @@ namespace MatterHackers.MatterControl.Library break; case "End file list": - printer.Connection.ReadLine.UnregisterEvent(Printer_LineRead, ref unregisterEvents); + printer.Connection.LineReceived.UnregisterEvent(Printer_LineRead, ref unregisterEvents); // Release the Load WaitOne autoResetEvent.Set(); @@ -128,7 +128,7 @@ namespace MatterHackers.MatterControl.Library public override void Dispose() { // In case "End file list" is never received - printer.Connection.ReadLine.UnregisterEvent(Printer_LineRead, ref unregisterEvents); + printer.Connection.LineReceived.UnregisterEvent(Printer_LineRead, ref unregisterEvents); // Release the Load WaitOne autoResetEvent?.Set(); diff --git a/PrinterCommunication/Io/PauseHandlingStream.cs b/PrinterCommunication/Io/PauseHandlingStream.cs index 91f7aca3a..3921fb176 100644 --- a/PrinterCommunication/Io/PauseHandlingStream.cs +++ b/PrinterCommunication/Io/PauseHandlingStream.cs @@ -56,7 +56,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io : base(internalStream) { this.printer = printer; - printer.Connection.ReadLine.RegisterEvent((s, e) => + printer.Connection.LineReceived.RegisterEvent((s, e) => { StringEventArgs currentEvent = e as StringEventArgs; if (currentEvent != null) diff --git a/PrinterCommunication/PrinterConnection.cs b/PrinterCommunication/PrinterConnection.cs index 5bd759a05..44c004640 100644 --- a/PrinterCommunication/PrinterConnection.cs +++ b/PrinterCommunication/PrinterConnection.cs @@ -117,9 +117,9 @@ namespace MatterHackers.MatterControl.PrinterCommunication public RootedObjectEventHandler PrintingStateChanged = new RootedObjectEventHandler(); - public RootedObjectEventHandler ReadLine = new RootedObjectEventHandler(); + public RootedObjectEventHandler LineReceived = new RootedObjectEventHandler(); - public RootedObjectEventHandler WroteLine = new RootedObjectEventHandler(); + public RootedObjectEventHandler LineSent = new RootedObjectEventHandler(); public bool WatingForPositionRead { @@ -1508,7 +1508,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication if (foundResponse.SendToDelegateFunctions) { - ReadLine.CallEvents(this, currentEvent); + LineReceived.CallEvents(this, currentEvent); } } } @@ -2573,7 +2573,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication if (foundStringEvent.SendToDelegateFunctions) { - WroteLine.CallEvents(this, currentEvent); + LineSent.CallEvents(this, currentEvent); } } } diff --git a/Queue/QueueData.cs b/Queue/QueueData.cs index ac19420f2..978a4d2e6 100644 --- a/Queue/QueueData.cs +++ b/Queue/QueueData.cs @@ -138,7 +138,7 @@ namespace MatterHackers.MatterControl.PrintQueue || ApplicationController.Instance.ActivePrinter.Connection.PrinterIsPaused)) { gotBeginFileList = false; - ApplicationController.Instance.ActivePrinter.Connection.ReadLine.RegisterEvent(GetSdCardList, ref unregisterEvents); + ApplicationController.Instance.ActivePrinter.Connection.LineReceived.RegisterEvent(GetSdCardList, ref unregisterEvents); StringBuilder commands = new StringBuilder(); commands.AppendLine("M21"); // Init SD card commands.AppendLine("M20"); // List SD card @@ -192,7 +192,7 @@ namespace MatterHackers.MatterControl.PrintQueue break; case "End file list": - ApplicationController.Instance.ActivePrinter.Connection.ReadLine.UnregisterEvent(GetSdCardList, ref unregisterEvents); + ApplicationController.Instance.ActivePrinter.Connection.LineReceived.UnregisterEvent(GetSdCardList, ref unregisterEvents); break; } } From 531376fb4f0fb9390f418ad290a7b30650e057a9 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Sat, 6 Jan 2018 22:06:42 -0800 Subject: [PATCH 2/6] Fix typo --- PrinterCommunication/Io/RequestTemperaturesStream.cs | 2 +- PrinterCommunication/PrinterConnection.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/PrinterCommunication/Io/RequestTemperaturesStream.cs b/PrinterCommunication/Io/RequestTemperaturesStream.cs index d39900c68..b42d1b0b8 100644 --- a/PrinterCommunication/Io/RequestTemperaturesStream.cs +++ b/PrinterCommunication/Io/RequestTemperaturesStream.cs @@ -46,7 +46,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io public override string ReadLine() { - if (!printerConnection.WatingForPositionRead + if (!printerConnection.WaitingForPositionRead && nextReadTimeMs < UiThread.CurrentTimerMs && printerConnection.PrinterIsConnected) { diff --git a/PrinterCommunication/PrinterConnection.cs b/PrinterCommunication/PrinterConnection.cs index 44c004640..bb49b31d9 100644 --- a/PrinterCommunication/PrinterConnection.cs +++ b/PrinterCommunication/PrinterConnection.cs @@ -121,7 +121,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication public RootedObjectEventHandler LineSent = new RootedObjectEventHandler(); - public bool WatingForPositionRead + public bool WaitingForPositionRead { get { From 98ac312331242402c4f94c1665fa4d75dc15f141 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Sat, 6 Jan 2018 22:23:44 -0800 Subject: [PATCH 3/6] Use lambdas --- PrinterCommunication/PrinterConnection.cs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/PrinterCommunication/PrinterConnection.cs b/PrinterCommunication/PrinterConnection.cs index bb49b31d9..0c9916dc5 100644 --- a/PrinterCommunication/PrinterConnection.cs +++ b/PrinterCommunication/PrinterConnection.cs @@ -316,8 +316,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication WriteLineStartCallBacks.AddCallbackToKey("G91", MovementWasSetToRelativeMode); WriteLineStartCallBacks.AddCallbackToKey("M80", AtxPowerUpWasWritenToPrinter); WriteLineStartCallBacks.AddCallbackToKey("M81", AtxPowerDownWasWritenToPrinter); - WriteLineStartCallBacks.AddCallbackToKey("M82", ExtruderWasSetToAbsoluteMode); - WriteLineStartCallBacks.AddCallbackToKey("M83", ExtruderWasSetToRelativeMode); + WriteLineStartCallBacks.AddCallbackToKey("M82", (s, e) => extruderMode = PrinterMachineInstruction.MovementTypes.Absolute); + WriteLineStartCallBacks.AddCallbackToKey("M83", (s, e) => extruderMode = PrinterMachineInstruction.MovementTypes.Relative); WriteLineStartCallBacks.AddCallbackToKey("M104", HotendTemperatureWasWritenToPrinter); WriteLineStartCallBacks.AddCallbackToKey("M106", FanSpeedWasWritenToPrinter); WriteLineStartCallBacks.AddCallbackToKey("M107", FanOffWasWritenToPrinter); @@ -2074,16 +2074,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication ReleaseMotors(); } - private void ExtruderWasSetToAbsoluteMode(object sender, EventArgs e) - { - extruderMode = PrinterMachineInstruction.MovementTypes.Absolute; - } - - private void ExtruderWasSetToRelativeMode(object sender, EventArgs e) - { - extruderMode = PrinterMachineInstruction.MovementTypes.Relative; - } - private void FileDeleteConfirmed(object sender, EventArgs e) { ReadLineStartCallBacks.RemoveCallbackFromKey("File deleted:", FileDeleteConfirmed); From 87fab816dd9a35feae03aee785f43800aca95bbe Mon Sep 17 00:00:00 2001 From: John Lewin Date: Sat, 6 Jan 2018 22:24:34 -0800 Subject: [PATCH 4/6] Remove seemingly unused code --- PrinterCommunication/PrinterConnection.cs | 26 ----------------------- 1 file changed, 26 deletions(-) diff --git a/PrinterCommunication/PrinterConnection.cs b/PrinterCommunication/PrinterConnection.cs index 0c9916dc5..6332dc025 100644 --- a/PrinterCommunication/PrinterConnection.cs +++ b/PrinterCommunication/PrinterConnection.cs @@ -146,12 +146,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication private const int MAX_INVALID_CONNECTION_CHARS = 3; - private static PrinterConnection globalInstance; - private object locker = new object(); - private readonly int JoinThreadTimeoutMs = 5000; - private PrintTask activePrintTask; private double actualBedTemperature; @@ -174,12 +170,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication private double currentSdBytes = 0; - private PrinterMachineInstruction.MovementTypes extruderMode = PrinterMachineInstruction.MovementTypes.Absolute; - private double fanSpeed; - private bool firmwareUriGcodeSend = false; - private int currentLineIndexToSend = 0; private bool forceImmediateWrites = false; @@ -283,7 +275,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication ReadLineContainsCallBacks.AddCallbackToKey("Resend:", PrinterRequestsResend); ReadLineContainsCallBacks.AddCallbackToKey("FIRMWARE_NAME:", PrinterStatesFirmware); - ReadLineStartCallBacks.AddCallbackToKey("EXTENSIONS:", PrinterStatesExtensions); #region hardware failure callbacks @@ -316,8 +307,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication WriteLineStartCallBacks.AddCallbackToKey("G91", MovementWasSetToRelativeMode); WriteLineStartCallBacks.AddCallbackToKey("M80", AtxPowerUpWasWritenToPrinter); WriteLineStartCallBacks.AddCallbackToKey("M81", AtxPowerDownWasWritenToPrinter); - WriteLineStartCallBacks.AddCallbackToKey("M82", (s, e) => extruderMode = PrinterMachineInstruction.MovementTypes.Absolute); - WriteLineStartCallBacks.AddCallbackToKey("M83", (s, e) => extruderMode = PrinterMachineInstruction.MovementTypes.Relative); WriteLineStartCallBacks.AddCallbackToKey("M104", HotendTemperatureWasWritenToPrinter); WriteLineStartCallBacks.AddCallbackToKey("M106", FanSpeedWasWritenToPrinter); WriteLineStartCallBacks.AddCallbackToKey("M107", FanOffWasWritenToPrinter); @@ -874,7 +863,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication //Attempt connecting to a specific printer this.stopTryingToConnect = false; this.FirmwareType = FirmwareTypes.Unknown; - firmwareUriGcodeSend = false; // On Android, there will never be more than one serial port available for us to connect to. Override the current .ComPort value to account for // this aspect to ensure the validation logic that verifies port availability/in use status can proceed without additional workarounds for Android @@ -1348,18 +1336,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication } } - public void PrinterStatesExtensions(object sender, EventArgs e) - { - FoundStringEventArgs foundStringEventArgs = e as FoundStringEventArgs; - if (foundStringEventArgs != null) - { - if (foundStringEventArgs.LineToCheck.Contains("URI_GCODE_SEND")) - { - firmwareUriGcodeSend = true; - } - } - } - public void PrinterStatesFirmware(object sender, EventArgs e) { FoundStringEventArgs foundStringEventArgs = e as FoundStringEventArgs; @@ -2220,8 +2196,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication default: #if DEBUG throw new Exception("We are not preparing to print so we should not be starting to print"); - //#else - CommunicationState = CommunicationStates.Connected; #endif break; } From eb57227267fd294c95417bb849108ff8a449f8a7 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Sun, 7 Jan 2018 08:16:36 -0800 Subject: [PATCH 5/6] Use auto properties --- .../Communication/FoundStringCallBacks.cs | 23 ++++--------------- 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/MatterControl.Printing/Communication/FoundStringCallBacks.cs b/MatterControl.Printing/Communication/FoundStringCallBacks.cs index 1bb37a28d..de5f9a622 100644 --- a/MatterControl.Printing/Communication/FoundStringCallBacks.cs +++ b/MatterControl.Printing/Communication/FoundStringCallBacks.cs @@ -34,29 +34,16 @@ namespace MatterHackers.SerialPortCommunication { public class FoundStringEventArgs : EventArgs { - public bool CallbackWasCalled { get; set; } - - private bool sendToDelegateFunctions = true; - private string lineToCheck; - public FoundStringEventArgs(string lineReceived) { - this.lineToCheck = lineReceived.Trim(); + this.LineToCheck = lineReceived.Trim(); } - public string LineToCheck { get { return lineToCheck; } } + public bool CallbackWasCalled { get; set; } - public bool SendToDelegateFunctions - { - get - { - return sendToDelegateFunctions; - } - set - { - sendToDelegateFunctions = value; - } - } + public string LineToCheck { get; } + + public bool SendToDelegateFunctions { get; set; } } public class FoundStringCallbacks From 98eacd8c44ad1503e286ef9028244e349ce14518 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Sun, 7 Jan 2018 09:27:25 -0800 Subject: [PATCH 6/6] Rename SendToDelegateFunctions to clarify behavior --- .../Communication/FoundStringCallBacks.cs | 5 ++++- PrinterCommunication/PrinterConnection.cs | 14 ++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/MatterControl.Printing/Communication/FoundStringCallBacks.cs b/MatterControl.Printing/Communication/FoundStringCallBacks.cs index de5f9a622..cfafd90f8 100644 --- a/MatterControl.Printing/Communication/FoundStringCallBacks.cs +++ b/MatterControl.Printing/Communication/FoundStringCallBacks.cs @@ -43,7 +43,10 @@ namespace MatterHackers.SerialPortCommunication public string LineToCheck { get; } - public bool SendToDelegateFunctions { get; set; } + /// + /// Used to conditionally invoke LineSent/LineReceived events. Setting to false suppresses notification, hiding lines from listeners + /// + public bool AllowListenerNotification { get; set; } } public class FoundStringCallbacks diff --git a/PrinterCommunication/PrinterConnection.cs b/PrinterCommunication/PrinterConnection.cs index 6332dc025..7be3fe42d 100644 --- a/PrinterCommunication/PrinterConnection.cs +++ b/PrinterCommunication/PrinterConnection.cs @@ -248,7 +248,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication MonitorPrinterTemperature = true; - StringComparer stringComparer = StringComparer.OrdinalIgnoreCase; ReadLineStartCallBacks.AddCallbackToKey("start", FoundStart); ReadLineStartCallBacks.AddCallbackToKey("start", PrintingCanContinue); @@ -1140,8 +1139,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication public void FoundStart(object sender, EventArgs e) { - FoundStringEventArgs foundStringEventArgs = e as FoundStringEventArgs; - foundStringEventArgs.SendToDelegateFunctions = false; + (e as FoundStringEventArgs).AllowListenerNotification = false; } public double GetActualHotendTemperature(int hotendIndex0Based) @@ -1482,7 +1480,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication ReadLineStartCallBacks.CheckForKeys(foundResponse); ReadLineContainsCallBacks.CheckForKeys(foundResponse); - if (foundResponse.SendToDelegateFunctions) + if (foundResponse.AllowListenerNotification) { LineReceived.CallEvents(this, currentEvent); } @@ -2025,13 +2023,9 @@ namespace MatterHackers.MatterControl.PrinterCommunication public void SuppressEcho(object sender, EventArgs e) { - FoundStringEventArgs foundStringEventArgs = e as FoundStringEventArgs; - foundStringEventArgs.SendToDelegateFunctions = false; + (e as FoundStringEventArgs).AllowListenerNotification = false; } - [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] - internal static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, IntPtr securityAttrs, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile); - private void ClearQueuedGCode() { gCodeFileStream0?.GCodeFile?.Clear(); @@ -2535,7 +2529,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication WriteLineStartCallBacks.CheckForKeys(foundStringEvent); WriteLineContainsCallBacks.CheckForKeys(foundStringEvent); - if (foundStringEvent.SendToDelegateFunctions) + if (foundStringEvent.AllowListenerNotification) { LineSent.CallEvents(this, currentEvent); }