From 280d158ad553eafbef6f004e496beb69a90590d4 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Tue, 8 Mar 2011 22:01:12 +0100 Subject: code cleanup --- pyfantom.py | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/pyfantom.py b/pyfantom.py index c74b8ce..1eb6c11 100644 --- a/pyfantom.py +++ b/pyfantom.py @@ -1,4 +1,5 @@ -from ctypes import * +"""NXT Fantom driver wrapper.""" +from ctypes import c_int, c_uint, c_ushort, c_char_p, byref, POINTER import ctypes.util import platform @@ -15,7 +16,7 @@ else: raise RuntimeError('unsupported platform') # Load library. -dll = cdll.LoadLibrary(libpath) +dll = ctypes.cdll.LoadLibrary(libpath) dll.nFANTOM100_createNXTIterator.argtypes = [c_ushort, c_uint, POINTER(c_int)] dll.nFANTOM100_createNXTIterator.restype = c_uint dll.nFANTOM100_destroyNXTIterator.argtypes = [c_int, POINTER(c_int)] @@ -24,7 +25,8 @@ dll.nFANTOM100_iNXTIterator_advance.argtypes = [c_uint, POINTER(c_int)] dll.nFANTOM100_iNXTIterator_advance.restype = None dll.nFANTOM100_iNXTIterator_getNXT.argtypes = [c_uint, POINTER(c_int)] dll.nFANTOM100_iNXTIterator_getNXT.restype = c_uint -dll.nFANTOM100_iNXTIterator_getName.argtypes = [c_uint, c_char_p, POINTER(c_int)] +dll.nFANTOM100_iNXTIterator_getName.argtypes = [c_uint, c_char_p, + POINTER(c_int)] dll.nFANTOM100_iNXTIterator_getName.restype = None class FantomException(RuntimeError): @@ -99,10 +101,12 @@ class Status: TooManyUnconfiguredDevices: "Too many unconfigured devices.", CommandMismatch: "Command mismatch in firmware response.", IllegalOperation: "Illegal operation.", - BluetoothCacheUpdateFailed: "Could not update local Bluetooth cache with new name.", + BluetoothCacheUpdateFailed: "Could not update local Bluetooth" + " cache with new name.", NonNXTDeviceSelected: "Selected device is not an NXT.", RetryConnection: "Communication error. Retry the operation.", - PowerCycleNXT: "Could not connect to NXT. Turn the NXT off and then back on before continuing.", + PowerCycleNXT: "Could not connect to NXT. Turn the NXT off and" + " then back on before continuing.", FeatureNotImplemented: "This feature is not yet implemented.", FWIllegalHandle: "Firmware reported an illegal handle.", FWIllegalFileName: "Firmware reported an illegal file name.", @@ -110,14 +114,18 @@ class Status: FWModuleNotFound: "Firmware could not find module.", FWFileExists: "Firmware reported that the file already exists.", FWFileIsFull: "Firmware reported that the file is full.", - FWAppendNotPossible: "Firmware reported the append operation is not possible.", + FWAppendNotPossible: "Firmware reported the append operation is" + " not possible.", FWNoWriteBuffers: "Firmware has no write buffers available.", FWFileIsBusy: "Firmware reported that file is busy.", FWUndefinedError: "Firmware reported the undefined error.", - FWNoLinearSpace: "Firmware reported that no linear space is available.", - FWHandleAlreadyClosed: "Firmware reported that handle has already been closed.", + FWNoLinearSpace: "Firmware reported that no linear space is" + " available.", + FWHandleAlreadyClosed: "Firmware reported that handle has already" + " been closed.", FWFileNotFound: "Firmware could not find file.", - FWNotLinearFile: "Firmware reported that the requested file is not linear.", + FWNotLinearFile: "Firmware reported that the requested file is" + " not linear.", FWEndOfFile: "Firmware reached the end of the file.", FWEndOfFileExpected: "Firmware expected an end of file.", FWNoMoreFiles: "Firmware cannot handle more files.", @@ -128,7 +136,7 @@ class Status: # }}} @staticmethod - def check (status): + def check(status): """Check status, raise on error.""" if status.value < Status.Success: if status.value in Status.description: @@ -140,12 +148,10 @@ class Status: class NXTIterator: """Interface to an iterator, to find connected NXT.""" - def __init__(self, search_bluetooth, - bluetooth_search_timeout_in_seconds = 5): + def __init__(self, search_bluetooth, bluetooth_search_timeout_s=5): """Initialize iterator.""" self.search_bluetooth = search_bluetooth - self.bluetooth_search_timeout_in_seconds = \ - bluetooth_search_timeout_in_seconds + self.bluetooth_search_timeout_s = bluetooth_search_timeout_s self.handle = None self.stop = False @@ -161,7 +167,7 @@ class NXTIterator: status = c_int(0) if self.handle is None: handle = dll.nFANTOM100_createNXTIterator(self.search_bluetooth, - self.bluetooth_search_timeout_in_seconds, byref(status)) + self.bluetooth_search_timeout_s, byref(status)) else: handle = self.handle dll.nFANTOM100_iNXTIterator_advance(handle, byref(status)) @@ -169,7 +175,7 @@ class NXTIterator: if status.value == Status.NoMoreItemsFound: self.stop = True raise StopIteration() - Status.check (status) + Status.check(status) self.handle = handle # Return itself (not part of the protocol, but it has get_nxt and # get_name). @@ -181,7 +187,7 @@ class NXTIterator: raise FantomException('invalid iterator') status = c_int(0) nxt = dll.nFANTOM100_iNXTIterator_getNXT(self.handle, byref(status)) - Status.check (status) + Status.check(status) # XXX return nxt.value @@ -190,9 +196,9 @@ class NXTIterator: if self.handle is None or self.stop: raise FantomException('invalid iterator') status = c_int(0) - name = create_string_buffer (256) + name = ctypes.create_string_buffer(256) dll.nFANTOM100_iNXTIterator_getName(self.handle, name, byref(status)) - Status.check (status) + Status.check(status) return name.value def __del__(self): -- cgit v1.2.3