#!/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 - Pi Zero")
#Pi Zero 6 contestant v2.3     
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('1350x250')

#text colour - Team Names
global c1, c2, c3, c4, c5, c6, c7
c1 = StringVar()
c2 = StringVar()
c3 = StringVar()
c4 = StringVar()
c5 = StringVar()
c6 = StringVar()
c7 = 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())
teame = ttk.Label(content, text=" TEAM E ", font=("Helvetica",32, "bold"), background=c2.get())
teamf = ttk.Label(content, text=" TEAM F ", font=("Helvetica",32, "bold"), background=c2.get())

# placing text 
teama.grid(column=1, row=1, pady=10, padx=15)
teamb.grid(column=2, row=1, padx=15)
teamc.grid(column=3, row=1, padx=15)
teamd.grid(column=4, row=1, padx=15)
teame.grid(column=5, row=1, padx=15)
teamf.grid(column=6, row=1, padx=15)

# 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="")
label_7.grid(column=1, row=8)
label_8 = ttk.Label(content, text="")
label_8.grid(column=1, row=9)
label_9 = ttk.Label(content, text="walkerworks 2018", foreground="blue")
label_9.grid(column=6, row=7)

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

GPIO.setup(4, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(17, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(27, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(25, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)


led_blue = 21
led_yellow =26
led_red = 19
led_green = 20
led_white = 16
led_purple = 13
spare = 12
buzzer = 6

GPIO.setup(led_blue,GPIO.OUT)
GPIO.setup(led_yellow,GPIO.OUT)
GPIO.setup(led_red,GPIO.OUT)
GPIO.setup(led_green,GPIO.OUT)
GPIO.setup(led_white,GPIO.OUT)
GPIO.setup(led_purple,GPIO.OUT)
GPIO.setup(spare,GPIO.OUT)
GPIO.setup(buzzer,GPIO.OUT)

GPIO.output(led_blue,GPIO.LOW)
GPIO.output(led_yellow,GPIO.LOW)
GPIO.output(led_red,GPIO.LOW)
GPIO.output(led_green,GPIO.LOW)
GPIO.output(led_white,GPIO.LOW)
GPIO.output(led_purple,GPIO.LOW)
GPIO.output(spare,GPIO.HIGH)
GPIO.output(buzzer,GPIO.HIGH)

def reset():
    GPIO.output(led_blue,GPIO.LOW)
    GPIO.output(led_yellow,GPIO.LOW)
    GPIO.output(led_red,GPIO.LOW)
    GPIO.output(led_green,GPIO.LOW)
    GPIO.output(led_white,GPIO.LOW)
    GPIO.output(led_purple,GPIO.LOW)    
    c7.set("gray84")
    teama.configure(background=c7.get())
    teamb.configure(background=c7.get())
    teamc.configure(background=c7.get())
    teamd.configure(background=c7.get())
    teame.configure(background=c7.get())
    teamf.configure(background=c7.get())    

def A():
    GPIO.output(led_blue,GPIO.HIGH)
    GPIO.output(buzzer,GPIO.LOW)
    c1.set("blue")
    teama.configure(background=c1.get())    
    time.sleep(0.5)
    GPIO.output(buzzer,GPIO.HIGH)
    while True:
        time.sleep(0.05)
        if GPIO.input(25)==0:
            reset()
            break

def B():
    GPIO.output(led_yellow,GPIO.HIGH)
    GPIO.output(buzzer,GPIO.LOW)
    c2.set("yellow")
    teamb.configure(background=c2.get())     
    time.sleep(0.5)
    GPIO.output(buzzer,GPIO.HIGH)
    while True:
        time.sleep(0.05)
        if GPIO.input(25)==0:
            reset()
            break
    
def C():
    GPIO.output(led_red,GPIO.HIGH)
    GPIO.output(buzzer,GPIO.LOW)
    c3.set("red")
    teamc.configure(background=c3.get())     
    time.sleep(0.5)
    GPIO.output(buzzer,GPIO.HIGH)
    while True:
        time.sleep(0.05)
        if GPIO.input(25)==0:
            reset()
            break
       
def D():
    GPIO.output(led_green,GPIO.HIGH)
    GPIO.output(buzzer,GPIO.LOW)
    c4.set("green")
    teamd.configure(background=c4.get()) 
    time.sleep(0.5)
    GPIO.output(buzzer,GPIO.HIGH)
    while True:
        time.sleep(0.05)
        if GPIO.input(25)==0:
            reset()
            break

def E():
    GPIO.output(led_white,GPIO.HIGH)
    GPIO.output(buzzer,GPIO.LOW)
    c5.set("white")
    teame.configure(background=c5.get()) 
    time.sleep(0.5)
    GPIO.output(buzzer,GPIO.HIGH)
    while True:
        time.sleep(0.05)
        if GPIO.input(25)==0:
            reset()
            break
def F():
    GPIO.output(led_purple,GPIO.HIGH)
    GPIO.output(buzzer,GPIO.LOW)
    c6.set("purple")
    teamf.configure(background=c6.get()) 
    time.sleep(0.5)
    GPIO.output(buzzer,GPIO.HIGH)
    while True:
        time.sleep(0.05)
        if GPIO.input(25)==0:
            reset()
            break
  
def keys():
    while True:
        if GPIO.input(4)==0:
            A()
        if GPIO.input(17)==0:        
            B()        
        if GPIO.input(18)==0:
            C()            
        if GPIO.input(27)==0:
            D()
        if GPIO.input(22)==0:
            E()
        if GPIO.input(23)==0:
            F()
        time.sleep(0.05)

def shutdown(shutdown):
    time.sleep(0.5)
    if GPIO.input(5)==1:
        return
    os.system("sudo poweroff")

GPIO.add_event_detect(5, GPIO.FALLING, bouncetime=1000, callback=shutdown)

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