#================================================================
#================================================================
# API-DIO(WDM)
# Pulse Measurement Sample
# main UI file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# PulseMeasurementUI class
#========================================
class PulseMeasurementUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Pulse Measurement')
        self.root.minsize(738,520)
        #----------------------------------------
        # 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 Initialization
        #----------------------------------------
        self.groupbox_init = tk.LabelFrame(self.root, text="<Initialization>")
        self.groupbox_init.place(x=15, y=10, width=713, height=52)
        #--------------------
        # Create Label for Device Name
        #--------------------
        tk.Label(self.groupbox_init, text='Device Name:', anchor='nw').place(x=5, y=5, width=75, height=19)
        #--------------------
        # Create Entry for Device Name
        #--------------------
        self.textbox_device_name_var = tk.StringVar(self.groupbox_init)
        self.textbox_device_name_var.set("DIO000")
        tk.Entry(self.groupbox_init, textvariable=self.textbox_device_name_var).place(x=125, y=5, width=90, height=19)
        #--------------------
        # Create Button for DioInit
        #--------------------
        self.button_dio_init = tk.Button(self.groupbox_init, text='DioInit')
        self.button_dio_init.place(x=510, y=5, width=180, height=24)
        #----------------------------------------
        # Create LabelFrame for Common Settings
        #----------------------------------------
        self.groupbox_common_settings = tk.LabelFrame(self.root, text="<Common Settings>")
        self.groupbox_common_settings.place(x=15, y=65, width=713, height=52)
        #--------------------
        # Create Label for Number of Channels
        #--------------------
        tk.Label(self.groupbox_common_settings, text='Number of Channels:', anchor='nw').place(x=5, y=5, width=115, height=19)
        #--------------------
        # Create Combobox for Number of Channels
        #--------------------
        self.combobox_channel_num = ttk.Combobox(self.groupbox_common_settings)
        self.combobox_channel_num.place(x=125, y=5, width=90, height=19)
        self.combobox_channel_num['value'] = ('1', '2', '3', '4')
        self.combobox_channel_num.current(3)
        self.combobox_channel_num['state'] = 'readonly'
        #----------------------------------------
        # Create LabelFrame for Pulse Measurement Settings
        #----------------------------------------
        self.groupbox_pulse_measurement_settings = tk.LabelFrame(self.root, text="<Pulse Measurement Settings>")
        self.groupbox_pulse_measurement_settings.place(x=15, y=120, width=713, height=87)
        #--------------------
        # Create Label for Ch0 to Ch3
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_settings, text='Ch 0', anchor='nw').place(x=155, y=-2, width=30, height=19)
        tk.Label(self.groupbox_pulse_measurement_settings, text='Ch 1', anchor='nw').place(x=245, y=-2, width=30, height=19)
        tk.Label(self.groupbox_pulse_measurement_settings, text='Ch 2', anchor='nw').place(x=335, y=-2, width=30, height=19)
        tk.Label(self.groupbox_pulse_measurement_settings, text='Ch 3', anchor='nw').place(x=425, y=-2, width=30, height=19)
        #--------------------
        # Create Label for Pulse Measurement Logic
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_settings, text='Measurement Time:', anchor='nw').place(x=5, y=17, width=115, height=19)
        #--------------------
        # Create Combobox for Ch0 to Ch3 Pulse Measurement Logic
        #--------------------
        self.combobox_ch0_pulse_measurement_logic = ttk.Combobox(self.groupbox_pulse_measurement_settings)
        self.combobox_ch0_pulse_measurement_logic.place(x=125, y=17, width=80, height=19)
        self.combobox_ch0_pulse_measurement_logic['value'] = ('OFF Time', 'ON Time')
        self.combobox_ch0_pulse_measurement_logic.current(0)
        self.combobox_ch0_pulse_measurement_logic['state'] = 'readonly'
        self.combobox_ch1_pulse_measurement_logic = ttk.Combobox(self.groupbox_pulse_measurement_settings)
        self.combobox_ch1_pulse_measurement_logic.place(x=215, y=17, width=80, height=19)
        self.combobox_ch1_pulse_measurement_logic['value'] = ('OFF Time', 'ON Time')
        self.combobox_ch1_pulse_measurement_logic.current(0)
        self.combobox_ch1_pulse_measurement_logic['state'] = 'readonly'
        self.combobox_ch2_pulse_measurement_logic = ttk.Combobox(self.groupbox_pulse_measurement_settings)
        self.combobox_ch2_pulse_measurement_logic.place(x=305, y=17, width=80, height=19)
        self.combobox_ch2_pulse_measurement_logic['value'] = ('OFF Time', 'ON Time')
        self.combobox_ch2_pulse_measurement_logic.current(0)
        self.combobox_ch2_pulse_measurement_logic['state'] = 'readonly'
        self.combobox_ch3_pulse_measurement_logic = ttk.Combobox(self.groupbox_pulse_measurement_settings)
        self.combobox_ch3_pulse_measurement_logic.place(x=395, y=17, width=80, height=19)
        self.combobox_ch3_pulse_measurement_logic['value'] = ('OFF Time', 'ON Time')
        self.combobox_ch3_pulse_measurement_logic.current(0)
        self.combobox_ch3_pulse_measurement_logic['state'] = 'readonly'
        #--------------------
        # Create Label for Pulse Measurement Period
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_settings, text='Measurement Period:', anchor='nw').place(x=5, y=42, width=115, height=19)
        #--------------------
        # Create Entry for Ch0 to Ch3 Pulse Measurement Period
        #--------------------
        self.textbox_ch0_pulse_measurement_period_var = tk.StringVar(self.groupbox_pulse_measurement_settings)
        self.textbox_ch0_pulse_measurement_period_var.set('0')
        tk.Entry(self.groupbox_pulse_measurement_settings, textvariable=self.textbox_ch0_pulse_measurement_period_var, justify=tk.RIGHT).place(x=125, y=42, width=80, height=19)
        self.textbox_ch1_pulse_measurement_period_var = tk.StringVar(self.groupbox_pulse_measurement_settings)
        self.textbox_ch1_pulse_measurement_period_var.set('0')
        tk.Entry(self.groupbox_pulse_measurement_settings, textvariable=self.textbox_ch1_pulse_measurement_period_var, justify=tk.RIGHT).place(x=215, y=42, width=80, height=19)
        self.textbox_ch2_pulse_measurement_period_var = tk.StringVar(self.groupbox_pulse_measurement_settings)
        self.textbox_ch2_pulse_measurement_period_var.set('0')
        tk.Entry(self.groupbox_pulse_measurement_settings, textvariable=self.textbox_ch2_pulse_measurement_period_var, justify=tk.RIGHT).place(x=305, y=42, width=80, height=19)
        self.textbox_ch3_pulse_measurement_period_var = tk.StringVar(self.groupbox_pulse_measurement_settings)
        self.textbox_ch3_pulse_measurement_period_var.set('0')
        tk.Entry(self.groupbox_pulse_measurement_settings, textvariable=self.textbox_ch3_pulse_measurement_period_var, justify=tk.RIGHT).place(x=395, y=42, width=80, height=19)
        #--------------------
        # Create Button for DioSetPulseMeasurementSetting
        #--------------------
        self.button_dio_set_pulse_measurement_setting = tk.Button(self.groupbox_pulse_measurement_settings, text='DioSetPulseMeasurementSetting')
        self.button_dio_set_pulse_measurement_setting.place(x=510, y=22, width=180, height=24)
        #----------------------------------------
        # Create LabelFrame for Pulse Measurement Operation
        #----------------------------------------
        self.groupbox_pulse_measurement_operation = tk.LabelFrame(self.root, text="<Pulse Measurement Operation>")
        self.groupbox_pulse_measurement_operation.place(x=15, y=210, width=713, height=172)
        #--------------------
        # Create Label for Ch0 to Ch3
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_operation, text='Ch 0', anchor='nw').place(x=155, y=8, width=30, height=19)
        tk.Label(self.groupbox_pulse_measurement_operation, text='Ch 1', anchor='nw').place(x=245, y=8, width=30, height=19)
        tk.Label(self.groupbox_pulse_measurement_operation, text='Ch 2', anchor='nw').place(x=335, y=8, width=30, height=19)
        tk.Label(self.groupbox_pulse_measurement_operation, text='Ch 3', anchor='nw').place(x=425, y=8, width=30, height=19)
        #--------------------
        # Create Label for Pulse Measurement Status
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_operation, text='Status:', anchor='nw').place(x=5, y=27, width=75, height=19)
        #--------------------
        # Create Entry for Ch0 to Ch3 Pulse Measurement Status
        #--------------------
        self.textbox_ch0_pulse_measurement_status_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch0_pulse_measurement_status_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch0_pulse_measurement_status_var, justify=tk.RIGHT).place(x=125, y=27, width=80, height=19)
        self.textbox_ch1_pulse_measurement_status_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch1_pulse_measurement_status_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch1_pulse_measurement_status_var, justify=tk.RIGHT).place(x=215, y=27, width=80, height=19)
        self.textbox_ch2_pulse_measurement_status_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch2_pulse_measurement_status_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch2_pulse_measurement_status_var, justify=tk.RIGHT).place(x=305, y=27, width=80, height=19)
        self.textbox_ch3_pulse_measurement_status_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch3_pulse_measurement_status_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch3_pulse_measurement_status_var, justify=tk.RIGHT).place(x=395, y=27, width=80, height=19)
        #--------------------
        # Create Label for [Hex]
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_operation, text='[Hex]', anchor='nw').place(x=475, y=27, width=85, height=19)
        #--------------------
        # Create Label for Pulse Measurement Error
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_operation, text='Error:', anchor='nw').place(x=5, y=52, width=75, height=19)
        #--------------------
        # Create Entry for Ch0 to Ch3 Pulse Measurement Error
        #--------------------
        self.textbox_ch0_pulse_measurement_error_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch0_pulse_measurement_error_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch0_pulse_measurement_error_var, justify=tk.RIGHT).place(x=125, y=52, width=80, height=19)
        self.textbox_ch1_pulse_measurement_error_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch1_pulse_measurement_error_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch1_pulse_measurement_error_var, justify=tk.RIGHT).place(x=215, y=52, width=80, height=19)
        self.textbox_ch2_pulse_measurement_error_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch2_pulse_measurement_error_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch2_pulse_measurement_error_var, justify=tk.RIGHT).place(x=305, y=52, width=80, height=19)
        self.textbox_ch3_pulse_measurement_error_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch3_pulse_measurement_error_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch3_pulse_measurement_error_var, justify=tk.RIGHT).place(x=395, y=52, width=80, height=19)
        #--------------------
        # Create Label for [Hex]
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_operation, text='[Hex]', anchor='nw').place(x=475, y=52, width=85, height=19)
        #--------------------
        # Create Label for Pulse Measurement Time Value
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_operation, text='Measurement Time:', anchor='nw').place(x=5, y=77, width=115, height=19)
        #--------------------
        # Create Entry for Ch0 to Ch3 Pulse Measurement Time Value
        #--------------------
        self.textbox_ch0_pulse_measurement_time_value_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch0_pulse_measurement_time_value_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch0_pulse_measurement_time_value_var, justify=tk.RIGHT).place(x=125, y=77, width=80, height=19)
        self.textbox_ch1_pulse_measurement_time_value_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch1_pulse_measurement_time_value_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch1_pulse_measurement_time_value_var, justify=tk.RIGHT).place(x=215, y=77, width=80, height=19)
        self.textbox_ch2_pulse_measurement_time_value_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch2_pulse_measurement_time_value_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch2_pulse_measurement_time_value_var, justify=tk.RIGHT).place(x=305, y=77, width=80, height=19)
        self.textbox_ch3_pulse_measurement_time_value_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch3_pulse_measurement_time_value_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch3_pulse_measurement_time_value_var, justify=tk.RIGHT).place(x=395, y=77, width=80, height=19)
        #--------------------
        # Create Combobox for Ch0 to Ch3 Pulse Measurement Time Unit
        #--------------------
        self.combobox_ch0_pulse_measurement_time_unit = ttk.Combobox(self.groupbox_pulse_measurement_operation)
        self.combobox_ch0_pulse_measurement_time_unit.place(x=125, y=102, width=80, height=19)
        self.combobox_ch0_pulse_measurement_time_unit['value'] = ('sec', 'msec', 'usec', 'nsec')
        self.combobox_ch0_pulse_measurement_time_unit.current(3)
        self.combobox_ch0_pulse_measurement_time_unit['state'] = 'readonly'
        self.combobox_ch1_pulse_measurement_time_unit = ttk.Combobox(self.groupbox_pulse_measurement_operation)
        self.combobox_ch1_pulse_measurement_time_unit.place(x=215, y=102, width=80, height=19)
        self.combobox_ch1_pulse_measurement_time_unit['value'] = ('sec', 'msec', 'usec', 'nsec')
        self.combobox_ch1_pulse_measurement_time_unit.current(3)
        self.combobox_ch1_pulse_measurement_time_unit['state'] = 'readonly'
        self.combobox_ch2_pulse_measurement_time_unit = ttk.Combobox(self.groupbox_pulse_measurement_operation)
        self.combobox_ch2_pulse_measurement_time_unit.place(x=305, y=102, width=80, height=19)
        self.combobox_ch2_pulse_measurement_time_unit['value'] = ('sec', 'msec', 'usec', 'nsec')
        self.combobox_ch2_pulse_measurement_time_unit.current(3)
        self.combobox_ch2_pulse_measurement_time_unit['state'] = 'readonly'
        self.combobox_ch3_pulse_measurement_time_unit = ttk.Combobox(self.groupbox_pulse_measurement_operation)
        self.combobox_ch3_pulse_measurement_time_unit.place(x=395, y=102, width=80, height=19)
        self.combobox_ch3_pulse_measurement_time_unit['value'] = ('sec', 'msec', 'usec', 'nsec')
        self.combobox_ch3_pulse_measurement_time_unit.current(3)
        self.combobox_ch3_pulse_measurement_time_unit['state'] = 'readonly'
        #--------------------
        # Create Button for DioStartPulseMeasurement
        #--------------------
        self.button_dio_start_pulse_measurement = tk.Button(self.groupbox_pulse_measurement_operation, text='DioStartPulseMeasurement')
        self.button_dio_start_pulse_measurement.place(x=510, y=5, width=180, height=24)
        #--------------------
        # Create Button for DioReadPulseMeasurementData
        #--------------------
        self.button_dio_read_pulse_measurement_data = tk.Button(self.groupbox_pulse_measurement_operation, text='DioReadPulseMeasurementData')
        self.button_dio_read_pulse_measurement_data.place(x=510, y=65, width=180, height=24)
        #--------------------
        # Create Button for DioStopPulseMeasurement
        #--------------------
        self.button_dio_stop_pulse_measurement = tk.Button(self.groupbox_pulse_measurement_operation, text='DioStopPulseMeasurement')
        self.button_dio_stop_pulse_measurement.place(x=510, y=125, width=180, height=24)
        #----------------------------------------
        # Create LabelFrame for Exit
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="<Exit>")
        self.groupbox_exit.place(x=15, y=385, width=713, height=52)
        #--------------------
        # Create Button for DioExit
        #--------------------
        self.button_dio_exit = tk.Button(self.groupbox_exit, text='DioExit')
        self.button_dio_exit.place(x=510, y=5, width=180, height=24)
        #----------------------------------------
        # Create Label for Return Code
        #----------------------------------------
        tk.Label(self.root, text='Return Code:', anchor='nw').place(x=22, y=445, width=75, height=19)
        #----------------------------------------
        # Create Entry for Return Code
        #----------------------------------------
        self.textbox_return_string_var = tk.StringVar(self.root)
        self.textbox_return_string_var.set('')
        tk.Entry(self.root, textvariable=self.textbox_return_string_var).place(x=142, y=445, width=350, height=19)
        #----------------------------------------
        # Create Button for Close
        #----------------------------------------
        self.button_close = tk.Button(self.root, text='Close')
        self.button_close.place(x=527, y=470, width=180, height=24)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_dio_init,
                       command_dio_set_pulse_measurement_setting,
                       command_dio_start_pulse_measurement,
                       command_dio_read_pulse_measurement_data,
                       command_dio_stop_pulse_measurement,
                       command_dio_exit,
                       command_close):
        #----------------------------------------
        # Set the callback function for the [DioInit] button
        #----------------------------------------
        self.button_dio_init.configure(command=command_dio_init)
        #----------------------------------------
        # Set the callback function for the [DioSetPulseMeasurementSetting] button
        #----------------------------------------
        self.button_dio_set_pulse_measurement_setting.configure(command=command_dio_set_pulse_measurement_setting)
        #----------------------------------------
        # Set the callback function for the [DioStartPulseMeasurement] button
        #----------------------------------------
        self.button_dio_start_pulse_measurement.configure(command=command_dio_start_pulse_measurement)
        #----------------------------------------
        # Set the callback function for the [DioReadPulseMeasurementData] button
        #----------------------------------------
        self.button_dio_read_pulse_measurement_data.configure(command=command_dio_read_pulse_measurement_data)
        #----------------------------------------
        # Set the callback function for the [DioStopPulseMeasurement] button
        #----------------------------------------
        self.button_dio_stop_pulse_measurement.configure(command=command_dio_stop_pulse_measurement)
        #----------------------------------------
        # Set the callback function for the [DioExit] button
        #----------------------------------------
        self.button_dio_exit.configure(command=command_dio_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()