#
# hello.temp.45.py
#
# receive and display temperature
# hello.temp.45.py serial_port
#
# Neil Gershenfeld
# CBA MIT 3/27/12
#
# (c) Massachusetts Institute of Technology 2012
# Permission granted for experimental and personal use;
# license for commercial sale available from MIT
#

from Tkinter import *
from numpy import log
import serial

WINDOW = 800 # 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)
   # NHQ103B375R5
   # R25 10000 (O)
   # B (25/85) 3750 (K)
   # R(T(C)) = R(25)*exp(B*(1/(T(C)+273.15)-(1/(25+273.15))))
   B = 3750.0
   R25 =  10000.0
   T = 1.0/(log(R/R25)/B+(1/(25.0+273.15))) - 273.15
   F = (T*1.8)+32
   filter = (1-eps)*filter + eps*F
   x = int(.2*WINDOW + (.9-.2)*WINDOW*(filter-20.0)/10.0)
   canvas.itemconfigure("text",text="%.2f F"%filter)
   canvas.coords('rect1',.2*WINDOW,.05*WINDOW,x,.2*WINDOW)
   canvas.coords('rect2',x,.05*WINDOW,.9*WINDOW,.2*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 the image to the screen (here is the description)
#
root = Tk()
root.title('TEMPERATURE ( Hit "x" to exit )')
root.bind('x','exit')
#canvas = Canvas(width=WINDOW, height=.25*WINDOW, background='Gray')
canvas = Canvas(width=800, height=400, background='Gray')
#canvas.create_text(80,80,text=".33",font=("Times", 15, "bold italic"),tags="text",fill="#ffffff")
#canvas.create_rectangle(.2*WINDOW,.05*WINDOW,.3*WINDOW,.2*WINDOW, tags='rect1', fill='#f08080')

#      ORIGINAL TWO RECTANGLES **********************************
canvas.create_rectangle(16,4,24,16, tags='rect1', fill='#f08080', width=5)
canvas.create_rectangle(24,.4,72,16, tags='rect2', fill='#f0fff0', width=5)

# *********************************************************************

#       ADDITIONS BY ME ***********************************************
#       WHAT'S UP WITH THE NUMBERS ??????? ****************************

canvas.create_rectangle(200,200,700,300, tags='rect3', fill='#b22222', width=5)
canvas.create_line(200,200,700,300, width=5)
canvas.create_line(200,300,700,200, width=5)
canvas.create_line(200,250,700,250, width=5)
canvas.create_line(450,200,450,300, width=5)
canvas.create_text(450,250,text=".33",font=("Times", 36, "bold italic"),tags="text",fill="#ffffff")
canvas.create_text(450,325,text="Week 16 - Temperature Sensor",font=("Times", 16, ),tags="text1",fill="#ffffff")
canvas.create_text(450,350,text="Karen Hardway",font=("Times", 16, ),tags="text1",fill="#ffffff")


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