import tkinter
from tkinter import ttk
import serial
import time 

#add serial read for arduino to send back that data was sent properly.
#change COM# to whatever COM the arduino is running off of 
arduino = serial.Serial('COM5', 9600)
time.sleep(1)

# data_entry(): grab user input and store in varible
def data_entry():
    volta = userinputA_entry.get()
    voltb = userinputB_entry.get()
    check = goodinput(volta)
    update = 0
    if check == 1 or check == 2: 
        update += 1
        chartosenda = charconversiona(volta)
        chartosenda = chartosenda + '\r'
        arduino.write(chartosenda.encode())
        #print("character to send channel a: ", chartosenda)
    else: 
        print("The input was invalid and not sent, try to choose an option listed")
    check = goodinput(voltb)
    #serialInst.write(voltb.encode("utf-8"))
    if check == 1 or check == 2:
        update += 1
        chartosendb = charconversionb(voltb)
        chartosendb = chartosendb + '\r'
        arduino.write(chartosendb.encode())
        #print("character to send channel b: ", chartosendb)
    else:
        print("The input was invalid and not sent, try to choose an option listed")
    if update == 2:
        temp = '!'
        temp = temp + '\r'
        arduino.write(temp.encode())



# voltage_display(): grab user input and print the values out to the terminal
def voltage_display():
    volta = userinputA_entry.get()
    voltb = userinputB_entry.get()
    check = goodinput(volta)
    if check == 1 or check == 2:
        if check == 2:
            print("Voltage channel A: ", float(volta))
        else:
            print("Voltage channel A: ", volta)
    else: 
        print("The input for channel A was invalid")
    check = goodinput(voltb)
    if check == 1 or check == 2:
        if check == 2:
            print("Voltage channel B: ", float(voltb))
        else:
            print("Voltage channel B: ", voltb)
    else: 
        print("The input for channel B was invalid")


def goodinput(voltinput):

    if voltinput == "Kill power":
        return 1
    elif voltinput == "Keep current voltage":
        return 1
    elif voltinput == "2":
        return 1
    elif voltinput == "2.5":
        return 2
    elif voltinput == "3":
        return 1
    elif voltinput == "3.3":
        return 2
    elif voltinput == "4":
        return 1
    elif voltinput == "4.5":
        return 2
    elif voltinput == "5":
        return 1
    elif voltinput == "5.5":
        return 2
    elif voltinput == "6":
        return 1
    elif voltinput == "6.5":
        return 2
    elif voltinput == "7":
        return 1
    elif voltinput == "7.5":
        return 2
    elif voltinput == "8":
        return 1
    elif voltinput == "8.5":
        return 2
    elif voltinput == "9":
        return 1
    elif voltinput == "9.5":
        return 2
    elif voltinput == "10":
        return 1
    elif voltinput == "10.5":
        return 2
    elif voltinput == "11":
        return 1
    elif voltinput == "11.5":
        return 2
    elif voltinput == "12":
        return 1
    elif voltinput == "12.5":
        return 2
    elif voltinput == "13":
        return 1
    elif voltinput == "13.5":
        return 2
    elif voltinput == "14":
        return 1
        
    return 0


def charconversiona(voltinput):

    if voltinput == "Kill power":
        voltinput = "0"
    elif voltinput == "Keep current voltage":
        voltinput = "a"
    elif voltinput == "2":
        voltinput = "b"
    elif voltinput == "2.5":
        voltinput = "c"
    elif voltinput == "3":
        voltinput = "d"
    elif voltinput == "3.3":
        voltinput = "e"
    elif voltinput == "4":
        voltinput = "f"
    elif voltinput == "4.5":
        voltinput = "g"
    elif voltinput == "5":
        voltinput = "h"
    elif voltinput == "5.5":
        voltinput = "i"
    elif voltinput == "6":
        voltinput = "j"
    elif voltinput == "6.5":
        voltinput = "k"
    elif voltinput == "7":
        voltinput = "l"
    elif voltinput == "7.5":
        voltinput = "m"
    elif voltinput == "8":
        voltinput = "n"
    elif voltinput == "8.5":
        voltinput = "o"
    elif voltinput == "9":
        voltinput = "p"
    elif voltinput == "9.5":
        voltinput = "q"
    elif voltinput == "10":
        voltinput = "r"
    elif voltinput == "10.5":
        voltinput = "s"
    elif voltinput == "11":
        voltinput = "t"
    elif voltinput == "11.5":
        voltinput = "u"
    elif voltinput == "12":
        voltinput = "v"
    elif voltinput == "12.5":
        voltinput = "w"
    elif voltinput == "13":
        voltinput = "x"
    elif voltinput == "13.5":
        voltinput = "y"
    elif voltinput == "14":
        voltinput = "z"

    return voltinput


