aboutsummaryrefslogtreecommitdiffhomepage
path: root/Porting/pyfantom-py3.diff
blob: 95ccdd3031213ade60eeaf339f7bcda42a66dd21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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)