Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions printrun/printcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def __init__(self, port = None, baud = None, dtr=None):
self.online = False
# is a print currently running, true if printing, false if paused
self.printing = False
self.sd_upload = False # the current print is an upload to SD
self.mainqueue = None
self.priqueue = Queue(0)
self.queueindex = 0
Expand Down Expand Up @@ -370,7 +371,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, upload = False):
"""Start a print.

The `mainqueue` is populated and then commands are gradually sent to
Expand All @@ -387,6 +388,10 @@ def startprint(self, gcode, startindex = 0):
startindex : int, default: 0
The index from the `gcode` array from which the printing will be
started.
upload : bool, default: False
True if the firmware is storing the lines in a file instead of
running them. No line number reset is sent, as it would be stored
too, so the caller has to send one before opening the file.

Returns
-------
Expand All @@ -405,8 +410,10 @@ def startprint(self, gcode, startindex = 0):
if not gcode or not gcode.lines:
return True

self.clear = False
self._send("M110 N-1", -1, True)
self.sd_upload = upload
if not upload:
self.clear = False
self._send("M110 N-1", -1, True)

resuming = (startindex != 0)
self.print_thread = threading.Thread(target = self._print,
Expand Down Expand Up @@ -633,7 +640,8 @@ def _sendnext(self):
if not self.paused:
self.queueindex = 0
self.lineno = 0
self._send("M110 N-1", -1, True)
if not self.sd_upload:
self._send("M110 N-1", -1, True)

def _send(self, command, lineno = 0, calcchecksum = False):
# Only add checksums if over serial (tcp does the flow control itself)
Expand Down
3 changes: 2 additions & 1 deletion printrun/pronsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,9 +1027,10 @@ def do_upload(self, l):
self._do_load(filename)
self.log(_("Uploading as %s") % targetname)
self.log(_("Uploading %s") % self.filename)
self.p.send_now("M110 N-1")
self.p.send_now("M28 " + targetname)
self.log(_("Press Ctrl-C to interrupt upload."))
self.p.startprint(self.fgcode)
self.p.startprint(self.fgcode, upload = True)
try:
sys.stdout.write(_("Progress: ") + "00.0%")
sys.stdout.flush()
Expand Down
3 changes: 2 additions & 1 deletion printrun/pronterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1464,14 +1464,15 @@ def upload(self, event):
dlg = wx.TextEntryDialog(self, _("Enter a target filename in 8.3 format:"), _("Pick SD filename"), dosify(self.filename))
if dlg.ShowModal() == wx.ID_OK:
self.p.send_now("M21")
self.p.send_now("M110 N-1")
self.p.send_now("M28 " + str(dlg.GetValue()))
self.recvlisteners.append(self.uploadtrigger)
dlg.Destroy()

def uploadtrigger(self, l):
if "Writing to file" in l:
self.uploading = True
self.p.startprint(self.fgcode)
self.p.startprint(self.fgcode, upload = True)
self.p.callback.end = self.endupload
self.recvlisteners.remove(self.uploadtrigger)
elif "open failed, File" in l:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_printcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,34 @@ def setUp(self):
self.start_cb = mock_callback(self, self.core, "startcb")
self.end_cb = mock_callback(self, self.core, "endcb")

def run_to_end(self, **kwargs):
"""Run the whole print and return the bytes written during it"""
self.mocked_serial.return_value.write.reset_mock()
self.core.startprint(self.print_code, **kwargs)
limit = time.monotonic() + 5
while self.core.printing and time.monotonic() < limit:
time.sleep(0.01)
self.assertFalse(self.core.printing)
return b"".join(c.args[0] for c
in self.mocked_serial.return_value.write.call_args_list)

def test_print_resets_line_number(self):
"""Test a print resets the line number at its start and end"""
written = self.run_to_end()
self.assertEqual(written.count(b"M110 N-1"), 2)
self.assertTrue(written.startswith(checksum_command("M110 N-1", -1)))

def test_upload_sends_only_its_lines(self):
"""Test an upload doesn't add lines to the file being written"""
written = self.run_to_end(upload=True)
self.assertNotIn(b"M110", written)
self.assertEqual(written, b"".join(self.parsed_print_code))

def test_print_after_upload_resets_line_number(self):
"""Test a normal print still resets the line number after an upload"""
self.run_to_end(upload=True)
self.assertEqual(self.run_to_end().count(b"M110 N-1"), 2)

def check_unfinished_print(self):
"""Check the first command was sent but the last wasn't"""
first_call = mock.call(self.parsed_print_code[0])
Expand Down