Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Commit 521309d

Browse files
committed
FTH232H: Make compatible with Python3
- Fix some indentations - Convert use of str() to bytes() - Append b' to byte literals - Fix some integer divisions / -> //
1 parent a92a23d commit 521309d

File tree

1 file changed

+69
-69
lines changed

1 file changed

+69
-69
lines changed

Adafruit_GPIO/FT232H.py

+69-69
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def __init__(self, vid=FT232H_VID, pid=FT232H_PID, serial=None):
164164
self._mpsse_enable()
165165
self._mpsse_sync()
166166
# Initialize all GPIO as inputs.
167-
self._write('\x80\x00\x00\x82\x00\x00')
167+
self._write(b'\x80\x00\x00\x82\x00\x00')
168168
self._direction = 0x0000
169169
self._level = 0x0000
170170

@@ -181,17 +181,17 @@ def _write(self, string):
181181
# Get modem status. Useful to enable for debugging.
182182
#ret, status = ftdi.poll_modem_status(self._ctx)
183183
#if ret == 0:
184-
# logger.debug('Modem status {0:02X}'.format(status))
184+
# logger.debug('Modem status {0:02X}'.format(status))
185185
#else:
186-
# logger.debug('Modem status error {0}'.format(ret))
186+
# logger.debug('Modem status error {0}'.format(ret))
187187
length = len(string)
188188
try:
189189
ret = ftdi.write_data(self._ctx, string, length)
190190
except TypeError:
191191
ret = ftdi.write_data(self._ctx, string); #compatible with libFtdi 1.3
192192
# Log the string that was written in a python hex string format using a very
193193
# ugly one-liner list comprehension for brevity.
194-
#logger.debug('Wrote {0}'.format(''.join(['\\x{0:02X}'.format(ord(x)) for x in string])))
194+
#logger.debug('Wrote {0}'.format(''.join(['\\x{0:02X}'.format(x) for x in bytearray(string)])))
195195
if ret < 0:
196196
raise RuntimeError('ftdi_write_data failed with error {0}: {1}'.format(ret, ftdi.get_error_string(self._ctx)))
197197
if ret != length:
@@ -227,7 +227,7 @@ def _poll_read(self, expected, timeout_s=5.0):
227227
index += ret
228228
# Buffer is full, return the result data.
229229
if index >= expected:
230-
return str(response)
230+
return bytes(response)
231231
time.sleep(0.01)
232232
raise RuntimeError('Timeout while polling ftdi_read_data for {0} bytes!'.format(expected))
233233

@@ -243,14 +243,14 @@ def _mpsse_sync(self, max_retries=10):
243243
error response. Should be called once after enabling MPSSE."""
244244
# Send a bad/unknown command (0xAB), then read buffer until bad command
245245
# response is found.
246-
self._write('\xAB')
246+
self._write(b'\xAB')
247247
# Keep reading until bad command response (0xFA 0xAB) is returned.
248248
# Fail if too many read attempts are made to prevent sticking in a loop.
249249
tries = 0
250250
sync = False
251251
while not sync:
252252
data = self._poll_read(2)
253-
if data == '\xFA\xAB':
253+
if data == b'\xFA\xAB':
254254
sync = True
255255
tries += 1
256256
if tries >= max_retries:
@@ -261,20 +261,20 @@ def mpsse_set_clock(self, clock_hz, adaptive=False, three_phase=False):
261261
to 30mhz and will pick that speed or the closest speed below it.
262262
"""
263263
# Disable clock divisor by 5 to enable faster speeds on FT232H.
264-
self._write('\x8A')
264+
self._write(b'\x8A')
265265
# Turn on/off adaptive clocking.
266266
if adaptive:
267-
self._write('\x96')
267+
self._write(b'\x96')
268268
else:
269-
self._write('\x97')
269+
self._write(b'\x97')
270270
# Turn on/off three phase clock (needed for I2C).
271271
# Also adjust the frequency for three-phase clocking as specified in section 2.2.4
272272
# of this document:
273273
# http://www.ftdichip.com/Support/Documents/AppNotes/AN_255_USB%20to%20I2C%20Example%20using%20the%20FT232H%20and%20FT201X%20devices.pdf
274274
if three_phase:
275-
self._write('\x8C')
275+
self._write(b'\x8C')
276276
else:
277-
self._write('\x8D')
277+
self._write(b'\x8D')
278278
# Compute divisor for requested clock.
279279
# Use equation from section 3.8.1 of:
280280
# http://www.ftdichip.com/Support/Documents/AppNotes/AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
@@ -284,14 +284,14 @@ def mpsse_set_clock(self, clock_hz, adaptive=False, three_phase=False):
284284
divisor = int(divisor*(2.0/3.0))
285285
logger.debug('Setting clockspeed with divisor value {0}'.format(divisor))
286286
# Send command to set divisor from low and high byte values.
287-
self._write(str(bytearray((0x86, divisor & 0xFF, (divisor >> 8) & 0xFF))))
287+
self._write(bytes(bytearray((0x86, divisor & 0xFF, (divisor >> 8) & 0xFF))))
288288

