Merge pull request #2850 from jlewin/design_tools

Refactor PrinterConnection
This commit is contained in:
johnlewin 2018-01-07 14:50:35 -08:00 committed by GitHub
commit d5df85f6ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 34 additions and 87 deletions

View file

@ -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
{

View file

@ -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();
}
}

View file

@ -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)
{

View file

@ -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();

View file

@ -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; }
/// <summary>
/// Used to conditionally invoke LineSent/LineReceived events. Setting to false suppresses notification, hiding lines from listeners
/// </summary>
public bool AllowListenerNotification { get; set; }
}
public class FoundStringCallbacks

View file

@ -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)

View file

@ -46,7 +46,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
public override string ReadLine()
{
if (!printerConnection.WatingForPositionRead
if (!printerConnection.WaitingForPositionRead
&& nextReadTimeMs < UiThread.CurrentTimerMs
&& printerConnection.PrinterIsConnected)
{

View file

@ -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);
}
}
}

View file

@ -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;
}
}