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/MatterControl.Printing/Communication/FoundStringCallBacks.cs b/MatterControl.Printing/Communication/FoundStringCallBacks.cs index 1bb37a28d..cfafd90f8 100644 --- a/MatterControl.Printing/Communication/FoundStringCallBacks.cs +++ b/MatterControl.Printing/Communication/FoundStringCallBacks.cs @@ -34,29 +34,19 @@ 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; } + + /// + /// 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/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/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 5bd759a05..7be3fe42d 100644 --- a/PrinterCommunication/PrinterConnection.cs +++ b/PrinterCommunication/PrinterConnection.cs @@ -117,11 +117,11 @@ 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 + public bool WaitingForPositionRead { get { @@ -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; @@ -256,7 +248,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication MonitorPrinterTemperature = true; - StringComparer stringComparer = StringComparer.OrdinalIgnoreCase; ReadLineStartCallBacks.AddCallbackToKey("start", FoundStart); ReadLineStartCallBacks.AddCallbackToKey("start", PrintingCanContinue); @@ -283,7 +274,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication ReadLineContainsCallBacks.AddCallbackToKey("Resend:", PrinterRequestsResend); ReadLineContainsCallBacks.AddCallbackToKey("FIRMWARE_NAME:", PrinterStatesFirmware); - ReadLineStartCallBacks.AddCallbackToKey("EXTENSIONS:", PrinterStatesExtensions); #region hardware failure callbacks @@ -316,8 +306,6 @@ 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("M104", HotendTemperatureWasWritenToPrinter); WriteLineStartCallBacks.AddCallbackToKey("M106", FanSpeedWasWritenToPrinter); WriteLineStartCallBacks.AddCallbackToKey("M107", FanOffWasWritenToPrinter); @@ -874,7 +862,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 @@ -1152,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) @@ -1348,18 +1334,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; @@ -1506,9 +1480,9 @@ namespace MatterHackers.MatterControl.PrinterCommunication ReadLineStartCallBacks.CheckForKeys(foundResponse); ReadLineContainsCallBacks.CheckForKeys(foundResponse); - if (foundResponse.SendToDelegateFunctions) + if (foundResponse.AllowListenerNotification) { - ReadLine.CallEvents(this, currentEvent); + LineReceived.CallEvents(this, currentEvent); } } } @@ -2049,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(); @@ -2074,16 +2044,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); @@ -2230,8 +2190,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; } @@ -2571,9 +2529,9 @@ namespace MatterHackers.MatterControl.PrinterCommunication WriteLineStartCallBacks.CheckForKeys(foundStringEvent); WriteLineContainsCallBacks.CheckForKeys(foundStringEvent); - if (foundStringEvent.SendToDelegateFunctions) + if (foundStringEvent.AllowListenerNotification) { - 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; } }