#================================================================
#================================================================
# API-DIO(WDM)
# Simple I/O Sample
# main UI file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import messagebox


#========================================
# SimpleIOUI class
#========================================
class SimpleIOUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Simple I/O')
        self.root.minsize(595,420)
        #----------------------------------------
        # 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=347, y=20, width=110, height=19)
        #----------------------------------------
        # Create LabelFrame for I/O Set
        #----------------------------------------
        self.groupbox_dir_settings = tk.LabelFrame(self.root, text="I/O Set")
        self.groupbox_dir_settings.place(x=15, y=60, width=570, height=50)
        #--------------------
        # Create Label for I/O Direction
        #--------------------
        tk.Label(self.groupbox_dir_settings, text='I/O Direction : ', anchor='nw').place(x=5, y=5, width=90, height=19)
        #--------------------
        # Create Entry for I/O Direction
        #--------------------
        self.textbox_dir_var = tk.StringVar(self.groupbox_dir_settings)
        tk.Entry(self.groupbox_dir_settings, textvariable=self.textbox_dir_var, justify=tk.RIGHT).place(x=108, y=5, width=60, height=19)
        #--------------------
        # Create Button for DioSetIoDirection
        #--------------------
        self.button_dio_set_io_direction = tk.Button(self.groupbox_dir_settings, text='DioSetIoDirection')
        self.button_dio_set_io_direction.place(x=330, y=5, width=110, height=19)
        #----------------------------------------
        # Create LabelFrame for 1 Port Input
        #----------------------------------------
        self.groupbox_inp_port = tk.LabelFrame(self.root, text="1 Port Input")
        self.groupbox_inp_port.place(x=15, y=120, width=570, height=50)
        #--------------------
        # Create Label for Logic PortNo
        #--------------------
        tk.Label(self.groupbox_inp_port, text='Logic PortNo : ', anchor='nw').place(x=5, y=5, width=100, height=19)
        #--------------------
        # Create Entry for Logic PortNo
        #--------------------
        self.textbox_inp_port_no_var = tk.StringVar(self.groupbox_inp_port)
        self.textbox_inp_port_no_var.set("0")
        tk.Entry(self.groupbox_inp_port, textvariable=self.textbox_inp_port_no_var, justify=tk.RIGHT).place(x=110, y=5, width=40, height=19)
        #--------------------
        # Create Label for Input Data
        #--------------------
        tk.Label(self.groupbox_inp_port, text='Input Data : ', anchor='nw').place(x=155, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Input Data
        #--------------------
        self.textbox_inp_port_data_var = tk.StringVar(self.groupbox_inp_port)
        tk.Entry(self.groupbox_inp_port, textvariable=self.textbox_inp_port_data_var, justify=tk.RIGHT).place(x=240, y=5, width=40, height=19)
        #--------------------
        # Create Label for Input Data Base
        #--------------------
        tk.Label(self.groupbox_inp_port, text='(Hex)', anchor='nw').place(x=280, y=5, width=80, height=19)
        #--------------------
        # Create Button for DioInpByte
        #--------------------
        self.button_dio_inp_byte = tk.Button(self.groupbox_inp_port, text='DioInpByte')
        self.button_dio_inp_byte.place(x=330, y=5, width=110, height=19)
        #----------------------------------------
        # Create LabelFrame for 1 Bit Input
        #----------------------------------------
        self.groupbox_inp_bit = tk.LabelFrame(self.root, text="1 Bit Input")
        self.groupbox_inp_bit.place(x=15, y=180, width=570, height=50)
        #--------------------
        # Create Label for Logic BitNo
        #--------------------
        tk.Label(self.groupbox_inp_bit, text='Logic BitNo : ', anchor='nw').place(x=5, y=5, width=100, height=19)
        #--------------------
        # Create Entry for Logic BitNo
        #--------------------
        self.textbox_inp_bit_no_var = tk.StringVar(self.groupbox_inp_bit)
        self.textbox_inp_bit_no_var.set("0")
        tk.Entry(self.groupbox_inp_bit, textvariable=self.textbox_inp_bit_no_var, justify=tk.RIGHT).place(x=110, y=5, width=40, height=19)
        #--------------------
        # Create Label for Input Data
        #--------------------
        tk.Label(self.groupbox_inp_bit, text='Input Data : ', anchor='nw').place(x=155, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Input Data
        #--------------------
        self.textbox_inp_bit_data_var = tk.StringVar(self.groupbox_inp_bit)
        tk.Entry(self.groupbox_inp_bit, textvariable=self.textbox_inp_bit_data_var, justify=tk.RIGHT).place(x=240, y=5, width=40, height=19)
        #--------------------
        # Create Button for DioInpBit
        #--------------------
        self.button_dio_inp_bit = tk.Button(self.groupbox_inp_bit, text='DioInpBit')
        self.button_dio_inp_bit.place(x=330, y=5, width=110, height=19)
        #----------------------------------------
        # Create LabelFrame for 1 Port Output
        #----------------------------------------
        self.groupbox_out_port = tk.LabelFrame(self.root, text="1 Port Output")
        self.groupbox_out_port.place(x=15, y=240, width=570, height=50)
        #--------------------
        # Create Label for Logic PortNo
        #--------------------
        tk.Label(self.groupbox_out_port, text='Logic PortNo : ', anchor='nw').place(x=5, y=5, width=100, height=19)
        #--------------------
        # Create Entry for Logic PortNo
        #--------------------
        self.textbox_out_port_no_var = tk.StringVar(self.groupbox_out_port)
        self.textbox_out_port_no_var.set("0")
        tk.Entry(self.groupbox_out_port, textvariable=self.textbox_out_port_no_var, justify=tk.RIGHT).place(x=110, y=5, width=40, height=19)
        #--------------------
        # Create Label for Output Data
        #--------------------
        tk.Label(self.groupbox_out_port, text='Output Data : ', anchor='nw').place(x=155, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Output Data
        #--------------------
        self.textbox_out_port_data_var = tk.StringVar(self.groupbox_out_port)
        self.textbox_out_port_data_var.set("55")
        tk.Entry(self.groupbox_out_port, textvariable=self.textbox_out_port_data_var, justify=tk.RIGHT).place(x=240, y=5, width=40, height=19)
        #--------------------
        # Create Label for Input Data Base
        #--------------------
        tk.Label(self.groupbox_out_port, text='(Hex)', anchor='nw').place(x=280, y=5, width=80, height=19)
        #--------------------
        # Create Button for DioOutByte
        #--------------------
        self.button_dio_out_byte = tk.Button(self.groupbox_out_port, text='DioOutByte')
        self.button_dio_out_byte.place(x=330, y=5, width=110, height=19)
        #--------------------
        # Create Button for DioEchoBackByte
        #--------------------
        self.button_dio_echo_back_byte = tk.Button(self.groupbox_out_port, text='DioEchoBackByte')
        self.button_dio_echo_back_byte.place(x=445, y=5, width=110, height=19)
        #----------------------------------------
        # Create LabelFrame for 1 Bit Output
        #----------------------------------------
        self.groupbox_out_bit = tk.LabelFrame(self.root, text="1 Bit Output")
        self.groupbox_out_bit.place(x=15, y=300, width=570, height=50)
        #--------------------
        # Create Label for Logic BitNo
        #--------------------
        tk.Label(self.groupbox_out_bit, text='Logic BitNo : ', anchor='nw').place(x=5, y=5, width=100, height=19)
        #--------------------
        # Create Entry for Logic BitNo
        #--------------------
        self.textbox_out_bit_no_var = tk.StringVar(self.groupbox_out_bit)
        self.textbox_out_bit_no_var.set("0")
        tk.Entry(self.groupbox_out_bit, textvariable=self.textbox_out_bit_no_var, justify=tk.RIGHT).place(x=110, y=5, width=40, height=19)
        #--------------------
        # Create Label for Output Data
        #--------------------
        tk.Label(self.groupbox_out_bit, text='Output Data : ', anchor='nw').place(x=155, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Output Data
        #--------------------
        self.textbox_out_bit_data_var = tk.StringVar(self.groupbox_out_bit)
        self.textbox_out_bit_data_var.set("1")
        tk.Entry(self.groupbox_out_bit, textvariable=self.textbox_out_bit_data_var, justify=tk.RIGHT).place(x=240, y=5, width=40, height=19)
        #--------------------
        # Create Button for DioOutBit
        #--------------------
        self.button_dio_out_bit = tk.Button(self.groupbox_out_bit, text='DioOutBit')
        self.button_dio_out_bit.place(x=330, y=5, width=110, height=19)
        #--------------------
        # Create Button for DioEchoBackBit
        #--------------------
        self.button_dio_echo_back_bit = tk.Button(self.groupbox_out_bit, text='DioEchoBackBit')
        self.button_dio_echo_back_bit.place(x=445, y=5, width=110, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.root, text='Return Code : ', anchor='nw').place(x=20, y=360, 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=360, width=225, height=19)
        #--------------------
        # Create Button for DioExit
        #--------------------
        self.button_dio_exit = tk.Button(self.root, text='DioExit')
        self.button_dio_exit.place(x=347, y=360, width=110, height=19)
        #--------------------
        # Create Button for Exit
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=347, y=390, width=110, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_dio_init,
                       command_dio_set_io_direction,
                       command_dio_inp_byte,
                       command_dio_inp_bit,
                       command_dio_out_byte,
                       command_dio_echo_back_byte,
                       command_dio_out_bit,
                       command_dio_echo_back_bit,
                       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 [DioSetIoDirection] button
        #----------------------------------------
        self.button_dio_set_io_direction.configure(command=command_dio_set_io_direction)
        #----------------------------------------
        # Set the callback function for the [DioInpByte] button
        #----------------------------------------
        self.button_dio_inp_byte.configure(command=command_dio_inp_byte)
        #----------------------------------------
        # Set the callback function for the [DioInpBit] button
        #----------------------------------------
        self.button_dio_inp_bit.configure(command=command_dio_inp_bit)
        #----------------------------------------
        # Set the callback function for the [DioOutByte] button
        #----------------------------------------
        self.button_dio_out_byte.configure(command=command_dio_out_byte)
        #----------------------------------------
        # Set the callback function for the [DioEchoBackByte] button
        #----------------------------------------
        self.button_dio_echo_back_byte.configure(command=command_dio_echo_back_byte)
        #----------------------------------------
        # Set the callback function for the [DioOutBit] button
        #----------------------------------------
        self.button_dio_out_bit.configure(command=command_dio_out_bit)
        #----------------------------------------
        # Set the callback function for the [DioEchoBackBit] button
        #----------------------------------------
        self.button_dio_echo_back_bit.configure(command=command_dio_echo_back_bit)
        #----------------------------------------
        # 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)
 
    #========================================
    # Display the error in a message box
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.root)

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