aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTat-Chee Wan (USM)2011-05-19 08:18:29 +0800
committerTat-Chee Wan (USM)2011-05-19 08:18:29 +0800
commit6a255ea54d7391a955bd0aff5089b0502fa9b639 (patch)
tree3c70532c4e9413eda4d44cf60e299fe0e6d6874e
parent0eb403fc41224724de87407fb33a0be9d5fbb640 (diff)
added statusvar object, conditionalize mac osx support in nxtiterator
-rw-r--r--pyfantom.py63
1 files changed, 39 insertions, 24 deletions
diff --git a/pyfantom.py b/pyfantom.py
index 3e20574..a5e4419 100644
--- a/pyfantom.py
+++ b/pyfantom.py
@@ -56,7 +56,7 @@ dll.nFANTOM100_iNXT_pollAvailableLength.restype = c_uint
dll.nFANTOM100_iNXT_readBufferData.argtypes = [c_uint, c_char_p, c_uint, c_uint,
POINTER(c_int)]
dll.nFANTOM100_iNXT_readBufferData.restype = c_uint
-dll.nFANTOM100_iNXT_sendDirectCommand.argtypes = [c_uint, c_ushort, c_char_p, c_uint,
+dll.nFANTOM100_iNXT_sendDirectCommand.argtypes = [c_uint, c_ushort, c_char_p, c_uint,
c_char_p, c_uint, POINTER(c_int)]
dll.nFANTOM100_iNXT_sendDirectCommand.restype = c_uint
dll.nFANTOM100_iNXT_getDeviceInfo.restype = None
@@ -187,6 +187,19 @@ class Status:
description = 'error %x (hex)' % status.value
raise FantomException(description)
+class StatusVar(Structure):
+ """Python wrapper for tStatus."""
+ _fields_ = [("value", c_int),
+ ("_fileName", c_char * kMaxFileNameLength),
+ ("_lineNumber", c_int)]
+
+ def __init__(self, code=0):
+ """Initialize Status Variable."""
+ self.value = 0
+
+ def __str__(self):
+ return str(self.value)
+
class NXTIterator:
"""Interface to an iterator, to find connected NXT."""
@@ -197,17 +210,22 @@ class NXTIterator:
self.bluetooth_search_timeout_s = bluetooth_search_timeout_s
self.handle = None
self.stop = False
- self.nsapp = NSApplication.sharedApplication()
- self.pool = NSAutoreleasePool.alloc().init()
+ self.nsapp = None
+ self.pool = None
+ if platform.system() == 'Darwin':
+ self.nsapp = NSApplication.sharedApplication()
+ self.pool = NSAutoreleasePool.alloc().init()
def destroy():
"""To be used in destructor."""
if self.debug:
print "pyfantom: Destroying NXTIterator."
- status = c_int(0)
+ status = StatusVar(0)
dll.nFANTOM100_destroyNXTIterator(self.handle, cast(byref(status), POINTER(c_int)))
Status.check(status)
- del self.pool
- del self.nsapp
+ if self.pool is not None:
+ del self.pool
+ if self.nsapp is not None:
+ del self.nsapp
if self.debug:
print "pyfantom: NXTIterator destroyed."
self.__destroy = destroy
@@ -221,7 +239,7 @@ class NXTIterator:
if self.stop:
raise StopIteration()
# Find first, or find next.
- status = c_int(0)
+ status = StatusVar(0)
if self.handle is None:
handle = dll.nFANTOM100_createNXTIterator(self.search_bluetooth,
self.bluetooth_search_timeout_s, cast(byref(status), POINTER(c_int)))
@@ -242,7 +260,7 @@ class NXTIterator:
"""Get the NXT instance."""
if self.handle is None or self.stop:
raise FantomException('invalid iterator')
- status = c_int(0)
+ status = StatusVar(0)
handle = dll.nFANTOM100_iNXTIterator_getNXT(self.handle, cast(byref(status), POINTER(c_int)))
Status.check(status)
return NXT(handle, self.search_bluetooth)
@@ -251,7 +269,7 @@ class NXTIterator:
"""Get the NXT resource name."""
if self.handle is None or self.stop:
raise FantomException('invalid iterator')
- status = c_int(0)
+ status = StatusVar(0)
name = ctypes.create_string_buffer(256)
dll.nFANTOM100_iNXTIterator_getName(self.handle, name, cast(byref(status), POINTER(c_int)))
Status.check(status)
@@ -279,7 +297,7 @@ class NXT:
self.handle = None
self.isBTLink = bluetooth # Not used for now
if isinstance(name_or_handle, basestring):
- status = c_int(0)
+ status = StatusVar(0)
handle = dll.nFANTOM100_createNXT(name_or_handle, cast(byref(status), POINTER(c_int)),
True)
Status.check(status)
@@ -294,7 +312,7 @@ class NXT:
if self.debug:
print "pyfantom: Destroying NXT."
if self.handle is not None:
- status = c_int(0)
+ status = StatusVar(0)
dll.nFANTOM100_destroyNXT(self.handle, cast(byref(status), POINTER(c_int)))
Status.check(status)
del self.pool
@@ -302,12 +320,12 @@ class NXT:
print "pyfantom: NXT destroyed."
else:
if self.debug:
- print "pyfantom: NXT handle is None!"
+ print "pyfantom: NXT handle is None!"
self.__destroy = destroy
def get_firmware_version(self):
"""Get protocol and firmware versions installed on this NXT."""
- status = c_int(0)
+ status = StatusVar(0)
protocol_major = c_ubyte(0)
protocol_minor = c_ubyte(0)
firmware_major = c_ubyte(0)
@@ -322,7 +340,7 @@ class NXT:
def write(self, data):
"""Write, in a generic fashion, to this NXT."""
- status = c_int(0)
+ status = StatusVar(0)
data_buffer = ctypes.create_string_buffer(data)
ret = dll.nFANTOM100_iNXT_write(self.handle, data_buffer, len(data),
cast(byref(status), POINTER(c_int)))
@@ -331,7 +349,7 @@ class NXT:
def read(self, length):
"""Read, in a generic fashion, from this NXT."""
- status = c_int(0)
+ status = StatusVar(0)
data_buffer = ctypes.create_string_buffer(length)
ret = dll.nFANTOM100_iNXT_read(self.handle, data_buffer, length,
cast(byref(status), POINTER(c_int)))
@@ -347,7 +365,7 @@ class NXT:
def send_direct_command(self, data, response=None, requireResponse=False):
"""Sends the specified direct command to this NXT."""
- status = c_int(0)
+ status = StatusVar(0)
data_buffer = ctypes.create_string_buffer(data)
if requireResponse:
assert response is not None
@@ -360,11 +378,11 @@ class NXT:
response_buffer, responseLen, cast(byref(status), POINTER(c_int)))
Status.check(status)
return ret
-
+
def poll_command_length(self, buffer=0):
"""Polls the data buffer on this NXT for the number of bytes available to be read."""
buffer_selector = c_uint(buffer)
- status = c_int(0)
+ status = StatusVar(0)
ret = dll.nFANTOM100_iNXT_pollAvailableLength(self.handle, buffer_selector,
cast(byref(status), POINTER(c_int)))
Status.check(status)
@@ -372,7 +390,7 @@ class NXT:
def poll_command(self, length, buffer=0):
"""Reads data from the data buffer on this NXT."""
- status = c_int(0)
+ status = StatusVar(0)
buffer_selector = c_uint(buffer)
data_buffer = ctypes.create_string_buffer(length)
ret = dll.nFANTOM100_iNXT_readBufferData(self.handle, data_buffer, buffer_selector, length,
@@ -383,7 +401,7 @@ class NXT:
def get_device_info(self):
"""Get basic information about this NXT."""
- status = c_int(0)
+ status = StatusVar(0)
name = ctypes.create_string_buffer(16)
bluetooth_address = (c_ubyte * 7)()
signal_strength = (c_ubyte * 4)()
@@ -401,7 +419,7 @@ class NXT:
def get_resource_string(self):
"""Get the NXT resource name."""
- status = c_int(0)
+ status = StatusVar(0)
name = ctypes.create_string_buffer(256)
dll.nFANTOM100_iNXT_getResourceString(self.handle, name,
cast(byref(status), POINTER(c_int)))
@@ -422,20 +440,17 @@ class NXT:
btpin = ctypes.create_string_buffer(256)
btpin.value = pin
paired_name = ctypes.create_string_buffer(256)
- #status = c_int(0)
status = StatusVar(0)
dll.nFANTOM100_pairBluetooth(resource, btpin, paired_name, cast(byref(status), POINTER(c_int)))
Status.check(status)
return paired_name.value
def unpair_bluetooth(self, resource):
- #status = c_int(0)
status = StatusVar(0)
dll.nFANTOM100_pairBluetooth(resource, cast(byref(status), POINTER(c_int)))
Status.check(status)
def ispaired_bluetooth(self, resource):
- #status = c_int(0)
status = StatusVar(0)
paired = dll.nFANTOM100_isPaired(resource, cast(byref(status), POINTER(c_int)))
Status.check(status)