#================================================================
#================================================================
# API-DIO(WDM)
# Interrupt Sample
# main UI file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk


#========================================
# InterruptUI class
#========================================
class InterruptUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title("Interrupt")
        self.root.minsize(510,300)
        #----------------------------------------
        # Set the font for controls
        #----------------------------------------
        self.root.option_add("*Font", ("MS Gothic", 9))
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self._create_ui()

    #========================================
    # UI creation function
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # Initialize
        #----------------------------------------
        #--------------------
        # Create Label for Device Name
        #--------------------
        tk.Label(self.root, text='Device Name : ', anchor='nw').place(x=20, y=20, width=90, height=19)
        #--------------------
        # Create Entry for Device Name
        #--------------------
        self.textbox_device_name_var = tk.StringVar(self.root)
        self.textbox_device_name_var.set('DIO000')
        tk.Entry(self.root, textvariable=self.textbox_device_name_var).place(x=110, y=20, width=90, height=19)
        #--------------------
        # Create Button for DioInit
        #--------------------
        self.button_dio_init = tk.Button(self.root, text='DioInit')
        self.button_dio_init.place(x=365, y=20, width=110, height=19)
        #----------------------------------------
        # Create LabelFrame for Interrupt Monitor
        #----------------------------------------
        self.groupbox_interrupt = tk.LabelFrame(self.root, text="Interrupt Monitor")
        self.groupbox_interrupt.place(x=15, y=60, width=475, height=155)
        #--------------------
        # Create Label for Bit0-3
        #--------------------
        tk.Label(self.groupbox_interrupt, text='0', anchor='nw').place(x=122, y=1, width=10, height=19)
        tk.Label(self.groupbox_interrupt, text='1', anchor='nw').place(x=165, y=1, width=10, height=19)
        tk.Label(self.groupbox_interrupt, text='2', anchor='nw').place(x=208, y=1, width=10, height=19)
        tk.Label(self.groupbox_interrupt, text='3', anchor='nw').place(x=251, y=1, width=10, height=19)
        #--------------------
        # Create Label for Up-count
        #--------------------
        tk.Label(self.groupbox_interrupt, text='Up-count : ', anchor='nw').place(x=5, y=25, width=100, height=19)
        #--------------------
        # Create Entry for Bit0-3 Up-count
        #--------------------
        self.textbox_up0_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_up0_var, justify=tk.RIGHT).place(x=110, y=25, width=36, height=19)
        self.textbox_up1_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_up1_var, justify=tk.RIGHT).place(x=153, y=25, width=36, height=19)
        self.textbox_up2_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_up2_var, justify=tk.RIGHT).place(x=196, y=25, width=36, height=19)
        self.textbox_up3_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_up3_var, justify=tk.RIGHT).place(x=239, y=25, width=36, height=19)
        #--------------------
        # Create Label for Down-count
        #--------------------
        tk.Label(self.groupbox_interrupt, text='Down-count : ', anchor='nw').place(x=5, y=53, width=100, height=19)
        #--------------------
        # Create Entry for Bit0-3 Down-count
        #--------------------
        self.textbox_down0_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_down0_var, justify=tk.RIGHT).place(x=110, y=53, width=36, height=19)
        self.textbox_down1_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_down1_var, justify=tk.RIGHT).place(x=153, y=53, width=36, height=19)
        self.textbox_down2_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_down2_var, justify=tk.RIGHT).place(x=196, y=53, width=36, height=19)
        self.textbox_down3_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_down3_var, justify=tk.RIGHT).place(x=239, y=53, width=36, height=19)
        #--------------------
        # Create LabelFrame for Interrupt Logic
        #--------------------
        self.groupbox_logic = tk.LabelFrame(self.groupbox_interrupt, text="Interrupt Logic")
        self.groupbox_logic.place(x=280, y=5, width=180, height=70)
        #--------------------
        # Create Radiobutton for Interrupt Logic
        #--------------------
        self.radio_logic_var = tk.IntVar()
        self.radio_logic_var.set(0)
        tk.Radiobutton(self.groupbox_logic, text='0 -> 1 (Rising edge)', anchor='nw', variable=self.radio_logic_var, value=0).place(x=1, y=5, width=170, height=19)
        tk.Radiobutton(self.groupbox_logic, text='1 -> 0 (Falling edge)', anchor='nw', variable=self.radio_logic_var, value=1).place(x=1, y=25, width=170, height=19)
        #--------------------
        # Create Button for Start Interrupt
        #--------------------
        self.button_int_start = tk.Button(self.groupbox_interrupt, text='Start Interrupt')
        self.button_int_start.place(x=348, y=80, width=110, height=19)
        #--------------------
        # Create Button for Stop Interrupt
        #--------------------
        self.button_int_stop = tk.Button(self.groupbox_interrupt, text='Stop Interrupt')
        self.button_int_stop.place(x=348, y=110, width=110, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.root, text='Return Code : ', anchor='nw').place(x=20, y=230, width=90, height=19)
        #--------------------
        # Create Entry for Return Code
        #--------------------
        self.textbox_return_string_var= tk.StringVar(self.root)
        tk.Entry(self.root, textvariable=self.textbox_return_string_var).place(x=105, y=230, width=250, height=19)
        #--------------------
        # Create Button for DioExit
        #--------------------
        self.button_dio_exit = tk.Button(self.root, text='DioExit')
        self.button_dio_exit.place(x=365, y=230, width=110, height=19)
        #--------------------
        # Create Button for Exit
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=365, y=260, width=110, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_dio_init,
                       command_int_start,
                       command_int_stop,
                       command_dio_exit,
                       command_exit):
        #----------------------------------------
        # Set the callback function for the [DioInit] button
        #----------------------------------------
        self.button_dio_init.configure(command=command_dio_init)
        #----------------------------------------
        # Set the callback function for the [Start Interrupt] button
        #----------------------------------------
        self.button_int_start.configure(command=command_int_start)
        #----------------------------------------
        # Set the callback function for the [Stop Interrupt] button
        #----------------------------------------
        self.button_int_stop.configure(command=command_int_stop)
        #----------------------------------------
        # Set the callback function for the [DioExit] button
        #----------------------------------------
        self.button_dio_exit.configure(command=command_dio_exit)
        #----------------------------------------
        # Set the callback function for the [Exit] button
        #----------------------------------------
        self.button_exit.configure(command=command_exit)

    #========================================
    # Run the main event loop
    #========================================
    def run(self):
        self.root.mainloop()