Migration to libusb1 and changes to make it compatible with python on Windows

This commit is contained in:
Juanmi Taboada 2017-12-04 11:24:21 +01:00
parent 26d72a69f0
commit 8494cca526
5 changed files with 24 additions and 29 deletions

View File

@ -8,6 +8,7 @@ Cody (Quantified Code Bot) <cody@quantifiedcode.com> Cody <cody@quantifiedcode.c
Renato Lorenzi <renato.lorenzi@senior.com.br> Renato.Lorenzi <renato.lorenzi@senior.com.br> Renato Lorenzi <renato.lorenzi@senior.com.br> Renato.Lorenzi <renato.lorenzi@senior.com.br>
Ahmed Tahri <nyuubi.10@gmail.com> TAHRI Ahmed <nyuubi.10@gmail.com> Ahmed Tahri <nyuubi.10@gmail.com> TAHRI Ahmed <nyuubi.10@gmail.com>
Michael Elsdörfer <michael@elsdoerfer.com> Michael Elsdörfer <michael@elsdoerfer.info> Michael Elsdörfer <michael@elsdoerfer.com> Michael Elsdörfer <michael@elsdoerfer.info>
Juanmi Taboada <juanmi@juanmitaboada.com> Juanmi Taboada <juanmi@juanmitaboada.com>
csoft2k <csoft2k@hotmail.com> csoft2k <csoft2k@hotmail.com>
Sergio Pulgarin <sergio.pulgarin@gmail.com> Sergio Pulgarin <sergio.pulgarin@gmail.com>
reck31 <rakesh.gunduka@gmail.com> reck31 <rakesh.gunduka@gmail.com>

View File

@ -10,6 +10,7 @@ Dean Rispin
Dmytro Katyukha Dmytro Katyukha
Hark Hark
Joel Lehtonen Joel Lehtonen
Juanmi Taboada // br0th3r
Kristi Kristi
ldos ldos
Lucy Linder Lucy Linder

View File

@ -107,7 +107,7 @@ setup(
'Topic :: Office/Business :: Financial :: Point-Of-Sale', 'Topic :: Office/Business :: Financial :: Point-Of-Sale',
], ],
install_requires=[ install_requires=[
'pyusb>=1.0.0', 'libusb1',
'Pillow>=2.0', 'Pillow>=2.0',
'qrcode>=4.0', 'qrcode>=4.0',
'pyserial', 'pyserial',

View File

@ -7,12 +7,12 @@ import time
import six import six
import yaml import yaml
from tempfile import gettempdir
logging.basicConfig() logging.basicConfig()
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
pickle_dir = environ.get('ESCPOS_CAPABILITIES_PICKLE_DIR', gettempdir())
pickle_dir = environ.get('ESCPOS_CAPABILITIES_PICKLE_DIR', '/tmp/')
pickle_path = path.join(pickle_dir, 'capabilities.pickle') pickle_path = path.join(pickle_dir, 'capabilities.pickle')
capabilities_path = environ.get( capabilities_path = environ.get(
'ESCPOS_CAPABILITIES_FILE', 'ESCPOS_CAPABILITIES_FILE',

View File

@ -13,8 +13,9 @@ from __future__ import division
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import usb.core from usb1 import USBContext
import usb.util
import platform
import serial import serial
import socket import socket
@ -33,6 +34,7 @@ class Usb(Escpos):
:parts: 1 :parts: 1
""" """
INTERFACE = 0
def __init__(self, idVendor, idProduct, timeout=0, in_ep=0x82, out_ep=0x01, *args, **kwargs): # noqa: N803 def __init__(self, idVendor, idProduct, timeout=0, in_ep=0x82, out_ep=0x01, *args, **kwargs): # noqa: N803
""" """
@ -52,29 +54,16 @@ class Usb(Escpos):
def open(self): def open(self):
""" Search device on USB tree and set it 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) self.ctx = USBContext()
if self.device is None: dev = self.ctx.getByVendorIDAndProductID(self.idVendor, self.idProduct)
if dev is None:
raise USBNotFoundError("Device not found or cable not plugged in.") raise USBNotFoundError("Device not found or cable not plugged in.")
check_driver = None self.device = dev.open()
if platform.system() == 'Linux' and self.device.kernelDriverActive(self.INTERFACE):
self.device.detachKernelDriver(self.INTERFACE)
try: self.device.claimInterface(self.INTERFACE)
check_driver = self.device.is_kernel_driver_active(0)
except NotImplementedError:
pass
if check_driver is None or check_driver:
try:
self.device.detach_kernel_driver(0)
except usb.core.USBError as e:
if check_driver is not None:
print("Could not detatch kernel driver: {0}".format(str(e)))
try:
self.device.set_configuration()
self.device.reset()
except usb.core.USBError as e:
print("Could not set configuration: {0}".format(str(e)))
def _raw(self, msg): def _raw(self, msg):
""" Print any command sent in raw format """ Print any command sent in raw format
@ -82,17 +71,21 @@ class Usb(Escpos):
:param msg: arbitrary code to be printed :param msg: arbitrary code to be printed
:type msg: bytes :type msg: bytes
""" """
self.device.write(self.out_ep, msg, self.timeout) self.device.bulkWrite(self.out_ep, msg, timeout=self.timeout)
def _read(self): def _read(self):
""" Reads a data buffer and returns it to the caller. """ """ Reads a data buffer and returns it to the caller. """
return self.device.read(self.in_ep, 16) return self.device.bulkRead(self.in_ep, 16, timeout=self.timeout)
def close(self): def close(self):
""" Release USB interface """ """ Release USB interface """
if self.device: if self.device:
usb.util.dispose_resources(self.device) self.device.releaseInterface(self.INTERFACE)
self.device = None self.device.close()
self.device = None
if self.ctx:
self.ctx.exit()
self.ctx = None
class Serial(Escpos): class Serial(Escpos):