1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import usb
16 from nxt.brick import Brick
17
18 ID_VENDOR_LEGO = 0x0694
19 ID_PRODUCT_NXT = 0x0002
20
22 'Object for USB connection to NXT'
23
24 bsize = 60
25
27 self.device = device
28 self.handle = None
29 self.debug = False
30
32 return 'USB (%s)' % (self.device.filename)
33
35 'Use to connect to NXT.'
36 if self.debug:
37 print 'Connecting via USB...'
38 config = self.device.configurations[0]
39 iface = config.interfaces[0][0]
40 self.blk_out, self.blk_in = iface.endpoints
41 self.handle = self.device.open()
42 self.handle.setConfiguration(1)
43 self.handle.claimInterface(0)
44 self.handle.reset()
45 if self.debug:
46 print 'Connected.'
47 return Brick(self)
48
50 'Use to close the connection.'
51 if self.debug:
52 print 'Closing USB connection...'
53 self.device = None
54 self.handle = None
55 self.blk_out = None
56 self.blk_in = None
57 if self.debug:
58 print 'USB connection closed.'
59
60 - def send(self, data):
61 'Use to send raw data over USB connection ***ADVANCED USERS ONLY***'
62 if self.debug:
63 print 'Send:',
64 print ':'.join('%02x' % ord(c) for c in data)
65 self.handle.bulkWrite(self.blk_out.address, data)
66
68 'Use to recieve raw data over USB connection ***ADVANCED USERS ONLY***'
69 data = self.handle.bulkRead(self.blk_in.address, 64)
70 if self.debug:
71 print 'Recv:',
72 print ':'.join('%02x' % (c & 0xFF) for c in data)
73
74 return ''.join(chr(d & 0xFF) for d in data)
75
77 'Use to look for NXTs connected by USB only. ***ADVANCED USERS ONLY***'
78
79 for bus in usb.busses():
80 for device in bus.devices:
81 if device.idVendor == ID_VENDOR_LEGO and \
82 device.idProduct == ID_PRODUCT_NXT:
83 yield USBSock(device)
84