#================================================================
#================================================================
# API-AIO(WDM)
# Digital general purpose input and output sample
# main UI file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import messagebox


#========================================
# DigitalIOUI class
#========================================
class DigitalIOUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Digital General Purpose Input and Output')
        self.root.minsize(670,490)
        #----------------------------------------
        # 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):
        #----------------------------------------
        # Create LabelFrame for Initialize
        #----------------------------------------
        self.groupbox_init = tk.LabelFrame(self.root, text="Initialize")
        self.groupbox_init.place(x=15, y=10, width=640, height=75)
        #--------------------
        # Create Label for Device Name
        #--------------------
        tk.Label(self.groupbox_init, text='Device Name : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Device Name
        #--------------------
        self.textbox_device_name_var = tk.StringVar(self.groupbox_init)
        self.textbox_device_name_var.set('AIO000')
        tk.Entry(self.groupbox_init, textvariable=self.textbox_device_name_var).place(x=110, y=5, width=100, height=19)
        #--------------------
        # Create Button for AioInit
        #--------------------
        self.button_aio_init = tk.Button(self.groupbox_init, text='AioInit')
        self.button_aio_init.place(x=510, y=5, width=120, height=19)
        #--------------------
        # Create Button for AioResetDevice
        #--------------------
        self.button_aio_reset_device = tk.Button(self.groupbox_init, text='AioResetDevice')
        self.button_aio_reset_device.place(x=510, y=30, width=120, height=19)
        #----------------------------------------
        # Create LabelFrame for I/O Direction Set
        #----------------------------------------
        self.groupbox_dir_settings = tk.LabelFrame(self.root, text="I/O Direction Set")
        self.groupbox_dir_settings.place(x=15, y=90, width=640, height=50)
        #--------------------
        # Create Label for I/O Direction
        #--------------------
        tk.Label(self.groupbox_dir_settings, text='I/O Direction : ', anchor='nw').place(x=15, y=5, width=100, height=19)
        #--------------------
        # Create Entry for I/O Direction
        #--------------------
        self.textbox_io_direction_var = tk.StringVar(self.groupbox_dir_settings)
        self.textbox_io_direction_var.set("0")
        tk.Entry(self.groupbox_dir_settings, textvariable=self.textbox_io_direction_var, justify=tk.RIGHT).place(x=110, y=5, width=100, height=19)
        #--------------------
        # Create Label for I/O Direction Base
        #--------------------
        tk.Label(self.groupbox_dir_settings, text='[Dec]', anchor='nw').place(x=215, y=5, width=90, height=19)
        #--------------------
        # Create Button for AioSetDioDirection
        #--------------------
        self.button_aio_set_dio_direction = tk.Button(self.groupbox_dir_settings, text='AioSetDioDirection')
        self.button_aio_set_dio_direction.place(x=510, y=5, width=120, height=19)
        #----------------------------------------
        # Create LabelFrame for 1Port Input
        #----------------------------------------
        self.groupbox_inp_port = tk.LabelFrame(self.root, text="1Port Input")
        self.groupbox_inp_port.place(x=15, y=145, width=640, height=50)
        #--------------------
        # Create Label for PortNo
        #--------------------
        tk.Label(self.groupbox_inp_port, text='PortNo : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # Create Entry for PortNo
        #--------------------
        self.textbox_input_byte_no_var = tk.StringVar(self.groupbox_inp_port)
        self.textbox_input_byte_no_var.set("0")
        tk.Entry(self.groupbox_inp_port, textvariable=self.textbox_input_byte_no_var, justify=tk.RIGHT).place(x=110, y=5, width=100, height=19)
        #--------------------
        # Create Label for PortNo Base
        #--------------------
        tk.Label(self.groupbox_inp_port, text='[Dec]', anchor='nw').place(x=215, y=5, width=90, height=19)
        #--------------------
        # Create Label for Input Data
        #--------------------
        tk.Label(self.groupbox_inp_port, text='Input Data : ', anchor='nw').place(x=260, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Input Data
        #--------------------
        self.textbox_input_byte_data_var = tk.StringVar(self.groupbox_inp_port)
        self.textbox_input_byte_data_var.set("00")
        tk.Entry(self.groupbox_inp_port, textvariable=self.textbox_input_byte_data_var, justify=tk.RIGHT).place(x=345, y=5, width=100, height=19)
        #--------------------
        # Create Label for Input Data Base
        #--------------------
        tk.Label(self.groupbox_inp_port, text='[Hex]', anchor='nw').place(x=450, y=5, width=80, height=19)
        #--------------------
        # Create Button for AioInputDiByte
        #--------------------
        self.button_aio_input_di_byte = tk.Button(self.groupbox_inp_port, text='AioInputDiByte')
        self.button_aio_input_di_byte.place(x=510, y=5, width=120, height=19)
        #----------------------------------------
        # Create LabelFrame for 1Bit Input
        #----------------------------------------
        self.groupbox_inp_bit = tk.LabelFrame(self.root, text="1Bit Input")
        self.groupbox_inp_bit.place(x=15, y=200, width=640, height=50)
        #--------------------
        # Create Label for BitNo
        #--------------------
        tk.Label(self.groupbox_inp_bit, text='BitNo : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # Create Entry for BitNo
        #--------------------
        self.textbox_input_bit_no_var = tk.StringVar(self.groupbox_inp_bit)
        self.textbox_input_bit_no_var.set("0")
        tk.Entry(self.groupbox_inp_bit, textvariable=self.textbox_input_bit_no_var, justify=tk.RIGHT).place(x=110, y=5, width=100, height=19)
        #--------------------
        # Create Label for BitNo Base
        #--------------------
        tk.Label(self.groupbox_inp_bit, text='[Dec]', anchor='nw').place(x=215, y=5, width=90, height=19)
        #--------------------
        # Create Label for Input Data
        #--------------------
        tk.Label(self.groupbox_inp_bit, text='Input Data : ', anchor='nw').place(x=260, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Input Data
        #--------------------
        self.textbox_input_bit_data_var = tk.StringVar(self.groupbox_inp_bit)
        self.textbox_input_bit_data_var.set("0")
        tk.Entry(self.groupbox_inp_bit, textvariable=self.textbox_input_bit_data_var, justify=tk.RIGHT).place(x=345, y=5, width=100, height=19)
        #--------------------
        # Create Label for Input Data Base
        #--------------------
        tk.Label(self.groupbox_inp_bit, text='[Dec]', anchor='nw').place(x=450, y=5, width=80, height=19)
        #--------------------
        # Create Button for AioInputDiBit
        #--------------------
        self.button_aio_input_di_bit = tk.Button(self.groupbox_inp_bit, text='AioInputDiBit')
        self.button_aio_input_di_bit.place(x=510, y=5, width=120, height=19)
        #----------------------------------------
        # Create LabelFrame for 1Port Output
        #----------------------------------------
        self.groupbox_out_port = tk.LabelFrame(self.root, text="1Port Output")
        self.groupbox_out_port.place(x=15, y=255, width=640, height=50)
        #--------------------
        # Create Label for PortNo
        #--------------------
        tk.Label(self.groupbox_out_port, text='PortNo : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # Create Entry for PortNo
        #--------------------
        self.textbox_output_byte_no_var = tk.StringVar(self.groupbox_out_port)
        self.textbox_output_byte_no_var.set("0")
        tk.Entry(self.groupbox_out_port, textvariable=self.textbox_output_byte_no_var, justify=tk.RIGHT).place(x=110, y=5, width=100, height=19)
        #--------------------
        # Create Label for PortNo Base
        #--------------------
        tk.Label(self.groupbox_out_port, text='[Dec]', anchor='nw').place(x=215, y=5, width=90, height=19)
        #--------------------
        # Create Label for Output Data
        #--------------------
        tk.Label(self.groupbox_out_port, text='Output Data : ', anchor='nw').place(x=260, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Output Data
        #--------------------
        self.textbox_output_byte_data_var = tk.StringVar(self.groupbox_out_port)
        self.textbox_output_byte_data_var.set("00")
        tk.Entry(self.groupbox_out_port, textvariable=self.textbox_output_byte_data_var, justify=tk.RIGHT).place(x=345, y=5, width=100, height=19)
        #--------------------
        # Create Label for Output Data Base
        #--------------------
        tk.Label(self.groupbox_out_port, text='[Hex]', anchor='nw').place(x=450, y=5, width=80, height=19)
        #--------------------
        # Create Button for AioOutputDoByte
        #--------------------
        self.button_aio_output_do_byte = tk.Button(self.groupbox_out_port, text='AioOutputDoByte')
        self.button_aio_output_do_byte.place(x=510, y=5, width=120, height=19)
        #----------------------------------------
        # Create LabelFrame for 1Bit Output
        #----------------------------------------
        self.groupbox_out_bit = tk.LabelFrame(self.root, text="1Bit Output")
        self.groupbox_out_bit.place(x=15, y=310, width=640, height=50)
        #--------------------
        # Create Label for BitNo
        #--------------------
        tk.Label(self.groupbox_out_bit, text='BitNo : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # Create Entry for BitNo
        #--------------------
        self.textbox_output_bit_no_var = tk.StringVar(self.groupbox_out_bit)
        self.textbox_output_bit_no_var.set("0")
        tk.Entry(self.groupbox_out_bit, textvariable=self.textbox_output_bit_no_var, justify=tk.RIGHT).place(x=110, y=5, width=100, height=19)
        #--------------------
        # Create Label for BitNo Base
        #--------------------
        tk.Label(self.groupbox_out_bit, text='[Dec]', anchor='nw').place(x=215, y=5, width=90, height=19)
        #--------------------
        # Create Label for Output Data
        #--------------------
        tk.Label(self.groupbox_out_bit, text='Output Data : ', anchor='nw').place(x=260, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Output Data
        #--------------------
        self.textbox_output_bit_data_var = tk.StringVar(self.groupbox_out_bit)
        self.textbox_output_bit_data_var.set("0")
        tk.Entry(self.groupbox_out_bit, textvariable=self.textbox_output_bit_data_var, justify=tk.RIGHT).place(x=345, y=5, width=100, height=19)
        #--------------------
        # Create Label for Output Data Base
        #--------------------
        tk.Label(self.groupbox_out_bit, text='[Dec]', anchor='nw').place(x=450, y=5, width=80, height=19)
        #--------------------
        # Create Button for AioOutputDoBit
        #--------------------
        self.button_aio_output_do_bit = tk.Button(self.groupbox_out_bit, text='AioOutputDoBit')
        self.button_aio_output_do_bit.place(x=510, y=5, width=120, height=19)
        #----------------------------------------
        # Create LabelFrame for Exit process
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="Exit")
        self.groupbox_exit.place(x=15, y=365, width=640, height=50)
        #--------------------
        # Create Button for AioExit
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=510, y=5, width=120, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.root, text='Return Code : ', anchor='nw').place(x=33, y=430, 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=127, y=430, width=380, height=19)
        #--------------------
        # Create Button for Close
        #--------------------
        self.button_close = tk.Button(self.root, text='Close')
        self.button_close.place(x=527, y=455, width=120, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_aio_init,
                       command_aio_reset_device,
                       command_aio_set_dio_direction,
                       command_aio_input_di_byte,
                       command_aio_input_di_bit,
                       command_aio_output_do_byte,
                       command_aio_output_do_bit,
                       command_aio_exit,
                       command_close):
        #----------------------------------------
        # Set the callback function for the [AioInit] button
        #----------------------------------------
        self.button_aio_init.configure(command=command_aio_init)
        #----------------------------------------
        # Set the callback function for the [AioResetDevice] button
        #----------------------------------------
        self.button_aio_reset_device.configure(command=command_aio_reset_device)
        #----------------------------------------
        # Set the callback function for the [AioSetDioDirection] button
        #----------------------------------------
        self.button_aio_set_dio_direction.configure(command=command_aio_set_dio_direction)
        #----------------------------------------
        # Set the callback function for the [AioInputDiByte] button
        #----------------------------------------
        self.button_aio_input_di_byte.configure(command=command_aio_input_di_byte)
        #----------------------------------------
        # Set the callback function for the [AioInputDiBit] button
        #----------------------------------------
        self.button_aio_input_di_bit.configure(command=command_aio_input_di_bit)
        #----------------------------------------
        # Set the callback function for the [AioOutputDoByte] button
        #----------------------------------------
        self.button_aio_output_do_byte.configure(command=command_aio_output_do_byte)
        #----------------------------------------
        # Set the callback function for the [AioOutputDoBit] button
        #----------------------------------------
        self.button_aio_output_do_bit.configure(command=command_aio_output_do_bit)
        #----------------------------------------
        # Set the callback function for the [AioExit] button
        #----------------------------------------
        self.button_aio_exit.configure(command=command_aio_exit)
        #----------------------------------------
        # Set the callback function for the [Close] button
        #----------------------------------------
        self.button_close.configure(command=command_close)

    #========================================
    # 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()