#!/usr/bin/env python3
import RPi.GPIO as GPIO # a Python module to control the GPIO interface. 
import tkinter as tk

GPIO.setwarnings(False) # Turn off "pin in use" warnings
GPIO.setmode(GPIO.BOARD) # use physical pin numbering
GPIO.setup(12, GPIO.OUT) # Set GPIO18 to output pin.
p = GPIO.PWM(12, 50) # physical pin 12 (GPIO18) at 50 Hz


class MyApp: # "Constructor"
    def __init__(self,parent):
        self.myParent = parent

        # set title
        self.myParent.title("GPIO PWM %") # set window title
        self.myParent.geometry('300x76')  # set window size

        # Make topmost container with everything else inside
        self.myContainer1 = tk.Frame(parent) # topmost frame: myContainer1
        self.myContainer1.grid()

        #------ constants for controlling layout ------
        button_width = 10

        button_padx = "2m"      # 'm' = millimeters
        button_pady = "1m"
        # -------------- end constants ----------------

        # two more containers, left one for menu buttons, right one for data, horizontal layout.
        # left frame inside myContainer1
        self.left_frame = tk.Frame(self.myContainer1, background="tan",
                    borderwidth=2, relief=tk.RIDGE,
                    height=76, width=50)
        self.left_frame.grid(row=0)

        # right frame inside myContainer1
        self.right_frame = tk.Frame(self.myContainer1, background="gray",
                    borderwidth=2, relief=tk.RIDGE,
                    height=76, width=250)
        self.right_frame.grid(row=0, column=1)
        
        # add buttons for menu selection to left_frame
        self.button1 = tk.Button(self.left_frame, text='Activate', command=self.set_percent)
        self.button1.configure(width=button_width, padx=button_padx, pady=button_pady)
        self.button4 = tk.Button(self.left_frame, text='Quit', command=self.button4Click)
        self.button4.configure(width=button_width, padx=button_padx, pady=button_pady)

        self.button1.grid()
        self.button4.grid()
        
    def button4Click(self):
        p.ChangeDutyCycle(0)
        self.myParent.destroy()

    def NewFrame(self, color): # new 'right_frame' inside myContainer1. Use 'color' for background
        self.right_frame.destroy() # delete old frame before creating new one
        self.right_frame = tk.Frame(self.myContainer1, background=color,
                    borderwidth=2, relief=tk.RIDGE,
                    height=120, width=250, padx='5m')
        self.right_frame.grid(row=0, column=1)

    def set_percent(self):
        color = '#9f3' # color input to NewFrame()
        self.NewFrame(color) # call function to build new frame
        # zero variables
        self.dc = 0
        p.start(self.dc)
        # set variable label to accept floats
        self.percent_label_text = tk.DoubleVar(self.right_frame) # assign to right_frame
        self.percent_label_text.set(self.dc)
        #place label in right_frame
        self.percent_label = tk.Label(self.right_frame, textvariable=self.percent_label_text, bg=color)
        
        # label formats assigned to right_frame
        self.label = tk.Label(self.right_frame, text="Set PWM Percent", bg=color)
        self.label1 = tk.Label(self.right_frame, text="Enter %:", bg=color)
        self.label2 = tk.Label(self.right_frame, text=" Percent: ", bg=color)

        # setup entry fields
        self.entry_create = tk.Text(self.right_frame, width=10, height=1) # create text entry box
        self.entry_create.focus_set() # place cursor in text box
        self.entry_create.bind("<Return>", self.calc_percent) # on <enter> keypress, execute function 'calc_vswr'


        # SCREEN LAYOUT AND POSITIONING

        # position various widgets. 'sticky' used to expand contents to fill complete cell width.
        self.label.grid(row=0, columnspan=2) 
        self.label1.grid(row=2)
        self.entry_create.grid(row=2, column=1, sticky=tk.W+tk.E)
        self.label2.grid(row=3)
        self.percent_label.grid(row=3, column=1, sticky=tk.W+tk.E)

    def calc_percent(self, event=None):
        # 1.0 = start at first char; tk.END = all way to end; strip() = remove newline & spaces at <return> press.
        entry_text = self.entry_create.get(1.0, tk.END).strip()
        if len(entry_text) > 0: # prevent user adding blank entry
            dc = float(entry_text)
            if dc <= 100: # limit to 100%
                p.ChangeDutyCycle(dc)
                self.label2.config(text="Percent: ")
                self.dc = str("%.1f" % dc) # format "dc" output as desired
        self.percent_label_text.set(self.dc) # set output string into label
        self.entry_create.delete(1.0, tk.END) # clear text box (and <return> before typing anything)
    
def centerWindow(): # open the Tkinter window in center of screen
    w = 300 # width for the Tk root
    h = 76 # height for the Tk root

    # get screen width and height
    ws = root.winfo_screenwidth() # screen width/2 (dual screens)
    hs = root.winfo_screenheight() # height of the screen

    # calculate x and y coordinates for the Tk root window
    x = ((ws/2) - (w/2))
    y = (hs/2) - (h/2)

    # set the dimensions of the screen 
    # and where it is placed
    root.geometry('%dx%d+%d+%d' % (w, h, x, y))


# main program loop ------------------------
root = tk.Tk()
centerWindow()
myapp = MyApp(root)
root.mainloop()
