-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
36 lines (26 loc) · 1010 Bytes
/
Copy pathbackend.py
File metadata and controls
36 lines (26 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Send a rendered receipt image to an ESC/POS printer over USB."""
import logging
import threading
from PIL import Image
import config
logger = logging.getLogger(__name__)
# One USB device, one job at a time. The bot prints from worker threads
# (asyncio.to_thread), so two messages can land here at once — serialize them
# or they fight over the same device.
_print_lock = threading.Lock()
def print_image(img: Image.Image) -> None:
"""Open the USB printer, print one bitmap, feed and cut."""
if config.USB_VID is None or config.USB_PID is None:
raise RuntimeError(
"Printer not configured: set PRINTER_USB_VID and PRINTER_USB_PID "
"(find them with `lsusb`)."
)
from escpos.printer import Usb
with _print_lock:
printer = Usb(config.USB_VID, config.USB_PID)
try:
printer.image(img)
printer.cut()
finally:
printer.close()
logger.info(f"Printed image {img.width}x{img.height}")