2015-11-28 19:59:14 -08:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright (c) 2015, Lars Brubaker
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
The views and conclusions contained in the software and documentation are those
|
|
|
|
|
|
of the authors and should not be interpreted as representing official policies,
|
|
|
|
|
|
either expressed or implied, of the FreeBSD Project.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2019-02-07 14:28:22 -08:00
|
|
|
|
using System;
|
2015-11-28 19:59:14 -08:00
|
|
|
|
using System.Collections.Generic;
|
2017-02-02 07:54:52 -08:00
|
|
|
|
using System.Diagnostics;
|
2017-02-02 16:38:48 -08:00
|
|
|
|
using System.IO;
|
2016-11-04 16:00:54 -07:00
|
|
|
|
using System.Text.RegularExpressions;
|
2017-02-02 07:54:52 -08:00
|
|
|
|
using System.Threading;
|
2019-02-07 14:28:22 -08:00
|
|
|
|
using MatterControl.Printing;
|
2017-02-02 16:38:48 -08:00
|
|
|
|
using MatterHackers.Agg.Image;
|
2017-08-20 02:34:39 -07:00
|
|
|
|
using MatterHackers.Agg.Platform;
|
2016-11-04 16:00:54 -07:00
|
|
|
|
using MatterHackers.Agg.UI;
|
2017-02-02 07:54:52 -08:00
|
|
|
|
using MatterHackers.MatterControl.PrinterControls;
|
|
|
|
|
|
using MatterHackers.MatterControl.SlicerConfiguration;
|
2015-11-28 19:59:14 -08:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl.PrinterCommunication.Io
|
|
|
|
|
|
{
|
2016-11-04 16:00:54 -07:00
|
|
|
|
public class QueuedCommandsStream : GCodeStreamProxy
|
|
|
|
|
|
{
|
2017-02-02 07:54:52 -08:00
|
|
|
|
private List<string> commandQueue = new List<string>();
|
|
|
|
|
|
private object locker = new object();
|
2019-02-07 14:28:22 -08:00
|
|
|
|
private int requestedExtruder;
|
2019-02-08 13:50:48 -08:00
|
|
|
|
private int extruderLastSwitchedTo;
|
|
|
|
|
|
PrinterMove lastDestination = PrinterMove.Unknown;
|
|
|
|
|
|
int extruderCount = 0;
|
2015-11-28 19:59:14 -08:00
|
|
|
|
|
2018-11-12 15:02:47 -08:00
|
|
|
|
public QueuedCommandsStream(PrinterConfig printer, GCodeStream internalStream)
|
|
|
|
|
|
: base(printer, internalStream)
|
2016-01-19 15:16:05 -08:00
|
|
|
|
{
|
2019-02-08 13:50:48 -08:00
|
|
|
|
extruderCount = printer.Settings.GetValue<int>(SettingsKey.extruder_count);
|
2016-01-19 15:16:05 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-02 16:14:37 -07:00
|
|
|
|
public int Count => commandQueue.Count;
|
|
|
|
|
|
|
2017-04-11 10:41:23 -07:00
|
|
|
|
public void Add(string line, bool forceTopOfQueue = false)
|
2016-11-04 16:00:54 -07:00
|
|
|
|
{
|
|
|
|
|
|
// lock queue
|
|
|
|
|
|
lock (locker)
|
2015-12-31 09:40:17 -08:00
|
|
|
|
{
|
2017-04-11 10:41:23 -07:00
|
|
|
|
if (forceTopOfQueue)
|
|
|
|
|
|
{
|
|
|
|
|
|
commandQueue.Insert(0, line);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-02-08 13:50:48 -08:00
|
|
|
|
// If we are not printing, check if we need to switch extruders
|
|
|
|
|
|
if (extruderCount > 1
|
|
|
|
|
|
&& !printer.Connection.Printing)
|
2019-02-07 14:28:22 -08:00
|
|
|
|
{
|
2019-02-08 13:50:48 -08:00
|
|
|
|
CheckIfNeedToSwitchExtruders(line);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
commandQueue.Add(line);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-02-07 14:28:22 -08:00
|
|
|
|
|
2019-02-08 17:53:32 -08:00
|
|
|
|
private void CheckIfNeedToSwitchExtruders(string lineIn)
|
2019-02-08 13:50:48 -08:00
|
|
|
|
{
|
2019-02-08 17:53:32 -08:00
|
|
|
|
if(lineIn == null)
|
2019-02-08 13:50:48 -08:00
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-08 17:53:32 -08:00
|
|
|
|
var lineNoComment = lineIn.Split(';')[0];
|
2019-02-08 13:50:48 -08:00
|
|
|
|
|
2019-02-08 17:53:32 -08:00
|
|
|
|
TrackExtruderState(lineNoComment);
|
2019-02-08 13:50:48 -08:00
|
|
|
|
|
|
|
|
|
|
if ((lineNoComment.StartsWith("G0 ") || lineNoComment.StartsWith("G1 ")) // is a G1 or G0
|
|
|
|
|
|
&& (lineNoComment.Contains("X") || lineNoComment.Contains("Y") || lineNoComment.Contains("Z")) // hase a move axis in it
|
|
|
|
|
|
&& extruderLastSwitchedTo != requestedExtruder) // is different than the last extruder set
|
|
|
|
|
|
{
|
|
|
|
|
|
string gcodeToQueue = "";
|
|
|
|
|
|
switch (requestedExtruder)
|
|
|
|
|
|
{
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
gcodeToQueue = printer.Settings.GetValue(SettingsKey.before_toolchange_gcode).Replace("\\n", "\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
gcodeToQueue = printer.Settings.GetValue(SettingsKey.before_toolchange_gcode_1).Replace("\\n", "\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (gcodeToQueue.Trim().Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (gcodeToQueue.Contains("\n"))
|
|
|
|
|
|
{
|
|
|
|
|
|
string[] linesToWrite = gcodeToQueue.Split(new string[] { "\n" }, StringSplitOptions.None);
|
|
|
|
|
|
for (int i = 0; i < linesToWrite.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
string gcodeLine = linesToWrite[i].Trim();
|
|
|
|
|
|
if (gcodeLine.Length > 0)
|
2019-02-07 14:28:22 -08:00
|
|
|
|
{
|
2019-02-08 13:50:48 -08:00
|
|
|
|
commandQueue.Add(gcodeLine);
|
2019-02-07 14:28:22 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-02-08 13:50:48 -08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
commandQueue.Add(gcodeToQueue);
|
|
|
|
|
|
}
|
2017-04-11 10:41:23 -07:00
|
|
|
|
}
|
2019-02-08 13:50:48 -08:00
|
|
|
|
|
|
|
|
|
|
extruderLastSwitchedTo = requestedExtruder;
|
2015-12-31 09:40:17 -08:00
|
|
|
|
}
|
2016-11-04 16:00:54 -07:00
|
|
|
|
}
|
2015-12-31 09:40:17 -08:00
|
|
|
|
|
2017-02-02 07:54:52 -08:00
|
|
|
|
public void Cancel()
|
|
|
|
|
|
{
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-06 14:17:49 -08:00
|
|
|
|
public ImageBuffer LoadImageAsset(string uri)
|
|
|
|
|
|
{
|
|
|
|
|
|
string filePath = Path.Combine("Images", "Macros", uri);
|
|
|
|
|
|
bool imageOnDisk = false;
|
|
|
|
|
|
if (uri.IndexOfAny(Path.GetInvalidFileNameChars()) == -1)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2017-08-20 02:34:39 -07:00
|
|
|
|
imageOnDisk = AggContext.StaticData.FileExists(filePath);
|
2017-02-06 14:17:49 -08:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
imageOnDisk = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (imageOnDisk)
|
|
|
|
|
|
{
|
2017-08-20 02:34:39 -07:00
|
|
|
|
return AggContext.StaticData.LoadImage(filePath);
|
2017-02-06 14:17:49 -08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var imageBuffer = new ImageBuffer(320, 10);
|
|
|
|
|
|
|
|
|
|
|
|
ApplicationController.Instance.DownloadToImageAsync(imageBuffer, uri, true);
|
|
|
|
|
|
|
|
|
|
|
|
return imageBuffer;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-04 16:00:54 -07:00
|
|
|
|
public override string ReadLine()
|
|
|
|
|
|
{
|
|
|
|
|
|
string lineToSend = null;
|
|
|
|
|
|
|
2019-02-08 13:50:48 -08:00
|
|
|
|
// lock queue
|
|
|
|
|
|
lock (locker)
|
2015-12-31 09:40:17 -08:00
|
|
|
|
{
|
2019-02-08 13:50:48 -08:00
|
|
|
|
if (commandQueue.Count > 0)
|
2016-11-04 16:00:54 -07:00
|
|
|
|
{
|
2019-02-08 13:50:48 -08:00
|
|
|
|
lineToSend = commandQueue[0];
|
|
|
|
|
|
lineToSend = printer.ReplaceMacroValues(lineToSend);
|
|
|
|
|
|
commandQueue.RemoveAt(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (lineToSend == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
lineToSend = base.ReadLine();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-08 17:53:32 -08:00
|
|
|
|
TrackExtruderState(lineToSend);
|
|
|
|
|
|
|
|
|
|
|
|
return lineToSend;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void TrackExtruderState(string line)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (line == null)
|
2019-02-08 13:50:48 -08:00
|
|
|
|
{
|
2019-02-08 17:53:32 -08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-11-04 16:00:54 -07:00
|
|
|
|
|
2019-02-08 17:53:32 -08:00
|
|
|
|
if (line.StartsWith("G28)"))
|
|
|
|
|
|
{
|
|
|
|
|
|
extruderLastSwitchedTo = 0;
|
|
|
|
|
|
requestedExtruder = 0;
|
2015-12-31 09:40:17 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-08 17:53:32 -08:00
|
|
|
|
if (line.StartsWith("T"))
|
|
|
|
|
|
{
|
|
|
|
|
|
GCodeFile.GetFirstNumberAfter("T", line, ref requestedExtruder);
|
|
|
|
|
|
}
|
2016-11-04 16:00:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-06 14:17:49 -08:00
|
|
|
|
public void Reset()
|
2017-02-03 17:54:49 -08:00
|
|
|
|
{
|
2017-02-06 14:17:49 -08:00
|
|
|
|
lock (locker)
|
|
|
|
|
|
{
|
|
|
|
|
|
commandQueue.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-11-04 16:00:54 -07:00
|
|
|
|
}
|
2015-11-28 19:59:14 -08:00
|
|
|
|
}
|