Initial version: TUI and math homework

This commit is contained in:
Dejvino 2025-12-23 00:00:27 +01:00
commit 61518315fd

105
print_server.py Normal file
View File

@ -0,0 +1,105 @@
import random
from datetime import datetime
from escpos.printer import Usb, Dummy
from escpos.exceptions import USBNotFoundError
# ==========================================
# CONFIGURATION
# ==========================================
# Replace these with your specific printer's values found via lsusb or Device Manager
# Example: 0x04b8 is Epson.
USB_VENDOR_ID = 0x0525
USB_PRODUCT_ID = 0xa700
INPUT_ENDPOINT = 0x81
OUTPUT_ENDPOINT = 0x01
# Set to True to print to console instead of physical printer (for testing)
USE_DUMMY_PRINTER = False
def get_printer():
"""
Initializes the printer connection.
Returns a printer object.
"""
if USE_DUMMY_PRINTER:
return Dummy()
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")
return p
except USBNotFoundError:
print("Error: Printer not found. Check USB connection and IDs.")
return None
except Exception as e:
print(f"Error initializing printer: {e}")
return None
def generate_homework_job(p):
"""
Sends ESC/POS commands to the printer to print the homework.
"""
# 1. Header (Center aligned, Double Height/Width)
p.set(align='center', height=2, width=2, bold=True)
p.text("MALA NASOBILKA\n")
p.text("-------------\n")
# Reset text formatting for body
p.set(align='left', height=1, width=1, bold=False)
today = datetime.now().strftime("%d.%m.%Y")
p.text(f"Datum: {today}\n\n")
# 2. Generate and Print Equations
p.text("Vypocitej:\n\n")
for i in range(1, 11):
num1 = random.randint(2, 12)
num2 = random.randint(2, 12)
# Format: "1) 5 x 4 = ____"
line = f"{i}) {num1} * {num2} = ____\n"
p.text(line)
p.text("\n") # Extra spacing for writing
p.text("Hodne stesti!\n")
# 4. Cut Paper
p.cut()
# If using Dummy, print the output to console to verify
if isinstance(p, Dummy):
print(p.output.decode('utf-8', errors='ignore'))
def run_tui():
print("==========================================")
print(" THERMAL PRINTER CONTROL CENTER ")
print("==========================================")
while True:
print("\nAvailable Jobs:")
print(" [1] Print Math Homework (Multiplication)")
print(" [q] Quit")
choice = input("\nSelect an option: ").strip().lower()
if choice == '1':
p = get_printer()
if p:
print("Printing...")
try:
generate_homework_job(p)
print("Success! Job sent to printer.")
except Exception as e:
print(f"Print Error: {e}")
finally:
if not isinstance(p, Dummy):
p.close()
elif choice == 'q':
print("Exiting...")
break
else:
print("Invalid selection. Please try again.")
if __name__ == '__main__':
run_tui()