From e4cdc106d8dac6dfcaf840a3c609c38bdbb9b9e9 Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Tue, 3 May 2022 10:19:19 -0700 Subject: [PATCH] Improving fan speed reporting --- MatterControl.Printing/GCode/GCodeFile.cs | 7 ++ .../GCode/GCodeFileStreamed.cs | 7 +- .../GCode/GCodeMemoryFile.cs | 66 +++++++++++++++++++ .../GCodeDetails/GCodeDetails.cs | 29 +++++++- 4 files changed, 106 insertions(+), 3 deletions(-) diff --git a/MatterControl.Printing/GCode/GCodeFile.cs b/MatterControl.Printing/GCode/GCodeFile.cs index 48357be04..330a64979 100644 --- a/MatterControl.Printing/GCode/GCodeFile.cs +++ b/MatterControl.Printing/GCode/GCodeFile.cs @@ -76,6 +76,13 @@ namespace MatterControl.Printing public abstract double GetLayerHeight(int layerIndex); + /// + /// Get the speed of the fan at the conculsion of this layer + /// + /// The layer to get the last fan speed for + /// The fan speed 0 to 255 + public abstract int GetLastFanSpeed(int layerIndex); + public abstract double GetLayerTop(int layerIndex); public abstract int GetLayerIndex(int instructionIndex); diff --git a/MatterControl.Printing/GCode/GCodeFileStreamed.cs b/MatterControl.Printing/GCode/GCodeFileStreamed.cs index 7e5fbd2dc..aab1b5075 100644 --- a/MatterControl.Printing/GCode/GCodeFileStreamed.cs +++ b/MatterControl.Printing/GCode/GCodeFileStreamed.cs @@ -164,7 +164,12 @@ namespace MatterControl.Printing throw new NotImplementedException(); } - public override double PercentComplete(int instructionIndex) + public override int GetLastFanSpeed(int layerIndex) + { + return -1; + } + + public override double PercentComplete(int instructionIndex) { lock(locker) { diff --git a/MatterControl.Printing/GCode/GCodeMemoryFile.cs b/MatterControl.Printing/GCode/GCodeMemoryFile.cs index 85b33357f..b51dfacdf 100644 --- a/MatterControl.Printing/GCode/GCodeMemoryFile.cs +++ b/MatterControl.Printing/GCode/GCodeMemoryFile.cs @@ -51,6 +51,8 @@ namespace MatterControl.Printing private double parsingLastZ; private readonly List toolChanges = new List(); + private Dictionary lastFanSpeed = new Dictionary(); + /// /// Gets total print time that will leave the heaters on at the conclusion of the print. /// @@ -290,6 +292,70 @@ namespace MatterControl.Printing return total; } + public override int GetLastFanSpeed(int requestedLayer) + { + if (LayerCount == 0 || requestedLayer < 0) + { + return 0; + } + + if (lastFanSpeed.ContainsKey(requestedLayer)) + { + return lastFanSpeed[requestedLayer]; + } + + var currentLayer = requestedLayer; + while (currentLayer >= 0) + { + int startInstruction = GetFirstLayerInstruction(currentLayer); + if (currentLayer == 0) + { + startInstruction = 0; + } + + int endInstruction = GetFirstLayerInstruction(currentLayer + 1); + + var foundSpeed = false; + var lastSpeed = 0; + for (int i = startInstruction; i < endInstruction; i++) + { + var line = Instruction(i).Line; + if (line.StartsWith("M107")) // fan off + { + foundSpeed = true; + lastSpeed = 0; + } + else if (line.StartsWith("M106")) // fan on + { + double speed = 0; + if (GCodeFile.GetFirstNumberAfter("S", line, ref speed, 0, "")) + { + foundSpeed = true; + lastSpeed = (int)speed; + } + } + } + + if (foundSpeed) + { + lastFanSpeed[requestedLayer] = lastSpeed; + return lastSpeed; + } + + currentLayer--; + + if (lastFanSpeed.ContainsKey(currentLayer)) + { + lastFanSpeed[currentLayer + 1] = lastFanSpeed[currentLayer]; + lastFanSpeed[requestedLayer] = lastFanSpeed[currentLayer]; + return lastFanSpeed[currentLayer]; + } + } + + lastFanSpeed[requestedLayer] = 0; + return 0; + } + /// /// Get the height of this layer (from the top of the previous layer to the top of this layer). /// diff --git a/MatterControlLib/PartPreviewWindow/GCodeDetails/GCodeDetails.cs b/MatterControlLib/PartPreviewWindow/GCodeDetails/GCodeDetails.cs index 320b415f5..7f9822a6e 100644 --- a/MatterControlLib/PartPreviewWindow/GCodeDetails/GCodeDetails.cs +++ b/MatterControlLib/PartPreviewWindow/GCodeDetails/GCodeDetails.cs @@ -27,7 +27,6 @@ of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. */ -using System; using System.Collections.Generic; using MatterControl.Printing; using MatterHackers.MatterControl.SlicerConfiguration; @@ -234,12 +233,38 @@ namespace MatterHackers.MatterControl.PartPreviewWindow double speed = 0; if (GCodeFile.GetFirstNumberAfter("S", line, ref speed, 0, "")) { - fanSpeeds += separator + $"{speed * 100 / 255:0}%"; + if (speed == 0) + { + fanSpeeds += separator + "Off"; + } + else + { + fanSpeeds += separator + $"{speed * 100 / 255:0}%"; + } separator = ", "; } } } + if (string.IsNullOrEmpty(fanSpeeds)) + { + // look back until we find a speed or go back to the begining + var speed = loadedGCode.GetLastFanSpeed(activeLayerIndex - 1); + if (speed >= 0) + { + if (speed == 0) + { + return "Off"; + } + + return $"{speed * 100 / 255:0}%"; + } + else + { + return "---"; + } + } + return fanSpeeds; }