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

Commit db73533

Browse files
authored
Merge pull request #28 from tcfranks/main
Add Missing Type Annotations
2 parents 5256986 + 7374e79 commit db73533

File tree

5 files changed

+102
-68
lines changed

5 files changed

+102
-68
lines changed

adafruit_thermal_printer/thermal_printer.py

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535

3636
from micropython import const
3737

38+
try:
39+
from typing import Optional, Type
40+
from typing_extensions import Literal
41+
from circuitpython_typing import ReadableBuffer
42+
from busio import UART
43+
except ImportError:
44+
pass
3845

3946
__version__ = "0.0.0+auto.0"
4047
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Thermal_Printer.git"
@@ -117,13 +124,17 @@ class _PrintModeBit:
117124
# which by design only implements get, set, init. As a result workaround
118125
# this pylint issue by disabling the warning.
119126
# pylint: disable=too-few-public-methods
120-
def __init__(self, mask):
127+
def __init__(self, mask: int) -> None:
121128
self._mask = mask
122129

123-
def __get__(self, obj, objtype):
130+
def __get__(
131+
self,
132+
obj: Optional["ThermalPrinter"],
133+
objtype: Type["ThermalPrinter"],
134+
) -> bool:
124135
return obj._print_mode & self._mask > 0
125136

126-
def __set__(self, obj, val):
137+
def __set__(self, obj: "ThermalPrinter", val: int) -> None:
127138
if val:
128139
obj._set_print_mode(self._mask)
129140
else:
@@ -134,13 +145,13 @@ def __set__(self, obj, val):
134145

135146
def __init__(
136147
self,
137-
uart,
148+
uart: UART,
138149
*,
139-
byte_delay_s=0.00057346,
140-
dot_feed_s=0.0021,
141-
dot_print_s=0.03,
142-
auto_warm_up=True
143-
):
150+
byte_delay_s: float = 0.00057346,
151+
dot_feed_s: float = 0.0021,
152+
dot_print_s: float = 0.03,
153+
auto_warm_up: bool = True,
154+
) -> None:
144155
"""Thermal printer class. Requires a serial UART connection with at
145156
least the TX pin connected. Take care connecting RX as the printer
146157
will output a 5V signal which can damage boards! If RX is unconnected
@@ -176,16 +187,16 @@ def __init__(
176187
if auto_warm_up:
177188
self.warm_up()
178189

179-
def _set_timeout(self, period_s):
190+
def _set_timeout(self, period_s: float) -> None:
180191
# Set a timeout before future commands can be sent.
181192
self._resume = time.monotonic() + period_s
182193

183-
def _wait_timeout(self):
194+
def _wait_timeout(self) -> None:
184195
# Ensure the timeout that was previously set has passed (will busy wait).
185196
while time.monotonic() < self._resume:
186197
pass
187198

188-
def _write_char(self, char):
199+
def _write_char(self, char: str) -> None:
189200
# Write a single character to the printer.
190201
if char == "\r":
191202
return # Strip carriage returns by skipping them.
@@ -207,33 +218,33 @@ def _write_char(self, char):
207218
self._column += 1
208219
self._set_timeout(delay)
209220

210-
def _write_print_mode(self):
221+
def _write_print_mode(self) -> None:
211222
# Write the printer mode to the printer.
212223
self.send_command(
213-
"\x1B!{0}".format(chr(self._print_mode))
224+
f"\x1B!{chr(self._print_mode)}"
214225
) # ESC + '!' + print mode byte
215226
# Adjust character height and column count based on print mode.
216227
self._char_height = 48 if self._print_mode & _DOUBLE_HEIGHT_MASK else 24
217228
self._max_column = 16 if self._print_mode & _DOUBLE_WIDTH_MASK else 32
218229

219-
def _set_print_mode(self, mask):
230+
def _set_print_mode(self, mask: int) -> None:
220231
# Enable the specified bits of the print mode.
221232
self._print_mode |= mask & 0xFF
222233
self._write_print_mode()
223234

224-
def _unset_print_mode(self, mask):
235+
def _unset_print_mode(self, mask: int) -> None:
225236
# Disable the specified bits of the print mode.
226237
self._print_mode &= ~(mask & 0xFF)
227238
self._write_print_mode()
228239

229-
def send_command(self, command):
240+
def send_command(self, command: str) -> None:
230241
"""Send a command string to the printer."""
231242
self._uart.write(bytes(command, "ascii"))
232243

233244
# Do initialization in warm_up instead of the initializer because this
234245
# initialization takes a long time (5 seconds) and shouldn't happen during
235246
# object creation (users need explicit control of when to start it).
236-
def warm_up(self, heat_time=120):
247+
def warm_up(self, heat_time: int = 120) -> None:
237248
"""Initialize the printer. Can specify an optional heat_time keyword
238249
to override the default heating timing of 1.2 ms. See the datasheet
239250
for details on the heating time value (duration in 10uS increments).
@@ -261,7 +272,7 @@ def warm_up(self, heat_time=120):
261272
# possibly paper 'stiction'. More heating interval = clearer print,
262273
# but slower printing speed.
263274
# Send ESC + '7' (print settings) + heating dots, heat time, heat interval.
264-
self.send_command("\x1B7\x0B{0}\x28".format(chr(heat_time)))
275+
self.send_command(f"\x1B7\x0B{chr(heat_time)}\x28")
265276
# Print density description from manual:
266277
# DC2 # n Set printing density
267278
# D4..D0 of n is used to set the printing density. Density is
@@ -271,9 +282,9 @@ def warm_up(self, heat_time=120):
271282
print_density = 10 # 100% (? can go higher, text is darker but fuzzy)
272283
print_break_time = 2 # 500 uS
273284
dc2_value = (print_break_time << 5) | print_density
274-
self.send_command("\x12#{0}".format(chr(dc2_value))) # DC2 + '#' + value
285+
self.send_command(f"\x12#{chr(dc2_value)}") # DC2 + '#' + value
275286

