from escpos.printer import Usb, Dummy from escpos.exceptions import USBNotFoundError from jobs.math_homework import MathHomeworkJob from jobs.unit_conversion import UnitConversionJob from jobs.chess_puzzle import ChessPuzzleJob from jobs.maze import MazeJob # ========================================== # 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 = 0x00 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="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.") return None except Exception as e: print(f"Error initializing printer: {e}") return None JOBS = [ MathHomeworkJob(), UnitConversionJob(), ChessPuzzleJob(), MazeJob() ] def run_tui(): print("==========================================") print(" THERMAL PRINTER CONTROL CENTER ") print("==========================================") while True: print("\nAvailable Jobs:") for i, job in enumerate(JOBS): print(f" [{i + 1}] {job.get_name()}") print(" [q] Quit") choice = input("\nSelect an option: ").strip().lower() if choice == 'q': print("Exiting...") break try: idx = int(choice) - 1 if 0 <= idx < len(JOBS): job = JOBS[idx] else: print("Invalid selection.") continue except ValueError: print("Invalid selection.") continue if job: p = get_printer() if p: print(f"Printing {job.get_name()}...") try: job.run(p) # If using Dummy, print the output to console to verify if isinstance(p, Dummy): print(p.output.decode('utf-8', errors='ignore')) print("Success! Job sent to printer.") except Exception as e: print(f"Print Error: {e}") finally: if not isinstance(p, Dummy): p.close() if __name__ == '__main__': run_tui()