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

Commit cfcb164

Browse files
committed
Remove sleep/wake, causes junk on first print. Use offline/online instead. Update simple demo.
1 parent 3ea17d2 commit cfcb164

File tree

3 files changed

+78
-51
lines changed

3 files changed

+78
-51
lines changed

adafruit_thermal_printer/thermal_printer.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ def _unset_print_mode(self, mask):
208208

209209
def send_command(self, command):
210210
"""Send a command string to the printer."""
211-
for char in command:
212-
self._write_char(char)
211+
self._uart.write(command)
213212

214213
# Do initialization in begin instead of the initializer because this
215214
# initialization takes a long time (5 seconds) and shouldn't happen during
@@ -223,7 +222,6 @@ def begin(self, heat_time=120):
223222
"""
224223
assert 0 <= heat_time <= 255
225224
self._set_timeout(0.5) # Half second delay for printer to initialize.
226-
self.wake()
227225
self.reset()
228226
# ESC 7 n1 n2 n3 Setting Control Parameter Command
229227
# n1 = "max heating dots" 0-255 -- max number of thermal print head
@@ -242,9 +240,8 @@ def begin(self, heat_time=120):
242240
# More heating time = darker print, but slower printing speed and
243241
# possibly paper 'stiction'. More heating interval = clearer print,
244242
# but slower printing speed.
245-
self.send_command('\x1B7') # ESC + '7' (print settings)
246-
# Send heating dots, heat time, heat interval.
247-
self.send_command('\x0B{0}\x28'.format(chr(heat_time)))
243+
# Send ESC + '7' (print settings) + heating dots, heat time, heat interval.
244+
self.send_command('\x1B7\x0B{0}\x28'.format(chr(heat_time)))
248245
# Print density description from manual:
249246
# DC2 # n Set printing density
250247
# D4..D0 of n is used to set the printing density. Density is
@@ -265,15 +262,21 @@ def reset(self):
265262
self._max_column = 32
266263
self._char_height = 24
267264
self._line_spacing = 6
265+
self._barcode_height = 50
266+
# Configure tab stops on recent printers.
267+
# ESC + 'D' + tab stop value list ending with null to terminate.
268+
self.send_command('\x1BD\x04\x08\x10\x14\x18\x1C\x00')
269+
268270

269271
def print(self, text, end='\n'):
270272
"""Print a line of text. Optionally specify the end keyword to
271273
override the new line printed after the text (set to None to disable
272274
the new line entirely).
273275
"""
274-
self.send_command(text)
276+
for char in text:
277+
self._write_char(char)
275278
if end is not None:
276-
self.send_command(end)
279+
self._write_char(end)
277280

278281
def print_barcode(self, text, barcode_type):
279282
"""Print a barcode with the specified text/number (the meaning
@@ -451,24 +454,6 @@ def online(self):
451454
"""
452455
self.send_command('\x1B=\x01') # ESC + '=' + 1
453456

454-
def sleep_after(self, seconds):
455-
"""Put the printer into sleep mode after the specified number of
456-
seconds (0-65535). Must call wake to wake up and send commands
457-
afterwards!
458-
"""
459-
assert 0 <= seconds <= 65535
460-
low_byte = seconds & 0xFF
461-
high_byte = (seconds >> 8) & 0xFF
462-
self.send_command('\x1B8{0}{1}'.format(chr(low_byte), chr(high_byte)))
463-
464-
def wake(self):
465-
"""Wake the thermal printer into an online state ready to receive
466-
commands.
467-
"""
468-
self.send_command('\xFF') # Wake command.
469-
time.sleep(0.050)
470-
self.send_command('\x1B8\x00\x00') # Sleep off
471-
472457
def has_paper(self):
473458
"""Return a boolean indicating if the printer has paper. You MUST have
474459
the serial RX line hooked up for this to work. NOTE: be VERY CAREFUL

adafruit_thermal_printer/thermal_printer_legacy.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,6 @@ def print_barcode(self, text, barcode_type):
108108
self._set_timeout((self._barcode_height + 40) * self._dot_print_s)
109109
self._column = 0
110110

111-
def wake(self):
112-
"""Wake the thermal printer into an online state ready to receive
113-
commands.
114-
"""
115-
self.send_command('\xFF') # Wake command.
116-
# Datasheet recommends a 50 mS delay before issuing further commands,
117-
# but in practice this alone isn't sufficient (e.g. text size/style
118-
# commands may still be misinterpreted on wake). A slightly longer
119-
# delay, interspersed with NUL chars (no-ops) seems to help.
120-
for _ in range(10):
121-
self.send_command('\x00')
122-
self._set_timeout(0.01)
123-
124-
def sleep_after(self, seconds):
125-
"""Put the printer into sleep mode after the specified number of
126-
seconds (0-255). Must call wake to wake up and send commands
127-
afterwards!
128-
"""
129-
# Firmware before 2.64 uses an 8-bit number of seconds instead of 16-bit.
130-
assert 0 <= seconds <= 255
131-
self.send_command('\x1B8{0}'.format(chr(seconds)))
132-
133111
def reset(self):
134112
"""Reset the printer."""
135113
# Issue a reset command to the printer. (ESC + @)
@@ -139,6 +117,7 @@ def reset(self):
139117
self._max_column = 32
140118
self._char_height = 24
141119
self._line_spacing = 6
120+
self._barcode_height = 50
142121
# Skip tab configuration on older printers.
143122

144123
def feed(self, lines):

examples/simpletest.py

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
# Create a serial connection for the printer. You must use the same baud rate
2626
# 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 119200.
28-
uart = busio.UART(TX, RX, baudrate=119200)
27+
# during power-up and it will show the baud rate). Most printers use 19200.
28+
uart = busio.UART(TX, RX, baudrate=19200)
2929

3030
# Create the printer instance.
3131
printer = thermal_printer.ThermalPrinter(uart)
@@ -35,6 +35,13 @@
3535
# automatically in the initializer above).
3636
printer.begin()
3737

38+
# Check if the printer has paper. This only works if the RX line is connected
39+
# on your board (but BE CAREFUL as mentioned above this RX line is 5V!)
40+
if printer.has_paper():
41+
print('Printer has paper!')
42+
else:
43+
print('Printer might be out of paper, or RX is disconnected!')
44+
3845
# Print a test page:
3946
printer.test_page()
4047

@@ -44,5 +51,61 @@
4451
# Print a line of text:
4552
printer.print('Hello world!')
4653

47-
# Move the paper forward two lines:
54+
# Print a bold line of text:
55+
printer.bold = True
56+
printer.print('Bold hello world!')
57+
printer.bold = False
58+
59+
# Print a normal/thin underline line of text:
60+
printer.underline_thin()
61+
printer.print('Thin underline!')
62+
63+
# Print a thick underline line of text:
64+
printer.underline_thick()
65+
printer.print('Thick underline!')
66+
67+
# Disable underlines.
68+
printer.underline_off()
69+
70+
# Print an inverted line.
71+
printer.inverse = True
72+
printer.print('Inverse hello world!')
73+
printer.inverse = False
74+
75+
# Print an upside down line.
76+
printer.upside_down = True
77+
printer.print('Upside down hello!')
78+
printer.upside_down = False
79+
80+
# Print a double height line.
81+
printer.double_height = True
82+
printer.print('Double height!')
83+
printer.double_height = False
84+
85+
# Print a double width line.
86+
printer.double_width = True
87+
printer.print('Double width!')
88+
printer.double_width = False
89+
90+
# Print a strike-through line.
91+
printer.strike = True
92+
printer.print('Strike-through hello!')
93+
printer.strike = False
94+
95+
# Print medium size text.
96+
printer.set_size_medium()
97+
printer.print('Medium size text!')
98+
99+
# Print large size text.
100+
printer.set_size_large()
101+
printer.print('Large size text!')
102+
103+
# Back to normal / small size text.
104+
printer.set_size_small()
105+
106+
# Print a UPC barcode.
107+
printer.print('UPCA barcode:')
108+
printer.print_barcode('123456789012', printer.UPC_A)
109+
110+
# Feed a few lines to see everything.
48111
printer.feed(2)

0 commit comments

Comments
 (0)