276-
def reset(self):
287+
def reset(self) -> None:
277288
"""Reset the printer."""
278289
# Issue a reset command to the printer. (ESC + @)
279290
self.send_command("\x1B@")
@@ -287,7 +298,7 @@ def reset(self):
287298
# ESC + 'D' + tab stop value list ending with null to terminate.
288299
self.send_command("\x1BD\x04\x08\x10\x14\x18\x1C\x00")
289300

290-
def print(self, text, end="\n"):
301+
def print(self, text: str, end: Optional[str] = "\n") -> None:
291302
"""Print a line of text. Optionally specify the end keyword to
292303
override the new line printed after the text (set to None to disable
293304
the new line entirely).
@@ -297,7 +308,7 @@ def print(self, text, end="\n"):
297308
if end is not None:
298309
self._write_char(end)
299310

300-
def print_barcode(self, text, barcode_type):
311+
def print_barcode(self, text: str, barcode_type: int) -> None:
301312
"""Print a barcode with the specified text/number (the meaning
302313
varies based on the type of barcode) and type. Type is a value from
303314
the datasheet or class-level variables like UPC_A, etc. for
@@ -309,14 +320,14 @@ def print_barcode(self, text, barcode_type):
309320
self.feed(1) # Recent firmware can't print barcode w/o feed first???
310321
self.send_command("\x1DH\x02") # Print label below barcode
311322
self.send_command("\x1Dw\x03") # Barcode width 3 (0.375/1.0mm thin/thick)
312-
self.send_command("\x1Dk{0}".format(chr(barcode_type))) # Barcode type
323+
self.send_command(f"\x1Dk{chr(barcode_type)}") # Barcode type
313324
# Write length and then string (note this only works with 2.64+).
314325
self.send_command(chr(len(text)))
315326
self.send_command(text)
316327
self._set_timeout((self._barcode_height + 40) * self._dot_print_s)
317328
self._column = 0
318329

