Merge 940306295c2aca973d87fe51f1e99d6c321d92e7 into d93e76e9040197da47832d88155940b5caa9bbfe

This commit is contained in:
William Rogers 2015-05-15 17:08:14 +00:00
commit e664ceba1b
2 changed files with 119 additions and 17 deletions

53
escpos/escpos.py Normal file → Executable file
View File

@ -20,7 +20,7 @@ from exceptions import *
class Escpos: class Escpos:
""" ESC/POS Printer object """ """ ESC/POS Printer object """
device = None device = None
img_cache = []
def _check_image_size(self, size): def _check_image_size(self, size):
""" Check and fix the size of the image to 32 bits """ """ Check and fix the size of the image to 32 bits """
@ -33,13 +33,45 @@ class Escpos:
else: else:
return (image_border / 2, (image_border / 2) + 1) return (image_border / 2, (image_border / 2) + 1)
def cache_image(self, path_img):
""" Open image file and store in the printer's cache """
img_no = len(self.img_cache)
im_open = Image.open(path_img)
im = im_open.convert("RGB")
# Convert the RGB image into printable image
(pix_line, img_size) = self._convert_image(im)
# Convert the buffered data directly to the printer readable format
cache = ""
buffer = ""
i = 0
cont = 0
buffer = "%02X%02X%02X%02X" % (((img_size[0]/img_size[1])/8), 0, img_size[1], 0)
cache = buffer.decode('hex')
buffer = ""
while i < len(pix_line):
hex_string = int(pix_line[i:i+8],2)
buffer += "%02X" % hex_string
i += 8
cont += 1
if cont % 4 == 0:
cache += buffer.decode("hex")
buffer = ""
cont = 0
self.img_cache.append(cache)
return img_no
def _print_image(self, line, size): def _print_image(self, line, size):
""" Print formatted image """ """ Print formatted image """
i = 0 i = 0
cont = 0 cont = 0
buffer = "" buffer = ""
self._raw(S_RASTER_N) self._raw(S_RASTER_N)
buffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1], 0) buffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1], 0)
self._raw(buffer.decode('hex')) self._raw(buffer.decode('hex'))
@ -97,20 +129,25 @@ class Escpos:
break break
elif im_color > (255 * 3 / pattern_len * pattern_len) and im_color <= (255 * 3): elif im_color > (255 * 3 / pattern_len * pattern_len) and im_color <= (255 * 3):
pix_line += im_pattern[-1] pix_line += im_pattern[-1]
break break
pix_line += im_right pix_line += im_right
img_size[0] += im_border[1] img_size[0] += im_border[1]
self._print_image(pix_line, img_size) return pix_line, img_size
def image(self,path_img): def image(self,path_img):
""" Open image file """ """ Open and immediately print image file """
im_open = Image.open(path_img) im_open = Image.open(path_img)
im = im_open.convert("RGB") im = im_open.convert("RGB")
# Convert the RGB image in printable image
self._convert_image(im)
# Convert the RGB image in printable image
(pix_line, img_size) = self._convert_image(im)
self._print_image(pix_line, img_size)
def cached_image(self, index):
""" Prints an image from the image cache """
self._raw(S_RASTER_N)
self._raw(self.img_cache[index])
def qr(self,text): def qr(self,text):
""" Print QR Code for the provided string """ """ Print QR Code for the provided string """

83
escpos/printer.py Normal file → Executable file
View File

