from Tkinter import *
 
root = Tk() #Makes the window
root.wm_title("LED Control Panel") #Makes the title that will appear in the top left
root.config(background = "#EEEEEE") #sets background color to white
 
 
#widgets:
 
#Left Frame and its contents
leftFrame = Frame(root, width=200, height = 600)
leftFrame.grid(row=0, column=0, padx=10, pady=2)
 
#Right Frame and its contents
rightFrame = Frame(root, width=200, height = 600)
rightFrame.grid(row=0, column=1, padx=10, pady=2)
 
#Canvas for drawing circles
circleCanvas = Canvas(rightFrame, width=100, height=100, bg='white')
circleCanvas.grid(row=1, column=0, padx=10, pady=2)
 
#Logging LED on/off status
LEDLog = Text(rightFrame, width = 30, height = 10, takefocus=0)
LEDLog.grid(row=3, column=0, padx=10, pady=2)
 
#Labels
firstLabel = Label(leftFrame, text="LED Toggle")
firstLabel.grid(row=0, column=0, padx=10, pady=2)
 
secondLabel = Label(rightFrame, text="Status History")
secondLabel.grid(row=2, column=0, padx=10, pady=2)
 
thirdLabel = Label(rightFrame, text="LED Visual Status")
thirdLabel.grid(row=0, column=0, padx=10, pady=2)
 
 
#Drawing circles for the LED visual status
def redCircle():
    circleCanvas.create_oval(20, 20, 80, 80, width=0, fill='red')
    LEDLog.insert(0.0, "Red\n")

def blueCircle():
    circleCanvas.create_oval(20, 20, 80, 80, width=0, fill='blue')
    LEDLog.insert(0.0, "Blue\n")
 
def whtCircle():
    circleCanvas.create_oval(19, 19, 81, 81, width=0, fill='white')
    LEDLog.insert(0.0, "Off\n")
 
 
import serial
import time
 
#Open serial port to the board
arduino = serial.Serial()
arduino.baudrate = 9600
arduino.port = 'COM12'

time.sleep(2) # waiting for initialization...
print("initializing")
 
#Turning Red LED on
def RLEDOn():
    arduino.open()
    arduino.write("R")
    arduino.close()
    redCircle()

#Turning Blue LED on
def BLEDOn():
    arduino.open()
    arduino.write("B")
    arduino.close()
    blueCircle()
 
#Turning LEDs off
def LEDOff():
    arduino.open()
    arduino.write("L")
    arduino.close()
    whtCircle()
 
#LED on/off buttons
newButton = Button(leftFrame, text="Red LED", command=RLEDOn)
newButton.grid(row=2, column=0, padx=10, pady=2)

newButton = Button(leftFrame, text="Blue LED", command=BLEDOn)
newButton.grid(row=3, column=0, padx=10, pady=2)
 
newButton = Button(leftFrame, text="All Off", command=LEDOff)
newButton.grid(row=4, column=0, padx=15, pady=2)
 
 
arduino.close() #close serial port
root.mainloop() #loop to update GUI