319-
def _print_bitmap(self, width, height, data):
330+
def _print_bitmap(self, width: int, height: int, data: ReadableBuffer) -> None:
320331
"""Print a bitmap image of the specified width, height and data bytes.
321332
Data bytes must be in 1-bit per pixel format, i.e. each byte represents
322333
8 pixels of image data along a row of the image. You will want to
@@ -338,9 +349,7 @@ def _print_bitmap(self, width, height, data):
338349
for row_start in range(0, height, chunk_height_limit):
339350
# Issue up to chunkHeightLimit rows at a time.
340351
chunk_height = min(height - row_start, chunk_height_limit)
341-
self.send_command(
342-
"\x12*{0}{1}".format(chr(chunk_height), chr(row_bytes_clipped))
343-
)
352+
self.send_command(f"\x12*{chr(chunk_height)}{chr(row_bytes_clipped)}")
344353
for _ in range(chunk_height):
345354
for _ in range(row_bytes_clipped):
346355
# Drop down to low level UART access to avoid newline and
@@ -352,7 +361,7 @@ def _print_bitmap(self, width, height, data):
352361
self._set_timeout(chunk_height * self._dot_print_s)
353362
self._column = 0
354363

355-
def test_page(self):
364+
def test_page(self) -> None:
356365
"""Print a test page."""
357366
self.send_command("\x12T") # DC2 + 'T' for test page
358367
# Delay for 26 lines w/text (ea. 24 dots high) +
@@ -361,7 +370,7 @@ def test_page(self):
361370
self._dot_print_s * 24 * 26 + self._dot_feed_s * (6 * 26 + 30)
362371
)
363372

364-
def set_defaults(self):
373+
def set_defaults(self) -> None:
365374
"""Set default printing and text options. This is useful to reset back
366375
to a good state after printing different size, weight, etc. text.
367376
"""
@@ -383,7 +392,7 @@ def set_defaults(self):
383392
self._set_charset()
384393
self._set_code_page()
385394

386-
def _set_justify(self, val):
395+
def _set_justify(self, val: Literal[0, 1, 2]) -> None:
387396
assert 0 <= val <= 2
388397
if val == JUSTIFY_LEFT:
389398
self.send_command("\x1Ba\x00") # ESC + 'a' + 0
@@ -404,7 +413,7 @@ def _set_justify(self, val):
404413
)
405414
# pylint: enable=line-too-long
406415

407-
def _set_size(self, val):
416+
def _set_size(self, val: Literal[0, 1, 2]) -> None:
408417
assert 0 <= val <= 2
409418
if val == SIZE_SMALL:
410419
self._char_height = 24
@@ -432,7 +441,7 @@ def _set_size(self, val):
432441
)
433442
# pylint: enable=line-too-long
434443

435-
def _set_underline(self, val):
444+
def _set_underline(self, val: Optional[Literal[0, 1]]) -> None:
436445
assert val is None or (0 <= val <= 1)
437446
if val is None:
438447
# Turn off underline.
@@ -454,7 +463,7 @@ def _set_underline(self, val):
454463
)
455464
# pylint: enable=line-too-long
456465

457-
def _set_inverse(self, inverse):
466+
def _set_inverse(self, inverse: bool) -> None:
458467
# Set the inverse printing state to enabled disabled with the specified
459468
# boolean value. This requires printer firmare 2.68+
460469
if inverse:
@@ -474,7 +483,7 @@ def _set_inverse(self, inverse):
474483
)
475484
# pylint: enable=line-too-long
476485

477-
def _set_up_down_mode(self, up_down_mode):
486+
def _set_up_down_mode(self, up_down_mode: bool) -> None:
478487
if up_down_mode:
479488
self.send_command("\x1B{\x01")
480489

@@ -496,35 +505,35 @@ def _set_up_down_mode(self, up_down_mode):
496505

497506
bold = _PrintModeBit(_BOLD_MASK)
498507

499-
def feed(self, lines):
508+
def feed(self, lines: int) -> None:
500509
"""Advance paper by specified number of blank lines."""
501510
assert 0 <= lines <= 255
502-
self.send_command("\x1Bd{0}".format(chr(lines)))
511+
self.send_command(f"\x1Bd{chr(lines)}")
503512
self._set_timeout(self._dot_feed_s * self._char_height)
504513
self._column = 0
505514

506-
def feed_rows(self, rows):
515+
def feed_rows(self, rows: int) -> None:
507516
"""Advance paper by specified number of pixel rows."""
508517
assert 0 <= rows <= 255
509-
self.send_command("\x1BJ{0}".format(chr(rows)))
518+
self.send_command(f"\x1BJ{chr(rows)}")
510519
self._set_timeout(rows * self._dot_feed_s)
511520
self._column = 0
512521

513-
def flush(self):
522+
def flush(self) -> None:
514523
"""Flush data pending in the printer."""
515524
self.send_command("\f")
516525

517-
def offline(self):
526+
def offline(self) -> None:
518527
"""Put the printer into an offline state. No other commands can be
519528
sent until an online call is made.
520529
"""
521530
self.send_command("\x1B=\x00") # ESC + '=' + 0
522531

523-
def online(self):
532+
def online(self) -> None:
524533
"""Put the printer into an online state after previously put offline."""
525534
self.send_command("\x1B=\x01") # ESC + '=' + 1
526535

527-
def has_paper(self):
536+
def has_paper(self) -> bool:
528537
"""Return a boolean indicating if the printer has paper. You MUST have
529538
the serial RX line hooked up for this to work. NOTE: be VERY CAREFUL
530539
to ensure your board can handle a 5V serial input before hooking up
@@ -537,38 +546,38 @@ def has_paper(self):
537546
return False
538547
return not status[0] & 0b00000100
539548

