from Tkinter import * # Import tk module
import tkFont
import serial
import time

arduino=serial.Serial() #Serial comunication with fabKit (Arduino UNO compatible)
arduino.port ="/dev/ttyUSB0" #Change this value for your COM / tty
arduino.baudrate = 9600 #comunication to 9600 bauds

arduino.open()

def sendOrder(order):
     vf['text']="Waiting response..."
     v0.update_idletasks()
     arduino.flushInput()
     arduino.flushOutput()
     arduino.write(order+"\r\n")

     if arduino.readline()=="OKI\r\n": # "OKI\r\n" is the default response of fabkit program

         if order=="b":
             v2['text']="LED 2: ON"
             v2['bg']="green"
         elif order=="a":
             v2['text']="LED 2: OFF"
             v2['bg']="red"
         elif order=="d":
             v1['text']="LED 1: ON"
             v1['bg']="green"
         elif order=="c":
             v1['text']="LED 1: OFF"
             v1['bg']="red"

     vf['text']=""

print ("Hello..!! Arsheena LED interface") # Say hello in console (for debug, you can erase)
v0 = Tk() # Tk() Main window
v0.wm_title("Asheena Fab Academy - Interface and Application Programming")
#v0.config(bg="black") # if you need change background color
v0.geometry("1000x500") # windows size for aplication

helv16 = tkFont.Font(family="Helvetica",size=16,weight="bold")
helv24 = tkFont.Font(family="Helvetica",size=24,weight="bold")
helv14 = tkFont.Font(family="Helvetica",size=14,weight="bold")
helv8 = tkFont.Font(family="Helvetica",size=8,weight="bold")
l = Label(v0,font=helv16,text="Arsheena Fab Academy - Interface and Application Programming")
l.place(relx=.5, rely=.2, anchor="c")

v1 = Label(v0,font=helv14,text="LED 1: OFF",bg="red")
v1.place(relx=.3, rely=.35, anchor="c")
v2 = Label(v0,font=helv14,text="LED 2: OFF",bg="red")
v2.place(relx=.7, rely=.35, anchor="c")

b1=Button(v0,text="Activate LED 1",width=20,command=lambda: sendOrder('d'))
b1.place(relx=.3, rely=.7, anchor="c")
#b1['text']="test"
b2=Button(v0,text="Disable LED 1",width=20,command=lambda: sendOrder("c"))
b2.place(relx=.7, rely=.7, anchor="c")

b3=Button(v0,text="Activate LED 2",width=20,command=lambda: sendOrder("b"))
b3.place(relx=.3, rely=.8, anchor="c")
#b1['text']="test"
b4=Button(v0,text="Disable LED 2",width=20,command=lambda: sendOrder("a"))
b4.place(relx=.7, rely=.8, anchor="c")


vf = Label(v0,font=helv8,text="Waiting response...")
vf .place(relx=.5, rely=.97, anchor="c")
vf['text']=""
v0.mainloop() # launch the main window
