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
Show file tree
Hide file tree
Changes from 3 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
14 changes: 8 additions & 6 deletions adafruit_thermal_printer/thermal_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ def _wait_timeout(self):
while time.monotonic() < self._resume:
pass

def _write_char(self, char):
def _write_char(self, char, *, encoding):
# 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 @@ -287,15 +287,17 @@ def reset(self):
# 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, end="\n"):
def print(self, text, *, end="\n", encoding="utf-8"):
"""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".
"""
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, barcode_type):
"""Print a barcode with the specified text/number (the meaning
Expand Down
2 changes: 1 addition & 1 deletion adafruit_thermal_printer/thermal_printer_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def feed(self, lines):
"""Advance paper by specified number of blank lines."""
# Just send line feeds for older printers.
for _ in range(lines):
self._write_char("\n")
self._write_char("\n", encoding="ascii")

def has_paper(self):
"""Return a boolean indicating if the printer has paper. You MUST have
Expand Down