Package nxt :: Module locator

Source Code for Module nxt.locator

 1  # nxt.locator module -- Locate LEGO Minstorms NXT bricks via USB or Bluetooth 
 2  # Copyright (C) 2006-2007  Douglas P Lau 
 3  # Copyright (C) 2009  Marcus Wanner 
 4  # 
 5  # This program is free software: you can redistribute it and/or modify 
 6  # it under the terms of the GNU General Public License as published by 
 7  # the Free Software Foundation, either version 3 of the License, or 
 8  # (at your option) any later version. 
 9  # 
10  # This program is distributed in the hope that it will be useful, 
11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
13  # GNU General Public License for more details. 
14   
15 -class BrickNotFoundError(Exception):
16 pass
17
18 -class NoBackendError(Exception):
19 pass
20
21 -def find_bricks(host=None, name=None):
22 """Used by find_one_brick to look for bricks ***ADVANCED USERS ONLY***""" 23 24 try: 25 import usbsock 26 usb_available = True 27 socks = usbsock.find_bricks(host, name) 28 for s in socks: 29 yield s 30 except ImportError: 31 usb_available = False 32 import sys 33 print >>sys.stderr, "USB unavailable, not searching there" 34 35 try: 36 from bluetooth import BluetoothError 37 try: 38 import bluesock 39 socks = bluesock.find_bricks(host, name) 40 for s in socks: 41 yield s 42 except BluetoothError: 43 pass 44 except ImportError: 45 import sys 46 print >>sys.stderr, "Bluetooth unavailable, not searching there" 47 if not usb_available: 48 raise NoBackendError("Neither USB nor Bluetooth could be used!")
49 50
51 -def find_one_brick(host=None, name=None):
52 """Use to find one brick. After it returns a usbsock object or a bluesock 53 object, use .connect() to connect to the brick, which returns a brick 54 object.""" 55 for s in find_bricks(host, name): 56 return s 57 raise BrickNotFoundError
58