thermal_printer_simpletest.py
1 # Simple demo of printer functionality. 2 # Author: Tony DiCola 3 import board 4 import busio 5 6 import adafruit_thermal_printer 7 8 # Pick which version thermal printer class to use depending on the version of 9 # your printer. Hold the button on the printer as it's powered on and it will 10 # print a test page that displays the firmware version, like 2.64, 2.68, etc. 11 # Use this version in the get_printer_class function below. 12 ThermalPrinter = adafruit_thermal_printer.get_printer_class(2.69) 13 14 # Define RX and TX pins for the board's serial port connected to the printer. 15 # Only the TX pin needs to be configued, and note to take care NOT to connect 16 # the RX pin if your board doesn't support 5V inputs. If RX is left unconnected 17 # the only loss in functionality is checking if the printer has paper--all other 18 # functions of the printer will work. 19 RX = board.RX 20 TX = board.TX 21 22 # Create a serial connection for the printer. You must use the same baud rate 23 # as your printer is configured (print a test page by holding the button 24 # during power-up and it will show the baud rate). Most printers use 19200. 25 uart = busio.UART(TX, RX, baudrate=19200) 26 27 # For a computer, use the pyserial library for uart access. 28 # import serial 29 # uart = serial.Serial("/dev/serial0", baudrate=19200, timeout=3000) 30 31 # Create the printer instance. 32 printer = ThermalPrinter(uart, auto_warm_up=False) 33 34 # Initialize the printer. Note this will take a few seconds for the printer 35 # to warm up and be ready to accept commands (hence calling it explicitly vs. 36 # automatically in the initializer with the default auto_warm_up=True). 37 printer.warm_up() 38 39 # Check if the printer has paper. This only works if the RX line is connected 40 # on your board (but BE CAREFUL as mentioned above this RX line is 5V!) 41 if printer.has_paper(): 42 print("Printer has paper!") 43 else: 44 print("Printer might be out of paper, or RX is disconnected!") 45 46 # Print a test page: 47 printer.test_page() 48 49 # Move the paper forward two lines: 50 printer.feed(2) 51 52 # Print a line of text: 53 printer.print("Hello world!") 54 55 # Print a bold line of text: 56 printer.bold = True 57 printer.print("Bold hello world!") 58 printer.bold = False 59 60 # Print a normal/thin underline line of text: 61 printer.underline = adafruit_thermal_printer.UNDERLINE_THIN 62 printer.print("Thin underline!") 63 64 # Print a thick underline line of text: 65 printer.underline = adafruit_thermal_printer.UNDERLINE_THICK 66 printer.print("Thick underline!") 67 68 # Disable underlines. 69 printer.underline = None 70 71 # Print an inverted line. 72 printer.inverse = True 73 printer.print("Inverse hello world!") 74 printer.inverse = False 75 76 # Print an upside down line. 77 printer.upside_down = True 78 printer.print("Upside down hello!") 79 printer.upside_down = False 80 81 # Print a double height line. 82 printer.double_height = True 83 printer.print("Double height!") 84 printer.double_height = False 85 86 # Print a double width line. 87 printer.double_width = True 88 printer.print("Double width!") 89 printer.double_width = False 90 91 # Print a strike-through line. 92 printer.strike = True 93 printer.print("Strike-through hello!") 94 printer.strike = False 95 96 # Print medium size text. 97 printer.size = adafruit_thermal_printer.SIZE_MEDIUM 98 printer.print("Medium size text!") 99 100 # Print large size text. 101 printer.size = adafruit_thermal_printer.SIZE_LARGE 102 printer.print("Large size text!") 103 104 # Back to normal / small size text. 105 printer.size = adafruit_thermal_printer.SIZE_SMALL 106 107 # Print center justified text. 108 printer.justify = adafruit_thermal_printer.JUSTIFY_CENTER 109 printer.print("Center justified!") 110 111 # Print right justified text. 112 printer.justify = adafruit_thermal_printer.JUSTIFY_RIGHT 113 printer.print("Right justified!") 114 115 # Back to left justified / normal text. 116 printer.justify = adafruit_thermal_printer.JUSTIFY_LEFT 117 118 # Print a UPC barcode. 119 printer.print("UPCA barcode:") 120 printer.print_barcode("123456789012", printer.UPC_A) 121 122 # Feed a few lines to see everything. 123 printer.feed(2)