289289
def mpsse_read_gpio(self):
290290
"""Read both GPIO bus states and return a 16 bit value with their state.
291291
D0-D7 are the lower 8 bits and C0-C7 are the upper 8 bits.
292292
"""
293293
# Send command to read low byte and high byte.
294-
self._write('\x81\x83')
294+
self._write(b'\x81\x83')
295295
# Wait for 2 byte response.
296296
data = self._poll_read(2)
297297
# Assemble response into 16 bit value.
@@ -304,11 +304,11 @@ def mpsse_gpio(self):
304304
"""Return command to update the MPSSE GPIO state to the current direction
305305
and level.
306306
"""
307-
level_low = chr(self._level & 0xFF)
308-
level_high = chr((self._level >> 8) & 0xFF)
309-
dir_low = chr(self._direction & 0xFF)
310-
dir_high = chr((self._direction >> 8) & 0xFF)
311-
return str(bytearray((0x80, level_low, dir_low, 0x82, level_high, dir_high)))
307+
level_low = (self._level & 0xFF)
308+
level_high = ((self._level >> 8) & 0xFF)
309+
dir_low = (self._direction & 0xFF)
310+
dir_high = ((self._direction >> 8) & 0xFF)
311+
return bytes(bytearray((0x80, level_low, dir_low, 0x82, level_high, dir_high)))
312312

