# # hello.step.45.py # # receive and display step response (edited) # hello.step.45.py serial_port # # Neil Gershenfeld # CBA MIT 7/19/09 # # edited by Ryan Castonia # # (c) Massachusetts Institute of Technology 2009 # Permission granted for experimental and personal use; # license for commercial sale available from MIT # from Tkinter import * import serial WINDOW = 600 NSAMPLES = 254 MAX = 1040 saveflag = 0 index = 0 path = [] step = [] path_filt = [] step_filt = [] baseline = [] yscale = 1 xscale = 1 yoffset = 1 xoffset = 1 eps = .9 def idle(parent,canvas): global index, channel, baseline, path, path_filt, step, step_filt, saveflag # # idle routine # lo_dn = ord(ser.read()) hi_dn = ord(ser.read()) lo_up = ord(ser.read()) hi_up = ord(ser.read()) if ((lo_dn == 1) & (hi_dn == 2) & (lo_up == 3) & (hi_up == 4)): if (path_filt == []): path_filt = path step_filt = step else: for i in range(len(path_filt)): path_filt[i] = (1-eps)*path_filt[i] + eps*path[i] for i in range(len(step_filt)): step_filt[i] = (1-eps)*step_filt[i] + eps*step[i] if (step_filt[1] > 600): canvas.itemconfigure("alert",text="You lose!!!") if (300< step_filt[1] < 600): canvas.itemconfigure("alert",text="Wait for reset") if (step_filt[1] < 300): canvas.itemconfigure("alert",text="Go") canvas.coords('x0',0,.95*WINDOW,step_filt[0],WINDOW) index = 0 path = [] step = [] else: value_up = 256*hi_up + lo_up value_dn = 256*hi_dn + lo_dn value = (value_up + (1023-value_dn))/2.0 index += 2 step.insert(0,value) y = WINDOW-(yscale*(value/float(MAX)+yoffset))*WINDOW x = WINDOW-(xscale*(index/float(NSAMPLES)-xoffset))*WINDOW path.insert(0,y) path.insert(0,x) parent.after_idle(idle,parent,canvas) def save_data(parent): global saveflag saveflag = 1 def store_baseline(parent): global path_filt, baseline baseline = [] for i in range(len(path_filt)): baseline.append(path_filt[i]) # # check command line arguments # if (len(sys.argv) != 2): print "command line: python hello.step.45.py serial_port" sys.exit() port = sys.argv[1] # # open serial port # ser = serial.Serial(port,9600) ser.setDTR() ser.flush() # # find framing # print "finding framing ..." byte2 = 0 byte3 = 0 byte4 = 0 while 1: byte1 = byte2 byte2 = byte3 byte3 = byte4 byte4 = ord(ser.read()) if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)): print "start plotting" break # # set up GUI # root = Tk() root.title('hello.step.45.py') root.bind('q','exit') canvas = Canvas(root, width=WINDOW, height=WINDOW, background='white') canvas.create_text(.5*WINDOW,.7*WINDOW,font=("Helvetica", 24),tags="alert",fill="#0000b0") canvas.pack() # ioframe = Frame(root) quitbtn = Button(ioframe, text="quit") quitbtn.bind('','exit') quitbtn.pack(side="left") ioframe.pack() # # start idle loop # root.after(100,idle,root,canvas) root.mainloop()