Week15 | Interface and Application Programming |
from Tkinter import * from numpy import log import serial WINDOW = 600 # window size eps = 0.5 # filter time constant filter = 0.0 # filtered value def idle(parent,canvas): global filter, eps # # idle routine # byte2 = 0 byte3 = 0 byte4 = 0 ser.flush() while 1: # # find framing # byte1 = byte2 byte2 = byte3 byte3 = byte4 byte4 = ord(ser.read()) if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)): break low = ord(ser.read()) high = ord(ser.read()) value = 256*high + low if (value > 511): value -= 1024 V = 2.5 - value*5.0/(20.0*512.0) R = 10000.0/(5.0/V-1.0) B = 3750.0 R25 = 10000.0 T = 1.0/(log(R/R25)/B+(1/(25.0+273.15))) - 273.15 filter = (1-eps)*filter +eps*T x = int(.2*WINDOW + (.9-.2)*WINDOW*(filter-20.0)/10.0) canvas.itemconfigure("text",text="%.4f"%(filter)) canvas.coords('rect1',.2*WINDOW,.25*WINDOW,x,.2*WINDOW) canvas.coords('rect2',x,.25*WINDOW,0.9*WINDOW,.2*WINDOW) canvas.coords('radio1',.2*WINDOW,.25*WINDOW,1*x,.2*WINDOW) canvas.coords('radio2',1*x,.25*WINDOW,0.9*WINDOW,.2*WINDOW) canvas.coords('pos',1*x,.35*WINDOW) canvas.update() parent.after_idle(idle,parent,canvas) # # check command line arguments # if (len(sys.argv) != 2): print ("command line: hello.temp.45.py serial_port") sys.exit() port = sys.argv[1] # # open serial port # ser = serial.Serial(port,9600) ser.setDTR() # # start plotting # root = Tk() root.title('Mi ventana J Lazarte (q to exit)') root.bind('q','exit') canvas = Canvas(root, width=WINDOW+50, height=.4*WINDOW, background='black') canvas.create_text(.4*WINDOW,.35*WINDOW,text='Temperature',font=("Helvetica", 24),tags=("pos","text"),fill="yellow") canvas.create_text(.55*WINDOW,.1*WINDOW,text='Temperature Measurement',font=("Helvetica", 24),fill="green") canvas.create_rectangle(.2*WINDOW,.25*WINDOW,.4*WINDOW,.4*WINDOW, tags='rect1', fill='yellow') canvas.create_rectangle(.3*WINDOW,.05*WINDOW,.9*WINDOW,.2*WINDOW, tags='rect2', fill='green') canvas.create_oval(0,150,150,300,tags='radio1', fill="red") canvas.create_oval(0,150,150,300,tags='radio2', fill="blue") canvas.pack() root.after(100,idle,root,canvas) root.mainloop() | Application Window |