313313
def mpsse_write_gpio(self):
314314
"""Write the current MPSSE GPIO state to the FT232H chip."""
@@ -481,21 +481,21 @@ def write(self, data):
481481
# Compute length low and high bytes.
482482
# NOTE: Must actually send length minus one because the MPSSE engine
483483
# considers 0 a length of 1 and FFFF a length of 65536
484-
# splitting into two lists for two commands to prevent buffer errors
485-
data1 = data[:len(data)/2]
486-
data2 = data[len(data)/2:]
484+
# splitting into two lists for two commands to prevent buffer errors
485+
data1 = data[:len(data)//2]
486+
data2 = data[len(data)//2:]
487487
len_low1 = (len(data1) - 1) & 0xFF
488488
len_high1 = ((len(data1) - 1) >> 8) & 0xFF
489-
len_low2 = (len(data2) - 1) & 0xFF
489+
len_low2 = (len(data2) - 1) & 0xFF
490490
len_high2 = ((len(data2) - 1) >> 8) & 0xFF
491491
self._assert_cs()
492492
# Send command and length, then data, split into two commands, handle for length 1
493-
if len(data1) > 0:
494-
self._ft232h._write(str(bytearray((command, len_low1, len_high1))))
495-
self._ft232h._write(str(bytearray(data1)))
493+
if len(data1) > 0:
494+
self._ft232h._write(bytes(bytearray((command, len_low1, len_high1))))
495+
self._ft232h._write(bytes(bytearray(data1)))
496496
if len(data2) > 0:
497-
self._ft232h._write(str(bytearray((command, len_low2, len_high2))))
498-
self._ft232h._write(str(bytearray(data2)))
497+
self._ft232h._write(bytes(bytearray((command, len_low2, len_high2))))
498+
self._ft232h._write(bytes(bytearray(data2)))
499499
self._deassert_cs()
500500

501501
def read(self, length):
@@ -513,21 +513,21 @@ def read(self, length):
513513
# Compute length low and high bytes.
514514
# NOTE: Must actually send length minus one because the MPSSE engine
515515
# considers 0 a length of 1 and FFFF a length of 65536
516-
#force odd numbers to round up instead of down
517-
lengthR = length
518-
if length % 2 == 1:
519-
lengthR += 1
520-
lengthR = lengthR/2
521-
#when odd length requested, get the remainder instead of the same number
522-
lenremain = length - lengthR
516+
#force odd numbers to round up instead of down
517+
lengthR = length
518+
if length % 2 == 1:
519+
lengthR += 1
520+
lengthR = lengthR//2
521+
#when odd length requested, get the remainder instead of the same number
522+
lenremain = length - lengthR
523523
len_low = (lengthR - 1) & 0xFF
524524
len_high = ((lengthR - 1) >> 8) & 0xFF
525525
self._assert_cs()
526526
# Send command and length.
527527
# Perform twice to prevent error from hardware defect/limits
528-
self._ft232h._write(str(bytearray((command, len_low, len_high))))
528+
self._ft232h._write(bytes(bytearray((command, len_low, len_high))))
529529
payload1 = self._ft232h._poll_read(lengthR)
530-
self._ft232h._write(str(bytearray((command, len_low, len_high))))
530+
self._ft232h._write(bytes(bytearray((command, len_low, len_high))))
531531
payload2 = self._ft232h._poll_read(lenremain)
532532
self._deassert_cs()
533533
# Read response bytes
@@ -559,12 +559,12 @@ def bulkread(self, data = [], lengthR = 'None', readmode = 1):
559559
len_highW = ((lengthW) >> 8) & 0xFF
560560
commandR = 0x20 | (self.lsbfirst << 3) | (self.read_clock_ve << 2)
561561
#force odd numbers to round up instead of down
562-
length = lengthR
563-
if lengthR % 2 == 1:
564-
length += 1
565-
length = length/2
562+
length = lengthR
563+
if lengthR % 2 == 1:
564+
length += 1
565+
length = length//2
566566
#when odd length requested, get the remainder instead of the same number
567-
lenremain = lengthR - length
567+
lenremain = lengthR - length
568568
len_lowR = (length - 1) & 0xFF
569569
len_highR = ((length - 1) >> 8) & 0xFF
570570
#logger debug info
@@ -573,12 +573,12 @@ def bulkread(self, data = [], lengthR = 'None', readmode = 1):
573573
#begin command set
574574
self._assert_cs()
575575
#write command, these have to be separated due to TypeError
576-
self._ft232h._write(str(bytearray((commandW, len_lowW, len_highW))))
577-
self._ft232h._write(str(bytearray(data)))
576+
self._ft232h._write(bytes(bytearray((commandW, len_lowW, len_highW))))
577+
self._ft232h._write(bytes(bytearray(data)))
578578
#read command, which is divided into two commands
579-
self._ft232h._write(str(bytearray((commandR, len_lowR, len_highR))))
579+
self._ft232h._write(bytes(bytearray((commandR, len_lowR, len_highR))))
580580
payload1 = self._ft232h._poll_read(length)
581-
self._ft232h._write(str(bytearray((commandR, len_lowR, len_highR))))
581+
self._ft232h._write(bytes(bytearray((commandR, len_lowR, len_highR))))
582582
payload2 = self._ft232h._poll_read(lenremain)
583583
self._deassert_cs()
584584
#end command set
@@ -601,26 +601,26 @@ def transfer(self, data):
601601
# Compute length low and high bytes.
602602
# NOTE: Must actually send length minus one because the MPSSE engine
603603
# considers 0 a length of 1 and FFFF a length of 65536
604-
data1 = data[:len(data)/2]
605-
data2 = data[len(data)/2:]
606-
len_low1 = (len(data1) - 1) & 0xFF
604+
data1 = data[:len(data)//2]
605+
data2 = data[len(data)//2:]
606+
len_low1 = (len(data1) - 1) & 0xFF
607607
len_high1 = ((len(data1) - 1) >> 8) & 0xFF
608-
len_low2 = (len(data2) - 1) & 0xFF
608+
len_low2 = (len(data2) - 1) & 0xFF
609609
len_high2 = ((len(data2) - 1) >> 8) & 0xFF
610-
payload1 = ''
611-
payload2 = ''
612-
#start command set
610+
payload1 = ''
611+
payload2 = ''
612+
#start command set
613613
self._assert_cs()
614614
# Perform twice to prevent error from hardware defect/limits
615-
# Send command and length, then data, split into two commands, handle for length 1
616-
if len(data1) > 0:
617-
self._ft232h._write(str(bytearray((command, len_low1, len_high1))))
618-
self._ft232h._write(str(bytearray(data1)))
619-
payload1 = self._ft232h._poll_read(len(data1))
620-
if len(data2) > 0:
621-
self._ft232h._write(str(bytearray((command, len_low2, len_high2))))
622-
self._ft232h._write(str(bytearray(data2)))
623-
payload2 = self._ft232h._poll_read(len(data2))
615+
# Send command and length, then data, split into two commands, handle for length 1
616+
if len(data1) > 0:
617+
self._ft232h._write(bytes(bytearray((command, len_low1, len_high1))))
618+
self._ft232h._write(bytes(bytearray(data1)))
619+
payload1 = self._ft232h._poll_read(len(data1))
620+
if len(data2) > 0:
621+
self._ft232h._write(bytes(bytearray((command, len_low2, len_high2))))
622+
self._ft232h._write(bytes(bytearray(data2)))
623+
payload2 = self._ft232h._poll_read(len(data2))
624624
#self._ft232h._write('\x87')
625625
self._deassert_cs()
626626
# Read response bytes.
@@ -642,7 +642,7 @@ def __init__(self, ft232h, address, clock_hz=100000):
642642
# Enable drive-zero mode to drive outputs low on 0 and tri-state on 1.
643643
# This matches the protocol for I2C communication so multiple devices can
644644
# share the I2C bus.
645-
self._ft232h._write('\x9E\x07\x00')
645+
self._ft232h._write(b'\x9E\x07\x00')
646646
self._idle()
647647

648648
def _idle(self):
@@ -660,9 +660,9 @@ def _transaction_start(self):
660660
def _transaction_end(self):
661661
"""End I2C transaction and get response bytes, including ACKs."""
662662
# Ask to return response bytes immediately.
663-
self._command.append('\x87')
663+
self._command.append(b'\x87')
664664
# Send the entire command to the MPSSE.
665-
self._ft232h._write(''.join(self._command))
665+
self._ft232h._write(b''.join(self._command))
666666
# Read response bytes and return them.
667667
return bytearray(self._ft232h._poll_read(self._expected))
668668

@@ -703,12 +703,12 @@ def _i2c_read_bytes(self, length=1):
703703
"""
704704
for i in range(length-1):
705705
# Read a byte and send ACK.
706-
self._command.append('\x20\x00\x00\x13\x00\x00')
706+
self._command.append(b'\x20\x00\x00\x13\x00\x00')
707707
# Make sure pins are back in idle state with clock low and data high.
708708
self._ft232h.output_pins({0: GPIO.LOW, 1: GPIO.HIGH}, write=False)
709709
self._command.append(self._ft232h.mpsse_gpio())
710710
# Read last byte and send NAK.
711-
self._command.append('\x20\x00\x00\x13\x00\xFF')
711+
self._command.append(b'\x20\x00\x00\x13\x00\xFF')
712712
# Make sure pins are back in idle state with clock low and data high.
713713
self._ft232h.output_pins({0: GPIO.LOW, 1: GPIO.HIGH}, write=False)
714714
self._command.append(self._ft232h.mpsse_gpio())
@@ -719,12 +719,12 @@ def _i2c_write_bytes(self, data):
719719
"""Write the specified number of bytes to the chip."""
720720
for byte in data:
721721
# Write byte.
722-
self._command.append(str(bytearray((0x11, 0x00, 0x00, byte))))
722+
self._command.append(bytes(bytearray((0x11, 0x00, 0x00, byte))))
723723
# Make sure pins are back in idle state with clock low and data high.
724724
self._ft232h.output_pins({0: GPIO.LOW, 1: GPIO.HIGH}, write=False)
725725
self._command.append(self._ft232h.mpsse_gpio() * _REPEAT_DELAY)
726726
# Read bit for ACK/NAK.
727-
self._command.append('\x22\x00')
727+
self._command.append(b'\x22\x00')
728728
# Increase expected response bytes.
729729
self._expected += len(data)
730730

0 commit comments

Comments
 (0)