From afc68340829e78bda2a3335d4830babf29c75f3c Mon Sep 17 00:00:00 2001 From: Christoph Heuel Date: Thu, 7 May 2015 20:54:32 +0200 Subject: [PATCH 01/17] After running 2to3 tool --- escpos/escpos.py | 4 ++-- escpos/printer.py | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/escpos/escpos.py b/escpos/escpos.py index b6befdd..3b3a49f 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -14,8 +14,8 @@ except ImportError: import qrcode import time -from constants import * -from exceptions import * +from .constants import * +from .exceptions import * class Escpos: """ ESC/POS Printer object """ diff --git a/escpos/printer.py b/escpos/printer.py index a34edb4..31b00c0 100644 --- a/escpos/printer.py +++ b/escpos/printer.py @@ -11,9 +11,9 @@ import usb.util import serial import socket -from escpos import * -from constants import * -from exceptions import * +from .escpos import * +from .constants import * +from .exceptions import * class Usb(Escpos): """ Define USB printer """ @@ -38,19 +38,19 @@ class Usb(Escpos): """ Search device on USB tree and set is as escpos device """ self.device = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct) if self.device is None: - print "Cable isn't plugged in" + print("Cable isn't plugged in") if self.device.is_kernel_driver_active(0): try: self.device.detach_kernel_driver(0) except usb.core.USBError as e: - print "Could not detatch kernel driver: %s" % str(e) + print("Could not detatch kernel driver: %s" % str(e)) try: self.device.set_configuration() self.device.reset() except usb.core.USBError as e: - print "Could not set configuration: %s" % str(e) + print("Could not set configuration: %s" % str(e)) def _raw(self, msg): @@ -88,9 +88,9 @@ class Serial(Escpos): self.device = serial.Serial(port=self.devfile, baudrate=self.baudrate, bytesize=self.bytesize, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=self.timeout, dsrdtr=True) if self.device is not None: - print "Serial printer enabled" + print("Serial printer enabled") else: - print "Unable to open serial printer on: %s" % self.devfile + print("Unable to open serial printer on: %s" % self.devfile) def _raw(self, msg): @@ -124,7 +124,7 @@ class Network(Escpos): self.device.connect((self.host, self.port)) if self.device is None: - print "Could not open socket for %s" % self.host + print("Could not open socket for %s" % self.host) def _raw(self, msg): @@ -154,7 +154,7 @@ class File(Escpos): self.device = open(self.devfile, "wb") if self.device is None: - print "Could not open the specified file %s" % self.devfile + print("Could not open the specified file %s" % self.devfile) def _raw(self, msg): From b99c076baec6f73631a70910e399401897ee6c0b Mon Sep 17 00:00:00 2001 From: Christoph Heuel Date: Tue, 19 May 2015 02:52:55 +0200 Subject: [PATCH 02/17] Fix for string operation * With Python version 3 data and text are treated different. Convert the * text accordingly. --- escpos/printer.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/escpos/printer.py b/escpos/printer.py index 31b00c0..de88814 100644 --- a/escpos/printer.py +++ b/escpos/printer.py @@ -159,7 +159,10 @@ class File(Escpos): def _raw(self, msg): """ Print any command sent in raw format """ - self.device.write(msg); + if type(msg) is str: + self.device.write(msg.encode()); + else: + self.device.write(msg); def __del__(self): From 7da2e32e3c687f54d8b33154e08b4f453c0d2488 Mon Sep 17 00:00:00 2001 From: Christoph Heuel Date: Thu, 4 Jun 2015 13:20:17 +0200 Subject: [PATCH 03/17] Integer is needed, not float * The size of the image can only be integer, not float. Using round to convert it back --- escpos/escpos.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/escpos/escpos.py b/escpos/escpos.py index 3b3a49f..876755e 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -29,9 +29,9 @@ class Escpos: else: image_border = 32 - (size % 32) if (image_border % 2) == 0: - return (image_border / 2, image_border / 2) + return (round(image_border / 2), round(image_border / 2)) else: - return (image_border / 2, (image_border / 2) + 1) + return (round(image_border / 2), round((image_border / 2) + 1)) def _print_image(self, line, size): From dd228c9fdae4f4eee972b7d6d91a36b1b4d42258 Mon Sep 17 00:00:00 2001 From: Christoph Heuel Date: Thu, 4 Jun 2015 13:57:59 +0200 Subject: [PATCH 04/17] Add text wrapping * The base class supports to give columns, how much it should wrapped. * This is meant for longer text. * The special instances need to initialize the super class with the * columns --- escpos/escpos.py | 17 ++++++++++++----- escpos/printer.py | 12 ++++++++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/escpos/escpos.py b/escpos/escpos.py index 876755e..810167e 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -13,6 +13,7 @@ except ImportError: import qrcode import time +import textwrap from .constants import * from .exceptions import * @@ -21,6 +22,8 @@ class Escpos: """ ESC/POS Printer object """ device = None + def __init__(self, columns=32): + self.columns = columns def _check_image_size(self, size): """ Check and fix the size of the image to 32 bits """ @@ -39,7 +42,7 @@ class Escpos: i = 0 cont = 0 buffer = "" - + self._raw(S_RASTER_N) buffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1], 0) self._raw(buffer.decode('hex')) @@ -97,7 +100,7 @@ class Escpos: break elif im_color > (255 * 3 / pattern_len * pattern_len) and im_color <= (255 * 3): pix_line += im_pattern[-1] - break + break pix_line += im_right img_size[0] += im_border[1] @@ -196,9 +199,9 @@ class Escpos: self._raw(BARCODE_TXT_BTH) elif pos.upper() == "ABOVE": self._raw(BARCODE_TXT_ABV) - else: # DEFAULT POSITION: BELOW + else: # DEFAULT POSITION: BELOW self._raw(BARCODE_TXT_BLW) - # Type + # Type if bc.upper() == "UPC-A": self._raw(BARCODE_UPC_A) elif bc.upper() == "UPC-E": @@ -221,7 +224,7 @@ class Escpos: else: raise exception.BarcodeCodeError() - + def text(self, txt): """ Print alpha-numeric text """ if txt: @@ -229,6 +232,10 @@ class Escpos: else: raise TextError() + def block_text(self, txt, columns=None): + '''Text is printed wrapped to specified columns''' + colCount = self.columns if columns == None else columns + self.text(textwrap.fill(txt, colCount)) def set(self, align='left', font='a', type='normal', width=1, height=1, density=9): """ Set text properties """ diff --git a/escpos/printer.py b/escpos/printer.py index de88814..1de54a3 100644 --- a/escpos/printer.py +++ b/escpos/printer.py @@ -18,7 +18,7 @@ from .exceptions import * class Usb(Escpos): """ Define USB printer """ - def __init__(self, idVendor, idProduct, interface=0, in_ep=0x82, out_ep=0x01): + def __init__(self, idVendor, idProduct, interface=0, in_ep=0x82, out_ep=0x01, *args, **kwargs): """ @param idVendor : Vendor ID @param idProduct : Product ID @@ -26,6 +26,7 @@ class Usb(Escpos): @param in_ep : Input end point @param out_ep : Output end point """ + Escpos.__init__(self, *args, **kwargs) self.idVendor = idVendor self.idProduct = idProduct self.interface = interface @@ -69,13 +70,14 @@ class Usb(Escpos): class Serial(Escpos): """ Define Serial printer """ - def __init__(self, devfile="/dev/ttyS0", baudrate=9600, bytesize=8, timeout=1): + def __init__(self, devfile="/dev/ttyS0", baudrate=9600, bytesize=8, timeout=1, *args, **kwargs): """ @param devfile : Device file under dev filesystem @param baudrate : Baud rate for serial transmission @param bytesize : Serial buffer size @param timeout : Read/Write timeout """ + Escpos.__init__(self, *args, **kwargs) self.devfile = devfile self.baudrate = baudrate self.bytesize = bytesize @@ -108,11 +110,12 @@ class Serial(Escpos): class Network(Escpos): """ Define Network printer """ - def __init__(self,host,port=9100): + def __init__(self,host,port=9100, *args, **kwargs): """ @param host : Printer's hostname or IP address @param port : Port to write to """ + Escpos.__init__(self, *args, **kwargs) self.host = host self.port = port self.open() @@ -141,10 +144,11 @@ class Network(Escpos): class File(Escpos): """ Define Generic file printer """ - def __init__(self, devfile="/dev/usb/lp0"): + def __init__(self, devfile="/dev/usb/lp0", *args, **kwargs): """ @param devfile : Device file under dev filesystem """ + Escpos.__init__(self, *args, **kwargs) self.devfile = devfile self.open() From 0ef2951c7e187719b99a4ac0912955a6d203d448 Mon Sep 17 00:00:00 2001 From: Christoph Heuel Date: Wed, 10 Jun 2015 03:21:52 +0200 Subject: [PATCH 05/17] Introduce new direct_image * The direct_image method prints directly from the PIL image object * The image should be converted to 1 bit before * The method was derived from png2escpos (https://github.com/twg/png2escpos) --- escpos/escpos.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/escpos/escpos.py b/escpos/escpos.py index 810167e..9d37709 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -14,6 +14,7 @@ except ImportError: import qrcode import time import textwrap +import binascii from .constants import * from .exceptions import * @@ -114,6 +115,38 @@ class Escpos: # Convert the RGB image in printable image self._convert_image(im) + def direct_image(self, image): + """ Send image to printer""" + mask = 0x80 + i = 0 + temp = 0 + + (width, height) = image.size + self._raw(S_RASTER_N) + headerX = int(width / 8) + headerY = height + buf = "%02X" % (headerX & 0xff) + buf += "%02X" % ((headerX >> 8) & 0xff) + buf += "%02X" % (headerY & 0xff) + buf += "%02X" % ((headerY >> 8) & 0xff) + #self._raw(binascii.unhexlify(buf)) + for y in range(height): + for x in range(width): + value = image.getpixel((x,y)) + value = (value << 8) | value; + if value == 0: + temp |= mask + + mask = mask >> 1 + + i += 1 + if i == 8: + buf += ("%02X" % temp) + mask = 0x80 + i = 0 + temp = 0 + self._raw(binascii.unhexlify(buf)) + def qr(self,text): """ Print QR Code for the provided string """ From 6734864a5b25f34678c14ad80122d99ab9ccadc8 Mon Sep 17 00:00:00 2001 From: Christoph Heuel Date: Mon, 15 Jun 2015 23:08:49 +0200 Subject: [PATCH 06/17] Use unhexlify * for decoding, use unhexlify --- escpos/escpos.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/escpos/escpos.py b/escpos/escpos.py index 9d37709..30fc126 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -46,7 +46,7 @@ class Escpos: self._raw(S_RASTER_N) buffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1], 0) - self._raw(buffer.decode('hex')) + self._raw(binascii.unhexlify(buffer)) buffer = "" while i < len(line): @@ -55,7 +55,7 @@ class Escpos: i += 8 cont += 1 if cont % 4 == 0: - self._raw(buffer.decode("hex")) + self._raw(binascii.unhexlify(buffer)) buffer = "" cont = 0 From 25b650c935ccd0ac723b06d5f21b353bae5ceca7 Mon Sep 17 00:00:00 2001 From: Christoph Heuel Date: Fri, 26 Jun 2015 02:12:30 +0200 Subject: [PATCH 07/17] Hexlify text * The original code did not convert the received text --- escpos/escpos.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/escpos/escpos.py b/escpos/escpos.py index 30fc126..bea1543 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -145,7 +145,7 @@ class Escpos: mask = 0x80 i = 0 temp = 0 - self._raw(binascii.unhexlify(buf)) + self._raw(binascii.unhexlify(bytes(buf, "ascii"))) def qr(self,text): From 546f47edcc4f6ccf4e4ffc049e5db74ee8462dfd Mon Sep 17 00:00:00 2001 From: Christoph Heuel Date: Thu, 27 Aug 2015 23:20:53 +0200 Subject: [PATCH 08/17] Fix text wrapping error after image * After an image the text wrapping was disturbed. --- escpos/escpos.py | 1 + 1 file changed, 1 insertion(+) diff --git a/escpos/escpos.py b/escpos/escpos.py index bea1543..a2b7fe0 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -146,6 +146,7 @@ class Escpos: i = 0 temp = 0 self._raw(binascii.unhexlify(bytes(buf, "ascii"))) + self._raw('\n') def qr(self,text): From 1a1ed5e7fcc7610601bc77a6faef5c9ac1de21be Mon Sep 17 00:00:00 2001 From: Christoph Heuel Date: Thu, 27 Aug 2015 23:45:15 +0200 Subject: [PATCH 09/17] Fix mixed tabs/space error --- escpos/escpos.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/escpos/escpos.py b/escpos/escpos.py index 97326c3..d331b02 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -112,13 +112,13 @@ class Escpos: """ Open image file """ im_open = Image.open(path_img) - # Remove the alpha channel on transparent images - if im_open.mode == 'RGBA': - im_open.load() - im = Image.new("RGB", im_open.size, (255, 255, 255)) - im.paste(im_open, mask=im_open.split()[3]) - else: - im = im_open.convert("RGB") + # Remove the alpha channel on transparent images + if im_open.mode == 'RGBA': + im_open.load() + im = Image.new("RGB", im_open.size, (255, 255, 255)) + im.paste(im_open, mask=im_open.split()[3]) + else: + im = im_open.convert("RGB") # Convert the RGB image in printable image self._convert_image(im) From 371d5e78bda7bb5a8c651d9997dbcc22ac4d0b2e Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Fri, 27 Nov 2015 20:49:28 +0100 Subject: [PATCH 10/17] moved .hgignore to .gitignore --- .hgignore => .gitignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) rename .hgignore => .gitignore (73%) diff --git a/.hgignore b/.gitignore similarity index 73% rename from .hgignore rename to .gitignore index aa3937a..9d3808a 100644 --- a/.hgignore +++ b/.gitignore @@ -1,10 +1,9 @@ # python temporary files -syntax: glob *.pyc # editor autosaves $~ +.idea/ # temporary data -syntax: regexp temp From 3ea52e52fd8b357a00bce8dbfcbe4061331acb08 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Fri, 27 Nov 2015 21:22:27 +0100 Subject: [PATCH 11/17] REFACTOR chained boolean expression in escpos --- escpos/escpos.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/escpos/escpos.py b/escpos/escpos.py index 045a4fd..37eed91 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -95,7 +95,7 @@ class Escpos: else: pix_line += im_pattern[x] break - elif im_color > (255 * 3 / pattern_len * pattern_len) and im_color <= (255 * 3): + elif (255 * 3 / pattern_len * pattern_len) < im_color <= (255 * 3): pix_line += im_pattern[-1] break pix_line += im_right From 096445631f42d6806f906fb6f81525c429aa6ea0 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Fri, 27 Nov 2015 21:24:47 +0100 Subject: [PATCH 12/17] REFACTOR use new-style class for Escpos --- escpos/escpos.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/escpos/escpos.py b/escpos/escpos.py index 37eed91..7a76b98 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -17,7 +17,7 @@ import time from constants import * from exceptions import * -class Escpos: +class Escpos(object): """ ESC/POS Printer object """ device = None From 07d8e073ae1e64f4885eb41abc430438fa006dbe Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Fri, 27 Nov 2015 21:20:12 +0100 Subject: [PATCH 13/17] REFACTOR style and PEP8, fixes #66 --- escpos/__init__.py | 2 +- escpos/constants.py | 150 +++++++++++++++++++++---------------------- escpos/escpos.py | 88 +++++++++++-------------- escpos/exceptions.py | 5 +- escpos/printer.py | 35 +++------- 5 files changed, 128 insertions(+), 152 deletions(-) diff --git a/escpos/__init__.py b/escpos/__init__.py index 22a5af6..0459ce8 100644 --- a/escpos/__init__.py +++ b/escpos/__init__.py @@ -1 +1 @@ -__all__ = ["constants","escpos","exceptions","printer"] +__all__ = ["constants", "escpos", "exceptions", "printer"] diff --git a/escpos/constants.py b/escpos/constants.py index 7511365..0123839 100644 --- a/escpos/constants.py +++ b/escpos/constants.py @@ -1,86 +1,86 @@ """ ESC/POS Commands (Constants) """ # Feed control sequences -CTL_LF = '\x0a' # Print and line feed -CTL_FF = '\x0c' # Form feed -CTL_CR = '\x0d' # Carriage return -CTL_HT = '\x09' # Horizontal tab -CTL_SET_HT = '\x1b\x44' # Set horizontal tab positions -CTL_VT = '\x1b\x64\x04' # Vertical tab +CTL_LF = '\x0a' # Print and line feed +CTL_FF = '\x0c' # Form feed +CTL_CR = '\x0d' # Carriage return +CTL_HT = '\x09' # Horizontal tab +CTL_SET_HT = '\x1b\x44' # Set horizontal tab positions +CTL_VT = '\x1b\x64\x04' # Vertical tab # Printer hardware -HW_INIT = '\x1b\x40' # Clear data in buffer and reset modes -HW_SELECT = '\x1b\x3d\x01' # Printer select -HW_RESET = '\x1b\x3f\x0a\x00' # Reset printer hardware +HW_INIT = '\x1b\x40' # Clear data in buffer and reset modes +HW_SELECT = '\x1b\x3d\x01' # Printer select +HW_RESET = '\x1b\x3f\x0a\x00' # Reset printer hardware # Cash Drawer -CD_KICK_2 = '\x1b\x70\x00' # Sends a pulse to pin 2 [] -CD_KICK_5 = '\x1b\x70\x01' # Sends a pulse to pin 5 [] +CD_KICK_2 = '\x1b\x70\x00' # Sends a pulse to pin 2 [] +CD_KICK_5 = '\x1b\x70\x01' # Sends a pulse to pin 5 [] # Paper -PAPER_FULL_CUT = '\x1d\x56\x00' # Full cut paper -PAPER_PART_CUT = '\x1d\x56\x01' # Partial cut paper +PAPER_FULL_CUT = '\x1d\x56\x00' # Full cut paper +PAPER_PART_CUT = '\x1d\x56\x01' # Partial cut paper # Text format -TXT_NORMAL = '\x1b\x21\x00' # Normal text -TXT_2HEIGHT = '\x1b\x21\x10' # Double height text -TXT_2WIDTH = '\x1b\x21\x20' # Double width text -TXT_4SQUARE = '\x1b\x21\x30' # Quad area text -TXT_UNDERL_OFF = '\x1b\x2d\x00' # Underline font OFF -TXT_UNDERL_ON = '\x1b\x2d\x01' # Underline font 1-dot ON -TXT_UNDERL2_ON = '\x1b\x2d\x02' # Underline font 2-dot ON -TXT_BOLD_OFF = '\x1b\x45\x00' # Bold font OFF -TXT_BOLD_ON = '\x1b\x45\x01' # Bold font ON -TXT_FONT_A = '\x1b\x4d\x00' # Font type A -TXT_FONT_B = '\x1b\x4d\x01' # Font type B -TXT_ALIGN_LT = '\x1b\x61\x00' # Left justification -TXT_ALIGN_CT = '\x1b\x61\x01' # Centering -TXT_ALIGN_RT = '\x1b\x61\x02' # Right justification +TXT_NORMAL = '\x1b\x21\x00' # Normal text +TXT_2HEIGHT = '\x1b\x21\x10' # Double height text +TXT_2WIDTH = '\x1b\x21\x20' # Double width text +TXT_4SQUARE = '\x1b\x21\x30' # Quad area text +TXT_UNDERL_OFF = '\x1b\x2d\x00' # Underline font OFF +TXT_UNDERL_ON = '\x1b\x2d\x01' # Underline font 1-dot ON +TXT_UNDERL2_ON = '\x1b\x2d\x02' # Underline font 2-dot ON +TXT_BOLD_OFF = '\x1b\x45\x00' # Bold font OFF +TXT_BOLD_ON = '\x1b\x45\x01' # Bold font ON +TXT_FONT_A = '\x1b\x4d\x00' # Font type A +TXT_FONT_B = '\x1b\x4d\x01' # Font type B +TXT_ALIGN_LT = '\x1b\x61\x00' # Left justification +TXT_ALIGN_CT = '\x1b\x61\x01' # Centering +TXT_ALIGN_RT = '\x1b\x61\x02' # Right justification # Char code table -CHARCODE_PC437 = '\x1b\x74\x00' # USA: Standard Europe -CHARCODE_JIS = '\x1b\x74\x01' # Japanese Katakana -CHARCODE_PC850 = '\x1b\x74\x02' # Multilingual -CHARCODE_PC860 = '\x1b\x74\x03' # Portuguese -CHARCODE_PC863 = '\x1b\x74\x04' # Canadian-French -CHARCODE_PC865 = '\x1b\x74\x05' # Nordic -CHARCODE_WEU = '\x1b\x74\x06' # Simplified Kanji, Hirakana -CHARCODE_GREEK = '\x1b\x74\x07' # Simplified Kanji -CHARCODE_HEBREW = '\x1b\x74\x08' # Simplified Kanji -CHARCODE_PC1252 = '\x1b\x74\x11' # Western European Windows Code Set -CHARCODE_PC866 = '\x1b\x74\x12' # Cirillic #2 -CHARCODE_PC852 = '\x1b\x74\x13' # Latin 2 -CHARCODE_PC858 = '\x1b\x74\x14' # Euro -CHARCODE_THAI42 = '\x1b\x74\x15' # Thai character code 42 -CHARCODE_THAI11 = '\x1b\x74\x16' # Thai character code 11 -CHARCODE_THAI13 = '\x1b\x74\x17' # Thai character code 13 -CHARCODE_THAI14 = '\x1b\x74\x18' # Thai character code 14 -CHARCODE_THAI16 = '\x1b\x74\x19' # Thai character code 16 -CHARCODE_THAI17 = '\x1b\x74\x1a' # Thai character code 17 -CHARCODE_THAI18 = '\x1b\x74\x1b' # Thai character code 18 +CHARCODE_PC437 = '\x1b\x74\x00' # USA: Standard Europe +CHARCODE_JIS = '\x1b\x74\x01' # Japanese Katakana +CHARCODE_PC850 = '\x1b\x74\x02' # Multilingual +CHARCODE_PC860 = '\x1b\x74\x03' # Portuguese +CHARCODE_PC863 = '\x1b\x74\x04' # Canadian-French +CHARCODE_PC865 = '\x1b\x74\x05' # Nordic +CHARCODE_WEU = '\x1b\x74\x06' # Simplified Kanji, Hirakana +CHARCODE_GREEK = '\x1b\x74\x07' # Simplified Kanji +CHARCODE_HEBREW = '\x1b\x74\x08' # Simplified Kanji +CHARCODE_PC1252 = '\x1b\x74\x11' # Western European Windows Code Set +CHARCODE_PC866 = '\x1b\x74\x12' # Cirillic #2 +CHARCODE_PC852 = '\x1b\x74\x13' # Latin 2 +CHARCODE_PC858 = '\x1b\x74\x14' # Euro +CHARCODE_THAI42 = '\x1b\x74\x15' # Thai character code 42 +CHARCODE_THAI11 = '\x1b\x74\x16' # Thai character code 11 +CHARCODE_THAI13 = '\x1b\x74\x17' # Thai character code 13 +CHARCODE_THAI14 = '\x1b\x74\x18' # Thai character code 14 +CHARCODE_THAI16 = '\x1b\x74\x19' # Thai character code 16 +CHARCODE_THAI17 = '\x1b\x74\x1a' # Thai character code 17 +CHARCODE_THAI18 = '\x1b\x74\x1b' # Thai character code 18 # Barcode format -BARCODE_TXT_OFF = '\x1d\x48\x00' # HRI barcode chars OFF -BARCODE_TXT_ABV = '\x1d\x48\x01' # HRI barcode chars above -BARCODE_TXT_BLW = '\x1d\x48\x02' # HRI barcode chars below -BARCODE_TXT_BTH = '\x1d\x48\x03' # HRI barcode chars both above and below -BARCODE_FONT_A = '\x1d\x66\x00' # Font type A for HRI barcode chars -BARCODE_FONT_B = '\x1d\x66\x01' # Font type B for HRI barcode chars -BARCODE_HEIGHT = '\x1d\x68\x64' # Barcode Height [1-255] -BARCODE_WIDTH = '\x1d\x77\x03' # Barcode Width [2-6] -BARCODE_UPC_A = '\x1d\x6b\x00' # Barcode type UPC-A -BARCODE_UPC_E = '\x1d\x6b\x01' # Barcode type UPC-E -BARCODE_EAN13 = '\x1d\x6b\x02' # Barcode type EAN13 -BARCODE_EAN8 = '\x1d\x6b\x03' # Barcode type EAN8 -BARCODE_CODE39 = '\x1d\x6b\x04' # Barcode type CODE39 -BARCODE_ITF = '\x1d\x6b\x05' # Barcode type ITF -BARCODE_NW7 = '\x1d\x6b\x06' # Barcode type NW7 +BARCODE_TXT_OFF = '\x1d\x48\x00' # HRI barcode chars OFF +BARCODE_TXT_ABV = '\x1d\x48\x01' # HRI barcode chars above +BARCODE_TXT_BLW = '\x1d\x48\x02' # HRI barcode chars below +BARCODE_TXT_BTH = '\x1d\x48\x03' # HRI barcode chars both above and below +BARCODE_FONT_A = '\x1d\x66\x00' # Font type A for HRI barcode chars +BARCODE_FONT_B = '\x1d\x66\x01' # Font type B for HRI barcode chars +BARCODE_HEIGHT = '\x1d\x68\x64' # Barcode Height [1-255] +BARCODE_WIDTH = '\x1d\x77\x03' # Barcode Width [2-6] +BARCODE_UPC_A = '\x1d\x6b\x00' # Barcode type UPC-A +BARCODE_UPC_E = '\x1d\x6b\x01' # Barcode type UPC-E +BARCODE_EAN13 = '\x1d\x6b\x02' # Barcode type EAN13 +BARCODE_EAN8 = '\x1d\x6b\x03' # Barcode type EAN8 +BARCODE_CODE39 = '\x1d\x6b\x04' # Barcode type CODE39 +BARCODE_ITF = '\x1d\x6b\x05' # Barcode type ITF +BARCODE_NW7 = '\x1d\x6b\x06' # Barcode type NW7 # Image format -S_RASTER_N = '\x1d\x76\x30\x00' # Set raster image normal size -S_RASTER_2W = '\x1d\x76\x30\x01' # Set raster image double width -S_RASTER_2H = '\x1d\x76\x30\x02' # Set raster image double height -S_RASTER_Q = '\x1d\x76\x30\x03' # Set raster image quadruple +S_RASTER_N = '\x1d\x76\x30\x00' # Set raster image normal size +S_RASTER_2W = '\x1d\x76\x30\x01' # Set raster image double width +S_RASTER_2H = '\x1d\x76\x30\x02' # Set raster image double height +S_RASTER_Q = '\x1d\x76\x30\x03' # Set raster image quadruple # Printing Density -PD_N50 = '\x1d\x7c\x00' # Printing Density -50% -PD_N37 = '\x1d\x7c\x01' # Printing Density -37.5% -PD_N25 = '\x1d\x7c\x02' # Printing Density -25% -PD_N12 = '\x1d\x7c\x03' # Printing Density -12.5% -PD_0 = '\x1d\x7c\x04' # Printing Density 0% -PD_P50 = '\x1d\x7c\x08' # Printing Density +50% -PD_P37 = '\x1d\x7c\x07' # Printing Density +37.5% -PD_P25 = '\x1d\x7c\x06' # Printing Density +25% -PD_P12 = '\x1d\x7c\x05' # Printing Density +12.5% +PD_N50 = '\x1d\x7c\x00' # Printing Density -50% +PD_N37 = '\x1d\x7c\x01' # Printing Density -37.5% +PD_N25 = '\x1d\x7c\x02' # Printing Density -25% +PD_N12 = '\x1d\x7c\x03' # Printing Density -12.5% +PD_0 = '\x1d\x7c\x04' # Printing Density 0% +PD_P50 = '\x1d\x7c\x08' # Printing Density +50% +PD_P37 = '\x1d\x7c\x07' # Printing Density +37.5% +PD_P25 = '\x1d\x7c\x06' # Printing Density +25% +PD_P12 = '\x1d\x7c\x05' # Printing Density +12.5% diff --git a/escpos/escpos.py b/escpos/escpos.py index 7a76b98..4bec5f2 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -12,41 +12,40 @@ except ImportError: from PIL import Image import qrcode -import time from constants import * from exceptions import * + class Escpos(object): """ ESC/POS Printer object """ - device = None + device = None - - def _check_image_size(self, size): + @staticmethod + def _check_image_size(size): """ Check and fix the size of the image to 32 bits """ if size % 32 == 0: - return (0, 0) + return 0, 0 else: image_border = 32 - (size % 32) if (image_border % 2) == 0: - return (image_border / 2, image_border / 2) + return image_border / 2, image_border / 2 else: - return (image_border / 2, (image_border / 2) + 1) - + return image_border / 2, (image_border / 2) + 1 def _print_image(self, line, size): """ Print formatted image """ i = 0 cont = 0 buffer = "" - + self._raw(S_RASTER_N) - buffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1]&0xff, size[1]>>8) + buffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1] & 0xff, size[1] >> 8) self._raw(buffer.decode('hex')) buffer = "" while i < len(line): - hex_string = int(line[i:i+8],2) + hex_string = int(line[i:i+8], 2) buffer += "%02X" % hex_string i += 8 cont += 1 @@ -55,19 +54,17 @@ class Escpos(object): buffer = "" cont = 0 - def _convert_image(self, im): """ Parse image and prepare it to a printable format """ - pixels = [] + pixels = [] pix_line = "" - im_left = "" + im_left = "" im_right = "" - switch = 0 - img_size = [ 0, 0 ] - + switch = 0 + img_size = [0, 0] if im.size[0] > 512: - print ("WARNING: Image is wider than 512 and could be truncated at print time ") + print ("WARNING: Image is wider than 512 and could be truncated at print time ") if im.size[1] > 0xffff: raise ImageSizeError() @@ -87,7 +84,7 @@ class Escpos(object): im_color = (RGB[0] + RGB[1] + RGB[2]) im_pattern = "1X0" pattern_len = len(im_pattern) - switch = (switch - 1 ) * (-1) + switch = (switch - 1) * (-1) for x in range(pattern_len): if im_color <= (255 * 3 / pattern_len * (x+1)): if im_pattern[x] == "X": @@ -97,30 +94,28 @@ class Escpos(object): break elif (255 * 3 / pattern_len * pattern_len) < im_color <= (255 * 3): pix_line += im_pattern[-1] - break + break pix_line += im_right img_size[0] += im_border[1] self._print_image(pix_line, img_size) - - def image(self,path_img): + def image(self, path_img): """ Open image file """ im_open = Image.open(path_img) - # Remove the alpha channel on transparent images - if im_open.mode == 'RGBA': - im_open.load() - im = Image.new("RGB", im_open.size, (255, 255, 255)) - im.paste(im_open, mask=im_open.split()[3]) - else: - im = im_open.convert("RGB") + # Remove the alpha channel on transparent images + if im_open.mode == 'RGBA': + im_open.load() + im = Image.new("RGB", im_open.size, (255, 255, 255)) + im.paste(im_open, mask=im_open.split()[3]) + else: + im = im_open.convert("RGB") # Convert the RGB image in printable image self._convert_image(im) - - def qr(self,text): + def qr(self, text): """ Print QR Code for the provided string """ qr_code = qrcode.QRCode(version=4, box_size=4, border=1) qr_code.add_data(text) @@ -131,8 +126,7 @@ class Escpos(object): # Convert the RGB image in printable image self._convert_image(im) - - def charcode(self,code): + def charcode(self, code): """ Set Character Code Table """ if code.upper() == "USA": self._raw(CHARCODE_PC437) @@ -184,19 +178,19 @@ class Escpos(object): # Align Bar Code() self._raw(TXT_ALIGN_CT) # Height - if height >=2 or height <=6: + if height >= 2 or height <= 6: self._raw(BARCODE_HEIGHT) else: raise BarcodeSizeError() # Width - if width >= 1 or width <=255: + if width >= 1 or width <= 255: self._raw(BARCODE_WIDTH) else: raise BarcodeSizeError() # Font if font.upper() == "B": self._raw(BARCODE_FONT_B) - else: # DEFAULT FONT: A + else: # DEFAULT FONT: A self._raw(BARCODE_FONT_A) # Position if pos.upper() == "OFF": @@ -205,9 +199,9 @@ class Escpos(object): self._raw(BARCODE_TXT_BTH) elif pos.upper() == "ABOVE": self._raw(BARCODE_TXT_ABV) - else: # DEFAULT POSITION: BELOW + else: # DEFAULT POSITION: BELOW self._raw(BARCODE_TXT_BLW) - # Type + # Type if bc.upper() == "UPC-A": self._raw(BARCODE_UPC_A) elif bc.upper() == "UPC-E": @@ -228,9 +222,8 @@ class Escpos(object): if code: self._raw(code) else: - raise exception.BarcodeCodeError() + raise BarcodeCodeError() - def text(self, txt): """ Print alpha-numeric text """ if txt: @@ -238,7 +231,6 @@ class Escpos(object): else: raise TextError() - def set(self, align='left', font='a', type='normal', width=1, height=1, density=9): """ Set text properties """ # Width @@ -251,7 +243,7 @@ class Escpos(object): elif width == 2 and height != 2: self._raw(TXT_NORMAL) self._raw(TXT_2WIDTH) - else: # DEFAULT SIZE: NORMAL + else: # DEFAULT SIZE: NORMAL self._raw(TXT_NORMAL) # Type if type.upper() == "B": @@ -303,10 +295,9 @@ class Escpos(object): self._raw(PD_P37) elif density == 8: self._raw(PD_P50) - else:# DEFAULT: DOES NOTHING + else: # DEFAULT: DOES NOTHING pass - def cut(self, mode=''): """ Cut paper """ # Fix the size between last line and cut @@ -314,10 +305,9 @@ class Escpos(object): self._raw("\n\n\n\n\n\n") if mode.upper() == "PART": self._raw(PAPER_PART_CUT) - else: # DEFAULT MODE: FULL CUT + else: # DEFAULT MODE: FULL CUT self._raw(PAPER_FULL_CUT) - def cashdraw(self, pin): """ Send pulse to kick the cash drawer """ if pin == 2: @@ -327,7 +317,6 @@ class Escpos(object): else: raise CashDrawerError() - def hw(self, hw): """ Hardware operations """ if hw.upper() == "INIT": @@ -336,17 +325,16 @@ class Escpos(object): self._raw(HW_SELECT) elif hw.upper() == "RESET": self._raw(HW_RESET) - else: # DEFAULT: DOES NOTHING + else: # DEFAULT: DOES NOTHING pass - def control(self, ctl, pos=4): """ Feed control sequences """ # Set tab positions if pos < 1 or pos > 16: raise TabError() else: - self._raw("".join([CTL_SET_HT,hex(pos)])) + self._raw("".join([CTL_SET_HT, hex(pos)])) # Set position if ctl.upper() == "LF": self._raw(CTL_LF) diff --git a/escpos/exceptions.py b/escpos/exceptions.py index 0d482aa..e1700e3 100644 --- a/escpos/exceptions.py +++ b/escpos/exceptions.py @@ -1,6 +1,5 @@ """ ESC/POS Exceptions classes """ -import os class Error(Exception): """ Base class for ESC/POS errors """ @@ -35,6 +34,7 @@ class BarcodeTypeError(Error): def __str__(self): return "No Barcode type is defined" + class BarcodeSizeError(Error): def __init__(self, msg=""): Error.__init__(self, msg) @@ -44,6 +44,7 @@ class BarcodeSizeError(Error): def __str__(self): return "Barcode size is out of range" + class BarcodeCodeError(Error): def __init__(self, msg=""): Error.__init__(self, msg) @@ -53,6 +54,7 @@ class BarcodeCodeError(Error): def __str__(self): return "Code was not supplied" + class ImageSizeError(Error): def __init__(self, msg=""): Error.__init__(self, msg) @@ -62,6 +64,7 @@ class ImageSizeError(Error): def __str__(self): return "Image height is longer than 255px and can't be printed" + class TextError(Error): def __init__(self, msg=""): Error.__init__(self, msg) diff --git a/escpos/printer.py b/escpos/printer.py index 3a3bc84..b6d9ca2 100644 --- a/escpos/printer.py +++ b/escpos/printer.py @@ -12,9 +12,9 @@ import serial import socket from escpos import * -from constants import * from exceptions import * + class Usb(Escpos): """ Define USB printer """ @@ -26,14 +26,13 @@ class Usb(Escpos): @param in_ep : Input end point @param out_ep : Output end point """ - self.idVendor = idVendor + self.idVendor = idVendor self.idProduct = idProduct self.interface = interface - self.in_ep = in_ep - self.out_ep = out_ep + self.in_ep = in_ep + self.out_ep = out_ep self.open() - def open(self): """ Search device on USB tree and set is as escpos device """ self.device = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct) @@ -52,7 +51,7 @@ class Usb(Escpos): self.device.detach_kernel_driver(0) except usb.core.USBError as e: if check_driver is not None: - print "Could not detatch kernel driver: %s" % str(e) + print "Could not detach kernel driver: %s" % str(e) try: self.device.set_configuration() @@ -60,12 +59,10 @@ class Usb(Escpos): except usb.core.USBError as e: print "Could not set configuration: %s" % str(e) - def _raw(self, msg): """ Print any command sent in raw format """ self.device.write(self.out_ep, msg, self.interface) - def __del__(self): """ Release USB interface """ if self.device: @@ -73,13 +70,12 @@ class Usb(Escpos): self.device = None - class Serial(Escpos): """ Define Serial printer """ def __init__(self, devfile="/dev/ttyS0", baudrate=9600, bytesize=8, timeout=1, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, - xonxoff=False , dsrdtr=True): + xonxoff=False, dsrdtr=True): """ @param devfile : Device file under dev filesystem @param baudrate : Baud rate for serial transmission @@ -91,10 +87,10 @@ class Serial(Escpos): @param xonxoff : Software flow control @param dsrdtr : Hardware flow control (False to enable RTS/CTS) """ - self.devfile = devfile + self.devfile = devfile self.baudrate = baudrate self.bytesize = bytesize - self.timeout = timeout + self.timeout = timeout self.parity = parity self.stopbits = stopbits @@ -103,7 +99,6 @@ class Serial(Escpos): self.open() - def open(self): """ Setup serial port and set is as escpos device """ self.device = serial.Serial(port=self.devfile, baudrate=self.baudrate, @@ -116,23 +111,20 @@ class Serial(Escpos): else: print "Unable to open serial printer on: %s" % self.devfile - def _raw(self, msg): """ Print any command sent in raw format """ self.device.write(msg) - def __del__(self): """ Close Serial interface """ if self.device is not None: self.device.close() - class Network(Escpos): """ Define Network printer """ - def __init__(self,host,port=9100): + def __init__(self, host, port=9100): """ @param host : Printer's hostname or IP address @param port : Port to write to @@ -141,7 +133,6 @@ class Network(Escpos): self.port = port self.open() - def open(self): """ Open TCP socket and set it as escpos device """ self.device = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -150,18 +141,15 @@ class Network(Escpos): if self.device is None: print "Could not open socket for %s" % self.host - def _raw(self, msg): """ Print any command sent in raw format """ self.device.send(msg) - def __del__(self): """ Close TCP connection """ self.device.close() - class File(Escpos): """ Define Generic file printer """ @@ -172,7 +160,6 @@ class File(Escpos): self.devfile = devfile self.open() - def open(self): """ Open system file """ self.device = open(self.devfile, "wb") @@ -180,11 +167,9 @@ class File(Escpos): if self.device is None: print "Could not open the specified file %s" % self.devfile - def _raw(self, msg): """ Print any command sent in raw format """ - self.device.write(msg); - + self.device.write(msg) def __del__(self): """ Close system file """ From ef8035527c22190a5c537b758c5d306e862a18a4 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Fri, 27 Nov 2015 21:38:59 +0100 Subject: [PATCH 14/17] REFACTOR do not shadow built-ins --- escpos/escpos.py | 28 ++++++++++++++-------------- escpos/exceptions.py | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/escpos/escpos.py b/escpos/escpos.py index 4bec5f2..3149044 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -37,21 +37,21 @@ class Escpos(object): """ Print formatted image """ i = 0 cont = 0 - buffer = "" + pbuffer = "" self._raw(S_RASTER_N) - buffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1] & 0xff, size[1] >> 8) - self._raw(buffer.decode('hex')) - buffer = "" + pbuffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1] & 0xff, size[1] >> 8) + self._raw(pbuffer.decode('hex')) + pbuffer = "" while i < len(line): hex_string = int(line[i:i+8], 2) - buffer += "%02X" % hex_string + pbuffer += "%02X" % hex_string i += 8 cont += 1 if cont % 4 == 0: - self._raw(buffer.decode("hex")) - buffer = "" + self._raw(pbuffer.decode("hex")) + pbuffer = "" cont = 0 def _convert_image(self, im): @@ -231,7 +231,7 @@ class Escpos(object): else: raise TextError() - def set(self, align='left', font='a', type='normal', width=1, height=1, density=9): + def set(self, align='left', font='a', text_type='normal', width=1, height=1, density=9): """ Set text properties """ # Width if height == 2 and width == 2: @@ -246,22 +246,22 @@ class Escpos(object): else: # DEFAULT SIZE: NORMAL self._raw(TXT_NORMAL) # Type - if type.upper() == "B": + if text_type.upper() == "B": self._raw(TXT_BOLD_ON) self._raw(TXT_UNDERL_OFF) - elif type.upper() == "U": + elif text_type.upper() == "U": self._raw(TXT_BOLD_OFF) self._raw(TXT_UNDERL_ON) - elif type.upper() == "U2": + elif text_type.upper() == "U2": self._raw(TXT_BOLD_OFF) self._raw(TXT_UNDERL2_ON) - elif type.upper() == "BU": + elif text_type.upper() == "BU": self._raw(TXT_BOLD_ON) self._raw(TXT_UNDERL_ON) - elif type.upper() == "BU2": + elif text_type.upper() == "BU2": self._raw(TXT_BOLD_ON) self._raw(TXT_UNDERL2_ON) - elif type.upper == "NORMAL": + elif text_type.upper == "NORMAL": self._raw(TXT_BOLD_OFF) self._raw(TXT_UNDERL_OFF) # Font diff --git a/escpos/exceptions.py b/escpos/exceptions.py index e1700e3..d1b1842 100644 --- a/escpos/exceptions.py +++ b/escpos/exceptions.py @@ -85,7 +85,7 @@ class CashDrawerError(Error): return "Valid pin must be set to send pulse" -class TabError(Error): +class TabPosError(Error): def __init__(self, msg=""): Error.__init__(self, msg) self.msg = msg From 8b5798eedf6c51c400c495f60d81c00d98040088 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Fri, 27 Nov 2015 21:51:58 +0100 Subject: [PATCH 15/17] ADD requirements.txt and requirements to setup.py --- requirements.txt | 1 + setup.py | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ecf975e --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +-e . \ No newline at end of file diff --git a/setup.py b/setup.py index adbb97a..e031c85 100755 --- a/setup.py +++ b/setup.py @@ -23,7 +23,13 @@ setup( 'Operating System :: GNU/Linux', 'Intended Audience :: Developers', 'Programming Language :: Python', - 'Topic :: System :: Pheripherals', + 'Topic :: System :: Peripherals', 'Topic :: Software Development :: Libraries :: Python Modules', ], + install_requires=[ + 'pyusb', + 'Pillow>=2.0', + 'qrcode>=4.0', + 'pyserial', + ], ) From 0dacc35d940e4c8ed09cacb8e1ab25b5a2b9fe08 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Fri, 27 Nov 2015 23:10:20 +0100 Subject: [PATCH 16/17] DOC, IMPROVE improve docstrings and add abstract method _raw to Escpos --- escpos/escpos.py | 106 ++++++++++++++++++++++++++++++++++++------- escpos/exceptions.py | 10 +++- escpos/printer.py | 12 ++--- 3 files changed, 105 insertions(+), 23 deletions(-) diff --git a/escpos/escpos.py b/escpos/escpos.py index 3149044..2130f1b 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -16,25 +16,47 @@ import qrcode from constants import * from exceptions import * +from abc import ABCMeta, abstractmethod # abstract base class support + class Escpos(object): """ ESC/POS Printer object """ + __metaclass__ = ABCMeta device = None + @abstractmethod + def _raw(self, msg): + """ Sends raw data to the printer + + This function has to be individually implemented by the implementations. + :param msg: message string to be sent to the printer + """ + pass + @staticmethod def _check_image_size(size): - """ Check and fix the size of the image to 32 bits """ + """ Check and fix the size of the image to 32 bits + + :param size: size of the image + :returns: tuple of image borders + :rtype: (int, int) + """ if size % 32 == 0: return 0, 0 else: image_border = 32 - (size % 32) if (image_border % 2) == 0: + # TODO check behaviour of / in newer versions of python return image_border / 2, image_border / 2 else: return image_border / 2, (image_border / 2) + 1 def _print_image(self, line, size): - """ Print formatted image """ + """ Print formatted image + + :param line: + :param size: + """ i = 0 cont = 0 pbuffer = "" @@ -55,7 +77,11 @@ class Escpos(object): cont = 0 def _convert_image(self, im): - """ Parse image and prepare it to a printable format """ + """ Parse image and prepare it to a printable format + + :param im: image data + :raises: ImageSizeError + """ pixels = [] pix_line = "" im_left = "" @@ -101,7 +127,10 @@ class Escpos(object): self._print_image(pix_line, img_size) def image(self, path_img): - """ Open image file """ + """ Open image file + + :param path_img: path to image + """ im_open = Image.open(path_img) # Remove the alpha channel on transparent images @@ -116,7 +145,10 @@ class Escpos(object): self._convert_image(im) def qr(self, text): - """ Print QR Code for the provided string """ + """ Print QR Code for the provided string + + :param text: text to generate a QR-Code from + """ qr_code = qrcode.QRCode(version=4, box_size=4, border=1) qr_code.add_data(text) qr_code.make(fit=True) @@ -127,7 +159,13 @@ class Escpos(object): self._convert_image(im) def charcode(self, code): - """ Set Character Code Table """ + """ Set Character Code Table + + Sends the control sequence from constants.py to the printer with :py:meth:`escpos.printer._raw()`. + + :param code: Name of CharCode + :raises: CharCodeError + """ if code.upper() == "USA": self._raw(CHARCODE_PC437) elif code.upper() == "JIS": @@ -146,8 +184,8 @@ class Escpos(object): self._raw(CHARCODE_GREEK) elif code.upper() == "HEBREW": self._raw(CHARCODE_HEBREW) - elif code.upper() == "LATVIAN": - self._raw(CHARCODE_PC755) + # elif code.upper() == "LATVIAN": # this is not listed in the constants + # self._raw(CHARCODE_PC755) elif code.upper() == "WPC1252": self._raw(CHARCODE_PC1252) elif code.upper() == "CIRILLIC2": @@ -174,7 +212,16 @@ class Escpos(object): raise CharCodeError() def barcode(self, code, bc, width, height, pos, font): - """ Print Barcode """ + """ Print Barcode + + :param code: data for barcode + :param bc: barcode format, see constants.py + :param width: barcode width, has to be between 1 and 255 + :param height: barcode height, has to be between 2 and 6 + :param pos: position of text in barcode, default when nothing supplied is below + :param font: select font, default is font A + :raises: BarcodeSizeError, BarcodeTypeError, BarcodeCodeError + """ # Align Bar Code() self._raw(TXT_ALIGN_CT) # Height @@ -225,14 +272,27 @@ class Escpos(object): raise BarcodeCodeError() def text(self, txt): - """ Print alpha-numeric text """ + """ Print alpha-numeric text + + The text has to be encoded in the currently selected codepage. + :param txt: text to be printed + :raises: TextError + """ if txt: self._raw(txt) else: raise TextError() def set(self, align='left', font='a', text_type='normal', width=1, height=1, density=9): - """ Set text properties """ + """ Set text properties by sending them to the printer + + :param align: alignment of text + :param font: font A or B + :param text_type: add bold or underlined + :param width: text width, normal or double width + :param height: text height, normal or double height + :param density: print density + """ # Width if height == 2 and width == 2: self._raw(TXT_NORMAL) @@ -299,7 +359,10 @@ class Escpos(object): pass def cut(self, mode=''): - """ Cut paper """ + """ Cut paper + + :param mode: set to 'PART' for a partial cut + """ # Fix the size between last line and cut # TODO: handle this with a line feed self._raw("\n\n\n\n\n\n") @@ -309,7 +372,12 @@ class Escpos(object): self._raw(PAPER_FULL_CUT) def cashdraw(self, pin): - """ Send pulse to kick the cash drawer """ + """ Send pulse to kick the cash drawer + + Kick cash drawer on pin 2 or pin 5. + :param pin: pin number + :raises: CashDrawerError + """ if pin == 2: self._raw(CD_KICK_2) elif pin == 5: @@ -318,7 +386,10 @@ class Escpos(object): raise CashDrawerError() def hw(self, hw): - """ Hardware operations """ + """ Hardware operations + + :param hw: hardware action + """ if hw.upper() == "INIT": self._raw(HW_INIT) elif hw.upper() == "SELECT": @@ -329,10 +400,13 @@ class Escpos(object): pass def control(self, ctl, pos=4): - """ Feed control sequences """ + """ Feed control sequences + + :raises: TabPosError + """ # Set tab positions if pos < 1 or pos > 16: - raise TabError() + raise TabPosError() else: self._raw("".join([CTL_SET_HT, hex(pos)])) # Set position diff --git a/escpos/exceptions.py b/escpos/exceptions.py index d1b1842..8235943 100644 --- a/escpos/exceptions.py +++ b/escpos/exceptions.py @@ -26,6 +26,7 @@ class Error(Exception): class BarcodeTypeError(Error): + """No Barcode type defined """ def __init__(self, msg=""): Error.__init__(self, msg) self.msg = msg @@ -36,6 +37,7 @@ class BarcodeTypeError(Error): class BarcodeSizeError(Error): + """ Barcode size is out of range """ def __init__(self, msg=""): Error.__init__(self, msg) self.msg = msg @@ -46,16 +48,18 @@ class BarcodeSizeError(Error): class BarcodeCodeError(Error): + """ No Barcode code was supplied """ def __init__(self, msg=""): Error.__init__(self, msg) self.msg = msg self.resultcode = 30 def __str__(self): - return "Code was not supplied" + return "No Barcode code was supplied" class ImageSizeError(Error): + """ Image height is longer than 255px and can't be printed """ def __init__(self, msg=""): Error.__init__(self, msg) self.msg = msg @@ -66,6 +70,7 @@ class ImageSizeError(Error): class TextError(Error): + """ Test sting must be supplied to the text() method """ def __init__(self, msg=""): Error.__init__(self, msg) self.msg = msg @@ -76,6 +81,7 @@ class TextError(Error): class CashDrawerError(Error): + """ Valid pin must be set to send pulse """ def __init__(self, msg=""): Error.__init__(self, msg) self.msg = msg @@ -86,6 +92,7 @@ class CashDrawerError(Error): class TabPosError(Error): + """ Valid tab positions must be in the range 0 to 16 """ def __init__(self, msg=""): Error.__init__(self, msg) self.msg = msg @@ -96,6 +103,7 @@ class TabPosError(Error): class CharCodeError(Error): + """ Valid char code must be set """ def __init__(self, msg=""): Error.__init__(self, msg) self.msg = msg diff --git a/escpos/printer.py b/escpos/printer.py index b6d9ca2..a82359a 100644 --- a/escpos/printer.py +++ b/escpos/printer.py @@ -20,11 +20,11 @@ class Usb(Escpos): 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 + :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 @@ -34,7 +34,7 @@ class Usb(Escpos): self.open() def open(self): - """ Search device on USB tree and set is as escpos device """ + """ Search device on USB tree and set it as escpos device """ self.device = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct) if self.device is None: print "Cable isn't plugged in" From 8fd05eb02bc78584bde9a55c0919991c81e0fdf0 Mon Sep 17 00:00:00 2001 From: Christoph Heuel Date: Sun, 29 Nov 2015 16:03:56 +0100 Subject: [PATCH 17/17] Add flush function * Flushing the file will cause the driver to flush the cache and print * the whole buffer. --- escpos/printer.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/escpos/printer.py b/escpos/printer.py index 986d366..fd4e9e8 100644 --- a/escpos/printer.py +++ b/escpos/printer.py @@ -176,6 +176,10 @@ class File(Escpos): if self.device is None: print("Could not open the specified file %s" % self.devfile) + def flush(self): + """Flush printing content""" + self.device.flush() + def _raw(self, msg): """ Print any command sent in raw format """