From 111d66d438755fe5dd712203af2535f0033abed8 Mon Sep 17 00:00:00 2001 From: Tat-Chee Wan (USM Signature) Date: Tue, 3 Jan 2017 08:28:38 +0800 Subject: Added python 3.x diff file created by 2to3. This was not tested with python 3 yet, but was reported to work. --- Porting/pyfantom-py3.diff | 258 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 258 insertions(+) create mode 100644 Porting/pyfantom-py3.diff diff --git a/Porting/pyfantom-py3.diff b/Porting/pyfantom-py3.diff new file mode 100644 index 0000000..95ccdd3 --- /dev/null +++ b/Porting/pyfantom-py3.diff @@ -0,0 +1,258 @@ +--- pyfantom.py (original) ++++ pyfantom.py (refactored) +@@ -43,7 +43,7 @@ + # Check platform. + platform_type = platform.system() + if DEBUG: +- print "Running on %s Platform" % platform_type ++ print("Running on %s Platform" % platform_type) + + if platform_type == 'Darwin': + import sys +@@ -65,7 +65,7 @@ + + if libpath is not None: + if DEBUG: +- print "Found Fantom Library (", libpath, ")" ++ print("Found Fantom Library (", libpath, ")") + else: + raise RuntimeError("Fantom Driver not found") + +@@ -117,7 +117,7 @@ + dll.nFANTOM100_isPaired.argtypes = [c_char_p, POINTER(c_int)] + dll.nFANTOM100_isPaired.restype = c_ushort + if DEBUG: +- print "Load Library Done" ++ print("Load Library Done") + + class FantomException(RuntimeError): + """Exception thrown on Fantom library error.""" +@@ -266,7 +266,7 @@ + def destroy(): + """To be used in destructor.""" + if self.debug: +- print "pyfantom: Destroying NXTIterator object." ++ print("pyfantom: Destroying NXTIterator object.") + status = StatusVar(0) + dll.nFANTOM100_destroyNXTIterator(self.handle, cast(byref(status), POINTER(c_int))) + Status.check(status) +@@ -277,14 +277,14 @@ + del self.nsapp + self.nsapp = None + if self.debug: +- print "pyfantom: NXTIterator object destroyed." ++ print("pyfantom: NXTIterator object destroyed.") + self.__destroy = destroy + + def __iter__(self): + """Return the iterator object itself.""" + return self + +- def next(self): ++ def __next__(self): + """Implement the iterator protocol.""" + if self.stop: + raise StopIteration() +@@ -334,7 +334,7 @@ + self.__destroy() + else: + if self.debug: +- print "pyfantom: No NXTIterator in __del__." ++ print("pyfantom: No NXTIterator in __del__.") + + class NXT: + """Interface to the NXT brick.""" +@@ -348,7 +348,7 @@ + self.handle = None + self.isBTLink = bluetooth # Not used for now + self.pool = None +- if isinstance(name_or_handle, basestring): ++ if isinstance(name_or_handle, str): + status = StatusVar(0) + handle = dll.nFANTOM100_createNXT(name_or_handle, cast(byref(status), POINTER(c_int)), + True) +@@ -363,20 +363,20 @@ + def destroy(): + """To be used in destructor.""" + if self.debug: +- print "pyfantom: Destroying NXT object." ++ print("pyfantom: Destroying NXT object.") + if self.handle is not None: + status = StatusVar(0) + dll.nFANTOM100_destroyNXT(self.handle, cast(byref(status), POINTER(c_int))) + Status.check(status) + self.handle = None + if self.debug: +- print "pyfantom: NXT object destroyed." ++ print("pyfantom: NXT object destroyed.") + if self.pool is not None: + del self.pool + self.pool = None + else: + if self.debug: +- print "pyfantom: NXT handle is None!" ++ print("pyfantom: NXT handle is None!") + self.__destroy = destroy + + def get_firmware_version(self): +@@ -401,7 +401,7 @@ + ret = dll.nFANTOM100_iNXT_write(self.handle, data_buffer, len(data), + cast(byref(status), POINTER(c_int))) + if self.debug: +- print "pyfantom: write() sent ", ret, " of ", len(data), " bytes." ++ print("pyfantom: write() sent ", ret, " of ", len(data), " bytes.") + Status.check(status) + return ret + +@@ -412,7 +412,7 @@ + ret = dll.nFANTOM100_iNXT_read(self.handle, data_buffer, length, + cast(byref(status), POINTER(c_int))) + if self.debug: +- print "pyfantom: read() returned ", ret, " of ", length, " bytes." ++ print("pyfantom: read() returned ", ret, " of ", length, " bytes.") + try: + Status.check(status) + except FantomException as err: +@@ -434,7 +434,7 @@ + Status.check(status) + if requireResponse: + if self.debug: +- print "send_direct_command() response: ", struct.unpack('%dB' % len(response_buffer), response_buffer) ++ print("send_direct_command() response: ", struct.unpack('%dB' % len(response_buffer), response_buffer)) + return response_buffer.raw[0:ret-1] # ret count includes Status byte, but Status byte is not included in message buffer + else: + return "" +@@ -491,12 +491,12 @@ + + def close(self): + if self.debug: +- print "pyfantom: close()" ++ print("pyfantom: close()") + if self.handle is not None: + self.__destroy() + else: + if self.debug: +- print "pyfantom: No NXT in close()." ++ print("pyfantom: No NXT in close().") + + def pair_bluetooth(self, resource, pin=BT_PIN): + btpin = ctypes.create_string_buffer(256) +@@ -525,7 +525,7 @@ + self.__destroy() + else: + if self.debug: +- print "pyfantom: No NXT in __del__." ++ print("pyfantom: No NXT in __del__.") + + if __name__ == '__main__': + import struct +@@ -548,19 +548,19 @@ + return NXT(rs) + + def get_nxt_info(i): +- print "resource string:", i.get_resource_string() +- print " firmware version:", i.get_firmware_version() +- print " get device info:", i.get_device_info() ++ print("resource string:", i.get_resource_string()) ++ print(" firmware version:", i.get_firmware_version()) ++ print(" get device info:", i.get_device_info()) + + def play_tone_nxt(i): + # Play Tone using Send Direct Command +- print "send direct command (tone)..." ++ print("send direct command (tone)...") + cmd = struct.pack('5B', 0x03, 0x00, 0x08, 0x10, 0x00) + rep = i.send_direct_command(cmd) +- print "direct command returned ", len(rep), " data bytes" ++ print("direct command returned ", len(rep), " data bytes") + time.sleep(1) + # Play Tone using Send Raw Command +- print "send raw command (tone)..." ++ print("send raw command (tone)...") + cmd = struct.pack('6B', 0x80, 0x03, 0x00, 0x18, 0x10, 0x00) + r = i.write(cmd) + +@@ -575,11 +575,11 @@ + # BATT_LEVEL: 0x0B + # VOLTAGE milivolts + # VOLTAGE milivolts +- print "send direct command (battery level)..." ++ print("send direct command (battery level)...") + cmd = struct.pack('1B', 0x0B) + rep = i.send_direct_command(cmd, 4, True) +- print "direct command returned ", len(rep), " data bytes" +- print "Battery Level", struct.unpack('%dB' % len(rep), rep) ++ print("direct command returned ", len(rep), " data bytes") ++ print("Battery Level", struct.unpack('%dB' % len(rep), rep)) + + # Use Send Raw Command to read Battery level + # Write GETBATTERYLEVEL DIRECT_CMD. +@@ -591,12 +591,12 @@ + # BATT_LEVEL: 0x0B + # VOLTAGE milivolts + # VOLTAGE milivolts +- print "send raw command (battery level)..." ++ print("send raw command (battery level)...") + cmd = struct.pack('2B', 0x00, 0x0B) + r = i.write(cmd) +- print "wrote", r ++ print("wrote", r) + rep = i.read(MAXBUFLEN) +- print "Battery Level", struct.unpack('%dB' % len(rep), rep) ++ print("Battery Level", struct.unpack('%dB' % len(rep), rep)) + + + +@@ -606,10 +606,10 @@ + # Query: + # SYS_CMD: 0x01 + # VERSION: 0x88 +- print "send raw command (version system command)..." ++ print("send raw command (version system command)...") + cmd = struct.pack('2B', 0x01, 0x88) + r = i.write(cmd) +- print "wrote", r ++ print("wrote", r) + # Response (7 data bytes): + # REPLY_CMD: 0x02 + # VERSION: 0x88 +@@ -619,11 +619,11 @@ + # FIRMWARE_VERSION minor + # FIRMWARE_VERSION major + rep = i.read(readbuflen) +- print "read", struct.unpack('%dB' % len(rep), rep) ++ print("read", struct.unpack('%dB' % len(rep), rep)) + + def read_emptybuf_nxt(i, readbuflen=MAXBUFLEN): + rep = i.read(readbuflen) +- print "read", struct.unpack('%dB' % len(rep), rep) ++ print("read", struct.unpack('%dB' % len(rep), rep)) + + + # check_bt == True: Bluetooth Interface +@@ -636,10 +636,10 @@ + empty_readbuf = False + + +- print "Retrieving list of NXT objects" ++ print("Retrieving list of NXT objects") + nxtlist = get_nxt_from_iterator(check_bt) + for i in nxtlist: +- print "nxtlist: ", i ++ print("nxtlist: ", i) + if get_info: + get_nxt_info(i) + if play_tone: +@@ -653,10 +653,10 @@ + i.close() + del i + +- print "Creating NXT objects from list of Resource Strings" ++ print("Creating NXT objects from list of Resource Strings") + nxtresourcelist = get_resource_string_from_iterator(check_bt) + for rs in nxtresourcelist: +- print "nxtresourcelist: ", rs ++ print("nxtresourcelist: ", rs) + i = NXT(rs, check_bt) + if get_info: + get_nxt_info(i) -- cgit v1.2.3