Files
Gracious 90137dd111 Add model table, PrinterError, and port upstream PRs #28/#12
Library changes on top of upstream 35c41a7:

- models.py: MODELS table (max width / density / needs_print_clear per
  model) and prepare_print(), replacing the duplicated width/density
  logic in the CLI. Adds d101 (192px head, tested on 12mm tape).
- _transceive raises PrinterError instead of returning None (callers
  used to crash with AttributeError on a silent printer).
- PR #28: send ALLOW_PRINT_CLEAR / SET_QUANTITY for D-series models
  (labels taller than ~210px failed on D11 without them) and pause 1ms
  between line packets.
- PR #12: replace the fixed sleep after END_PAGE_PRINT with
  get_print_status() polling (30s deadline, fallback for firmwares that
  do not answer). get_print_status returns PrintStatus(finished,
  progress, feed_progress, error).
- set_dimension parameter names match their use.

Verified on a D101 over Bluetooth.
2026-09-21 08:02:58 +01:00

90 lines
2.0 KiB
Python

import logging
import re
import click
from PIL import Image
from niimprint import (
MODELS,
BluetoothTransport,
PrinterClient,
SerialTransport,
prepare_print,
)
@click.command("print")
@click.option(
"-m",
"--model",
type=click.Choice(list(MODELS), False),
default="b21",
show_default=True,
help="Niimbot printer model",
)
@click.option(
"-c",
"--conn",
type=click.Choice(["usb", "bluetooth"]),
default="usb",
show_default=True,
help="Connection type",
)
@click.option(
"-a",
"--addr",
help="Bluetooth MAC address OR serial device path",
)
@click.option(
"-d",
"--density",
type=click.IntRange(1, 5),
default=5,
show_default=True,
help="Print density",
)
@click.option(
"-r",
"--rotate",
type=click.Choice(["0", "90", "180", "270"]),
default="0",
show_default=True,
help="Image rotation (clockwise)",
)
@click.option(
"-i",
"--image",
type=click.Path(exists=True),
required=True,
help="Image path",
)
@click.option(
"-v",
"--verbose",
is_flag=True,
help="Enable verbose logging",
)
def print_cmd(model, conn, addr, density, rotate, image, verbose):
logging.basicConfig(
level="DEBUG" if verbose else "INFO",
format="%(levelname)s | %(module)s:%(funcName)s:%(lineno)d - %(message)s",
)
if conn == "bluetooth":
assert addr is not None, "--addr argument required for bluetooth connection"
addr = addr.upper()
assert re.fullmatch(r"([0-9A-F]{2}:){5}([0-9A-F]{2})", addr), "Bad MAC address"
transport = BluetoothTransport(addr)
if conn == "usb":
port = addr if addr is not None else "auto"
transport = SerialTransport(port=port)
image, density = prepare_print(model, Image.open(image), density, int(rotate))
printer = PrinterClient(transport)
printer.print_image(image, density=density, model=model)
if __name__ == "__main__":
print_cmd()