Skip to content

Commit

Permalink
Add line operations
Browse files Browse the repository at this point in the history
  • Loading branch information
duk3luk3 committed Dec 22, 2023
1 parent 549734f commit b973587
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 32 deletions.
79 changes: 50 additions & 29 deletions printrun/printcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def __init__(self, port = None, baud = None, dtr=None):
self.startcb = None # impl ()
self.endcb = None # impl ()
self.onlinecb = None # impl ()
self.loud = False # emit sent and received lines to terminal
self.loud = True # emit sent and received lines to terminal
self.tcp_streaming_mode = False
self.greetings = ['start', 'Grbl ']
self.wait = 0 # default wait period for send(), send_now()
Expand Down Expand Up @@ -536,7 +536,7 @@ def _sender(self):
def _checksum(self, command):
return reduce(lambda x, y: x ^ y, map(ord, command))

def startprint(self, gcode, startindex = 0):
def startprint(self, gcode, startindex = 0, really_start=True):
"""Start a print.
The `mainqueue` is populated and then commands are gradually sent to
Expand Down Expand Up @@ -575,10 +575,14 @@ def startprint(self, gcode, startindex = 0):
self._send("M110 N-1")

resuming = (startindex != 0)
self.print_thread = threading.Thread(target = self._print,
name = 'print thread',
kwargs = {"resuming": resuming})
self.print_thread.start()

if really_start:
self.print_thread = threading.Thread(target = self._print,
name = 'print thread',
kwargs = {"resuming": resuming})
self.print_thread.start()
else:
self.pause()
return True

def cancelprint(self):
Expand All @@ -600,6 +604,21 @@ def runSmallScript(self, filename):
except:
pass

def current_line(self, context=5):
if self.mainqueue and self.mainqueue.has_index(self.queueindex):
lines = []
index = self.queueindex - context
while index < self.queueindex + context:
(layer, line) = self.mainqueue.idxs(index)
gline = self.mainqueue.all_layers[layer][line]

prefix = "> " if index == self.queueindex else " "
suffix = " <" if index == self.queueindex else ""

lines.append((index, layer, prefix + gline.raw + suffix))
index = index + 1
return lines

def pause(self):
"""Pauses an ongoing print.
Expand All @@ -616,12 +635,13 @@ def pause(self):
self.paused = True
self.printing = False

# ';@pause' in the gcode file calls pause from the print thread
if not threading.current_thread() is self.print_thread:
try:
self.print_thread.join()
except:
self.logError(traceback.format_exc())
if self.print_thread:
# ';@pause' in the gcode file calls pause from the print thread
if not threading.current_thread() is self.print_thread:
try:
self.print_thread.join()
except:
self.logError(traceback.format_exc())

self.print_thread = None

Expand All @@ -634,7 +654,7 @@ def pause(self):
self.pauseRelative = self.analyzer.relative
self.pauseRelativeE = self.analyzer.relative_e

def resume(self):
def resume(self, restore=True):
"""Resumes a paused print.
`printcore` will first attempt to set the position and conditions it
Expand All @@ -649,22 +669,23 @@ def resume(self):
"""
if not self.paused: return False
# restores the status
self.send_now("G90") # go to absolute coordinates

xyFeed = '' if self.xy_feedrate is None else ' F' + str(self.xy_feedrate)
zFeed = '' if self.z_feedrate is None else ' F' + str(self.z_feedrate)

self.send_now("G1 X%s Y%s%s" % (self.pauseX, self.pauseY, xyFeed))
self.send_now("G1 Z" + str(self.pauseZ) + zFeed)
self.send_now("G92 E" + str(self.pauseE))

# go back to relative if needed
if self.pauseRelative:
self.send_now("G91")
if self.pauseRelativeE:
self.send_now('M83')
# reset old feed rate
self.send_now("G1 F" + str(self.pauseF))
if restore:
self.send_now("G90") # go to absolute coordinates

xyFeed = '' if self.xy_feedrate is None else ' F' + str(self.xy_feedrate)
zFeed = '' if self.z_feedrate is None else ' F' + str(self.z_feedrate)

self.send_now("G1 X%s Y%s%s" % (self.pauseX, self.pauseY, xyFeed))
self.send_now("G1 Z" + str(self.pauseZ) + zFeed)
self.send_now("G92 E" + str(self.pauseE))

# go back to relative if needed
if self.pauseRelative:
self.send_now("G91")
if self.pauseRelativeE:
self.send_now('M83')
# reset old feed rate
self.send_now("G1 F" + str(self.pauseF))

self.paused = False
self.printing = True
Expand Down
26 changes: 23 additions & 3 deletions printrun/pronsole.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is part of the Printrun suite.
# file is part of the Printrun suite.
#
# Printrun is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -932,6 +932,16 @@ def statuschecker(self):
# File loading handling
# --------------------------------------------------------------

def do_line(self, l=''):
lines = self.p.current_line()
for line in lines:
self.log("%04d: %s" % (line[0], line[2]))

def do_skip(self, l):
skip = int(l)
self.p.queueindex = self.p.queueindex + skip


def do_load(self, filename):
self._do_load(filename)

Expand Down Expand Up @@ -1094,7 +1104,16 @@ def do_print(self, l):
self.log(_("Printing %s") % self.filename)
self.log(_("You can monitor the print with the monitor command."))
self.sdprinting = False
self.p.startprint(self.fgcode)
really = not (l and l == 'pause')
self.p.startprint(self.fgcode, really_start=really)
if not really:
self.paused = True

def do_next(self, l):
if self.paused:
self.p.printing = True
self.p._sendnext()
self.p.printing = False

def do_pause(self, l):
if self.sdprinting:
Expand All @@ -1121,7 +1140,8 @@ def do_resume(self, l):
self.p.send_now("M24")
return
else:
self.p.resume()
do_restore = not l or l != 'direct'
self.p.resume(do_restore)

def help_resume(self):
self.log(_("Resumes a paused print."))
Expand Down

0 comments on commit b973587

Please sign in to comment.