diff --git a/examples/start_bootloader.py b/examples/start_bootloader.py index 7fff842..0e6f51e 100644 --- a/examples/start_bootloader.py +++ b/examples/start_bootloader.py @@ -4,3 +4,4 @@ u2f = USB2FIR() u2f.start_bootloader() +u2f.close() diff --git a/examples/view_matplot.py b/examples/view_matplot.py index b56c75e..1a1b763 100644 --- a/examples/view_matplot.py +++ b/examples/view_matplot.py @@ -12,15 +12,14 @@ plt.ion() ir = frame.reshape((24, 32)) -graph = plt.imshow(ir,interpolation='none') +graph = plt.imshow(ir, interpolation='none') plt.colorbar() -plt.clim(18,35) +plt.clim(18, 35) plt.draw() try: - while(1): - + while 1: u2f.updateFrame(frame) ir = frame.reshape((24, 32))[:, ::-1] @@ -30,7 +29,8 @@ plt.pause(0.0001) except KeyboardInterrupt: - print("CTRL-C: Program Stopping via Keyboard Interrupt...") + u2f.close() + print("CTRL-C: Program Stopping via Keyboard Interrupt...") finally: print("Exiting Loop") diff --git a/examples/view_tk.py b/examples/view_tk.py index 80c073e..4ea8f85 100644 --- a/examples/view_tk.py +++ b/examples/view_tk.py @@ -1,7 +1,9 @@ -import Tkinter as tk -import Queue -from pyusb2fir import USB2FIR -import threading +import queue +import threading +import tkinter as tk + +from pyUSB2FIR.pyusb2fir.usb2fir import USB2FIR + def rgb(minimum, maximum, value): minimum, maximum = float(minimum), float(maximum) @@ -37,9 +39,7 @@ def __init__(self, *args, **kwargs): self.label_temp = tk.Label(textvariable=self.tempstr) self.label_temp.pack() - - self.queue = Queue.Queue() - + self.queue = queue.Queue() self.updateMap() def updateMap(self): @@ -47,7 +47,7 @@ def updateMap(self): try: tempvalues = self.queue.get_nowait() self.setTempValues(tempvalues) - except Queue.Empty: + except queue.Empty: pass self.after(100, self.updateMap) @@ -95,4 +95,7 @@ def setTempValues(self, tempvalues): t.start() app.mainloop() + t.do_run = False + +u2f.close() diff --git a/pyusb2fir/__init__.py b/pyusb2fir/__init__.py index dd310f1..52ac072 100644 --- a/pyusb2fir/__init__.py +++ b/pyusb2fir/__init__.py @@ -15,4 +15,4 @@ # You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE # along with pyUSB2FIR. If not, see -from .usb2fir import USB2FIR \ No newline at end of file +from .usb2fir import USB2FIR diff --git a/pyusb2fir/usb2fir.py b/pyusb2fir/usb2fir.py index f5d95b0..3cc56a9 100644 --- a/pyusb2fir/usb2fir.py +++ b/pyusb2fir/usb2fir.py @@ -16,8 +16,8 @@ # along with pyUSB2FIR. If not, see import libusb1 -import usb1 import numpy as np +import usb1 USB2FIR_VID = 0x04D8 USB2FIR_PID = 0xEE7D @@ -46,42 +46,42 @@ def uint4_to_int4(i): else: return i + def uint6_to_int6(i): if i > 31: return i - 64 else: return i + def uint8_to_int8(i): if i > 127: return i - 256 else: return i + def uint10_to_int10(i): if i > 511: return i - 1024 else: return i + def uint16_to_int16(i): if i > 32767: return i - 65536 else: return i -class MLXCommonParameters: - +class MLXCommonParameters: def __init__(self, eepromdata): - - # extract VDD sensor parameters self.kVdd = uint8_to_int8(eepromdata[0x33] >> 8) * 32 self.vdd25 = ((eepromdata[0x33] & 0xff) - 256) * 32 - 8192 - # extract Ta sensor parameters self.KvPTAT = eepromdata[0x32] >> 10 @@ -98,7 +98,6 @@ def __init__(self, eepromdata): self.alphaPTAT = (eepromdata[0x10] >> 12) / 4.0 + 8.0 - # extract offset offsetAverage = uint16_to_int16(eepromdata[0x11]) @@ -126,18 +125,18 @@ def __init__(self, eepromdata): # extract sensitivity alphaRef = eepromdata[0x21] - alphaScale = (eepromdata[0x20] >> 12) + 30 - accColumnScale = (eepromdata[0x20] & 0x00F0) >> 4; - accRowScale = (eepromdata[0x20] & 0x0F00) >> 8; - accRemScale = eepromdata[0x20] & 0x000F; + alphaScale = int((eepromdata[0x20] >> 12) + 30) + accColumnScale = (eepromdata[0x20] & 0x00F0) >> 4 + accRowScale = (eepromdata[0x20] & 0x0F00) >> 8 + accRemScale = eepromdata[0x20] & 0x000F accRow = [] for i in range(24): - accRow.append(uint4_to_int4((eepromdata[0x22 + i / 4] >> ((i % 4) * 4)) & 0xF)) + accRow.append(uint4_to_int4((eepromdata[0x22 + i // 4] >> ((i % 4) * 4)) & 0xF)) accColumn = [] for i in range(32): - accColumn.append(uint4_to_int4((eepromdata[0x28 + i / 4] >> ((i % 4) * 4)) & 0xF)) + accColumn.append(uint4_to_int4((eepromdata[0x28 + i // 4] >> ((i % 4) * 4)) & 0xF)) self.alpha = [] for i in range(24): @@ -198,7 +197,7 @@ def __init__(self, eepromdata): # extract corner temperatures - step = ((eepromdata[0x3F] & 0x3000) >> 12) * 10; + step = ((eepromdata[0x3F] & 0x3000) >> 12) * 10 self.ct = [-40, 0, 0, 0] self.ct[2] = ((eepromdata[0x3F] & 0x00F0) >> 4) * step self.ct[3] = ((eepromdata[0x3F] & 0x0F00) >> 8) * step + self.ct[2] @@ -213,7 +212,7 @@ def __init__(self, eepromdata): # extract the sensitivity alphaCP - alphaScale = ((eepromdata[0x20] & 0xF000) >> 12) + 27 + alphaScale = int(((eepromdata[0x20] & 0xF000) >> 12) + 27) self.cpAlpha = [0.0, 0.0] self.cpAlpha[0] = (uint10_to_int10(eepromdata[0x39] & 0x03FF) + 0.0) / (1 << alphaScale) self.cpAlpha[1] = uint6_to_int6((eepromdata[0x39] & 0xFC00) >> 10) + 0.0 @@ -241,14 +240,14 @@ def __init__(self, eepromdata): self.tgc = uint8_to_int8(eepromdata[0x3C] & 0x0ff) / 32.0 # extract resolution setting - self.resolutionEE = (eepromdata[0x38] & 0x3000) >> 12; + self.resolutionEE = (eepromdata[0x38] & 0x3000) >> 12 self.alphaCorrR = [0] * 4 self.alphaCorrR[0] = 1 / (1 + self.ksTo[0] * 40) self.alphaCorrR[1] = 1 - self.alphaCorrR[2] = (1 + self.ksTo[2] * self.ct[2]); - self.alphaCorrR[3] = self.alphaCorrR[2] * (1 + self.ksTo[3] * (self.ct[3] - self.ct[2])); + self.alphaCorrR[2] = (1 + self.ksTo[2] * self.ct[2]) + self.alphaCorrR[3] = self.alphaCorrR[2] * (1 + self.ksTo[3] * (self.ct[3] - self.ct[2])) @@ -258,8 +257,10 @@ def __init__(self, i2caddress=0x33, refreshRate=3): Initialize and open connection to USB2FIR. """ ctx = usb1.LibUSBContext() + self.usbdev = ctx.getByVendorIDAndProductID(USB2FIR_VID, USB2FIR_PID) self.usbhandle = self.usbdev.open() + self.usbhandle.claimInterface(0) self.i2caddress = i2caddress @@ -365,7 +366,7 @@ def updateFrame(self, frame): ta = (ptatArt / (1 + self.commonParameters.KvPTAT * (vdd - 3.3)) - self.commonParameters.vPTAT25) ta = ta / self.commonParameters.KtPTAT + 25 - tr = ta - 8; + tr = ta - 8 ta4 = np.power((ta + 273.15), 4) tr4 = np.power((tr + 273.15), 4) @@ -384,16 +385,19 @@ def updateFrame(self, frame): irData = uint16_to_int16(irData) + 0.0 irData = irData * gain irData = irData - self.commonParameters.offset[pixelidx] * (1 + self.commonParameters.kta[pixelidx] * (ta - 25)) * (1 + self.commonParameters.kv[pixelidx] * (vdd - 3.3)) - irData = irData / emissivity; + irData = irData / emissivity irData = irData - self.commonParameters.tgc * irDataCP - alphaCompensated = (self.commonParameters.alpha[pixelidx] - self.commonParameters.tgc * self.commonParameters.cpAlpha[subpage]) * (1 + self.commonParameters.KsTa * (ta - 25)); + alphaCompensated = (self.commonParameters.alpha[pixelidx] - self.commonParameters.tgc * + self.commonParameters.cpAlpha[subpage]) * ( + 1 + self.commonParameters.KsTa * (ta - 25)) - Sx = np.power(alphaCompensated, 3) * (irData + alphaCompensated * taTr); - Sx = np.sqrt(np.sqrt(Sx)) * self.commonParameters.ksTo[1]; + Sx = np.power(alphaCompensated, 3) * (irData + alphaCompensated * taTr) + Sx = np.sqrt(np.sqrt(Sx)) * self.commonParameters.ksTo[1] - To = np.sqrt(np.sqrt(irData / (alphaCompensated * (1 - self.commonParameters.ksTo[1] * 273.15) + Sx) + taTr)) - 273.15; + To = np.sqrt(np.sqrt( + irData / (alphaCompensated * (1 - self.commonParameters.ksTo[1] * 273.15) + Sx) + taTr)) - 273.15 if To < self.commonParameters.ct[1]: r = 0 @@ -403,10 +407,13 @@ def updateFrame(self, frame): r = 2 else: r = 3 - - To = np.sqrt(np.sqrt(irData / (alphaCompensated * self.commonParameters.alphaCorrR[r] * (1 + self.commonParameters.ksTo[r] * (To - self.commonParameters.ct[r]))) + taTr)) - 273.15; + + To = np.sqrt(np.sqrt(irData / (alphaCompensated * self.commonParameters.alphaCorrR[r] * ( + 1 + self.commonParameters.ksTo[r] * (To - self.commonParameters.ct[r]))) + taTr)) - 273.15 frame[pixelidx] = To pixelidx = pixelidx + 2 + def close(self): + self.usbhandle.close() + - diff --git a/setup.py b/setup.py index eb75139..4f04778 100644 --- a/setup.py +++ b/setup.py @@ -17,10 +17,12 @@ from setuptools import setup + def readme(): with open("README.rst") as f: return f.read() + setup(name='pyusb2fir', version='1.0', description='USB2FIR - Interface for far infrared thermal sensor array MLX90640', @@ -39,4 +41,4 @@ def readme(): 'libusb1', 'numpy' ], - zip_safe=False) \ No newline at end of file + zip_safe=False)