From da8ab4e448172d93bc75734f9abd540dbe3dca37 Mon Sep 17 00:00:00 2001 From: Dejvino Date: Tue, 23 Dec 2025 00:56:47 +0100 Subject: [PATCH] Fix printer config to generate mazes --- jobs/maze.py | 34 +++++++++++++++++++++++++++++----- print_server.py | 9 +++++++-- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/jobs/maze.py b/jobs/maze.py index 9aabb33..3fb752b 100644 --- a/jobs/maze.py +++ b/jobs/maze.py @@ -1,4 +1,5 @@ import random +from PIL import Image, ImageDraw from .base import Job class MazeJob(Job): @@ -9,20 +10,43 @@ class MazeJob(Job): # Width and Height in cells. # Total width in chars = 2 * w + 1. # w=15 -> 31 chars (Fits comfortably on 80mm printers, tight on 58mm) - w = 15 - h = 15 + w = 14 + h = 32 maze = self.generate_maze(w, h) p.text("Najdi cestu z S do E:\n\n") - # Center the maze + # Generate and print image + img = self._generate_image(maze) p.set(align='center') - for row in maze: - p.text("".join(row) + "\n") + p.image(img, impl="bitImageColumn") p.set(align='left') p.text("\n") + def _generate_image(self, maze): + # Cell size in pixels + cell_size = 16 + rows = len(maze) + cols = len(maze[0]) + w, h = cols * cell_size, rows * cell_size + + # Create new 1-bit image (White background) + # '1' mode = 1-bit pixels, black and white, stored with one pixel per byte + img = Image.new('1', (w, h), 1) + draw = ImageDraw.Draw(img) + + for r in range(rows): + for c in range(cols): + # Draw walls as black rectangles + if maze[r][c] == '#': + x = c * cell_size + y = r * cell_size + # fill=0 means Black in '1' mode + draw.rectangle([x, y, x + cell_size, y + cell_size], fill=0) + + return img + def generate_maze(self, width, height): rows = 2 * height + 1 cols = 2 * width + 1 diff --git a/print_server.py b/print_server.py index b9f48f0..149bdc6 100644 --- a/print_server.py +++ b/print_server.py @@ -12,7 +12,7 @@ from jobs.maze import MazeJob # Example: 0x04b8 is Epson. USB_VENDOR_ID = 0x0525 USB_PRODUCT_ID = 0xa700 -INPUT_ENDPOINT = 0x81 +INPUT_ENDPOINT = 0x00 OUTPUT_ENDPOINT = 0x01 # Set to True to print to console instead of physical printer (for testing) @@ -29,7 +29,12 @@ def get_printer(): try: # Initialize USB printer # profile="TM-T88V" is a generic profile, works for many ESC/POS printers - p = Usb(USB_VENDOR_ID, USB_PRODUCT_ID, 0, INPUT_ENDPOINT, OUTPUT_ENDPOINT, profile="TM-T88V") + + p = Usb(USB_VENDOR_ID, USB_PRODUCT_ID, 0, INPUT_ENDPOINT, OUTPUT_ENDPOINT, profile="default") + p.profile.profile_data['media']['width']['mm'] = 80 + p.profile.profile_data['media']['width']['pixels'] = 512 + + # p = Usb(USB_VENDOR_ID, USB_PRODUCT_ID, 0, INPUT_ENDPOINT, OUTPUT_ENDPOINT, profile="TM-T88V") return p except USBNotFoundError: print("Error: Printer not found. Check USB connection and IDs.")