From d70d274a441ce0ce68c8e0f4a4d214e624c8fd78 Mon Sep 17 00:00:00 2001 From: TC Wan Date: Thu, 21 Apr 2011 14:39:50 +0800 Subject: reenable nxos test compilation, added comment regarding buffer usage --- Debugger/debug_comm.S | 4 +++- SConstruct | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Debugger/debug_comm.S b/Debugger/debug_comm.S index 5783dbd..06e7424 100644 --- a/Debugger/debug_comm.S +++ b/Debugger/debug_comm.S @@ -602,12 +602,14 @@ _dbg__comm_readbuf_reset: * On Exit: * R0-R3: Destroyed */ - dbg__copyNxtDebugMsg: ldr r3, =debug_nxtMsgLength str r1, [r3, #NXTCOMMCHANNEL_OFFSET] str r2, [r3] ldr r1, =debug_InCommBuf +/* FIXME: We can probably save some cycles and buffer space by using the + * NXT comms buffer specified in R0 directly without copying to internal buffers + */ _dbg_memcpy r1, r0, r2, r3 /* r3: scratch register */ bx lr #endif diff --git a/SConstruct b/SConstruct index 898bb91..fa88d7a 100644 --- a/SConstruct +++ b/SConstruct @@ -164,15 +164,15 @@ mycflags.append('-g') mycflags.append('-ggdb') # Big Endian Output (disabled by default) #mycflags.append('-D__BIG_ENDIAN__') -# Test build for NXT Firmware first -#mycflags.append('-D__NXOS__') +# Test build for NxOS (Comment out for NXT Firmware) +mycflags.append('-D__NXOS__') myasflags.append('-g') myasflags.append('-ggdb') # Big Endian Output (disabled by default) #mycflags.append('-D__BIG_ENDIAN__') -# Test build for NXT Firmware first -#myasflags.append('-D__NXOS__') +# Test build for NxOS (Comment out for NXT Firmware) +myasflags.append('-D__NXOS__') env.Replace(CCFLAGS = mycflags, ASFLAGS = myasflags ) -- cgit v1.2.3 From 75ee993f3b9a69580cf88a629bcbadbb526d4531 Mon Sep 17 00:00:00 2001 From: TC Wan Date: Wed, 27 Apr 2011 14:50:31 +0800 Subject: ignore pyfantom.py script in git --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 8579431..46f8223 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,6 @@ commit.msg # The option-cache scons.options + +# pyfantom related +pyfantom.py -- cgit v1.2.3 From b8a9597a5e1fba0b024110e2d42640481c41677f Mon Sep 17 00:00:00 2001 From: Tat-Chee Wan (USM) Date: Wed, 27 Apr 2011 14:52:48 +0800 Subject: wip pyfantom integration with nxt-python Work In Progress: refactor nxt-python to add pyfantom support --- nxt-python-fantom/nxt/bluesock.py | 6 ++- nxt-python-fantom/nxt/fantomglue.py | 78 ++++++++++++++++++++++++++++------ nxt-python-fantom/nxt/pyusbglue.py | 84 +++++++++++++++++++++++++++++++++++++ nxt-python-fantom/nxt/usbsock.py | 45 ++++++++------------ 4 files changed, 172 insertions(+), 41 deletions(-) create mode 100644 nxt-python-fantom/nxt/pyusbglue.py diff --git a/nxt-python-fantom/nxt/bluesock.py b/nxt-python-fantom/nxt/bluesock.py index c57894d..b4a2739 100644 --- a/nxt-python-fantom/nxt/bluesock.py +++ b/nxt-python-fantom/nxt/bluesock.py @@ -15,7 +15,11 @@ try: import bluetooth except ImportError: - import lightblueglue as bluetooth + try: + import lightblueglue as bluetooth + except ImportError: + import pyfantom as bluetooth + import os from .brick import Brick diff --git a/nxt-python-fantom/nxt/fantomglue.py b/nxt-python-fantom/nxt/fantomglue.py index 3e3c68f..89c08b2 100644 --- a/nxt-python-fantom/nxt/fantomglue.py +++ b/nxt-python-fantom/nxt/fantomglue.py @@ -16,40 +16,92 @@ import pyfantom -FANTOM_BT='BT' -FANTOM_USB='USB' - +RFCOMM=11 # lightblue const +FANTOM_BT = RFCOMM # For compatibilty with lightblue +FANTOM_USB = 0 +# Bluetooth Iterator def discover_devices(lookup_names=False): # parameter is ignored pairs = [] - d = pyfantom.finddevices(proto = FANTOM_BT) - for p in d: - h = p[0] - n = p[1] + for d in NXTIterator(True): + # h: host, n: name + h = d.get_resource_string() + nxt = d.get_nxt() + device_info = nxt.get_device_info() + n = device_info.name + del nxt pairs.append((h, n)) return pairs class BluetoothSocket: def __init__(self, proto = FANTOM_BT, _sock=None): - if _sock is None: - _sock = pyfantom.socket(proto, passkey = None) + # We instantiate a NXT object only when we connect if none supplied self._sock = _sock self._proto = proto def connect(self, addrport): addr, port = addrport - self._sock.connect( (addr, port )) + if self._sock is None: + # Port is ignored + self._sock = NXT(addr) def send(self, data): - return self._sock.send( data ) + return self._sock.write( data ) def recv(self, numbytes): - return self._sock.recv( numbytes ) + return self._sock.read( numbytes ) def close(self): - return self._sock.close() + del self._sock class BluetoothError(IOError): pass +def _check_brick(arg, value): + return arg is None or arg == value + + +def find_devices(lookup_names=False): # parameter is ignored + devicelist = [] + for d in NXTIterator(False): + nxt = d.get_nxt() + devicelist.append(nxt) + return devicelist + +# FIXME: The semantics of find_devices is different from discover_devices +# +# # h: host, n: name +# hostmatched = None +# namematched = None +# h = d.get_resource_string() +# nxt = d.get_nxt() +# device_info = nxt.get_device_info() +# n = device_info.name +# if _check_brick(host, h) and _check_brick(name, n): +# yield nxt +# else: +# del nxt # Does not match criteria + +class USBSocket: + + def __init__(self, device=None): + # We instantiate a NXT object only when we connect if none supplied + #self.device = device + self._sock = device + self.debug = False + + def connect(self): + addr, port = addrport + if self._sock is None: + # Port is ignored + self._sock = NXT(addr) + + def send(self, data): + return self._sock.write( data ) + + def recv(self, numbytes): + return self._sock.read( numbytes ) + + def close(self): + del self._sock diff --git a/nxt-python-fantom/nxt/pyusbglue.py b/nxt-python-fantom/nxt/pyusbglue.py new file mode 100644 index 0000000..2d64d24 --- /dev/null +++ b/nxt-python-fantom/nxt/pyusbglue.py @@ -0,0 +1,84 @@ +# bluetooth.py module -- Glue code from NXT_Python to Lightblue, allowing +# NXT_Python to run on Mac without modification. Supports subset of +# PyBluez/bluetooth.py used by NXT_Python. +# +# Copyright (C) 2011 Tat-Chee Wan +# +# 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 +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +import usb + + +ID_VENDOR_LEGO = 0x0694 +ID_PRODUCT_NXT = 0x0002 + +class USBSocket: + bsize = 60 # USB socket block size + + def __init__(self, device): + self.device = device + self.handle = None + self.debug = False + + def __str__(self): + return 'USB (%s)' % (self.device.filename) + + def connect(self): + 'Use to connect to NXT.' + if self.debug: + print 'PyUSB Connecting...' + 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.' + 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 + if self.debug: + print 'USB connection closed.' + + def send(self, data): + 'Use to send raw data over USB connection ***ADVANCED USERS ONLY***' + if self.debug: + print 'Send:', + print ':'.join('%02x' % ord(c) for c in data) + self.handle.bulkWrite(self.blk_out.address, data) + + def recv(self, numbytes): + 'Use to recieve raw data over USB connection ***ADVANCED USERS ONLY***' + data = self.handle.bulkRead(self.blk_in.address, numbytes) + 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 find_devices(lookup_names=False): # parameter is ignored + devicelist = [] + for bus in usb.busses(): + for device in bus.devices: + if device.idVendor == ID_VENDOR_LEGO and device.idProduct == ID_PRODUCT_NXT: + devicelist.append(device) + return devicelist + + diff --git a/nxt-python-fantom/nxt/usbsock.py b/nxt-python-fantom/nxt/usbsock.py index 1856a10..3e2ed58 100644 --- a/nxt-python-fantom/nxt/usbsock.py +++ b/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,25 @@ 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) + # FIXME: This breaks encapsulation + return 'USB (%s)' % (self.sock.filename) 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 +52,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) -- cgit v1.2.3 From 0bf5f4efbea18e4402814f8cacd79967a4257e50 Mon Sep 17 00:00:00 2001 From: TC Wan Date: Wed, 27 Apr 2011 15:21:17 +0800 Subject: wip pyfantom support Some minor cleanups for pyfantom support --- nxt-python-fantom/nxt/fantomglue.py | 4 ++++ nxt-python-fantom/nxt/pyusbglue.py | 4 ++-- nxt-python-fantom/nxt/usbsock.py | 3 +-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/nxt-python-fantom/nxt/fantomglue.py b/nxt-python-fantom/nxt/fantomglue.py index 89c08b2..5e85e03 100644 --- a/nxt-python-fantom/nxt/fantomglue.py +++ b/nxt-python-fantom/nxt/fantomglue.py @@ -91,6 +91,10 @@ class USBSocket: self._sock = device self.debug = False + def device_name(self): + devinfo = self._sock.deviceinfo() + return devinfo.name + def connect(self): addr, port = addrport if self._sock is None: diff --git a/nxt-python-fantom/nxt/pyusbglue.py b/nxt-python-fantom/nxt/pyusbglue.py index 2d64d24..40f708b 100644 --- a/nxt-python-fantom/nxt/pyusbglue.py +++ b/nxt-python-fantom/nxt/pyusbglue.py @@ -28,8 +28,8 @@ class USBSocket: self.handle = None self.debug = False - def __str__(self): - return 'USB (%s)' % (self.device.filename) + def device_name(self): + return self.device.filename def connect(self): 'Use to connect to NXT.' diff --git a/nxt-python-fantom/nxt/usbsock.py b/nxt-python-fantom/nxt/usbsock.py index 3e2ed58..78c21ab 100644 --- a/nxt-python-fantom/nxt/usbsock.py +++ b/nxt-python-fantom/nxt/usbsock.py @@ -29,8 +29,7 @@ class USBSock(object): self.debug = False def __str__(self): - # FIXME: This breaks encapsulation - return 'USB (%s)' % (self.sock.filename) + return 'USB (%s)' % (self.sock.device_name()) def connect(self): 'Use to connect to NXT.' -- cgit v1.2.3