#!/usr/bin/python
from Tkinter import *
import ttk
import time
import RPi.GPIO as GPIO
import threading
import os
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

def init(root):
    root.title("QUIZ Control & Indication Panel v1.2")

def close_window():
    root.after(0, root.destroy)
    
root = Tk()
#padding window 
content = ttk.Frame(root, padding=(5,5,5,5))
#set window size
root.geometry('1020x250')

#text colour - Team Names
global c1, c2, c3, c4, c5
c1 = StringVar()
c2 = StringVar()
c3 = StringVar()
c4 = StringVar()
c5 = StringVar()

content.grid(column=0, row=0)

# defining labels for Team Names

teama = ttk.Label(content, text=" TEAM A ", font=("Helvetica",32, "bold"), background=c2.get())
teamb = ttk.Label(content, text=" TEAM B ", font=("Helvetica",32, "bold"), background=c2.get())
teamc = ttk.Label(content, text=" TEAM C ", font=("Helvetica",32, "bold"), background=c2.get())
teamd = ttk.Label(content, text=" TEAM D ", font=("Helvetica",32, "bold"), background=c2.get())

# placing text 
teama.grid(column=1, row=1, pady=10, padx=20)
teamb.grid(column=2, row=1, padx=20)
teamc.grid(column=3, row=1, padx=20)
teamd.grid(column=4, row=1, padx=20)

# titles

label_1 = ttk.Label(content, text="")
label_1.grid(column=1, row=0, pady=20)
label_2 = ttk.Label(content, text="")
label_2.grid(column=1, row=3)
label_3 = ttk.Label(content, text="")
label_3.grid(column=1, row=4)
label_4 = ttk.Label(content, text="")
label_4.grid(column=1, row=5)
label_5 = ttk.Label(content, text="")
label_5.grid(column=1, row=6)
label_6 = ttk.Label(content, text="")
label_6.grid(column=1, row=7)
label_7 = ttk.Label(content, text="walkerworks 2016", foreground="blue")
label_7.grid(column=4, row=8)

#Close Button
close = ttk.Button(content, command=close_window, text="Close")
close.grid(column=1, row=8)

GPIO.setup(17, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(10, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(9, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(11, GPIO.IN, pull_up_down = GPIO.PUD_UP)

rly1 = 18
rly2 = 23
rly3 = 24
rly4 = 25
rly5 = 8

GPIO.setup(rly1,GPIO.OUT)
GPIO.setup(rly2,GPIO.OUT)
GPIO.setup(rly3,GPIO.OUT)
GPIO.setup(rly4,GPIO.OUT)
GPIO.setup(rly5,GPIO.OUT)

GPIO.output(rly1,GPIO.LOW)
GPIO.output(rly2,GPIO.LOW)
GPIO.output(rly3,GPIO.LOW)
GPIO.output(rly4,GPIO.LOW)
GPIO.output(rly5,GPIO.HIGH)

def reset():
    GPIO.output(rly1,GPIO.LOW)
    GPIO.output(rly2,GPIO.LOW)
    GPIO.output(rly3,GPIO.LOW)
    GPIO.output(rly4,GPIO.LOW)
    c5.set("gray84")
    teama.configure(background=c5.get())
    teamb.configure(background=c5.get())
    teamc.configure(background=c5.get())
    teamd.configure(background=c5.get())    

def A():
    GPIO.output(rly1,GPIO.HIGH)
    GPIO.output(rly5,GPIO.LOW)
    c1.set("blue")
    teama.configure(background=c1.get())    
    time.sleep(0.5)
    GPIO.output(rly5,GPIO.HIGH)
    while True:
        time.sleep(0.05)
        if GPIO.input(9)==0:
            reset()
            break
def B():
    GPIO.output(rly2,GPIO.HIGH)
    GPIO.output(rly5,GPIO.LOW)
    c2.set("yellow")
    teamb.configure(background=c2.get())     
    time.sleep(0.5)
    GPIO.output(rly5,GPIO.HIGH)
    while True:
        time.sleep(0.05)
        if GPIO.input(9)==0:
            reset()
            break   
def C():
    GPIO.output(rly3,GPIO.HIGH)
    GPIO.output(rly5,GPIO.LOW)
    c3.set("red")
    teamc.configure(background=c3.get())     
    time.sleep(0.5)
    GPIO.output(rly5,GPIO.HIGH)
    while True:
        time.sleep(0.05)
        if GPIO.input(9)==0:
            reset()
            break       
def D():
    GPIO.output(rly4,GPIO.HIGH)
    GPIO.output(rly5,GPIO.LOW)
    c4.set("green")
    teamd.configure(background=c4.get()) 
    time.sleep(0.5)
    GPIO.output(rly5,GPIO.HIGH)
    while True:
        time.sleep(0.05)
        if GPIO.input(9)==0:
            reset()
            break
    
def keys():
    while True:
        if GPIO.input(17)==0:
            A()
        if GPIO.input(21)==0:        
            B()
        if GPIO.input(22)==0:
            C()
        if GPIO.input(10)==0:
            D()
        time.sleep(0.05)

def shutdown(shutdown):
    os.system("sudo poweroff")            
            
GPIO.add_event_detect(11, GPIO.FALLING, bouncetime=500, callback=shutdown)

t = threading.Thread(target=keys)
t.daemon=True
t.start()
    
init(root)
root.mainloop()