diff --git a/printrun/eventhandler.py b/printrun/eventhandler.py index f4180eacf..785b570ed 100644 --- a/printrun/eventhandler.py +++ b/printrun/eventhandler.py @@ -69,6 +69,12 @@ def on_end(self): def on_layerchange(self, layer): pass + def on_meatpack(self, active): + pass + + def on_advancedok(self, active): + pass + def on_printpresend(self, gline, next_gline, index): pass diff --git a/printrun/gui/log.py b/printrun/gui/log.py index 2743f034a..28bc66245 100644 --- a/printrun/gui/log.py +++ b/printrun/gui/log.py @@ -13,10 +13,47 @@ # You should have received a copy of the GNU General Public License # along with Printrun. If not, see . +import math + import wx from .utils import make_button +class SendModeIndicator(wx.StaticBitmap): + """A dot showing how lines are being sent to the printer""" + SIZE = 12 + # (MeatPack packing, Advanced OK sending ahead) + COLORS = {(False, False): (150, 150, 150), + (True, False): (60, 130, 230), + (False, True): (235, 160, 30), + (True, True): (40, 190, 80)} + TIPS = {(False, False): _("Normal: lines are plain text, one at a time"), + (True, False): _("MeatPack: lines are packed"), + (False, True): _("Advanced OK: lines are sent ahead of replies"), + (True, True): _("MeatPack and Advanced OK: lines are packed and sent ahead of replies")} + + def __init__(self, parent): + super().__init__(parent, bitmap = self.dot(self.COLORS[(False, False)])) + self.set_state(False, False) + + @classmethod + def dot(cls, color): + """Make a round dot with a smooth edge""" + middle = cls.SIZE / 2 + data = bytearray() + for y in range(cls.SIZE): + for x in range(cls.SIZE): + distance = math.hypot(x + 0.5 - middle, y + 0.5 - middle) + coverage = min(max(middle - distance, 0), 1) + data.extend((*color, int(255 * coverage))) + return wx.Bitmap.FromBufferRGBA(cls.SIZE, cls.SIZE, bytes(data)) + + def set_state(self, packing, sending_ahead): + state = (bool(packing), bool(sending_ahead)) + self.SetBitmap(self.dot(self.COLORS[state])) + self.SetToolTip(wx.ToolTip(self.TIPS[state])) + + class LogPane(wx.BoxSizer): def __init__(self, root, parentpanel = None): @@ -42,6 +79,9 @@ def deselect(ev): root.commandbox.Bind(wx.EVT_SET_FOCUS, deselect) root.commandbox.history = [""] root.commandbox.histindex = 1 + root.send_mode_indicator = SendModeIndicator(bottom_panel) + lbrs.Add(root.send_mode_indicator, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT, 4) + root.update_send_mode_indicator() lbrs.Add(root.commandbox, 1) root.sendbtn = make_button(bottom_panel, _("Send"), root.sendline, _("Send Command to Printer"), style = wx.BU_EXACTFIT, container = lbrs) bottom_panel.SetSizer(lbrs) diff --git a/printrun/meatpack.py b/printrun/meatpack.py new file mode 100644 index 000000000..91b68468c --- /dev/null +++ b/printrun/meatpack.py @@ -0,0 +1,88 @@ +# This 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 +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Printrun is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Printrun. If not, see . + +"""MeatPack G-code compression, the sending side. + +Each byte on the wire carries two characters from a 15 character alphabet, +the first in the low nibble and the second in the high nibble. The nibble +value 0xF means the character doesn't fit the alphabet and is sent as a whole +byte after the packed byte. + +Commands are 0xFF, 0xFF and a command byte. They can be sent at any time +between lines, in either mode. +""" + +import re + +_COMMAND = b"\xff\xff" +ENABLE_PACKING = _COMMAND + b"\xfb" +DISABLE_PACKING = _COMMAND + b"\xfa" +RESET_ALL = _COMMAND + b"\xf9" +QUERY_CONFIG = _COMMAND + b"\xf8" +ENABLE_NO_SPACES = _COMMAND + b"\xf7" +DISABLE_NO_SPACES = _COMMAND + b"\xf6" + +# In no-spaces mode the firmware decodes the space entry as 'E'. +_ALPHABET = b"0123456789.E\nGX" +_LITERAL = 0xF +_NIBBLES = tuple(_ALPHABET.find(i) if i in _ALPHABET else _LITERAL + for i in range(256)) +_NEWLINE = _ALPHABET.index(b"\n") + +_MOVE_EXP = re.compile(r"\s*G[0-3](?![0-9])") +_EMERGENCY_EXP = re.compile(r"\s*(N\d+\s*)?M(108|112|410|876)(?![0-9])") + + +def strip_spaces(command): + """Remove the spaces from a move, where they serve no purpose. + + Other commands are returned unchanged, since they may carry text. + """ + if _MOVE_EXP.match(command): + return command.replace(" ", "") + return command + + +def is_emergency(command): + """Tell if a command is one the firmware's emergency parser looks for. + + That parser reads the serial data before it is unpacked. + """ + return _EMERGENCY_EXP.match(command) is not None + + +def pack(line): + """Pack the bytes of one line, ending in a newline, for no-spaces mode. + """ + out = bytearray() + nibbles = _NIBBLES + i = 0 + while i < len(line): + first = line[i] + nibble = nibbles[first] + if nibble == _NEWLINE: + # The firmware doesn't read a second character after a newline + out.append(_NEWLINE) + i += 1 + continue + second = line[i + 1] + second_nibble = nibbles[second] + out.append(nibble | second_nibble << 4) + if nibble == _LITERAL: + out.append(first) + if second_nibble == _LITERAL: + out.append(second) + i += 2 + return bytes(out) diff --git a/printrun/printcore.py b/printrun/printcore.py index 4819738d1..5598eca2a 100644 --- a/printrun/printcore.py +++ b/printrun/printcore.py @@ -24,11 +24,13 @@ from queue import Queue, Empty as QueueEmpty import time import logging +import re import traceback from functools import wraps, reduce from collections import deque from printrun import gcoder from printrun import device +from printrun import meatpack from .utils import set_utf8_locale, install_locale, decode_utf8 try: set_utf8_locale() @@ -46,6 +48,20 @@ def inner(*args, **kw): return inner +# An ok from firmware with ADVANCED_OK, reporting its free queue space +OK_EXP = re.compile(r"ok(?: N(-?\d+))?(?: P\d+ B(\d+))?") +LINE_NUMBER_EXP = re.compile(r"N(-?\d+)") +# Firmware's serial receive buffer is 128 bytes by default, and it may not read +# from it for some time. Leave room for another line as long as the longest so +# far within what it can hold. +RX_BYTES_LIMIT = 127 +RX_BYTES_PER_LINE = 40 +# With this long of silence from the firmware, assume that an ok was lost +OK_TIMEOUT = 30 +# Firmware discards unread lines when it asks for a resend, so there will never +# be a reply to some of the lines sent. This long of silence means it's done. +RESEND_TIMEOUT = 0.5 + PR_EOF = None #printrun's marker for EOF PR_AGAIN = b'' #printrun's marker for timeout/no data SYS_EOF = b'' #python's marker for EOF @@ -147,6 +163,20 @@ def __init__(self, port = None, baud = None, dtr=None): self.loud = False # emit sent and received lines to terminal self.tcp_streaming_mode = False + self.advanced_ok = False # send ahead if the firmware reports queue space + self.flow_control = False # advanced_ok is in effect for this connection + self.flow_lock = threading.Lock() + self.unacked = deque() # (line number, bytes) of lines without an ok + self.last_acked = None # line number in the last ok that had one + self.unacked_bytes = 0 + self.line_bytes = RX_BYTES_PER_LINE # room to leave for the next line + self.queue_lines = 0 # lines the firmware can queue, 0 until reported + self.resend_pending = False # waiting for oks before resending + self.last_received = 0 + self.meatpack = False # use MeatPack if the firmware reports it + self.meatpack_active = False # lines are being packed + self.meatpack_resync = False # the firmware's unpacking may be out of step + self.write_lock = threading.Lock() self.greetings = ['start', 'Grbl '] self.wait = 0 # default wait period for send(), send_now() self.read_thread = None @@ -197,6 +227,8 @@ def disconnect(self): self.printing = False self.print_thread.join() self._stop_sender() + self._meatpack_stop() + self._flow_reset() try: self.printer.disconnect() except device.DeviceError: @@ -229,6 +261,12 @@ def connect(self, port = None, baud = None, dtr=None): self._logError("Connection error: %s" % e) self.printer = None return + self.flow_control = (self.advanced_ok + and not self.printer.has_flow_control) + self._flow_reset() + self._meatpack_plain() + if self.meatpack: + self._meatpack_reset() self._callback('connect') self.stop_read_thread = False self.read_thread = threading.Thread(target = self._listen, @@ -304,9 +342,165 @@ def _listen_until_online(self): if line.startswith(tuple(self.greetings)) \ or line.startswith('ok') or "T:" in line: self.online = True + self._flow_reset() self._callback('online') + if self.meatpack: + self._send("M115") return + def _flow_reset(self): + with self.flow_lock: + self.unacked.clear() + self.unacked_bytes = 0 + self.line_bytes = RX_BYTES_PER_LINE + self.last_acked = None + active = self.queue_lines > 0 + self.queue_lines = 0 + self.resend_pending = False + if active: + self._callback('advancedok', False) + + def _flow_sent(self, command, size): + """Count a line as sent and waiting for its ok""" + match = LINE_NUMBER_EXP.match(command) + with self.flow_lock: + self.unacked.append((int(match[1]) if match else None, size)) + self.unacked_bytes += size + self.line_bytes = max(self.line_bytes, size) + + def _flow_ack(self, number): + """Remove the line an ok is for. An ok with a line number also covers + the lines before it. An ok without one is for a line without one, or + for a line that was refused. + """ + lines = [line for line, _size in self.unacked] + if number is not None and number in lines: + count = lines.index(number) + 1 + elif None in lines: + count = 0 + _line, size = self.unacked[lines.index(None)] + del self.unacked[lines.index(None)] + self.unacked_bytes -= size + else: + count = 1 if lines else 0 + for _i in range(count): + self.unacked_bytes -= self.unacked.popleft()[1] + + def _flow_ok(self, line): + """Count a line as done, and learn how many lines the firmware can + queue. It reports its free space while still holding the line it just + finished, so the most it reports is one less than it can hold. + """ + match = OK_EXP.match(line) + number = int(match[1]) if match[1] else None + started = False + with self.flow_lock: + self._flow_ack(number) + if number is not None: + self.last_acked = number + if match[2]: + started = not self.queue_lines + self.queue_lines = max(self.queue_lines, int(match[2]) + 1) + if started: + self._callback('advancedok', True) + + def _flow_resend(self, lineno): + """Note a resend request. Lines already on their way will each be + refused with a request for this same line, so let those finish first. + """ + with self.flow_lock: + if not self.resend_pending: + self.resend_pending = True + self.resendfrom = lineno + + def _can_send(self): + """Tell if another line may be sent to the printer now""" + if not self.queue_lines: + return self.clear + with self.flow_lock: + silence = time.monotonic() - self.last_received + if self.unacked and silence > OK_TIMEOUT: + logging.warning(_("No reply from the printer, assuming that an ok was lost.")) + if self.unacked and (silence > OK_TIMEOUT + or (self.resend_pending + and silence > RESEND_TIMEOUT)): + self.unacked.clear() + self.unacked_bytes = 0 + if not self.unacked: + if self.resend_pending: + self.resend_pending = False + # The firmware may have asked for a line it then received + if (self.last_acked is not None + and self.resendfrom <= self.last_acked): + self.resendfrom = self.last_acked + 1 + return True + return (not self.resend_pending + and len(self.unacked) < self.queue_lines + and self.unacked_bytes + self.line_bytes <= RX_BYTES_LIMIT) + def _write_meatpack_command(self, command): + try: + self.printer.write(command) + self.writefailures = 0 + except device.DeviceError as e: + self._logError("Can't write to printer (disconnected?)" + " {0}".format(e)) + self.writefailures += 1 + + def _meatpack_reset(self): + """Return the firmware to plain text, in case a previous connection + didn't get to do it. The newline ends the line that firmware without + MeatPack will make of the command bytes. + """ + self._write_meatpack_command(meatpack.RESET_ALL + + meatpack.DISABLE_NO_SPACES + b"\n") + + def _meatpack_start(self): + with self.write_lock: + if self.meatpack_active: + return + self._write_meatpack_command(meatpack.ENABLE_NO_SPACES + + meatpack.ENABLE_PACKING) + self.meatpack_active = True + logging.info(_("MeatPack enabled")) + self._callback('meatpack', True) + + def _meatpack_stop(self): + with self.write_lock: + if not self.meatpack_active: + return + self.meatpack_active = False + self._write_meatpack_command(meatpack.DISABLE_PACKING + + meatpack.DISABLE_NO_SPACES) + self._callback('meatpack', False) + + def _meatpack_plain(self): + """Note that the firmware is reading plain text""" + if self.meatpack_active: + self.meatpack_active = False + self._callback('meatpack', False) + + def meatpack_query(self): + """Ask the firmware to report its MeatPack state. The reply is a line + like `[MP] PV01 ON NSP`. A newline can only be sent in plain text, + where it ends the line that firmware without MeatPack will make of the + command bytes. + """ + with self.write_lock: + self._write_meatpack_command( + meatpack.QUERY_CONFIG + + (b"" if self.meatpack_active else b"\n")) + + def _meatpack_listen(self, line): + """Follow the firmware's MeatPack state from what it reports + """ + if line.strip() == "start": + # The firmware restarted, so it's reading plain text again + self._meatpack_plain() + if self.meatpack: + self._send("M115") + elif self.meatpack and line.startswith("Cap:MEATPACK:1"): + self._meatpack_start() + def _listen(self): """This function acts on messages from the firmware """ @@ -320,8 +514,16 @@ def _listen(self): break if line.startswith('DEBUG_'): continue + if line: + self.last_received = time.monotonic() + self._meatpack_listen(line) if line.startswith(tuple(self.greetings)) or line.startswith('ok'): self.clear = True + if self.flow_control: + if line.startswith('ok'): + self._flow_ok(line) + else: + self._flow_reset() if line.startswith('ok') and "T:" in line: self._callback('temp', line) elif line.startswith('Error'): @@ -329,13 +531,17 @@ def _listen(self): # Teststrings for resend parsing # Firmware exp. result # line="rs N2 Expected checksum 67" # Teacup 2 if line.lower().startswith("resend") or line.startswith("rs"): + self.meatpack_resync = True for haystack in ["N:", "N", ":"]: line = line.replace(haystack, " ") linewords = line.split() while len(linewords) != 0: try: toresend = int(linewords.pop(0)) - self.resendfrom = toresend + if self.flow_control: + self._flow_resend(toresend) + else: + self.resendfrom = toresend break except: pass @@ -361,10 +567,10 @@ def _sender(self): command = self.priqueue.get(True, 0.1) except QueueEmpty: continue - while self.printer and self.printing and not self.clear: + while self.printer and self.printing and not self._can_send(): time.sleep(0.001) self._send(command) - while self.printer and self.printing and not self.clear: + while self.printer and self.printing and not self._can_send(): time.sleep(0.001) def _checksum(self, command): @@ -575,7 +781,7 @@ def _host_command_cb(self, command): def _sendnext(self): if not self.printer: return - while self.printer and self.printing and not self.clear: + while self.printer and self.printing and not self._can_send(): time.sleep(0.001) # Only wait for oks when using serial connections or when not using tcp # in streaming mode @@ -635,7 +841,31 @@ def _sendnext(self): self.lineno = 0 self._send("M110 N-1", -1, True) + def _encode(self, command): + """Make the bytes that send a command in the current MeatPack state + """ + line = (command + "\n").encode('ascii') + if not self.meatpack_active: + return line + prefix = b"" + if self.meatpack_resync: + # A corrupted line, or the unread data the firmware discards when + # it asks for a resend, can leave it waiting for a byte that won't + # come. Commands are read ahead of the unpacking, so they can reset it. + self.meatpack_resync = False + prefix = (meatpack.RESET_ALL + meatpack.ENABLE_NO_SPACES + + meatpack.ENABLE_PACKING) + if meatpack.is_emergency(command): + # Send it unpacked so the firmware's emergency parser can see it. + # That parser ignores everything up to a newline. + return (prefix + meatpack.DISABLE_PACKING + b"\n" + line + + meatpack.ENABLE_PACKING) + return prefix + meatpack.pack(line) + def _send(self, command, lineno = 0, calcchecksum = False): + # The checksum has to match the line without its spaces + if self.meatpack_active: + command = meatpack.strip_spaces(command) # Only add checksums if over serial (tcp does the flow control itself) if calcchecksum and not self.printer.has_flow_control: prefix = "N" + str(lineno) + " " + command @@ -656,7 +886,11 @@ def _send(self, command, lineno = 0, calcchecksum = False): self._callback('send', command, gline) try: - self.printer.write((command + "\n").encode('ascii')) + with self.write_lock: + data = self._encode(command) + if self.flow_control: + self._flow_sent(command, len(data)) + self.printer.write(data) self.writefailures = 0 except device.DeviceError as e: self._logError("Can't write to printer (disconnected?)" @@ -786,6 +1020,28 @@ def layerchange(self, layer): """ pass + def meatpack(self, active): + """Called when MeatPack packing of sent lines starts or stops. + + Parameters: + ----------- + active : bool + True if lines are now being packed. + + """ + pass + + def advancedok(self, active): + """Called when sending ahead of replies starts or stops. + + Parameters: + ----------- + active : bool + True if lines are now being sent ahead of the replies to them. + + """ + pass + def online(self): """Called when printer gets online.""" pass diff --git a/printrun/pronsole.py b/printrun/pronsole.py index c954e4d3c..df873e886 100644 --- a/printrun/pronsole.py +++ b/printrun/pronsole.py @@ -184,6 +184,8 @@ def __init__(self): self.settings._port_list = self.scanserial self.update_build_dimensions(None, self.settings.build_dimensions) self.update_tcp_streaming_mode(None, self.settings.tcp_streaming_mode) + self.update_advanced_ok(None, self.settings.advanced_ok) + self.update_meatpack(None, self.settings.meatpack) self.monitoring = 0 self.starttime = 0 self.extra_print_time = 0 @@ -200,7 +202,7 @@ def __init__(self): self.cache_dir.mkdir(parents=True, exist_ok=True) self.history_file = self.cache_dir / "history" self.history_file.touch(exist_ok=True) - self.lineignorepattern = re.compile(r"ok ?\d*$|.*busy: ?processing|.*busy: ?heating|.*Active Extruder: ?\d*$") + self.lineignorepattern = re.compile(r"ok ?\d*$|ok( N-?\d+)? P\d+ B\d+$|.*busy: ?processing|.*busy: ?heating|.*Active Extruder: ?\d*$") # -------------------------------------------------------------- # General console handling @@ -722,6 +724,11 @@ def update_build_dimensions(self, param, value): def update_tcp_streaming_mode(self, param, value): self.p.tcp_streaming_mode = self.settings.tcp_streaming_mode + def update_advanced_ok(self, param, value): + self.p.advanced_ok = self.settings.advanced_ok + def update_meatpack(self, param, value): + self.p.meatpack = self.settings.meatpack + def update_rpc_server(self, param, value): if value: if self.rpc_server is None: @@ -1412,6 +1419,19 @@ def do_gettemp(self, l): def help_gettemp(self): self.log(_("Read the extruder and bed temperature.")) + def do_meatpack(self, l): + if not self.p.online: + self.logError(_("Printer is not online.")) + return + self.log(_("MeatPack setting: %s, packing lines: %s") % + (_("on") if self.p.meatpack else _("off"), + _("yes") if self.p.meatpack_active else _("no"))) + self.log(_("Asking the firmware, which replies with [MP] and its state if it supports MeatPack on this port.")) + self.p.meatpack_query() + + def help_meatpack(self): + self.log(_("Show whether Printrun is packing lines with MeatPack, and ask the firmware for its MeatPack state.")) + def do_settemp(self, l): l = l.lower().replace(", ", ".") for i in self.temps.keys(): diff --git a/printrun/pronterface.py b/printrun/pronterface.py index 8c7bd09f6..5ce12ef96 100644 --- a/printrun/pronterface.py +++ b/printrun/pronterface.py @@ -218,6 +218,8 @@ def __init__(self, app, filename = None, size = winsize): self.p.callback.printsend = self.printsentcb self.p.callback.start = self.startcb self.p.callback.end = self.endcb + self.p.callback.meatpack = lambda active: wx.CallAfter(self.update_send_mode_indicator) + self.p.callback.advancedok = lambda active: wx.CallAfter(self.update_send_mode_indicator) self.cur_button = None self.predisconnect_mainqueue = None self.predisconnect_queueindex = None @@ -331,6 +333,25 @@ def on_maximize(self, event): def on_exit(self, event): self.Close() + def update_meatpack(self, param, value): + super().update_meatpack(param, value) + self.update_send_mode_indicator() + + def update_advanced_ok(self, param, value): + super().update_advanced_ok(param, value) + self.update_send_mode_indicator() + + def update_send_mode_indicator(self): + """Show the dot while an option for sending is enabled or still in use, nothing otherwise""" + indicator = getattr(self, "send_mode_indicator", None) + if not indicator: + return + packing = self.p.meatpack_active + sending_ahead = self.p.queue_lines > 0 + indicator.set_state(packing, sending_ahead) + indicator.Show(self.p.meatpack or self.p.advanced_ok or packing or sending_ahead) + indicator.GetParent().Layout() + def on_settings_change(self, changed_settings): if self.gviz: self.gviz.on_settings_change(changed_settings) diff --git a/printrun/settings.py b/printrun/settings.py index 9afceeb2a..2580c0911 100644 --- a/printrun/settings.py +++ b/printrun/settings.py @@ -387,6 +387,8 @@ def __init__(self, root): "This will break things such as ETA prediction, but can result in smoother prints.")), root.update_tcp_streaming_mode) self._add(BooleanSetting("rpc_server", True, _("RPC Server:"), _("Enable RPC server to allow remotely querying print status")), root.update_rpc_server) self._add(BooleanSetting("dtr", True, _("DTR:"), _("Disabling DTR would prevent Arduino (RAMPS) from resetting upon connection"), "Printer")) + self._add(BooleanSetting("advanced_ok", False, _("Advanced OK:"), _("Send lines ahead of the replies to them, if the firmware reports its free queue space (Marlin's ADVANCED_OK). This keeps its queue full, which helps when waiting for replies limits print speed. Serial connections only. Takes effect on the next connection."), "Printer"), root.update_advanced_ok) + self._add(BooleanSetting("meatpack", False, _("MeatPack:"), _("Compress G-code sent to the printer, if its firmware reports MeatPack support. Sends fewer bytes, which helps when the serial connection limits print speed. Takes effect on the next connection."), "Printer"), root.update_meatpack) if sys.platform != "win32": self._add(StringSetting("devicepath", "", _("Device Name Pattern:"), _("Custom device pattern: for example /dev/3DP_* "), "Printer")) self._add(SpinSetting("bedtemp_abs", 110, 0, 400, _("Bed Temperature for ABS:"), _("Heated Build Platform temp for ABS (deg C)"), "Printer"), root.set_temp_preset) diff --git a/tests/test_advanced_ok.py b/tests/test_advanced_ok.py new file mode 100644 index 000000000..399302df7 --- /dev/null +++ b/tests/test_advanced_ok.py @@ -0,0 +1,211 @@ +"""Test suite for sending ahead of replies in `printrun/printcore.py`. + +""" +# How to run the tests (requires Python 3.11+): +# python3 -m unittest discover tests + +# Standard libraries: +import collections +import time +import unittest +from unittest import mock + +# 3rd Party/Custom libraries: +import serial +from printrun import gcoder +from printrun import printcore + + +OK = b"ok P15 B3\n" # Firmware with room for 4 lines, BUFSIZE - 1 free when idle + + +class TestAdvancedOk(unittest.TestCase): + """Checks of how many lines are outstanding, against a fake printer""" + + def setUp(self): + patcher = mock.patch("printrun.device.Device._disable_ttyhup") + patcher.start() + self.addCleanup(patcher.stop) + self.replies = collections.deque([b"ok\n"]) + class_mock = mock.create_autospec(serial.Serial) + self.serial = class_mock.return_value + self.serial.is_open = True + self.serial.readline.side_effect = self.read + self.enterContext(mock.patch("serial.Serial", class_mock)) + self.core = printcore.printcore() + self.addCleanup(self.core.disconnect) + self.core.advanced_ok = True + self.gcode = gcoder.GCode([f"G1 X{i}" for i in range(1000)]) + + def read(self, *args): + time.sleep(0.001) + return self.replies.popleft() if self.replies else b"" + + def sent_moves(self): + return [c.args[0] for c in self.serial.write.call_args_list + if b"G1" in c.args[0]] + + def wait_for(self, condition): + limit = time.monotonic() + 2 + while not condition() and time.monotonic() < limit: + time.sleep(0.002) + self.assertTrue(condition()) + + def settle(self): + """Give the print thread time to send everything it's allowed to""" + time.sleep(0.05) + + def start(self): + self.core.connect("/mocked/port", 1000) + self.wait_for(lambda: self.core.online) + self.core.startprint(self.gcode) + self.wait_for(lambda: self.serial.write.call_count >= 2) + self.replies.append(OK) # For M110, which reports the queue size + self.wait_for(lambda: self.core.queue_lines == 4) + self.wait_for(lambda: self.sent_moves()) + self.settle() + + def test_one_at_a_time_until_reported(self): + """Test firmware without ADVANCED_OK gets one line per ok""" + self.core.connect("/mocked/port", 1000) + self.wait_for(lambda: self.core.online) + self.core.startprint(self.gcode) + self.wait_for(lambda: self.serial.write.call_count >= 2) + self.replies.append(b"ok\n") + self.wait_for(lambda: len(self.sent_moves()) == 1) + self.settle() + self.assertEqual(len(self.sent_moves()), 1) + self.replies.append(b"ok\n") + self.wait_for(lambda: len(self.sent_moves()) == 2) + self.settle() + self.assertEqual(len(self.sent_moves()), 2) + + def test_setting_off(self): + """Test the option off ignores the reported queue space""" + self.core.advanced_ok = False + self.core.connect("/mocked/port", 1000) + self.wait_for(lambda: self.core.online) + self.core.startprint(self.gcode) + self.wait_for(lambda: self.serial.write.call_count >= 2) + self.replies.append(OK) + self.wait_for(lambda: len(self.sent_moves()) == 1) + self.settle() + self.assertEqual(len(self.sent_moves()), 1) + self.assertEqual(self.core.queue_lines, 0) + + def test_fills_queue(self): + """Test lines are sent up to the queue size, and one more per ok""" + self.start() + self.assertEqual(len(self.core.unacked), 4) + self.replies.append(OK) + self.wait_for(lambda: len(self.core.unacked) == 4 + and len(self.sent_moves()) == 5) + self.settle() + self.assertEqual(len(self.sent_moves()), 5) + + def test_serial_buffer_limit(self): + """Test the bytes on their way stay within the firmware's buffer""" + self.gcode = gcoder.GCode( + [f"G1 X{i}.12345 Y{i}.12345 Z1.12345 E{i}.12345" for i in range(40)]) + self.start() + self.assertLess(len(self.core.unacked), 4) + self.assertLessEqual(self.core.unacked_bytes, + printcore.RX_BYTES_LIMIT) + + def test_serial_buffer_limit_with_lines_over_estimate(self): + """Test lines just over the initial estimate can't pass the limit""" + self.gcode = gcoder.GCode( + [f"G1 X{100 + i}.123 Y{100 + i}.123 E1.123" for i in range(100)] + + [f"G1 X1{100 + i}.1234 Y1{100 + i}.1234 E1.1234" for i in range(100)]) + self.start() + peak = 0 + for _i in range(150): + peak = max(peak, self.core.unacked_bytes) + self.replies.append(OK) + time.sleep(0.004) + self.assertLessEqual(peak, printcore.RX_BYTES_LIMIT) + + def test_resend_after_lines_on_their_way(self): + """Test a resend waits for the replies to the lines already sent, and + only the first request of a burst counts""" + self.start() + sent = len(self.sent_moves()) + first = self.sent_moves()[0] + self.replies.extend([b"Error:Line Number\n", b"Resend: 0\n", b"ok\n", + b"Error:Line Number\n", b"Resend: 0\n", b"ok\n"]) + self.wait_for(lambda: len(self.core.unacked) == 2) + self.settle() + self.assertEqual(len(self.sent_moves()), sent) + self.replies.extend([b"Error:Line Number\n", b"Resend: 0\n", b"ok\n", + b"Error:Line Number\n", b"Resend: 0\n", b"ok\n"]) + self.wait_for(lambda: len(self.sent_moves()) > sent) + self.assertEqual(self.sent_moves()[sent], first) + self.settle() + self.assertEqual(self.sent_moves()[sent + 1], + self.sent_moves()[1]) + + def test_ok_covers_earlier_lines(self): + """Test an ok with a line number also covers any lost before it""" + self.start() + self.assertEqual(self.core.unacked[0][0], 0) + self.replies.append(b"ok N2 P15 B3\n") + self.wait_for(lambda: self.core.last_acked == 2) + self.settle() + self.assertEqual(self.core.unacked[0][0], 3) + self.assertEqual(len(self.core.unacked), 4) + + def test_no_resend_of_received_line(self): + """Test a line isn't resent if its ok arrives after the request. The + firmware doesn't reply to a line it already has.""" + self.start() + self.core.send_now("M105") + self.replies.append(b"ok N0 P15 B3\n") + self.wait_for(lambda: b"M105\n" in [ + c.args[0] for c in self.serial.write.call_args_list]) + sent = len(self.sent_moves()) + self.replies.extend([b"Error:No Checksum\n", b"Resend: 1\n", b"ok\n"]) + self.replies.extend(b"ok N%d P15 B3\n" % n for n in range(1, sent)) + self.wait_for(lambda: len(self.sent_moves()) > sent) + self.assertIn(b"N%d " % sent, self.sent_moves()[sent]) + + def test_resend_with_discarded_lines(self): + """Test a resend isn't held up by lines the firmware discarded, which + it will never reply to""" + self.start() + sent = len(self.sent_moves()) + first = self.sent_moves()[0] + self.replies.extend([b"Error:Line Number\n", b"Resend: 0\n", b"ok\n"]) + self.wait_for(lambda: len(self.core.unacked) == 3) + self.settle() + self.assertEqual(len(self.sent_moves()), sent) + with mock.patch("printrun.printcore.RESEND_TIMEOUT", 0.05): + self.wait_for(lambda: len(self.sent_moves()) > sent) + self.assertEqual(self.sent_moves()[sent], first) + + def test_lost_ok(self): + """Test sending continues after silence, if an ok never comes""" + self.start() + sent = len(self.sent_moves()) + self.settle() + self.assertEqual(len(self.sent_moves()), sent) + with mock.patch("printrun.printcore.OK_TIMEOUT", 0.05): + self.wait_for(lambda: len(self.sent_moves()) > sent) + + def test_disconnect_stops_sending_ahead(self): + """Test disconnecting ends the state and reports it""" + calls = [] + self.core.callback.advancedok = calls.append + self.start() + self.core.disconnect() + self.assertEqual(self.core.queue_lines, 0) + self.assertEqual(calls, [True, False]) + + def test_firmware_restart(self): + """Test a restart goes back to one line at a time until reported""" + self.start() + self.replies.append(b"start\n") + self.wait_for(lambda: self.core.queue_lines == 0) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_meatpack.py b/tests/test_meatpack.py new file mode 100644 index 000000000..a47216521 --- /dev/null +++ b/tests/test_meatpack.py @@ -0,0 +1,344 @@ +"""Test suite for `printrun/meatpack.py` and its use in `printrun/printcore.py`. + +""" +# How to run the tests (requires Python 3.11+): +# python3 -m unittest discover tests + +# Standard libraries: +import collections +import time +import unittest +from unittest import mock + +# 3rd Party/Custom libraries: +import serial +from printrun import meatpack +from printrun import printcore + + +class Firmware: + """The receiving side, following Marlin's feature/meatpack.cpp""" + + def __init__(self): + self.active = False + self.no_spaces = False + self.text = bytearray() + self.cmd_count = 0 + self.cmd_is_next = False + self.full_char_count = 0 + self.second_char = None + + def table(self): + space = b"E" if self.no_spaces else b" " + return b"0123456789." + space + b"\nGX" + + def feed(self, data): + for byte in data: + if byte == 0xFF: + if self.cmd_count: + self.cmd_is_next = True + self.cmd_count = 0 + else: + self.cmd_count = 1 + continue + if self.cmd_is_next: + self.command(byte) + self.cmd_is_next = False + continue + if self.cmd_count: + self.inner(0xFF) + self.cmd_count = 0 + self.inner(byte) + + def command(self, byte): + if byte == 0xFB: + self.active = True + elif byte == 0xFA: + self.active = False + elif byte == 0xF9: + self.active = self.no_spaces = False + self.full_char_count = 0 + self.second_char = None + elif byte == 0xF7: + self.no_spaces = True + elif byte == 0xF6: + self.no_spaces = False + + def inner(self, byte): + if not self.active: + self.text.append(byte) + return + if self.full_char_count: + self.text.append(byte) + if self.second_char is not None: + self.text.append(self.second_char) + self.second_char = None + self.full_char_count -= 1 + return + low, high = byte & 0xF, byte >> 4 + table = self.table() + if low == 0xF: + self.full_char_count = 1 + if high == 0xF: + self.full_char_count = 2 + else: + self.second_char = table[high] + return + self.text.append(table[low]) + if table[low] != ord("\n"): + if high == 0xF: + self.full_char_count = 1 + else: + self.text.append(table[high]) + + +def unpack(*lines): + """Pack lines and return the text the firmware reads from them""" + firmware = Firmware() + firmware.feed(meatpack.ENABLE_NO_SPACES + meatpack.ENABLE_PACKING) + for line in lines: + firmware.feed(meatpack.pack(line)) + return bytes(firmware.text) + + +class TestPack(unittest.TestCase): + """Checks that packed lines come back unchanged""" + + def test_round_trip(self): + """Test even and odd lengths, and characters in and out of the table""" + for line in (b"\n", + b"G\n", + b"M\n", + b"G1\n", + b"M1\n", + b"MS\n", + b"G1X1.5Y2.25E0.012\n", + b"G1X1.5Y2.25E0.01F\n", + b"N12G1X-1.5*33\n", + b"M117 Hello, world!\n", + b"M23 FILE.GCO\n", + b"ab\n", + b"abc\n"): + with self.subTest(line=line): + self.assertEqual(unpack(line), line) + + def test_every_ascii_character(self): + """Test all characters a line can hold, in both positions of a byte""" + chars = bytes(c for c in range(1, 128) if c != ord("\n")) + for line in (chars, b"0" + chars): + self.assertEqual(unpack(line + b"\n"), line + b"\n") + + def test_lines_in_sequence(self): + """Test that lines stay in step with the ones before them""" + lines = (b"G1X1\n", b"M105\n", b"G1X1.5\n", b"M\n", b"G\n", b"T0\n") + self.assertEqual(unpack(*lines), b"".join(lines)) + + def test_no_double_command_byte(self): + """Test that a packed line never looks like the start of a command""" + chars = bytes(c for c in range(1, 128) if c != ord("\n")) + self.assertNotIn(b"\xff\xff", meatpack.pack(chars * 2 + b"\n")) + + def test_size(self): + """Test that a move takes close to half the bytes""" + line = meatpack.strip_spaces("G1 X120.125 Y100.5 E1.25") + "\n" + original = len("G1 X120.125 Y100.5 E1.25\n") + self.assertLessEqual(len(meatpack.pack(line.encode())), original * 0.6) + + def test_strip_spaces(self): + """Test that only moves lose their spaces""" + for command, expected in (("G0 X1", "G0X1"), + ("G1 X1 Y2 E3", "G1X1Y2E3"), + ("G2 X1 I2", "G2X1I2"), + ("G3 X1 J2", "G3X1J2"), + ("G28 X", "G28 X"), + ("M117 G1 X1", "M117 G1 X1"), + ("M23 A B.GCO", "M23 A B.GCO")): + with self.subTest(command=command): + self.assertEqual(meatpack.strip_spaces(command), expected) + + def test_is_emergency(self): + """Test which commands bypass packing""" + for command in ("M108", "M112", "M410", "M876 S1", "N5 M112*33"): + with self.subTest(command=command): + self.assertTrue(meatpack.is_emergency(command)) + for command in ("M1080", "M105", "G1 X1", "M117 M112"): + with self.subTest(command=command): + self.assertFalse(meatpack.is_emergency(command)) + + +class TestPrintcore(unittest.TestCase): + """Checks of the negotiation in printcore against a fake printer""" + + CAPABLE = b"Cap:MEATPACK:1\n" + + def setUp(self): + patcher = mock.patch("printrun.device.Device._disable_ttyhup") + patcher.start() + self.addCleanup(patcher.stop) + self.replies = collections.deque([b"ok\n"]) + self.serial = self.mock_serial() + self.core = printcore.printcore() + self.addCleanup(self.core.disconnect) + self.core.meatpack = True + + def mock_serial(self): + class_mock = mock.create_autospec(serial.Serial) + instance = class_mock.return_value + instance.is_open = True + instance.readline.side_effect = self.read + self.enterContext(mock.patch("serial.Serial", class_mock)) + return instance + + def read(self, *args): + time.sleep(0.002) + return self.replies.popleft() if self.replies else b"" + + def written(self): + return b"".join(c.args[0] for c in self.serial.write.call_args_list) + + def firmware_text(self): + firmware = Firmware() + firmware.feed(self.written()) + return bytes(firmware.text), firmware + + def wait_for(self, condition): + limit = time.monotonic() + 2 + while not condition() and time.monotonic() < limit: + time.sleep(0.002) + self.assertTrue(condition()) + + def connect(self, capable=True): + self.core.connect("/mocked/port", 1000) + self.wait_for(lambda: self.core.online) + self.wait_for(lambda: b"M115\n" in self.written()) + if capable: + self.replies.append(self.CAPABLE) + self.wait_for(lambda: self.core.meatpack_active) + + def test_disabled_sends_plain(self): + """Test that nothing changes on the wire with the setting off""" + self.core.meatpack = False + self.core.connect("/mocked/port", 1000) + self.wait_for(lambda: self.core.online) + self.replies.append(self.CAPABLE) + self.core.send_now("G1 X1") + self.wait_for(lambda: b"G1 X1\n" in self.written()) + self.assertFalse(self.core.meatpack_active) + self.assertNotIn(b"\xff", self.written()) + + def test_firmware_without_meatpack(self): + """Test that lines stay plain if the firmware doesn't report it""" + self.connect(capable=False) + self.replies.append(b"Cap:MEATPACK:0\n") + self.core.send_now("G1 X1") + self.wait_for(lambda: b"G1 X1\n" in self.written()) + self.assertFalse(self.core.meatpack_active) + + def test_packs_after_negotiation(self): + """Test the firmware reads packed commands, moves without spaces""" + self.connect() + self.core.send_now("G1 X1.5 Y2") + self.core.send_now("M117 Two words") + self.wait_for(lambda: self.core.priqueue.empty()) + self.wait_for(lambda: self.firmware_text()[0].endswith(b"words\n")) + self.assertTrue(self.firmware_text()[0].endswith( + b"M115\nG1X1.5Y2\nM117 Two words\n")) + self.assertTrue(self.firmware_text()[1].active) + + def test_checksum_matches_sent_line(self): + """Test the checksum covers the line as the firmware receives it""" + self.connect() + # pylint: disable-next=protected-access + self.core._send("G1 X1 Y2", 7, True) + line = "N7 G1X1Y2" + # pylint: disable-next=protected-access + expected = f"{line}*{self.core._checksum(line)}\n".encode() + self.assertTrue(self.firmware_text()[0].endswith(expected)) + + def test_emergency_is_unpacked(self): + """Test the raw stream holds the emergency command on its own line""" + self.connect() + self.core.send_now("M112") + self.wait_for(lambda: b"\nM112\n" in self.written()) + text, firmware = self.firmware_text() + self.assertTrue(text.endswith(b"\nM112\n")) + self.assertTrue(firmware.active) + + def test_state_callback(self): + """Test the callback follows packing on, restart and disconnect""" + calls = [] + self.core.callback.meatpack = calls.append + self.connect() + self.assertEqual(calls, [True]) + self.replies.append(b"start\n") + self.wait_for(lambda: calls == [True, False]) + self.wait_for(lambda: self.written().endswith(b"M115\n")) + self.replies.append(self.CAPABLE) + self.wait_for(lambda: calls == [True, False, True]) + self.core.disconnect() + self.assertEqual(calls, [True, False, True, False]) + + def test_query_while_plain(self): + """Test a query is sent with a newline to end the line for firmware + without MeatPack""" + self.connect(capable=False) + self.core.meatpack_query() + self.assertTrue(self.written().endswith(meatpack.QUERY_CONFIG + b"\n")) + + def test_query_while_packing(self): + """Test a query leaves later packed lines readable""" + self.connect() + self.core.meatpack_query() + self.assertTrue(self.written().endswith(meatpack.QUERY_CONFIG)) + self.core.send_now("M105") + self.wait_for(lambda: self.firmware_text()[0].endswith(b"\nM105\n")) + + def test_resend_resets_unpacking(self): + """Test a resent line is read correctly after the firmware discarded + part of an earlier line, leaving it waiting for a byte""" + self.connect() + firmware = Firmware() + firmware.feed(meatpack.ENABLE_NO_SPACES + meatpack.ENABLE_PACKING) + firmware.feed(meatpack.pack(b"N5 G1X1*7\n")[:1]) # Its literal N is lost + self.assertEqual(firmware.full_char_count, 1) + self.serial.write.reset_mock() + self.replies.append(b"Resend: 5\n") + self.wait_for(lambda: self.core.meatpack_resync) + self.core.send_now("M105") + self.wait_for(lambda: self.serial.write.called) + firmware.text.clear() + firmware.feed(self.written()) + self.assertEqual(bytes(firmware.text), b"M105\n") + self.assertTrue(firmware.active) + self.assertTrue(firmware.no_spaces) + + def test_firmware_restart(self): + """Test a restart goes back to plain lines and negotiates again""" + self.connect() + self.replies.append(b"start\n") + self.wait_for(lambda: not self.core.meatpack_active) + self.wait_for(lambda: self.written().endswith(b"M115\n")) + self.replies.append(self.CAPABLE) + self.wait_for(lambda: self.core.meatpack_active) + + def test_disconnect_returns_to_plain(self): + """Test the firmware is left reading plain text""" + self.connect() + self.core.disconnect() + firmware = self.firmware_text()[1] + self.assertFalse(firmware.active) + self.assertFalse(firmware.no_spaces) + + def test_connect_clears_earlier_state(self): + """Test a connection recovers firmware left in packed mode""" + firmware = Firmware() + firmware.feed(meatpack.ENABLE_NO_SPACES + meatpack.ENABLE_PACKING) + self.core.connect("/mocked/port", 1000) + self.wait_for(lambda: self.serial.write.called) + firmware.feed(self.serial.write.call_args_list[0].args[0]) + self.assertFalse(firmware.active) + self.assertFalse(firmware.no_spaces) + + +if __name__ == '__main__': + unittest.main()