Skip to content

Commit

Permalink
converted to Python3
Browse files Browse the repository at this point in the history
  • Loading branch information
brandoncurtis authored and kekiefer committed Apr 27, 2017
1 parent c80ae2c commit c856c6a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
16 changes: 9 additions & 7 deletions pylepton/Lepton.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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()
3 changes: 2 additions & 1 deletion pylepton/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__all__ = ["Lepton"]

from Lepton import Lepton
# relative imports in Python3 must be explicit
from .Lepton import Lepton
3 changes: 2 additions & 1 deletion pylepton/ioctl_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 | \
Expand Down
2 changes: 1 addition & 1 deletion pylepton_capture
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c856c6a

Please sign in to comment.