aboutsummaryrefslogtreecommitdiff
path: root/AT91SAM7S256/armdebug/nxt-python-fantom/nxt/usbsock.py
diff options
context:
space:
mode:
Diffstat (limited to 'AT91SAM7S256/armdebug/nxt-python-fantom/nxt/usbsock.py')
-rw-r--r--AT91SAM7S256/armdebug/nxt-python-fantom/nxt/usbsock.py44
1 files changed, 17 insertions, 27 deletions
diff --git a/AT91SAM7S256/armdebug/nxt-python-fantom/nxt/usbsock.py b/AT91SAM7S256/armdebug/nxt-python-fantom/nxt/usbsock.py
index 1856a10..78c21ab 100644
--- a/AT91SAM7S256/armdebug/nxt-python-fantom/nxt/usbsock.py
+++ b/AT91SAM7S256/armdebug/nxt-python-fantom/nxt/usbsock.py
@@ -12,11 +12,12 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
-import usb
-from nxt.brick import Brick
+try:
+ import pyusbglue as usb
+except ImportError:
+ import pyfantom as usb
-ID_VENDOR_LEGO = 0x0694
-ID_PRODUCT_NXT = 0x0002
+from nxt.brick import Brick
class USBSock(object):
'Object for USB connection to NXT'
@@ -24,36 +25,24 @@ class USBSock(object):
bsize = 60 # USB socket block size
def __init__(self, device):
- self.device = device
- self.handle = None
+ self.sock = USBSocket(device)
self.debug = False
def __str__(self):
- return 'USB (%s)' % (self.device.filename)
+ return 'USB (%s)' % (self.sock.device_name())
def connect(self):
'Use to connect to NXT.'
if self.debug:
print 'Connecting via USB...'
- config = self.device.configurations[0]
- iface = config.interfaces[0][0]
- self.blk_out, self.blk_in = iface.endpoints
- self.handle = self.device.open()
- self.handle.setConfiguration(1)
- self.handle.claimInterface(0)
- self.handle.reset()
- if self.debug:
- print 'Connected.'
+ self.sock.connect()
return Brick(self)
def close(self):
'Use to close the connection.'
if self.debug:
print 'Closing USB connection...'
- self.device = None
- self.handle = None
- self.blk_out = None
- self.blk_in = None
+ self.sock.close()
if self.debug:
print 'USB connection closed.'
@@ -62,21 +51,22 @@ class USBSock(object):
if self.debug:
print 'Send:',
print ':'.join('%02x' % ord(c) for c in data)
- self.handle.bulkWrite(self.blk_out.address, data)
+ self.sock.send(data)
def recv(self):
'Use to recieve raw data over USB connection ***ADVANCED USERS ONLY***'
- data = self.handle.bulkRead(self.blk_in.address, 64)
+ data = self.sock.recv(64)
if self.debug:
print 'Recv:',
print ':'.join('%02x' % (c & 0xFF) for c in data)
# NOTE: bulkRead returns a tuple of ints ... make it sane
return ''.join(chr(d & 0xFF) for d in data)
+def _check_brick(arg, value):
+ return arg is None or arg == value
+
def find_bricks(host=None, name=None):
'Use to look for NXTs connected by USB only. ***ADVANCED USERS ONLY***'
- # FIXME: probably should check host and name
- for bus in usb.busses():
- for device in bus.devices:
- if device.idVendor == ID_VENDOR_LEGO and device.idProduct == ID_PRODUCT_NXT:
- yield USBSock(device)
+ for d in usb.find_devices(lookup_names=True):
+ # FIXME: probably should check host and name
+ yield USBSock(d)