aboutsummaryrefslogtreecommitdiff
path: root/AT91SAM7S256/armdebug/nxt-python-fantom/nxt/fantomglue.py
diff options
context:
space:
mode:
Diffstat (limited to 'AT91SAM7S256/armdebug/nxt-python-fantom/nxt/fantomglue.py')
-rw-r--r--AT91SAM7S256/armdebug/nxt-python-fantom/nxt/fantomglue.py69
1 files changed, 63 insertions, 6 deletions
diff --git a/AT91SAM7S256/armdebug/nxt-python-fantom/nxt/fantomglue.py b/AT91SAM7S256/armdebug/nxt-python-fantom/nxt/fantomglue.py
index 96bcd3c..359427d 100644
--- a/AT91SAM7S256/armdebug/nxt-python-fantom/nxt/fantomglue.py
+++ b/AT91SAM7S256/armdebug/nxt-python-fantom/nxt/fantomglue.py
@@ -42,6 +42,13 @@ class BluetoothSocket:
self._sock = _sock
self._proto = proto
+ def __str__(self):
+ return 'FantomGlue BT (%s)' % self.device_name()
+
+ def device_name(self):
+ devinfo = self._sock.get_device_info()
+ return devinfo.name
+
def connect(self, addrport):
addr, port = addrport
if self._sock is None:
@@ -68,12 +75,10 @@ def _check_brick(arg, value):
def find_devices(lookup_names=False): # parameter is ignored
devicelist = []
for d in pyfantom.NXTIterator(False):
- addr = d.get_resource_string()
- print "NXT addr: ", addr
+ #name = d.get_name()
+ #addr = d.get_resource_string()
+ #print "NXT addr: ", addr
nxt = d.get_nxt()
- # BUG?: If nxt.get_firware_version() is enabled, d.get_nxt() will throw an exception
- # Related to Reference Counting for Obj-C Objects?
- #print " firmware version:", nxt.get_firmware_version()
devicelist.append(nxt)
return devicelist
@@ -100,14 +105,22 @@ class USBSocket:
self._sock = device
self.debug = False
+ def __str__(self):
+ return 'FantomGlue USB (%s)' % self.device_name()
+
def device_name(self):
- devinfo = self._sock.deviceinfo()
+ devinfo = self._sock.get_device_info()
return devinfo.name
def connect(self):
if self._sock is None:
# Port is ignored
+ if self.debug:
+ print "No NXT object assigned"
self._sock = pyfantom.NXT(addr) # FIXME: No address!
+ else:
+ if self.debug:
+ print "Using existing NXT object"
def send(self, data):
return self._sock.write( data )
@@ -118,3 +131,47 @@ class USBSocket:
def close(self):
if self._sock is not None:
del self._sock
+
+if __name__ == '__main__':
+ #get_info = False
+ get_info = True
+ write_read = True
+ for i in find_devices():
+ if get_info:
+ print " firmware version:", i.get_firmware_version()
+ print " get device info:", i.get_device_info()
+ rs = i.get_resource_string()
+ print " resource string:", rs
+ if write_read:
+ nxt = USBSocket(i)
+ import struct
+ # Write VERSION SYS_CMD.
+ # Query:
+ # SYS_CMD: 0x01
+ # VERSION: 0x88
+ cmd = struct.pack('2B', 0x01, 0x88)
+ r = nxt.send(cmd)
+ print "wrote", r
+ # Response:
+ # REPLY_CMD: 0x02
+ # VERSION: 0x88
+ # SUCCESS: 0x00
+ # PROTOCOL_VERSION minor
+ # PROTOCOL_VERSION major
+ # FIRMWARE_VERSION minor
+ # FIRMWARE_VERSION major
+ #rep = nxt.recv(USB_BUFSIZE) # Doesn't work if it exceeds buffer content length (exception)
+ #rep = nxt.recv(7) # Works, since reply is 7 bytes
+ rep = nxt.recv(5) # Works, read length < remaining buffer content length
+ print "read", struct.unpack('%dB' % len(rep), rep)
+ rep = nxt.recv(2) # Works, read length <= remaining buffer content length
+ print "read", struct.unpack('%dB' % len(rep), rep)
+ #rep = nxt.recv(1) # Doesn't work if it exceeds buffer content length (exception)
+ # Same thing, without response.
+ #cmd = struct.pack('2B', 0x81, 0x88)
+ #r = nxt.send(cmd)
+ #print "wrote", r
+ #rep = nxt.recv(USB_BUFSIZE)
+ #rep = nxt.recv(7)
+ #print "read", struct.unpack('%dB' % len(rep), rep)
+ del nxt