Skip to content

Commit

Permalink
configure pytuyo as a package
Browse files Browse the repository at this point in the history
  • Loading branch information
swinman committed Jun 7, 2020
1 parent dc95c0f commit f0cd08e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 36 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/*
dist/*
*egg-info*
71 changes: 37 additions & 34 deletions pytuyo.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import usb
import logging
import time
import argparse
from collections import deque
#!/usr/bin/python3

log = logging.getLogger(__name__)
import logging as _logging
from collections import deque as _deque
import usb as _usb

_log = _logging.getLogger(__name__)

CMD_TERMINATOR = b'\r'
MSG_TERMINATOR = b'\r'
Expand All @@ -20,7 +20,7 @@ class Pytuyo(object):
def __init__(self, usb_dev):
self._usb_dev = usb_dev
self._epin = None
self._rxqueue = deque(maxlen=MAX_RXQUEUE_LEN)
self._rxqueue = _deque(maxlen=MAX_RXQUEUE_LEN)

self.data_cb = None
self.device_info_cb = None
Expand Down Expand Up @@ -50,11 +50,11 @@ def setup(self):
wIndex=0
length=1
res1 = d.ctrl_transfer(bmRequestType, bRequest, wValue, wIndex, length)
log.debug("Device Vendor resp: {}".format(res1))
_log.debug("Device Vendor resp: {}".format(res1))

def send_cmd(self, cmd):
if self._waiting_resp:
log.warning("Cannot send mitutuyo cmd - still waiting response")
_log.warning("Cannot send mitutuyo cmd - still waiting response")
return

if not isinstance(cmd, bytes):
Expand All @@ -70,8 +70,8 @@ def send_cmd(self, cmd):
bRequest=0x03
try:
self._usb_dev.ctrl_transfer(bmRequestType, bRequest, 0, 0, cmd)
except usb.USBError as e:
log.error(str(e))
except _usb.USBError as e:
_log.error(str(e))

self._waiting_resp = True

Expand All @@ -84,7 +84,7 @@ def request_device_info(self):
def _process_data_resp(self, response):
MIN_DATA_LEN=4
if len(response) < MIN_DATA_LEN:
log.error("Invalid data measurement resp '{}'".format(response))
_log.error("Invalid data measurement resp '{}'".format(response))
return

#ignore first two characters - always "1A"
Expand All @@ -93,24 +93,23 @@ def _process_data_resp(self, response):
try:
val = float(measure_str)
except ValueError as e:
log.error("Unable to parse measurement '{}' to float".format(measure_str))
_log.error("Unable to parse measurement '{}' to float".format(measure_str))
return

log.debug("Received measure data value: {}".format(val))
_log.debug("Received measure data value: {}".format(val))

if self.data_cb: self.data_cb(val)

def _process_device_info_resp(self, response):
log.debug("Received device info msg : {}".format(response))
_log.debug("Received device info msg : {}".format(response))

if self.device_info_cb: self.device_info_cb(response)

def _process_status_resp(self, response):
log.debug("Received device status msg : {}".format(response))
_log.debug("Received device status msg : {}".format(response))

if self.status_cb: self.status_cb(response)


def _rx(self):
if self._epin is None:
raise Exception("Device not setup correctly for reading - no interrupt IN endpoint")
Expand All @@ -123,9 +122,9 @@ def _rx(self):

self._rxqueue.extend(resp)

except usb.USBError as e:
except _usb.USBError as e:
if e.errno == 110:
log.debug("USB timeout waiting for response")
_log.debug("USB timeout waiting for response")
return
else:
raise
Expand Down Expand Up @@ -161,32 +160,36 @@ def check_resp(self):
elif msg_c == STATUS_MSG:
self._process_status_resp(resp[1:])
else:
log.error("Ignoring unexpected device resp {}".format(resp))
_log.error("Ignoring unexpected device resp {}".format(resp))

return resp


def make_parser():
""" create the argument parser """
parser = argparse.ArgumentParser(description="Interact with Mitutoyo USB-ITN with pyusb")

parser.add_argument('-i', '--request-device-info', type=bool, default=True,
help='request device info')
parser.add_argument('-n', '--read-count', type=int, default=1,
help='Read count. -1 for inf')
parser.add_argument('-t', '--read-interval', type=float, default=1,
help='Read interval in seconds')

return parser

if __name__ == '__main__':
import sys
import time
logging.basicConfig(level=logging.INFO)
import argparse


def make_parser():
""" create the argument parser """
parser = argparse.ArgumentParser(description="Interact with Mitutoyo USB-ITN with pyusb")

parser.add_argument('-i', '--request-device-info', type=bool, default=True,
help='request device info')
parser.add_argument('-n', '--read-count', type=int, default=1,
help='Read count. -1 for inf')
parser.add_argument('-t', '--read-interval', type=float, default=1,
help='Read interval in seconds')

return parser

_logging.basicConfig(level=_logging.INFO)
parser = make_parser()
args = parser.parse_args()

d = usb.core.find(idVendor=0x0fe7, idProduct=0x4001)
d = _usb.core.find(idVendor=0x0fe7, idProduct=0x4001)
if d is None:
print("Could not find USB-ITN (idVendor=0x0fe7, idProduct=0x4001)")
sys.exit(1)
Expand Down
9 changes: 7 additions & 2 deletions read.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#!/usr/bin/python3

import usb
import logging
import sys
import time
import logging
import usb

log = logging.getLogger(__name__)
d = usb.core.find(idVendor=0x0fe7, idProduct=0x4001)

if d is None:
log.error("No Mitutoyo device matching 0fe7:4001 found")
sys.exit(1)

if d.is_kernel_driver_active(0):
d.detach_kernel_driver(0)
#except usb.USBError as e:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyusb
20 changes: 20 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python

from setuptools import setup
import shlex
from subprocess import check_output

GIT_HEAD_REV = check_output(shlex.split('git rev-parse --short HEAD')).strip().decode()

with open('requirements.txt') as f:
requirements = f.read().splitlines()

setup(name='pytuyo',
version='1.0+{}'.format(GIT_HEAD_REV),
url="https://github.com/rabryan/pytuyo",
description='Python Mitutoyo Interface',
author='Richard Bryan',
author_email='rabryan@lucidsci.com',
py_modules=['pytuyo'],
install_requires = requirements,
)

0 comments on commit f0cd08e

Please sign in to comment.