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

update _write_char #29

Merged
merged 7 commits into from
Jan 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions adafruit_thermal_printer/thermal_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ def _wait_timeout(self) -> None:
while time.monotonic() < self._resume:
pass

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

def print(self, text: str, end: Optional[str] = "\n") -> None:
def print(
self, text: str, end: Optional[str] = "\n", *, encoding: str = "utf-8"
) -> None:
"""Print a line of text. Optionally specify the end keyword to
override the new line printed after the text (set to None to disable
the new line entirely).
the new line entirely). Optionally specify the encoding. Some
printers only accept the more restrictive encodings "cp437" and
"ascii".
Note: Encodings other than "utf-8" are not accepted by
microcontrollers.
"""
for char in text:
self._write_char(char)
self._write_char(char, encoding=encoding)
if end is not None:
self._write_char(end)
self._write_char(end, encoding=encoding)

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