diff --git a/adafruit_thermal_printer/__init__.py b/adafruit_thermal_printer/__init__.py index b0a8fed..5a2722e 100644 --- a/adafruit_thermal_printer/__init__.py +++ b/adafruit_thermal_printer/__init__.py @@ -29,11 +29,19 @@ def get_printer_class(version): assert version is not None assert version >= 0.0 # pylint: disable=import-outside-toplevel - if version < 2.64: - import adafruit_thermal_printer.thermal_printer_legacy as thermal_printer - elif version < 2.68: + + # I reversed order of checking the version + + # TODO the legacy printer should be a base class for all newer printer. It'll make it easier + # to upgrade the library with newer firmware + if version >= 2.168: + import adafruit_thermal_printer.thermal_printer_2168 as thermal_printer + elif version >= 2.68: + import adafruit_thermal_printer.thermal_printer as thermal_printer + elif version >= 2.64: import adafruit_thermal_printer.thermal_printer_264 as thermal_printer else: - import adafruit_thermal_printer.thermal_printer as thermal_printer + import adafruit_thermal_printer.thermal_printer_legacy as thermal_printer + # pylint: enable=import-outside-toplevel return thermal_printer.ThermalPrinter diff --git a/adafruit_thermal_printer/thermal_printer.py b/adafruit_thermal_printer/thermal_printer.py index fdeebd2..0c85ca4 100644 --- a/adafruit_thermal_printer/thermal_printer.py +++ b/adafruit_thermal_printer/thermal_printer.py @@ -102,7 +102,7 @@ # else it will be very easy to break or introduce subtle incompatibilities with # older firmware printers. class ThermalPrinter: - """Thermal printer for printers with firmware version 2.68 or higher.""" + """Thermal printer for printers with firmware version from 2.68 and below 2.168""" # Barcode types. These vary based on the firmware version so are made # as class-level variables that users can reference (i.e. @@ -180,6 +180,7 @@ def __init__( self._char_height = 24 self._line_spacing = 6 self._barcode_height = 50 + self.up_down_mode = True # pylint: disable=line-too-long # Byte delay calculated based on assumption of 19200 baud. # From Arduino library code, see formula here: @@ -387,6 +388,9 @@ def set_defaults(self): self.underline = None self.inverse = False self.upside_down = False + # this should work in 2.68 according to user manual v 4.0 + # but it does't work with 2.168 hence i implemented the below + self.up_down_mode = True self.double_height = False self.double_width = False self.strike = False @@ -487,7 +491,19 @@ def _set_inverse(self, inverse): ) # pylint: enable=line-too-long - upside_down = _PrintModeBit(_UPDOWN_MASK) + def _set_up_down_mode(self, up_down_mode): + if up_down_mode: + self.send_command("\x1B{\x01") + + else: + self.send_command("\x1B{\x00") + + up_down_mode = property( + None, _set_up_down_mode, None, "Turns on/off upside-down printing mode" + ) + # The above Should work in 2.68 so its here and not in 2.168 module + + upside_down = _PrintModeBit(_UPDOWN_MASK) # Don't work in 2.168 hence the above double_height = _PrintModeBit(_DOUBLE_HEIGHT_MASK) @@ -522,8 +538,7 @@ def offline(self): self.send_command("\x1B=\x00") # ESC + '=' + 0 def online(self): - """Put the printer into an online state after previously put offline. - """ + """Put the printer into an online state after previously put offline.""" self.send_command("\x1B=\x01") # ESC + '=' + 1 def has_paper(self): diff --git a/adafruit_thermal_printer/thermal_printer_2168.py b/adafruit_thermal_printer/thermal_printer_2168.py new file mode 100644 index 0000000..0cf6aa6 --- /dev/null +++ b/adafruit_thermal_printer/thermal_printer_2168.py @@ -0,0 +1,94 @@ +# The MIT License (MIT) +# +# Copyright (c) 2020 Tony DiCola, Grzegorz Nowicki +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +""" +`adafruit_thermal_printer.thermal_printer_2168.ThermalPrinter` +============================================================== + +Thermal printer control module built to work with small serial thermal +receipt printers. Note that these printers have many different firmware +versions and care must be taken to select the appropriate module inside this +package for your firmware printer: + +* thermal_printer_2168 = Printers with firmware version 2.168+. +* thermal_printer = The latest printers with firmware version 2.68 up to 2.168 +* thermal_printer_264 = Printers with firmware version 2.64 up to 2.68. +* thermal_printer_legacy = Printers with firmware version before 2.64. + +* Author(s): Tony DiCola, Grzegorz Nowicki +""" + + +import adafruit_thermal_printer.thermal_printer as thermal_printer + + +# pylint: disable=too-many-arguments +class ThermalPrinter(thermal_printer.ThermalPrinter): + """Thermal printer for printers with firmware version from 2.168""" + + # Barcode types. These vary based on the firmware version so are made + # as class-level variables that users can reference (i.e. + # ThermalPrinter.UPC_A, etc) and write code that is independent of the + # printer firmware version. + UPC_A = 65 + UPC_E = 66 + EAN13 = 67 + EAN8 = 68 + CODE39 = 69 + ITF = 70 + CODABAR = 71 + CODE93 = 72 + CODE128 = 73 + + def __init__( + self, + uart, + byte_delay_s=0.00057346, + dot_feed_s=0.0021, + dot_print_s=0.03, + auto_warm_up=True, + ): + """Thermal printer class. Requires a serial UART connection with at + least the TX pin connected. Take care connecting RX as the printer + will output a 5V signal which can damage boards! If RX is unconnected + the only loss in functionality is the has_paper function, all other + printer functions will continue to work. The byte_delay_s, dot_feed_s, + and dot_print_s values are delays which are used to prevent overloading + the printer with data. Use the default delays unless you fully + understand the workings of the printer and how delays, baud rate, + number of dots, heat time, etc. relate to each other. + """ + super().__init__( + uart, + byte_delay_s=byte_delay_s, + dot_feed_s=dot_feed_s, + dot_print_s=dot_print_s, + auto_warm_up=auto_warm_up, + ) + + def warm_up(self, heat_time=120): + """Apparently there are no parameters for setting darkness in 2.168 + (at least commands from 2.68 dont work), So it is little + compatibility method to reuse older code. + """ + self._set_timeout(0.5) # Half second delay for printer to initialize. + self.reset() diff --git a/adafruit_thermal_printer/thermal_printer_264.py b/adafruit_thermal_printer/thermal_printer_264.py index 26bbb2a..7fe4859 100644 --- a/adafruit_thermal_printer/thermal_printer_264.py +++ b/adafruit_thermal_printer/thermal_printer_264.py @@ -28,7 +28,8 @@ versions and care must be taken to select the appropriate module inside this package for your firmware printer: -* thermal_printer = The latest printers with firmware version 2.68+ +* thermal_printer_2168 = Printers with firmware version 2.168+. +* thermal_printer = The latest printers with firmware version 2.68 up to 2.168 * thermal_printer_264 = Printers with firmware version 2.64 up to 2.68. * thermal_printer_legacy = Printers with firmware version before 2.64. diff --git a/adafruit_thermal_printer/thermal_printer_legacy.py b/adafruit_thermal_printer/thermal_printer_legacy.py index ae64c52..5f4b995 100644 --- a/adafruit_thermal_printer/thermal_printer_legacy.py +++ b/adafruit_thermal_printer/thermal_printer_legacy.py @@ -28,7 +28,8 @@ versions and care must be taken to select the appropriate module inside this package for your firmware printer: -* thermal_printer = The latest printers with firmware version 2.68+ +* thermal_printer_2168 = Printers with firmware version 2.168+. +* thermal_printer = The latest printers with firmware version 2.68 up to 2.168 * thermal_printer_264 = Printers with firmware version 2.64 up to 2.68. * thermal_printer_legacy = Printers with firmware version before 2.64.