540-
def _set_line_height(self, height):
549+
def _set_line_height(self, height: int) -> None:
541550
"""Set the line height in pixels. This is the total amount of space
542551
between lines, including the height of text. The smallest value is 24
543552
and the largest is 255.
544553
"""
545554
assert 24 <= height <= 255
546555
self._line_spacing = height - 24
547-
self.send_command("\x1B3{0}".format(chr(height))) # ESC + '3' + height
556+
self.send_command(f"\x1B3{chr(height)}") # ESC + '3' + height
548557

549-
def _set_barcode_height(self, height):
558+
def _set_barcode_height(self, height: int) -> None:
550559
"""Set the barcode height in pixels. Must be a value 1 - 255."""
551560
assert 1 <= height <= 255
552561
self._barcode_height = height
553-
self.send_command("\x1Dh{0}".format(chr(height))) # ASCII GS + 'h' + height
562+
self.send_command(f"\x1Dh{chr(height)}") # ASCII GS + 'h' + height
554563

555-
def _set_charset(self, charset=0):
564+
def _set_charset(self, charset: int = 0) -> None:
556565
"""Alters the character set for ASCII characters 0x23-0x7E. See
557566
datasheet for details on character set values (0-15). Note this is only
558567
supported on more recent firmware printers!
559568
"""
560569
assert 0 <= charset <= 15
561-
self.send_command("\x1BR{0}".format(chr(charset))) # ESC + 'R' + charset
570+
self.send_command(f"\x1BR{chr(charset)}") # ESC + 'R' + charset
562571

563-
def _set_code_page(self, code_page=0):
572+
def _set_code_page(self, code_page: int = 0) -> None:
564573
"""Select alternate code page for upper ASCII symbols 0x80-0xFF. See
565574
datasheet for code page values (0 - 47). Note this is only supported
566575
on more recent firmware printers!
567576
"""
568577
assert 0 <= code_page <= 47
569-
self.send_command("\x1Bt{0}".format(chr(code_page))) # ESC + 't' + code page
578+
self.send_command(f"\x1Bt{chr(code_page)}") # ESC + 't' + code page
570579

571-
def tab(self):
580+
def tab(self) -> None:
572581
"""Print a tab (i.e. move to next 4 character block). Note this is
573582
only supported on more recent firmware printers!"""
574583
self.send_command("\t")

adafruit_thermal_printer/thermal_printer_2168.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323

2424
from adafruit_thermal_printer import thermal_printer
2525

26+
try:
27+
import typing # pylint: disable=unused-import
28+
from busio import UART
29+
except ImportError:
30+
pass
2631

2732
# pylint: disable=too-many-arguments
2833
class ThermalPrinter(thermal_printer.ThermalPrinter):
@@ -44,12 +49,12 @@ class ThermalPrinter(thermal_printer.ThermalPrinter):
4449

4550
def __init__(
4651
self,
47-
uart,
48-
byte_delay_s=0.00057346,
49-
dot_feed_s=0.0021,
50-
dot_print_s=0.03,
51-
auto_warm_up=True,
52-
):
52+
uart: UART,
53+
byte_delay_s: float = 0.00057346,
54+
dot_feed_s: float = 0.0021,
55+
dot_print_s: float = 0.03,
56+
auto_warm_up: bool = True,
57+
) -> None:
5358
"""Thermal printer class. Requires a serial UART connection with at
5459
least the TX pin connected. Take care connecting RX as the printer
5560
will output a 5V signal which can damage boards! If RX is unconnected
@@ -68,7 +73,7 @@ def __init__(
6873
auto_warm_up=auto_warm_up,
6974
)
7075

71-
def warm_up(self, heat_time=120):
76+
def warm_up(self, heat_time: int = 120) -> None:
7277
"""Apparently there are no parameters for setting darkness in 2.168
7378
(at least commands from 2.68 dont work), So it is little
7479
compatibility method to reuse older code.

0 commit comments

Comments
 (0)