#SomeFrames.py

import gpiozero as gp
import tkinter as tk

ws = tk.Tk()
ws.title('Some Frames')
ws.geometry('940x500')
ws.config(bg='white')

# LabelFrame Size
box_height = 225
box_width = 160

# hardware LEDs and Buttons
hwLedAC = gp.LED(8)
hwBtnAC = gp.Button(0)
hwLedONcmd = gp.LED(7)
hwLedDCout = gp.LED(1)
hwBtnDCout = gp.Button(6)
hwBtnov = gp.Button(13)
hwLedov = gp.LED(16)
hwBtnoc = gp.Button(19)
hwLedoc = gp.LED(20)
hwBtnot = gp.Button(26)
hwLedot = gp.LED(21)

# gui LEDs and Buttons
# guiACLed      AC input available
# guiOnCMDLed   P/S ON Command 
# guiLedDCout   P/S DC Output OK
# guiLedov      P/S OverVoltage Fault
# guiLedoc      P/S OverCurrent Fault
# guiLedot      P/S OverTemperature Fault

# gui LED images
redLed = tk.PhotoImage(file='redLed.png')
grnLed = tk.PhotoImage(file='grnLed.png')

# AC Power Input Functions
def acOn():
    hwLedAC.on()
    guiACLed.config(image=grnLed)
    
def acOff():
    hwLedAC.off()
    guiACLed.config(image=redLed)
    
# ON / OFF Control Functions
def onCmd():
    hwLedONcmd.on()
    guiOnCMDLed.config(image=grnLed)
    
def offCmd():
    hwLedONcmd.off()
    guiOnCMDLed.config(image=redLed)

# DC Output Status Functions
def dcOutOK():
    hwLedDCout.on()
    guiLedDCout.config(image=grnLed)

def dcOutFault():
    hwLedDCout.off()
    guiLedDCout.config(image=redLed)
    
# FAULTS Functions
def ovOK():
    hwLedov.off()
    print("OV is OK")
    guiLedov.config(image=grnLed)

def ovFAULT():
    hwLedov.on()
    print("OV is FAULTED")
    guiLedov.config(image=redLed)
    
def ocOK():
    hwLedoc.off()
    print("OC is OK")
    guiLedoc.config(image=grnLed)

def ocFAULT():
    hwLedoc.on()
    print("OC is FAULTED")
    guiLedoc.config(image=redLed)
    
def otOK():
    hwLedot.off()
    print("OT is OK")
    guiLedot.config(image=grnLed)

def otFAULT():
    hwLedot.on()
    print("OT is FAULTED")
    guiLedot.config(image=redLed)

# AC Power Available
PWR = tk.LabelFrame(ws, bd=7, background='white', text="AC Power", relief="ridge", width=box_width, height=box_height, padx=10, pady=10)
PWR.grid_propagate(False)
guiACLed =tk.Label(PWR, image=redLed)
guiACLed.grid(row=1, column=0, pady=10)
acTxt = tk.Label(PWR, text="AC Input\nOK", bg='white', font=('Times', 14))
acTxt.grid(row=0, column=0, padx=10, pady=10)
PWR.place(x=50, y=50)

hwBtnAC.when_pressed = acOn
hwBtnAC.when_released = acOff

# Control: P/S On/Off
CTRL = tk.LabelFrame(ws, bd=7, background='white', text="Control", relief="ridge", width=box_width, height=box_height, padx=10, pady=10)
CTRL.grid_propagate(False)
CTRL.place(x=215, y=50)
onButton=tk.Button(CTRL, text="Turn ON", command=onCmd, font=('Times', 14), bg="red", fg="white")
onButton.grid(row=0, column=0, pady=10)
offButton=tk.Button(CTRL, text="Turn OFF", command=offCmd, font=('Times', 14), bg="green", fg="white")
offButton.grid(row=1, column=0, pady=10)

# Status
STAT = tk.LabelFrame(ws, bd=5, background='white', text="Status", relief="ridge", width=box_width, height=box_height, padx=10, pady=10)
STAT.place(x=380, y=50)
STAT.grid_propagate(False)
guiOnCMDLed = tk.Label(STAT, image=redLed)
guiOnCMDLed.grid(row=1, column=0, pady=10)
tk.Label(STAT, text="ON\nCommand", bg='white', font=('Times', 14)).grid(row=1, column=1, padx=10, pady=10)
guiLedDCout =tk.Label(STAT, image=redLed)
guiLedDCout.grid(row=2, column=0, pady=10)
tk.Label(STAT, text="DC Output\nOK", bg='white', font=('Times', 14)).grid(row=2, column=1, padx=10, pady=10)

hwBtnDCout.when_pressed = dcOutOK
hwBtnDCout.when_released = dcOutFault

# Faults
FLTS = tk.LabelFrame(ws, bd=5, background='white', text="Faults", relief="ridge", width=box_width, height=box_height, padx=10, pady=10)
FLTS.place(x=545, y=50)
FLTS.grid_propagate(False)
guiLedov = tk.Label(FLTS, image=grnLed)
guiLedov.grid(row=0, column=0, pady=10)
tk.Label(FLTS, text="Over\nVoltage", bg='white', font=('Times', 14)).grid(row=0, column=1, padx=10, pady=10)

guiLedoc = tk.Label(FLTS, image=grnLed)
guiLedoc.grid(row=1, column=0, pady=10)
tk.Label(FLTS, text="Over\nCurrent", bg='white', font=('Times', 14)).grid(row=1, column=1, padx=10, pady=10)

guiLedot = tk.Label(FLTS, image=grnLed)
guiLedot.grid(row=2, column=0, pady=10)
tk.Label(FLTS, text="Over\nTemp", bg='white', font=('Times', 14)).grid(row=2, column=1, padx=10, pady=10)

hwBtnov.when_pressed = ovOK
hwBtnov.when_released = ovFAULT

hwBtnoc.when_pressed = ocOK
hwBtnoc.when_released = ocFAULT

hwBtnot.when_pressed = otOK
hwBtnot.when_released = otFAULT

# DC Output Voltage and Current
OUT = tk.LabelFrame(ws, bd=5, background='white', text="DC Output", relief="ridge", width=box_width, height=box_height, padx=10, pady=10)
OUT.place(x=710, y=50)
OUT.grid_propagate(False)
tk.Label(OUT, borderwidth=5, relief='ridge', text="Voltage\nOutput", font=('Times', 14)).grid(row=0, column=0, pady=10)
tk.Label(OUT, text="Volts", bg='white', font=('Times', 14)).grid(row=0, column=1, padx=10, pady=10)
tk.Label(OUT, borderwidth=5, relief='ridge', text="Current\nOutput", font=('Times', 14)).grid(row=1, column=0, pady=10)
tk.Label(OUT, text="Amps", bg='white', font=('Times', 14)).grid(row=1, column=1, padx=10, pady=10)


ws.mainloop()