@ -18,6 +18,8 @@ from exceptions import *
class Usb(Escpos): class Usb(Escpos):
""" Define USB printer """ """ Define USB printer """
is_open = False
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):
""" """
@param idVendor : Vendor ID @param idVendor : Vendor ID
@ -36,9 +38,14 @@ class Usb(Escpos):
def open(self): def open(self):
""" Search device on USB tree and set is as escpos device """ """ Search device on USB tree and set is as escpos device """
if self.is_open:
return # Already open; no need to reopen
self.device = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct) self.device = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
if self.device is None: if self.device is None:
print "Cable isn't plugged in" print "Cable isn't plugged in"
else:
self.is_open = True
if self.device.is_kernel_driver_active(0): if self.device.is_kernel_driver_active(0):
try: try:
@ -50,9 +57,21 @@ class Usb(Escpos):
self.device.set_configuration() self.device.set_configuration()
self.device.reset() self.device.reset()
except usb.core.USBError as e: except usb.core.USBError as e:
# Seems fatal when it occurs. Should the device be closed as a result?
#self.close()
print "Could not set configuration: %s" % str(e) print "Could not set configuration: %s" % str(e)
def close(self):
""" Manually release USB interface """
self.is_open = False
if self.device:
usb.util.dispose_resources(self.device)
self.device = None
def _raw(self, msg): def _raw(self, msg):
""" Print any command sent in raw format """ """ Print any command sent in raw format """
self.device.write(self.out_ep, msg, self.interface) self.device.write(self.out_ep, msg, self.interface)
@ -60,15 +79,15 @@ class Usb(Escpos):
def __del__(self): def __del__(self):
""" Release USB interface """ """ Release USB interface """
if self.device: self.close()
usb.util.dispose_resources(self.device)
self.device = None
class Serial(Escpos): class Serial(Escpos):
""" Define Serial printer """ """ Define Serial printer """
is_open = False
def __init__(self, devfile="/dev/ttyS0", baudrate=9600, bytesize=8, timeout=1): def __init__(self, devfile="/dev/ttyS0", baudrate=9600, bytesize=8, timeout=1):
""" """
@param devfile : Device file under dev filesystem @param devfile : Device file under dev filesystem
@ -84,15 +103,29 @@ class Serial(Escpos):
def open(self): def open(self):
""" Setup serial port and set is as escpos device """ """ Setup serial port and set it as escpos device """
if self.is_open:
return # Already open; no need to reopen
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) 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: if self.device is not None:
print "Serial printer enabled" print "Serial printer enabled"
self.is_open = True
else: else:
print "Unable to open serial printer on: %s" % self.devfile print "Unable to open serial printer on: %s" % self.devfile
def close(self):
""" Manually close Serial interface """
self.is_open = False
if self.device is not None:
self.device.close()
self.device = None
def _raw(self, msg): def _raw(self, msg):
""" Print any command sent in raw format """ """ Print any command sent in raw format """
self.device.write(msg) self.device.write(msg)
@ -100,14 +133,14 @@ class Serial(Escpos):
def __del__(self): def __del__(self):
""" Close Serial interface """ """ Close Serial interface """
if self.device is not None: self.close()
self.device.close()
class Network(Escpos): class Network(Escpos):
""" Define Network printer """ """ Define Network printer """
is_open = False
def __init__(self,host,port=9100): def __init__(self,host,port=9100):
""" """
@param host : Printer's hostname or IP address @param host : Printer's hostname or IP address
@ -120,11 +153,26 @@ class Network(Escpos):
def open(self): def open(self):
""" Open TCP socket and set it as escpos device """ """ Open TCP socket and set it as escpos device """
if self.is_open:
return # Already open; no need to reopen
self.device = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.device = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.device.connect((self.host, self.port)) self.device.connect((self.host, self.port))
if self.device is None: if self.device is None:
print "Could not open socket for %s" % self.host print "Could not open socket for %s" % self.host
else:
self.is_open = True
def close(self):
""" Manually close TCP connection """
self.is_open = False
if self.device is not None:
self.device.close()
self.device = None
def _raw(self, msg): def _raw(self, msg):
@ -134,13 +182,15 @@ class Network(Escpos):
def __del__(self): def __del__(self):
""" Close TCP connection """ """ Close TCP connection """
self.device.close() self.close()
class File(Escpos): class File(Escpos):
""" Define Generic file printer """ """ Define Generic file printer """
is_open = False
def __init__(self, devfile="/dev/usb/lp0"): def __init__(self, devfile="/dev/usb/lp0"):
""" """
@param devfile : Device file under dev filesystem @param devfile : Device file under dev filesystem
@ -151,10 +201,25 @@ class File(Escpos):
def open(self): def open(self):
""" Open system file """ """ Open system file """
if self.is_open:
return # Already open; no need to reopen
self.device = open(self.devfile, "wb") self.device = open(self.devfile, "wb")
if self.device is None: 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
else:
self.is_open = True
def close(self):
""" Manually close system file """
self.is_open = False
if self.device is not None:
self.device.close()
self.device = None
def _raw(self, msg): def _raw(self, msg):
@ -164,4 +229,4 @@ class File(Escpos):
def __del__(self): def __del__(self):
""" Close system file """ """ Close system file """
self.device.close() self.close()