# -*- coding: utf-8 -*-
from serial import *
from Tkinter import *
WINDOW = 500 # window size
BG='white' #Default BG color
serialPort = "COM8" #my PC is using COM7
baudRate = 9600
ser = Serial(serialPort , baudRate, timeout=0, writeTimeout=0) #ensure non-blocking

#make a TkInter Window
root = Tk()
root.wm_title("Reading Temperature")
##
### make a scrollbar
##scrollbar = Scrollbar(root)
##scrollbar.pack(side=RIGHT, fill=Y)
##
### make a text box to put the serial output
##log = Text ( root, width=30, height=30, takefocus=0)
##log.pack()



canvas =Canvas(root, width=WINDOW, height=.25*WINDOW, background=BG) #Set the Canvas
canvas.create_text(.1*WINDOW,.05*WINDOW,text="Cool ",font=("Helvetica", 24),tags="status",fill="#0000b0") #Set the Status Temperature
canvas.create_text(.1*WINDOW,.125*WINDOW,text="Temp: ",font=("Helvetica", 24),tags="temp",fill="#0000b0") #Set the Temperature Label
canvas.create_text(.3*WINDOW,.155*WINDOW,text=".33",font=("Helvetica", 24),tags="text",fill="#0000b0") #Set the Temperature number
canvas.create_text(.4*WINDOW,.125*WINDOW,text="°C",font=("Helvetica", 24),tags="DC",fill="#0000b0") #Set the °C
canvas.create_oval(220, 50,270, 100, width=2, fill='white', tags="oval")

#canvas.create_rectangle(.2*WINDOW,.05*WINDOW,.3*WINDOW,.2*WINDOW, tags='rect1', fill='#b00000')
#canvas.create_rectangle(.3*WINDOW,.05*WINDOW,.9*WINDOW,.2*WINDOW, tags='rect2', fill='#0000b0')
canvas.pack()


### attach text box to scrollbar
##log.config(yscrollcommand=scrollbar.set)
##scrollbar.config(command=log.yview)


#make our own buffer
#useful for parsing commands
#Serial.readline seems unreliable at times too
serBuffer = ""

def readSerial():
    while True:
        c = ser.read() # attempt to read a character from Serial
        
        #was anything read?
        if len(c) == 0:
            break
        
        # get the buffer from outside of this function
        global serBuffer
        
        # check if character is a delimeter
        if c == '\r':
            c = '' # don't want returns. chuck it
            
        if c == '\n':
            serBuffer += "\n" # add the newline to the buffer
            
            #add the line to the TOP of the log
            #log.insert('0.0', serBuffer)
            #print serBuffer  
            canvas.itemconfigure("text",text=serBuffer)
            if serBuffer <= '28':
                canvas.itemconfig("status", text="Cool") # change text
                canvas.itemconfig("oval", fill="blue") # change color
                
            elif serBuffer > '28' and serBuffer <= '30':
                canvas.itemconfig("status", text="Warm") # change text
                canvas.itemconfig("oval", fill="yellow") # change color
            else:
                canvas.itemconfig("status", text="Hot!") # change text
                canvas.itemconfig("oval", fill="red") # change color
            
            serBuffer = "" # empty the buffer
        else:
            serBuffer += c # add to the buffer
    
    root.after(10, readSerial) # check serial again soon


# after initializing serial, an arduino may need a bit of time to reset
root.after(100, readSerial)

root.mainloop()
