#
# hello3.step.py
# receive and plot step response
# Neil Gershenfeld CBA MIT
# 10/29/05
#
from Tkinter import *
import serial

WINDOW = 400
NSAMPLES = 254
MAX = 1040
MAX_VOLTAGE = 5.0
eps = .9
saveflag = 0
index = 0
path = []
step = []
path_filt = []
step_filt = []
baseline = []

def idle(parent,canvas):
   global index, channel, baseline, path, path_filt, step, step_filt, saveflag
   #
   # idle routine
   #
   eps = float(sfilter.get())
 #  lo_dn = ord(ser.read())
 #  hi_dn = ord(ser.read())
   uplo = ord(ser.read())
   uphi = ord(ser.read())
      
   #convert these two to one number
   voltage_value = 256*uphi + uplo # this number is between 0-1023
   #convert to 0-5 volt values
   voltage_value_normalized = float( voltage_value / 1023.0 ) # range [0,1]
   voltage_value_volts = voltage_value_normalized * MAX_VOLTAGE # range [0,5]
   vvsn = int ( voltage_value / 50 ) # correlate voltage value to text size
   # vvsn stands for voltage value size normalized
   r=int (voltage_value_volts * 51)
   b=int (255 - r)
   g=0			
   vvcn =  "#%02x%02x%02x"% (r,g,b)   
   # vvsn stands for voltage value color normalized

   #read 1 2 3 4 again and ignore
   temp1 = ord(ser.read())
   temp2 = ord(ser.read())
   temp3 = ord(ser.read())
   temp4 = ord(ser.read())

   print "-------"
   print temp1, temp2, temp3, temp4
   print uphi, uplo, voltage_value, voltage_value_volts

   #show convetred value on screen
   

   canvas.delete("voltage")
   canvas.create_text(voltage_value_normalized*WINDOW,voltage_value_normalized*WINDOW,font=("Helvetica", vvsn),tags="voltage",fill=vvcn)
   canvas.itemconfigure("voltage",text="%.2f [V]"%voltage_value_volts)	

   parent.after_idle(idle,parent,canvas)

"""
   lo_dn =0
   hi_dn =0
   lo_up =0
   hi_up =0
   


   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]
      canvas.delete("path")
      canvas.create_line(path_filt,tag="path",width=3,fill="#00b000")
      if (baseline != []):
         canvas.delete("baseline_path")
         canvas.create_line(baseline,tag="baseline_path",width=3,fill="#b00000")
      canvas.itemconfigure("y0",text="y[0]: %.2f"%step_filt[0])
      canvas.itemconfigure("y1",text="y[1]: %.2f"%step_filt[1])
      canvas.itemconfigure("y-1",text="y[-1]: %.2f"%step_filt[-1])
      canvas.coords('x0',0,.95*WINDOW,step_filt[0],WINDOW)
      if (saveflag == 1):
         file = open(soutfile.get(),"w")
	 for i in range(len(step_filt)):
	    file.write("%f\n"%(step_filt[i]))
	 file.close()
	 print 'saved to '+soutfile.get()
         saveflag = 0
      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)
      path.insert(0,WINDOW-value*WINDOW/float(MAX))
      path.insert(0,WINDOW-index*WINDOW/float(NSAMPLES))
"""
   #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])

#
# open serial port
#
#ser = serial.Serial('/dev/ttyUSB0',9600)

ser = serial.Serial('/dev/ttyS0',9600)
#ser = serial.Serial('COM6',9600)
ser.setDTR()
#
# 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
#
# start plotting
#
root = Tk()
root.title('hello3.step.py')
root.bind('q','exit')

canvas = Canvas(root, width=WINDOW, height=WINDOW, background='white')
canvas.create_text(.5*WINDOW,.5*WINDOW,font=("Helvetica", 24),tags="voltage",fill="#000000")
canvas.create_text(.2*WINDOW,.9*WINDOW,font=("Helvetica", 24),tags="y0",fill="#0000b0")
canvas.create_text(.5*WINDOW,.9*WINDOW,font=("Helvetica", 24),tags="y1",fill="#0000b0")
canvas.create_text(.8*WINDOW,.9*WINDOW,font=("Helvetica", 24),tags="y-1",fill="#0000b0")
canvas.create_rectangle(0,.95*WINDOW,0,WINDOW, tags='x0', fill='#b00000')
canvas.pack()

ioframe = Frame(root)
Label(ioframe, text=" filter (0-1):").pack(side="left")
sfilter = StringVar()
sfilter.set(str(eps))
Entry(ioframe, width=5, textvariable=sfilter).pack(side="left")
Label(ioframe, text=" ").pack(side="left")
basebtn = Button(ioframe, text="store baseline")
basebtn.bind('<Button-1>',store_baseline)
basebtn.pack(side="left")
Label(ioframe, text=" ").pack(side="left")
savebtn = Button(ioframe, text="save data")
savebtn.bind('<Button-1>',save_data)
savebtn.pack(side="left")
Label(ioframe, text=" output file:").pack(side="left")
soutfile = StringVar()
soutfile.set("out.dat")
Entry(ioframe, width=10, textvariable=soutfile).pack(side="left")
Label(ioframe, text=" ").pack(side="left")
quitbtn = Button(ioframe, text="quit")
quitbtn.bind('<Button-1>','exit')
quitbtn.pack(side="left")
ioframe.pack()

root.after(100,idle,root,canvas)
root.mainloop()
