diff --git a/pylepton/Lepton.py b/pylepton/Lepton.py index 082649c..9532f54 100755 --- a/pylepton/Lepton.py +++ b/pylepton/Lepton.py @@ -5,7 +5,8 @@ import struct import time -from ioctl_numbers import _IOR, _IOW +# relative imports in Python3 must be explicit +from .ioctl_numbers import _IOR, _IOW from fcntl import ioctl SPI_IOC_MAGIC = ord("k") @@ -77,7 +78,8 @@ def __init__(self, spi_dev = "/dev/spidev0.0"): 0) # __u32 pad; def __enter__(self): - self.__handle = open(self.__spi_dev, "w+") + # "In Python 3 the only way to open /dev/tty under Linux appears to be 1) in binary mode and 2) with buffering disabled." + self.__handle = open(self.__spi_dev, "wb+", buffering=0) ioctl(self.__handle, SPI_IOC_RD_MODE, struct.pack("=B", Lepton.MODE)) ioctl(self.__handle, SPI_IOC_WR_MODE, struct.pack("=B", Lepton.MODE)) @@ -152,7 +154,7 @@ def capture(self, data_buffer = None, log_time = False, debug_print = False, ret if retry_reset and (self.__capture_buf[20, 0] & 0xFF0F) != 0x1400: # make sure that this is a well-formed frame, should find line 20 here # Leave chip select deasserted for at least 185 ms to reset if debug_print: - print "Garbage frame number reset waiting..." + print("Garbage frame number reset waiting...") time.sleep(0.185) else: break @@ -163,16 +165,16 @@ def capture(self, data_buffer = None, log_time = False, debug_print = False, ret end = time.time() if debug_print: - print "---" + print("---") for i in range(Lepton.ROWS): fid = self.__capture_buf[i, 0, 0] crc = self.__capture_buf[i, 1, 0] fnum = fid & 0xFFF - print "0x{0:04x} 0x{1:04x} : Row {2:2} : crc={1}".format(fid, crc, fnum) - print "---" + print("0x{0:04x} 0x{1:04x} : Row {2:2} : crc={1}".format(fid, crc, fnum)) + print("---") if log_time: - print "frame processed int {0}s, {1}hz".format(end-start, 1.0/(end-start)) + print("frame processed int {0}s, {1}hz".format(end-start, 1.0/(end-start))) # TODO: turn on telemetry to get real frame id, sum on this array is fast enough though (< 500us) return data_buffer, data_buffer.sum() diff --git a/pylepton/__init__.py b/pylepton/__init__.py index 82eaf3b..5541eee 100644 --- a/pylepton/__init__.py +++ b/pylepton/__init__.py @@ -1,3 +1,4 @@ __all__ = ["Lepton"] -from Lepton import Lepton +# relative imports in Python3 must be explicit +from .Lepton import Lepton diff --git a/pylepton/ioctl_numbers.py b/pylepton/ioctl_numbers.py index b6e8de3..0c13fe6 100644 --- a/pylepton/ioctl_numbers.py +++ b/pylepton/ioctl_numbers.py @@ -39,7 +39,8 @@ def _IOC(dir, type, nr, size): - if isinstance(size, str) or isinstance(size, unicode): + # in Python3, unicode -> str; str -> bytes + if isinstance(size, bytes) or isinstance(size, str): size = struct.calcsize(size) return dir << _IOC_DIRSHIFT | \ type << _IOC_TYPESHIFT | \ diff --git a/pylepton_capture b/pylepton_capture index 613c414..59e1cec 100755 --- a/pylepton_capture +++ b/pylepton_capture @@ -31,7 +31,7 @@ if __name__ == '__main__': (options, args) = parser.parse_args() if len(args) < 1: - print "You must specify an output filename" + print("You must specify an output filename") sys.exit(1) image = capture(flip_v = options.flip_v, device = options.device)