def charconversionb(voltinput):

    if voltinput == "Kill power":
        voltinput = "1"
    elif voltinput == "Keep current voltage":
        voltinput = "A"
    elif voltinput == "2":
        voltinput = "B"
    elif voltinput == "2.5":
        voltinput = "C"
    elif voltinput == "3":
        voltinput = "D"
    elif voltinput == "3.3":
        voltinput = "E"
    elif voltinput == "4":
        voltinput = "F"
    elif voltinput == "4.5":
        voltinput = "G"
    elif voltinput == "5":
        voltinput = "H"
    elif voltinput == "5.5":
        voltinput = "I"
    elif voltinput == "6":
        voltinput = "J"
    elif voltinput == "6.5":
        voltinput = "K"
    elif voltinput == "7":
        voltinput = "L"
    elif voltinput == "7.5":
        voltinput = "M"
    elif voltinput == "8":
        voltinput = "N"
    elif voltinput == "8.5":
        voltinput = "O"
    elif voltinput == "9":
        voltinput = "P"
    elif voltinput == "9.5":
        voltinput = "Q"
    elif voltinput == "10":
        voltinput = "R"
    elif voltinput == "10.5":
        voltinput = "S"
    elif voltinput == "11":
        voltinput = "T"
    elif voltinput == "11.5":
        voltinput = "U"
    elif voltinput == "12":
        voltinput = "V"
    elif voltinput == "12.5":
        voltinput = "W"
    elif voltinput == "13":
        voltinput = "X"
    elif voltinput == "13.5":
        voltinput = "Y"
    elif voltinput == "14":
        voltinput = "Z"

    return voltinput

#creates gui with tkinter and titles it.
root = tkinter.Tk()
root.title("Power Supply Entry Program")

#creates frame inside root gui and uses function pack to handle the frame display
frame = tkinter.Frame(root)
frame.pack()

# creates and labels a frame inside the root frame of size 50x50
userframe = tkinter.LabelFrame(frame, text = "User Input")
userframe.grid(row = 0, column = 0, padx = 50, pady = 50)

# creates and user label inside the userframe and names the frame 
userinputA = tkinter.Label(userframe, text="Voltage for chanelA")
userinputA.grid(row=0, column=0)

# creates and user label inside the userframe and names the frame
userinputB = tkinter.Label(userframe, text="Voltage for chanelB")
userinputB.grid(row= 0, column=1)

# create a user input with combobox with options stay and 0-14 
userinputA_entry = ttk.Combobox(userframe, 
values = ["Keep current voltage","Kill power", "2", "2.5", "3", "3.3", "4","4.5", "5", "5.5", "6", "6.5", "7", "7.5", "8", "8.5", "9", "9.5", "10", "10.5", "11", "11.5", "12", "12.5", "13", "13.5", "14"])
userinputA_entry.current(0)
userinputB_entry = ttk.Combobox(userframe, 
values = ["Keep current voltage","Kill power", "2", "2.5", "3", "3.3", "4","4.5", "5", "5.5", "6", "6.5", "7", "7.5", "8", "8.5", "9", "9.5", "10", "10.5", "11", "11.5", "12", "12.5", "13", "13.5", "14"])
userinputB_entry.current(0)

#place user entries on the grid
userinputA_entry.grid(row = 1, column = 0)
userinputB_entry.grid(row = 1, column = 1)

#create sepret frame for the confirm button to send values via serial
buttonframe = tkinter.LabelFrame(frame, text = "Voltage Confirm")
buttonframe.grid(row = 1, column = 0, padx = 20, pady = 20)

buttonlabel = tkinter.Label(buttonframe)
buttonlabel.grid(row = 2, column = 1)
cfmbutton = tkinter.Button(buttonframe, text = "Confirm", command = data_entry)
cfmbutton.grid(row = 2, column = 1)

#create another frame for another button that will print the voltages
buttonprintframe = tkinter.LabelFrame(frame, text = "Print Voltage")
buttonprintframe.grid(row = 1, column = 1, padx = 20, pady = 20)


buttonlabel = tkinter.Label(buttonprintframe)
buttonlabel.grid(row = 2, column = 0)
pbutton = tkinter.Button(buttonprintframe, text = "Print", command = voltage_display)
pbutton.grid(row = 2, column = 0)

#loops widget dimensions for an overal better looking GUI
for widget in userframe.winfo_children():
    widget.grid_configure(padx = 10, pady = 5)
for widget in buttonframe.winfo_children():
    widget.grid_configure(padx = 5, pady = 5)

root.mainloop()