Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for finish to send end_print #12

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion niimprint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def print_cmd(model, conn, addr, density, rotate, image, verbose):
assert image.width <= max_width_px, f"Image width too big for {model.upper()}"

printer = PrinterClient(transport)
printer.print_image(image, density=density)
printer.print_image(image, model, density=density)


if __name__ == "__main__":
Expand Down
31 changes: 25 additions & 6 deletions niimprint/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import time

import serial
from dataclasses import dataclass
from PIL import Image, ImageOps
from serial.tools.list_ports import comports as list_comports

Expand Down Expand Up @@ -42,6 +43,13 @@ class RequestCodeEnum(enum.IntEnum):
GET_PRINT_STATUS = 163 # 0xA3


@dataclass
class PrintStatus:
finished: bool
progress: int
error: bool


def _packet_to_int(x):
return int.from_bytes(x.data, "big")

Expand Down Expand Up @@ -100,18 +108,27 @@ def __init__(self, transport):
self._transport = transport
self._packetbuf = bytearray()

def print_image(self, image: Image, density: int = 3):
def print_image(self, image: Image, model: str, density: int = 3):
self.set_label_density(density)
self.set_label_type(1)
self.start_print()
# self.allow_print_clear() # Something unsupported in protocol decoding (B21)
if model != "b21":
self.allow_print_clear() # Something unsupported in protocol decoding (B21) but needed for D11
self.start_page_print()
self.set_dimension(image.height, image.width)
# self.set_quantity(1) # Same thing (B21)
if model != "b21":
self.set_quantity(1) # Same thing (B21)
for pkt in self._encode_image(image):
self._send(pkt)
self.end_page_print()
time.sleep(0.3) # FIXME: Check get_print_status()
while True:
status = self.get_print_status()
if status.error:
raise RuntimeError("Failure during printing")
if status.finished:
break
logging.info(f"Printing.. {status.progress}%")

while not self.end_print():
time.sleep(0.1)

Expand Down Expand Up @@ -284,5 +301,7 @@ def set_quantity(self, n):

def get_print_status(self):
packet = self._transceive(RequestCodeEnum.GET_PRINT_STATUS, b"\x01", 16)
page, progress1, progress2 = struct.unpack(">HBB", packet.data)
return {"page": page, "progress1": progress1, "progress2": progress2}
finished = bool(packet.data[1])
progress = packet.data[2]
error = bool(packet.data[6])
return PrintStatus(finished, progress, error)