Simple test

Ensure your device works with this simple test.

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