#!/usr/bin/python3
from tkinter import *                 # imports the Tkinter lib
import RPi.GPIO as GPIO               # imports the GPIO lib
import time                           # import time lib
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)               # disable warnings
GPIO.setup(18, GPIO.OUT)              # ServoMotor connected to GPIO 18
GPIO.setup(23,GPIO.OUT)		  	          # LED connected to GPIO 23
pwm = GPIO.PWM(18, 100)               # setup GPIO 18 as PWM and provide starting value (0-100)
pwm.start(5)

root = Tk()				     # create the root object
root.wm_title("GUI")			             # sets title of the window
root.configure(bg="#99B898")		       # change the background color 

root.attributes("-fullscreen", True) # set to fullscreen by default

# we can exit when we press the escape key
def end_fullscreen(event):
	root.attributes("-fullscreen", False)

def btnClicked():
  if GPIO.input(23):
    GPIO.output(23,GPIO.LOW)
    ledButton["text"]="LED OFF"
  else:
    GPIO.output(23,GPIO.HIGH)
    ledButton["text"]="LED ON"

def btnExit():
  	root.destroy()


def update(angle):
        duty = float(angle) / 10.0 +2.5
        pwm.ChangeDutyCycle(duty)
        

label_1 = Label(root, text="Raspberry Pi Graphical User Interface", font="Verdana 26 bold",
			fg="#000",
			bg="#99B898",
			pady = 60,
			padx = 100)
exitButton = Button(root, text="Exit", background = "#C06C84",
      command=btnExit, height=10, width=40, font = "Arial 16 bold", activebackground = "red")
	


ledButton = Button(root, text="LED OFF",background = "#C06C84", 
      command=btnClicked, height=10, width=40, font = "Arial 16 bold", activebackground ="#C06C84")


        
ServoC= Scale(root, from_=0, to=180,
     orient=HORIZONTAL, command=update, background = "#C06C84",
     width =80, label = "ServoMotorAngleController",length = 500,  activebackground = "#C06C84",font = "Arial 16 bold")

label_1.grid(row=0, column=0)
exitButton.grid(row = 1 ,column = 0)
ledButton.grid(row = 1 ,column = 1)
ServoC.grid(row=2 , column = 1)

root.bind("<Escape>", end_fullscreen)
root.mainloop()				# starts the GUI loop
