<commit_before>__author__ = 'benoit'

import usb.core
from PIL import Image

class Epson_Thermal(object):

    printer = None

    def __init__(self, idVendor, idProduct, interface=0, in_ep=0x82, out_ep=0x01):
        """
        @param idVendor  : Vendor ID
        @param idProduct : Product ID
        @param interface : USB device interface
        @param in_ep     : Input end point
        @param out_ep    : Output end point
        """
        self.idVendor  = idVendor
        self.idProduct = idProduct
        self.interface = interface
        self.in_ep     = in_ep
        self.out_ep    = out_ep

        # Search device on USB tree and set is as printer
        self.printer = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
        if self.printer is None:
            print("Cable isn't plugged in")

        if self.printer.is_kernel_driver_active(0):
            try:
                self.printer.detach_kernel_driver(0)
            except usb.core.USBError as e:
                print("Could not detatch kernel driver: %s" % str(e))

        try:
            self.printer.set_configuration()
            self.printer.reset()
        except usb.core.USBError as e:
            print("Could not set configuration: %s" % str(e))

    def _write(self, msg):
        self.printer.write(self.out_ep, msg, self.interface)


    def linefeed(self):
        self._write(chr(10)) # LF

    def print_text(self, msg):
        self._write(msg)

    def cut(self):
        byte_array = [
            chr(29), # GS
            chr(86), # V
            chr(0)]  # \0
        command = ''.join(byte_array)
        self._write(command)

    def print_bitmap(self, pixels, w, h):

        dyl = chr(2 * h % 256)
        dyh = chr(int(2 * h / 256))

        # Set the size of the print area
        byte_array = [
            chr(27),    # ESC
            chr(87),    # W
            chr(46),     # xL
            chr(0),     # xH
            chr(0),     # yL
            chr(0),     # yH
            chr(0),     # dxL
            chr(2),     # dxH
            dyl,
            dyh]

        # Enter page mode
        byte_array.append(chr(27))
        byte_array.append(chr(76))

        self._write(''.join(byte_array))

        # Calculate nL and nH
        nh = chr(int(w / 256))
        nl = chr(w % 256)

        offset = 0

        while offset < h:
            byte_array = [
                chr(27),  # ESC
                chr(42),  # *
                chr(33),  # double density mode
                nl,
                nh]

            for x in range(w):
                for k in range(3):
                    slice = 0
                    for b in range(8):
                        y = offset + (k * 8) + b
                        i = (y * w) + x
                        v = 0
                        if i < len(pixels):
                            v = pixels[i]
                        slice |= (v << (7 - b))

                    byte_array.append(chr(slice))

            offset += 24

            
            byte_array.append(chr(27)) # ESC
            byte_array.append(chr(74)) # J
            byte_array.append(chr(48))
            self._write(''.join(byte_array))


        # Return to standard mode
        self._write(chr(12))


if __name__ == '__main__':
    from util import BitmapData
    printer = Epson_Thermal(0x04b8,0x0e03)  # EPSON TM-T20
    printer.print_text("Hello world")
    printer.linefeed()
    bitmap = BitmapData.fromFileImage("logo.png")
    printer.print_bitmap(bitmap.pixels, bitmap.width, bitmap.height)
    printer.linefeed()
<commit_msg>send bytes all at once in printBitmap<commit_after>__author__ = 'benoit'

import usb.core
from PIL import Image

class Epson_Thermal(object):

    printer = None

    def __init__(self, idVendor, idProduct, interface=0, in_ep=0x82, out_ep=0x01):
        """
        @param idVendor  : Vendor ID
        @param idProduct : Product ID
        @param interface : USB device interface
        @param in_ep     : Input end point
        @param out_ep    : Output end point
        """
        self.idVendor  = idVendor
        self.idProduct = idProduct
        self.interface = interface
        self.in_ep     = in_ep
        self.out_ep    = out_ep

        # Search device on USB tree and set is as printer
        self.printer = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
        if self.printer is None:
            print("Cable isn't plugged in")

        if self.printer.is_kernel_driver_active(0):
            try:
                self.printer.detach_kernel_driver(0)
            except usb.core.USBError as e:
                print("Could not detatch kernel driver: %s" % str(e))

        try:
            self.printer.set_configuration()
            self.printer.reset()
        except usb.core.USBError as e:
            print("Could not set configuration: %s" % str(e))

    def write_bytes(self, byte_array):
        msg = ''.join([chr(b) for b in byte_array])
        self.printer.write(self.out_ep, msg, self.interface)


    def linefeed(self):
        self.write_bytes([10]) # LF

    def print_text(self, msg):
        self.printer.write(self.out_ep, msg, self.interface)

    def cut(self):
        byte_array = [
            29, # GS
            86, # V
            0]  # \0
        self.write_bytes(byte_array)

    def print_bitmap(self, pixels, w, h):

        byte_array = []

        dyl = 2 * h % 256
        dyh = int(2 * h / 256)

        # Set the size of the print area
        byte_array.extend([
            27,    # ESC
            87,    # W
            46,     # xL
            0,     # xH
            0,     # yL
            0,     # yH
            0,     # dxL
            2,     # dxH
            dyl,
            dyh])

        # Enter page mode
        byte_array.extend([
            27,
            76])


        # Calculate nL and nH
        nh = int(w / 256)
        nl = w % 256

        offset = 0

        while offset < h:
            byte_array.extend([
                27,  # ESC
                42,  # *
                33,  # double density mode
                nl,
                nh])

            for x in range(w):
                for k in range(3):
                    slice = 0
                    for b in range(8):
                        y = offset + (k * 8) + b
                        i = (y * w) + x
                        v = 0
                        if i < len(pixels):
                            v = pixels[i]
                        slice |= (v << (7 - b))

                    byte_array.append(slice)

            offset += 24

            byte_array.extend([
                27,   # ESC
                74,   # J
                48])

        # Return to standard mode
        byte_array.append(12)

        self.write_bytes(byte_array)


if __name__ == '__main__':
    from util import BitmapData
    printer = Epson_Thermal(0x04b8,0x0e03)  # EPSON TM-T20
    printer.print_text("Hello world")
    printer.linefeed()
    bitmap = BitmapData.fromFileImage("logo.png")
    printer.print_bitmap(bitmap.pixels, bitmap.width, bitmap.height)
    printer.linefeed()

{'fraction_non_alphanumeric': 0.10019564002235885, 'fraction_numerical': 0.022079373951928452, 'mean_word_length': 1.3419502617801047, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '605400', 'n_tokens_mistral': 2311, 'n_tokens_neox': 2141, 'n_words': 544}