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
6 changes: 6 additions & 0 deletions printrun/eventhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 40 additions & 0 deletions printrun/gui/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,47 @@
# You should have received a copy of the GNU General Public License
# along with Printrun. If not, see <http://www.gnu.org/licenses/>.

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):
Expand All @@ -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)
Expand Down
88 changes: 88 additions & 0 deletions printrun/meatpack.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

"""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)
Loading