Exemplo n.º 1
0
class ThermalPrinter():
    def __init__(self):
        self.p = Usb(0x0fe6, 0x811e, 98, 0x82, 0x02)

    def insert_imagen(self, img_surce):
        self.p.image(img_surce)

    def insert_qr(self, content, size):
        self.p.qr(content, size=size)  #size 1 - 16 default 3

    def insert_barcode(self, code, bc, height, width):
        self.p.barcode(code, bc, height=height, width=width)

    def insert_text(self, txt):
        self.p.text(txt)

    def insert_raw_txt(self, txt):
        self.p._raw(txt)

    # harware action, may be: INIT, SELECT, RESET
    def hardware_operation(self, hw):
        self.p.hw(hw)

    #string for the following control sequences:
    # LF for Line Feed
    # FF for Form Feed
    # CR for Carriage Return
    # HT for Horizontal Tab
    # VT for Vertical Tab
    #pos = horizontal tab position 1 - 16
    def feed_control(self, ctl, pos):
        self.p.control()

        self.p.charcode()


#   #def insert_textln(self, txt):
# align = CENTER LEFT RIGHT  default left
# font_type = A or B         default A
# textt_type = B(bold), U(underline), U2(underline, version2), BU(for bold and underlined, NORMAL(normal text) defaul tnormal
# width = width multiplier decimal 1 - 8 defaul 1
# heigh =  height multiplier decimal 1 - 8 defaul 1
# density = density 0 - 8
# def set_txt(self, align, font, text_type, density, height, width):

    def set_txt(self, *args, **Kwargs):
        print(Kwargs)
        self.p.set(Kwargs)

    def cut_paper(self):
        self.p.cut()
Exemplo n.º 2
0
class PosPrinter:
    """
    Class to interface with the POS printer.
    """

    def __init__(self, product_id, vendor_id, test_mode=False):
        """
        Also does error handling

        :param product_id: Printer product ID as integer
        :param vendor_id: Printer vendor ID as integer
        :param test_mode: boolean, when set to true modifies error messages and prevents modification of the global
            settings. Used for print tests (duh)

        :return: nil
        """

        self.product_id = product_id
        self.vendor_id = vendor_id

        self._printer = None

        self.test_mode = test_mode

        # changes message end if test mode is true
        message_end = "\n\nPrinting disabled"
        if self.test_mode:
            message_end = ""

        self.failed = True  # can be used to determine if a popup was shown - set false if successful in try below

        # in case printing is disabled (and test mode is disabled), just do nothing
        # if it's in test mode, you want it to actually do stuff and test...
        if not SETTINGS["printer"]["enable"] and not self.test_mode:
            return

        try:
            self._printer = Usb(self.product_id, self.vendor_id)
            self._printer.hw("INIT")
            self.failed = False
        except escpos.exceptions.USBNotFoundError:
            popup_message("Printer not found - please check the printer is plugged in and turned on and that the vendor"
                          f" and product IDs correct.{message_end}",
                          error=True)
            if not self.test_mode:
                SETTINGS["printer"]["enable"] = False
        except USBError as e:
            if "Errno 13" in str(e):
                popup_message(f"USB error 13 - it looks like something else is using the printer.\n\n{message_end}",
                              error=True)
                if not self.test_mode:
                    SETTINGS["printer"]["enable"] = False
            else:
                popup_message(f"{e}{message_end}", error=True)
                logger.exception("Unrecognised USB error")
                if not self.test_mode:
                    SETTINGS["printer"]["enable"] = False
        except NoBackendError:
            print("nobackend")
            popup_message(f"There was an issue connecting to the printer. Please refer to the documentation.{message_end}",
                          error=True)
            logger.exception("No backend was available - Possible mis-installation of libusb-1.0.dll or WinUSB driver.")
            if not self.test_mode:
                SETTINGS["printer"]["enable"] = False

    def print(self, data):
        """
        Prints data if printer is initialised.

        :param data: binary data to send to POS printer
        :return: nil
        """

        if self._printer is None:
            helper.add_event((lambda: True), (lambda: popup_message("Printer not initialised.\n\nPrinting disabled.",
                                                                    error=True)))
            self.failed = True

            if not self.test_mode:
                SETTINGS["printer"]["enable"] = False

            return
        self._printer._raw(data)

    def close(self):
        """
        Closes printer connection

        :return: nil
        """
        self._printer = None

    def reset(self):
        """
        Closes and reopens printer connection

        :return: nil
        """

        self.close()
        try:
            self._printer = Usb(self.product_id, self.vendor_id)
        except:  # in the event of any error (eg NoBackendAvail)
            # TODO: Change to raise an exception or do something other than this
            self._printer = None

    def update_ids(self, product_id, vendor_id):
        """
        Changes printer IDs and then resets

        :param product_id: printer product ID as integer
        :param vendor_id: printer vendor ID as integer
        :return: nil
        """

        self.product_id = product_id
        self.vendor_id = vendor_id
        self.reset()