Skip to content
This repository was archived by the owner on May 16, 2023. It is now read-only.

Commit 93a1c7a

Browse files
committed
Rename begin to warm_up and automatically invoke from initializer based on feedback. Warnings given to disable this behavior when explicit initialization is desired.
1 parent b4bfee5 commit 93a1c7a

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

adafruit_thermal_printer/thermal_printer.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ def __set__(self, obj, val):
137137
# pylint: enable=protected-access
138138
# pylint: enable=too-few-public-methods
139139

140-
def __init__(self, uart, byte_delay_s=0.00057346, dot_feed_s=0.0021,
141-
dot_print_s=0.03):
140+
def __init__(self, uart, *, byte_delay_s=0.00057346, dot_feed_s=0.0021,
141+
dot_print_s=0.03, auto_warm_up=True):
142142
"""Thermal printer class. Requires a serial UART connection with at
143143
least the TX pin connected. Take care connecting RX as the printer
144144
will output a 5V signal which can damage boards! If RX is unconnected
@@ -147,7 +147,10 @@ def __init__(self, uart, byte_delay_s=0.00057346, dot_feed_s=0.0021,
147147
and dot_print_s values are delays which are used to prevent overloading
148148
the printer with data. Use the default delays unless you fully
149149
understand the workings of the printer and how delays, baud rate,
150-
number of dots, heat time, etc. relate to each other.
150+
number of dots, heat time, etc. relate to each other. Can set
151+
auto_warm_up to a boolean value (default True) to automatically call
152+
the warm_up function which will initialize the printer (but can take a
153+
significant amount of time, on the order 0.5-5 seconds, be warned!).
151154
"""
152155
self.max_chunk_height = 255
153156
self._resume = 0
@@ -167,6 +170,8 @@ def __init__(self, uart, byte_delay_s=0.00057346, dot_feed_s=0.0021,
167170
self._dot_feed_s = dot_feed_s
168171
self._dot_print_s = dot_print_s
169172
self.reset()
173+
if auto_warm_up:
174+
self.warm_up()
170175

171176
def _set_timeout(self, period_s):
172177
# Set a timeout before future commands can be sent.
@@ -220,10 +225,10 @@ def send_command(self, command):
220225
"""Send a command string to the printer."""
221226
self._uart.write(command)
222227

223-
# Do initialization in begin instead of the initializer because this
228+
# Do initialization in warm_up instead of the initializer because this
224229
# initialization takes a long time (5 seconds) and shouldn't happen during
225230
# object creation (users need explicit control of when to start it).
226-
def begin(self, heat_time=120):
231+
def warm_up(self, heat_time=120):
227232
"""Initialize the printer. Can specify an optional heat_time keyword
228233
to override the default heating timing of 1.2 ms. See the datasheet
229234
for details on the heating time value (duration in 10uS increments).

examples/simpletest.py

+19-21
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
import board
44
import busio
55

6-
# Pick which version thermal printer to import. Only ONE of these lines should
7-
# be uncommented depending on the version of your printer. Hold the button on
8-
# the printer as it's powered on and it will print a test page that displays
9-
# the firmware version, like 2.64, 2.68, etc.
10-
# Use this line for printers with version 2.68 or higher:
11-
import adafruit_thermal_printer.thermal_printer as thermal_printer
12-
# Use this line for printers with version 2.64 up to (but not including) 2.68:
13-
#import adafruit_thermal_printer.thermal_printer_264 as thermal_printer
14-
# Use this line for printers with version up to (but not including) 2.64:
15-
#import adafruit_thermal_printer.thermal_printer_legacy as thermal_printer
6+
import adafruit_thermal_printer
7+
8+
9+
# Pick which version thermal printer class to use depending on the version of
10+
# your printer. Hold the button on the printer as it's powered on and it will
11+
# print a test page that displays the firmware version, like 2.64, 2.68, etc.
12+
# Use this version in the get_printer_class function below.
13+
ThermalPrinter = adafruit_thermal_printer.get_printer_class(2.69)
1614

1715
# Define RX and TX pins for the board's serial port connected to the printer.
1816
# Only the TX pin needs to be configued, and note to take care NOT to connect
@@ -28,12 +26,12 @@
2826
uart = busio.UART(TX, RX, baudrate=19200)
2927

3028
# Create the printer instance.
31-
printer = thermal_printer.ThermalPrinter(uart)
29+
printer = ThermalPrinter(uart, auto_warm_up=False)
3230

3331
# Initialize the printer. Note this will take a few seconds for the printer
3432
# to warm up and be ready to accept commands (hence calling it explicitly vs.
35-
# automatically in the initializer above).
36-
printer.begin()
33+
# automatically in the initializer with the default auto_warm_up=True).
34+
printer.warm_up()
3735

3836
# Check if the printer has paper. This only works if the RX line is connected
3937
# on your board (but BE CAREFUL as mentioned above this RX line is 5V!)
@@ -57,11 +55,11 @@
5755
printer.bold = False
5856

5957
# Print a normal/thin underline line of text:
60-
printer.underline = thermal_printer.UNDERLINE_THIN
58+
printer.underline = adafruit_thermal_printer.UNDERLINE_THIN
6159
printer.print('Thin underline!')
6260

6361
# Print a thick underline line of text:
64-
printer.underline = thermal_printer.UNDERLINE_THICK
62+
printer.underline = adafruit_thermal_printer.UNDERLINE_THICK
6563
printer.print('Thick underline!')
6664

6765
# Disable underlines.
@@ -93,26 +91,26 @@
9391
printer.strike = False
9492

9593
# Print medium size text.
96-
printer.size = thermal_printer.SIZE_MEDIUM
94+
printer.size = adafruit_thermal_printer.SIZE_MEDIUM
9795
printer.print('Medium size text!')
9896

9997
# Print large size text.
100-
printer.size = thermal_printer.SIZE_LARGE
98+
printer.size = adafruit_thermal_printer.SIZE_LARGE
10199
printer.print('Large size text!')
102100

103101
# Back to normal / small size text.
104-
printer.size = thermal_printer.SIZE_SMALL
102+
printer.size = adafruit_thermal_printer.SIZE_SMALL
105103

106104
# Print center justified text.
107-
printer.justify = thermal_printer.JUSTIFY_CENTER
105+
printer.justify = adafruit_thermal_printer.JUSTIFY_CENTER
108106
printer.print('Center justified!')
109107

110108
# Print right justified text.
111-
printer.justify = thermal_printer.JUSTIFY_RIGHT
109+
printer.justify = adafruit_thermal_printer.JUSTIFY_RIGHT
112110
printer.print('Right justified!')
113111

114112
# Back to left justified / normal text.
115-
printer.justify = thermal_printer.JUSTIFY_LEFT
113+
printer.justify = adafruit_thermal_printer.JUSTIFY_LEFT
116114

117115
# Print a UPC barcode.
118116
printer.print('UPCA barcode:')

0 commit comments

Comments
 (0)