Skip to content

Commit

Permalink
Revert changes to usbsock - new implementation isn't quite there yet
Browse files Browse the repository at this point in the history
  • Loading branch information
Eelviny committed Apr 22, 2016
1 parent db7214f commit 6a79923
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions nxt/usbsock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Copyright (C) 2006, 2007 Douglas P Lau
# Copyright (C) 2009 Marcus Wanner
# Copyright (C) 2011 Paul Hollensen, Marcus Wanner
# Copyright (C) 2016 Alan Aguiar
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -20,65 +19,69 @@
ID_VENDOR_LEGO = 0x0694
ID_PRODUCT_NXT = 0x0002

IN_ENDPOINT = 0x82
OUT_ENDPOINT = 0x01

NXT_CONFIGURATION = 1
NXT_INTERFACE = 0

TIMEOUT = 250

class USBSock(object):
'Object for USB connection to NXT'

bsize = 60 # USB socket block size

type = 'usb'

def __init__(self, device):
self.device = device
self.handle = None
self.debug = False

def __str__(self):
return 'USB (%s)' % (self.device.filename)

def _debug(self, message, err=''):
if self.debug:
print(message, err)

def connect(self):
'Use to connect to NXT.'
self._debug('Connecting via USB...')
try:
if os.name != 'nt' and self.device.is_kernel_driver_active(NXT_INTERFACE):
self.device.detach_kernel_driver(NXT_INTERFACE)
self.device.set_configuration(NXT_CONFIGURATION)
except Exception as err:
self._debug('ERROR:usbsock:connect', err)
raise
self._debug('Connected.')
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)
if os.name != 'nt': # http://code.google.com/p/nxt-python/issues/detail?id=33
self.handle.reset()
if self.debug:
print('Connected.')
return Brick(self)

def close(self):
'Use to close the connection.'
self._debug('Closing USB connection...')
if self.debug:
print('Closing USB connection...')
self.device = None
self._debug('USB connection closed.')
self.handle = None
self.blk_out = None
self.blk_in = None
if self.debug:
print('USB connection closed.')

def send(self, data):
'Use to send raw data over USB connection'
self._debug('Send:', end=' ')
self._debug(':'.join('%02x' % ord(c) for c in data))
self.device.write(OUT_ENDPOINT, data, TIMEOUT)
'Use to send raw data over USB connection ***ADVANCED USERS ONLY***'
if self.debug:
print('Send:', end=' ')
print(':'.join('%02x' % ord(c) for c in data))
self.handle.bulkWrite(self.blk_out.address, data)

def recv(self):
'Use to recieve raw data over USB connection'
data = self.device.read(IN_ENDPOINT, 64, TIMEOUT)
self._debug('Recv:', end=' ')
self._debug(':'.join('%02x' % (c & 0xFF) for c in data))
'Use to recieve raw data over USB connection ***ADVANCED USERS ONLY***'
data = self.handle.bulkRead(self.blk_in.address, 64)
if self.debug:
print('Recv:', end=' ')
print(':'.join('%02x' % (c & 0xFF) for c in data))
# NOTE: bulkRead returns a tuple of ints ... make it sane
return bytearray(data)

def find_bricks(host=None, name=None):
'Use to look for NXTs connected by USB only'
for device in usb.core.find(find_all=True, idVendor=ID_VENDOR_LEGO, idProduct=ID_PRODUCT_NXT):
yield USBSock(device)

'Use to look for NXTs connected by USB only. ***ADVANCED USERS ONLY***'
# FIXME: probably should check host (MAC)
# if anyone knows how to do this, please file a bug report
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)

0 comments on commit 6a79923

Please sign in to comment.