# Project#5 - Relay GUI
# Information - In this project, we're going to cteate a graphical user interface (GUI) with tkinter to...
# control a relay on the RPInoRPI
# If you want to create awesome GUIs, I highly recommend this tutorial: https://www.youtube.com/watch?v=YXPyB4XeYLA
from tkinter import *  # Import GUY module
import time            # For delays
import serial          # For microcontroller communication

root = Tk()                 # Needs to be first to great the GUI window
root.title("RELAY GUI")  # Give the GUI a title
root.geometry('135x150')    # 100x100 pixel frame.  Play with these numbers and see what happens!

try:  # Try to establish communication with the RPInoRPI board.  if successful, only the 'try:' will be executed
    MCU = serial.Serial(port='COM10', baudrate=115200, timeout=0.01)  # Timeout = 0.01.  More on this later.
    time.sleep(1)  # Wait 5 seconds.
except:  # If communication with the RPInoRPI could not be established, then perform the following instead:
    print('System Locked - Cannot communicate with MCU.  Close this program, then reconnect to MCU and restart program')
    while True:  # Forever loop.  Close the program and start again.  Check connection to the RPInoRPI
        pass  # Do nothing

def write_read(x):  # This function acts to send data (x) to the RPInoRPI.  Nothing more.
    MCU.write(bytes(x, 'utf-8'))  # Write the value stored in 'x' to the RPInoRPI

def buttonclick(number):  # This function takes care of all buttons when pressed.
    # However, there is only one button on this GUI.
    global Relay1_Entry  # Set Relay1_Entry window as global variable. Now we can access it across functions.
    if number == 1:   # If fault entry button was selected, execute the following
        x = '1'       # x holds the command to be sent to the RPInoRPI
        write_read(x) # write x to the MCU
        value = Relay1_Entry.get() # Get the current value in the window (Either 'ON' or 'OFF' and place in 'value'
        Relay1_Entry.configure(state='normal')  # Make it so that we can make changes to the window valye
        Relay1_Entry.delete(0, END)  # Clear the window
        if value == 'OFF':                # If 'value' was 'OFF', then change to 'ON'
            Relay1_Entry.insert(0, 'ON')  # Change to 'ON'
        else:                             # Otherwise, change to 'OFF'
            Relay1_Entry.insert(0, 'OFF') # Change to 'OFF'
        Relay1_Entry.configure(state='disable')  # Make it so that we can no longer change what is in the entry window

# This is where we set up our GUI
Name_Label = Label(root, text='TOGGLE RELAY STATES...') # A Labal is basically a title.  This is at the top of the GUI
Name_Label.configure(justify='left')                    # Set the justification to left.  Can be center or right
Spacer_Label1 = Label(root, text='')                    # This label is empty.  We're going to use it as a small spacer
Relay1 = Button(root, text='RELAY#1', padx=5, pady=10, command=lambda: buttonclick(1))  # This is our ONLY button!
#  Create this button to send a value of 1 using 'lambda' to the buttonclick function when pressed.
# In other words, when you press the relay1 button, you'll go to the buttonclick function and send along a '1'
Relay1_Entry = Entry(root, width=5, borderwidth=5)  # This is an entry window.  It will hold either 'ON' or 'OFF'
# You can set the border outline width and the width of the window itself.  Play with these values and see what happens!
Relay1_Entry.insert(0,'OFF') # Set the starting value to 'OFF', as the relay will start in the OFF position
Relay1_Entry.configure(state='disable')  # Make it so that you can't manually change what is in this window.

# GRID SETTINGS
# Allocate the area for the operator name and email inputs.
Name_Label.grid(row=1,column=0)  # The following four attributes are the email and name entry
Spacer_Label1.grid(row=2,column=0)
Relay1.grid(row=3,column=0)
Relay1_Entry.grid(row=4, column=0, padx=5, pady=5)
root.mainloop()  # Display everything on the screen and loop until you exit

