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

Commit a3d9c90

Browse files
committed
Change functions to write-only properties based on feedback and acceptance of increased memory usage for new global constants.
1 parent 6330565 commit a3d9c90

File tree

2 files changed

+71
-50
lines changed

2 files changed

+71
-50
lines changed

adafruit_thermal_printer/thermal_printer.py

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
_DOUBLE_HEIGHT_MASK = const(1 << 4)
5050
_DOUBLE_WIDTH_MASK = const(1 << 5)
5151
_STRIKE_MASK = const(1 << 6)
52+
53+
# External constants:
54+
JUSTIFY_LEFT = const(0)
55+
JUSTIFY_CENTER = const(1)
56+
JUSTIFY_RIGHT = const(2)
57+
SIZE_SMALL = const(0)
58+
SIZE_MEDIUM = const(1)
59+
SIZE_LARGE = const(2)
60+
UNDERLINE_THIN = const(0)
61+
UNDERLINE_THICK = const(1)
5262
# pylint: enable=bad-whitespace
5363

5464

@@ -344,9 +354,9 @@ def set_defaults(self):
344354
to a good state after printing different size, weight, etc. text.
345355
"""
346356
self.online()
347-
self.justify_left()
348-
self.set_size_small()
349-
self.underline_off()
357+
self.justify = JUSTIFY_LEFT
358+
self.size = SIZE_SMALL
359+
self.underline = None
350360
self.inverse = False
351361
self.upside_down = False
352362
self.double_height = False
@@ -358,50 +368,61 @@ def set_defaults(self):
358368
self._set_charset()
359369
self._set_code_page()
360370

361-
def justify_left(self):
362-
"""Set left justification of text."""
363-
self.send_command('\x1Ba\x00') # ESC + 'a' + 0
364-
365-
def justify_center(self):
366-
"""Set center justification of text."""
367-
self.send_command('\x1Ba\x01') # ESC + 'a' + 1
368-
369-
def justify_right(self):
370-
"""Set right justification of text."""
371-
self.send_command('\x1Ba\x02') # ESC + 'a' + 2
371+
def _set_justify(self, val):
372+
assert 0 <= val <= 2
373+
if val == JUSTIFY_LEFT:
374+
self.send_command('\x1Ba\x00') # ESC + 'a' + 0
375+
elif val == JUSTIFY_CENTER:
376+
self.send_command('\x1Ba\x01') # ESC + 'a' + 1
377+
elif val == JUSTIFY_RIGHT:
378+
self.send_command('\x1Ba\x02') # ESC + 'a' + 2
372379

373-
def set_size_small(self):
374-
"""Set small text size."""
375-
self._char_height = 24
376-
self._max_column = 32
377-
self.send_command('\x1D!\x00') # ASCII GS + '!' + 0x00
378-
self._column = 0
379-
380-
def set_size_medium(self):
381-
"""Set medium (double height) text size."""
382-
self._char_height = 48
383-
self._max_column = 32
384-
self.send_command('\x1D!\x01') # ASCII GS + '!' + 0x01
385-
self._column = 0
380+
# pylint: disable=line-too-long
381+
# Write-only property, can't assume we can read state from the printer
382+
# since there is no command for it and hooking up RX is discouraged
383+
# (5V will damage many boards).
384+
justify = property(None, _set_justify, None, "Set the justification of text, must be a value of JUSTIFY_LEFT, JUSTIFY_CENTER, or JUSTIFY_RIGHT.")
385+
# pylint: enable=line-too-long
386386

387-
def set_size_large(self):
388-
"""Set large (double height & width) text size."""
389-
self._char_height = 48
390-
self._max_column = 16
391-
self.send_command('\x1D!\x11') # ASCII GS + '!' + 0x11
387+
def _set_size(self, val):
388+
assert 0 <= val <= 2
389+
if val == SIZE_SMALL:
390+
self._char_height = 24
391+
self._max_column = 32
392+
self.send_command('\x1D!\x00') # ASCII GS + '!' + 0x00
393+
elif val == SIZE_MEDIUM:
394+
self._char_height = 48
395+
self._max_column = 32
396+
self.send_command('\x1D!\x01') # ASCII GS + '!' + 0x01
397+
elif val == SIZE_LARGE:
398+
self._char_height = 48
399+
self._max_column = 16
400+
self.send_command('\x1D!\x11') # ASCII GS + '!' + 0x11
392401
self._column = 0
393402

394-
def underline_off(self):
395-
"""Turn off underline printing."""
396-
self.send_command('\x1B-\x00') # ESC + '-' + 0
403+
# pylint: disable=line-too-long
404+
# Write-only property, can't assume we can read state from the printer
405+
# since there is no command for it and hooking up RX is discouraged
406+
# (5V will damage many boards).
407+
size = property(None, _set_size, None, "Set the size of text, must be a value of SIZE_SMALL, SIZE_MEDIUM, or SIZE_LARGE.")
408+
# pylint: enable=line-too-long
397409

398-
def underline_thin(self):
399-
"""Turn on normal/thin underline printing."""
400-
self.send_command('\x1B-\x01') # ESC + '-' + 1
410+
def _set_underline(self, val):
411+
assert val is None or (0 <= val <= 1)
412+
if val is None:
413+
# Turn off underline.
414+
self.send_command('\x1B-\x00') # ESC + '-' + 0
415+
elif val == UNDERLINE_THIN:
416+
self.send_command('\x1B-\x01') # ESC + '-' + 1
417+
elif val == UNDERLINE_THICK:
418+
self.send_command('\x1B-\x02') # ESC + '-' + 2
401419

402-
def underline_thick(self):
403-
"""Turn on thick underline printing."""
404-
self.send_command('\x1B-\x02') # ESC + '-' + 2
420+
# pylint: disable=line-too-long
421+
# Write-only property, can't assume we can read state from the printer
422+
# since there is no command for it and hooking up RX is discouraged
423+
# (5V will damage many boards).
424+
underline = property(None, _set_underline, None, "Set the underline state of the text, must be None (off), UNDERLINE_THIN, or UNDERLINE_THICK.")
425+
# pylint: enable=line-too-long
405426

406427
def _set_inverse(self, inverse):
407428
# Set the inverse printing state to enabled disabled with the specified

examples/simpletest.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@
5757
printer.bold = False
5858

5959
# Print a normal/thin underline line of text:
60-
printer.underline_thin()
60+
printer.underline = thermal_printer.UNDERLINE_THIN
6161
printer.print('Thin underline!')
6262

6363
# Print a thick underline line of text:
64-
printer.underline_thick()
64+
printer.underline = thermal_printer.UNDERLINE_THICK
6565
printer.print('Thick underline!')
6666

6767
# Disable underlines.
68-
printer.underline_off()
68+
printer.underline = None
6969

7070
# Print an inverted line.
7171
printer.inverse = True
@@ -93,26 +93,26 @@
9393
printer.strike = False
9494

9595
# Print medium size text.
96-
printer.set_size_medium()
96+
printer.size = thermal_printer.SIZE_MEDIUM
9797
printer.print('Medium size text!')
9898

9999
# Print large size text.
100-
printer.set_size_large()
100+
printer.size = thermal_printer.SIZE_LARGE
101101
printer.print('Large size text!')
102102

103103
# Back to normal / small size text.
104-
printer.set_size_small()
104+
printer.size = thermal_printer.SIZE_SMALL
105105

106106
# Print center justified text.
107-
printer.justify_center()
107+
printer.justify = thermal_printer.JUSTIFY_CENTER
108108
printer.print('Center justified!')
109109

110110
# Print right justified text.
111-
printer.justify_right()
111+
printer.justify = thermal_printer.JUSTIFY_RIGHT
112112
printer.print('Right justified!')
113113

114114
# Back to left justified / normal text.
115-
printer.justify_left()
115+
printer.justify = thermal_printer.JUSTIFY_LEFT
116116

117117
# Print a UPC barcode.
118118
printer.print('UPCA barcode:')

0 commit comments

Comments
 (0)