From 45fef79e3d0889cdb2f013090e3194e1b6c04ae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 21 May 2013 23:30:29 +0200 Subject: [PATCH] Use M26 to reset SD position to 0 when cancelling and restarting a print, preprocess gcode before sending to printer --- octoprint/printer.py | 8 ++++++-- octoprint/static/js/ui.js | 2 +- octoprint/util/comm.py | 24 ++++++++++++++++++------ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/octoprint/printer.py b/octoprint/printer.py index 5f837be..5b4f3c8 100644 --- a/octoprint/printer.py +++ b/octoprint/printer.py @@ -619,9 +619,13 @@ class SdFileStreamer(threading.Thread): with open(self._file, "r") as f: self._comm.startSdFileTransfer(sdFilename) for line in f: - self._comm.sendCommand(line) + if ";" in line: + line = line[0:line.find(";")] + line = line.strip() + if len(line) > 0: + self._comm.sendCommand(line) + time.sleep(0.001) # do not send too fast self._progressCallback(sdFilename, float(f.tell()) / float(size)) - time.sleep(0.001) # do not send too fast finally: self._comm.endSdFileTransfer(sdFilename) self._finishCallback(sdFilename) diff --git a/octoprint/static/js/ui.js b/octoprint/static/js/ui.js index cbc7f34..0020824 100644 --- a/octoprint/static/js/ui.js +++ b/octoprint/static/js/ui.js @@ -291,7 +291,7 @@ function PrinterStateViewModel(loginStateViewModel) { self._processSdUploadData = function(data) { if (self.isLoading()) { var progress = Math.round(data.progress * 100); - self.filename("Streaming to SD... (" + progress + "%)"); + self.filename("Streaming... (" + progress + "%)"); } } diff --git a/octoprint/util/comm.py b/octoprint/util/comm.py index b84ba11..01c77f7 100644 --- a/octoprint/util/comm.py +++ b/octoprint/util/comm.py @@ -71,6 +71,7 @@ class VirtualPrinter(): self._selectedSdFileSize = None self._selectedSdFilePos = None self._writingToSd = False + self._newSdFilePos = None def write(self, data): if self.readList is None: @@ -105,6 +106,9 @@ class VirtualPrinter(): self._startSdPrint() elif 'M25' in data: self._pauseSdPrint() + elif 'M26' in data: + pos = int(re.search("S([0-9]+)", data).group(1)) + self._setSdPos(pos) elif 'M27' in data: self._reportSdStatus() elif 'M28' in data: @@ -147,6 +151,9 @@ class VirtualPrinter(): self._sdPrintingSemaphore.clear() self.readList.append("ok") + def _setSdPos(self, pos): + self._newSdFilePos = pos + def _reportSdStatus(self): if self._sdPrinter is not None and self._sdPrintingSemaphore.is_set: self.readList.append("SD printing byte %d/%d" % (self._selectedSdFilePos, self._selectedSdFileSize)) @@ -174,8 +181,12 @@ class VirtualPrinter(): self._selectedSdFilePos = 0 with open(self._selectedSdFile, "rb") as f: for line in f: + # reset position if requested by client + if self._newSdFilePos is not None: + f.seek(self._newSdFilePos) + self._newSdFilePos = None + # read current file position - print("Progress: %d (%d / %d)" % ((round(self._selectedSdFilePos * 100 / self._selectedSdFileSize)), self._selectedSdFilePos, self._selectedSdFileSize)) self._selectedSdFilePos = f.tell() # if we are paused, wait for unpausing @@ -193,8 +204,6 @@ class VirtualPrinter(): except: pass - print line - time.sleep(0.01) self._sdPrintingSemaphore.clear() @@ -373,7 +382,7 @@ class MachineCom(object): if self._state == self.STATE_CLOSED_WITH_ERROR: return "Error: %s" % (self.getShortErrorString()) if self._state == self.STATE_RECEIVING_FILE: - return "Streaming file to SD" + return "Sending file to SD" return "?%d?" % (self._state) def getShortErrorString(self): @@ -792,6 +801,8 @@ class MachineCom(object): if not self.isOperational() or self.isPrinting(): return + if self.isPaused(): + self.sendCommand("M26 S0") # reset position in file to byte 0 self.sendCommand("M24") self._printSection = 'CUSTOM' @@ -803,7 +814,8 @@ class MachineCom(object): if self.isOperational(): self._changeState(self.STATE_OPERATIONAL) if self._sdPrinting: - self.sendCommand("M25") + self.sendCommand("M25") # pause print + self.sendCommand("M26 S0") # reset position in file to byte 0 def setPause(self, pause): if not pause and self.isPaused(): @@ -816,7 +828,7 @@ class MachineCom(object): if pause and self.isPrinting(): self._changeState(self.STATE_PAUSED) if self._sdPrinting: - self.sendCommand("M25") + self.sendCommand("M25") # pause print def setFeedrateModifier(self, type, value): self._feedRateModifier[type] = value