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

Commit 38c8659

Browse files
authored
Merge pull request #29 from mushunrek/main
update _write_char
2 parents 391383f + 486cc84 commit 38c8659

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

adafruit_thermal_printer/thermal_printer.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,12 @@ def _wait_timeout(self) -> None:
196196
while time.monotonic() < self._resume:
197197
pass
198198

199-
def _write_char(self, char: str) -> None:
199+
def _write_char(self, char: str, *, encoding: str = "utf-8") -> None:
200200
# Write a single character to the printer.
201201
if char == "\r":
202202
return # Strip carriage returns by skipping them.
203203
self._wait_timeout()
204-
self._uart.write(bytes(char, "ascii"))
204+
self._uart.write(bytes(char, encoding))
205205
delay = self._byte_delay_s
206206
# Add extra delay for newlines or moving past the last column.
207207
if char == "\n" or self._column == self._max_column:
@@ -298,15 +298,21 @@ def reset(self) -> None:
298298
# ESC + 'D' + tab stop value list ending with null to terminate.
299299
self.send_command("\x1BD\x04\x08\x10\x14\x18\x1C\x00")
300300

301-
def print(self, text: str, end: Optional[str] = "\n") -> None:
301+
def print(
302+
self, text: str, end: Optional[str] = "\n", *, encoding: str = "utf-8"
303+
) -> None:
302304
"""Print a line of text. Optionally specify the end keyword to
303305
override the new line printed after the text (set to None to disable
304-
the new line entirely).
306+
the new line entirely). Optionally specify the encoding. Some
307+
printers only accept the more restrictive encodings "cp437" and
308+
"ascii".
309+
Note: Encodings other than "utf-8" are not accepted by
310+
microcontrollers.
305311
"""
306312
for char in text:
307-
self._write_char(char)
313+
self._write_char(char, encoding=encoding)
308314
if end is not None:
309-
self._write_char(end)
315+
self._write_char(end, encoding=encoding)
310316

311317
def print_barcode(self, text: str, barcode_type: int) -> None:
312318
"""Print a barcode with the specified text/number (the meaning

0 commit comments

Comments
 (0)