aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNicolas Schodet2011-03-08 23:18:55 +0100
committerNicolas Schodet2011-03-08 23:45:05 +0100
commit99e96dee78a6e9733182e9cd94a2926e9d89e902 (patch)
tree9a95d4a1d93493abbe6f44e500c1fa7dd6ac6e0e
parent280d158ad553eafbef6f004e496beb69a90590d4 (diff)
add NXT class and basic methods
-rw-r--r--pyfantom.py102
1 files changed, 97 insertions, 5 deletions
diff --git a/pyfantom.py b/pyfantom.py
index 1eb6c11..788ece8 100644
--- a/pyfantom.py
+++ b/pyfantom.py
@@ -1,7 +1,8 @@
"""NXT Fantom driver wrapper."""
-from ctypes import c_int, c_uint, c_ushort, c_char_p, byref, POINTER
+from ctypes import c_int, c_uint, c_ushort, c_ubyte, c_char_p, byref, POINTER
import ctypes.util
import platform
+import collections
# Check platform.
if platform.system() == 'Darwin':
@@ -28,6 +29,19 @@ dll.nFANTOM100_iNXTIterator_getNXT.restype = c_uint
dll.nFANTOM100_iNXTIterator_getName.argtypes = [c_uint, c_char_p,
POINTER(c_int)]
dll.nFANTOM100_iNXTIterator_getName.restype = None
+dll.nFANTOM100_createNXT.argtypes = [c_char_p, POINTER(c_int), c_ushort]
+dll.nFANTOM100_createNXT.restype = c_uint
+dll.nFANTOM100_destroyNXT.argtypes = [c_uint, POINTER(c_int)]
+dll.nFANTOM100_destroyNXT.restype = None
+dll.nFANTOM100_iNXT_getFirmwareVersion.argtypes = [c_uint, POINTER(c_ubyte),
+ POINTER(c_ubyte), POINTER(c_ubyte), POINTER(c_ubyte), POINTER(c_int)]
+dll.nFANTOM100_iNXT_getFirmwareVersion.argtypes = None
+dll.nFANTOM100_iNXT_getDeviceInfo.argtypes = [c_uint, c_char_p,
+ POINTER(c_ubyte), POINTER(c_ubyte), POINTER(c_uint), POINTER(c_int)]
+dll.nFANTOM100_iNXT_getDeviceInfo.restype = None
+dll.nFANTOM100_iNXT_getResourceString.argtypes = [c_uint, c_char_p,
+ POINTER(c_int)]
+dll.nFANTOM100_iNXT_getResourceString.restype = None
class FantomException(RuntimeError):
"""Exception thrown on Fantom library error."""
@@ -186,10 +200,9 @@ class NXTIterator:
if self.handle is None or self.stop:
raise FantomException('invalid iterator')
status = c_int(0)
- nxt = dll.nFANTOM100_iNXTIterator_getNXT(self.handle, byref(status))
+ handle = dll.nFANTOM100_iNXTIterator_getNXT(self.handle, byref(status))
Status.check(status)
- # XXX
- return nxt.value
+ return NXT(handle)
def get_name(self):
"""Get the NXT resource name."""
@@ -201,12 +214,91 @@ class NXTIterator:
Status.check(status)
return name.value
+ get_resource_string = get_name
+
def __del__(self):
"""Destroy iterator."""
if self.handle is not None:
status = c_int(0)
dll.nFANTOM100_destroyNXTIterator(self.handle, byref(status))
+class NXT:
+ """Interface to the NXT brick."""
+
+ DeviceInfo = collections.namedtuple('DeviceInfo',
+ 'name bluetooth_address signal_strength available_flash')
+
+ def __init__(self, name_or_handle):
+ """Initialize interface."""
+ if isinstance(name_or_handle, basestring):
+ status = c_int(0)
+ handle = dll.nFANTOM100_createNXT(name_or_handle, byref(status),
+ True)
+ Status.check(status)
+ self.handle = handle
+ else:
+ self.handle = name_or_handle
+
+ def get_firmware_version(self):
+ """Get protocol and firmware versions installed on this NXT."""
+ status = c_int(0)
+ protocol_major = c_ubyte(0)
+ protocol_minor = c_ubyte(0)
+ firmware_major = c_ubyte(0)
+ firmware_minor = c_ubyte(0)
+ dll.nFANTOM100_iNXT_getFirmwareVersion(self.handle,
+ byref(protocol_major), byref(protocol_minor),
+ byref(firmware_major), byref(firmware_minor),
+ byref(status))
+ Status.check(status)
+ return (protocol_major.value, protocol_minor.value,
+ firmware_major.value, firmware_minor.value)
+
+ def get_device_info(self):
+ """Get basic information about this NXT."""
+ status = c_int(0)
+ name = ctypes.create_string_buffer(16)
+ bluetooth_address = (c_ubyte * 7)()
+ signal_strength = (c_ubyte * 4)()
+ available_flash = c_uint(0)
+ dll.nFANTOM100_iNXT_getDeviceInfo(self.handle, name,
+ bluetooth_address, signal_strength, byref(available_flash),
+ byref(status))
+ return self.DeviceInfo(
+ name = name.value,
+ bluetooth_address = ':'.join('%02x' % c
+ for c in bluetooth_address[0:5]),
+ signal_strength = tuple(c for c in signal_strength),
+ available_flash = available_flash.value,
+ )
+
+ def get_resource_string(self):
+ """Get the NXT resource name."""
+ status = c_int(0)
+ name = ctypes.create_string_buffer(256)
+ dll.nFANTOM100_iNXT_getResourceString(self.handle, name,
+ byref(status))
+ Status.check(status)
+ return name.value
+
+ def __del__(self):
+ """Destroy interface."""
+ if self.handle is not None:
+ status = c_int(0)
+ dll.nFANTOM100_destroyNXT(self.handle, byref(status))
+
if __name__ == '__main__':
for i in NXTIterator(False):
- print i.get_name()
+ print "name:", i.get_name()
+ print "resource string:", i.get_resource_string()
+ print "get_nxt:"
+ nxt = i.get_nxt()
+ print " firmware version:", nxt.get_firmware_version()
+ print " get device info:", nxt.get_device_info()
+ rs = nxt.get_resource_string()
+ print " resource string:", rs
+ del nxt
+ print "NXT():"
+ nxt = NXT(rs)
+ print " resource string:", nxt.get_resource_string()
+ del nxt