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


#========================================
# PwmOutputUI class
#========================================
class PwmOutputUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('PWM Pulse Output')
        self.root.minsize(688,635)
        #----------------------------------------
        # 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=663, 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=130, 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=663, 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 PWM Operation Settings
        #----------------------------------------
        self.groupbox_pwm_operation_settings = tk.LabelFrame(self.root, text="<PWM Operation Settings>")
        self.groupbox_pwm_operation_settings.place(x=15, y=120, width=663, height=270)
        #--------------------
        # Create LabelFrame for Pulse Period Settings
        #--------------------
        self.groupbox_pulse_period_settings = tk.LabelFrame(self.groupbox_pwm_operation_settings, text="<Pulse Period Settings>")
        self.groupbox_pulse_period_settings.place(x=7, y=5, width=643, height=82)
        #--------------------
        # Create Label for Pulse Period
        #--------------------
        tk.Label(self.groupbox_pulse_period_settings, text='Pulse Period:', anchor='nw').place(x=5, y=17, width=85, height=19)
        #--------------------
        # Create Label for Ch0 to Ch3
        #--------------------
        tk.Label(self.groupbox_pulse_period_settings, text='Ch 0', anchor='nw').place(x=124, y=-2, width=30, height=19)
        tk.Label(self.groupbox_pulse_period_settings, text='Ch 1', anchor='nw').place(x=214, y=-2, width=30, height=19)
        tk.Label(self.groupbox_pulse_period_settings, text='Ch 2', anchor='nw').place(x=304, y=-2, width=30, height=19)
        tk.Label(self.groupbox_pulse_period_settings, text='Ch 3', anchor='nw').place(x=394, y=-2, width=30, height=19)
        #--------------------
        # Create Entry for Ch0 to Ch3 Pulse Period Value
        #--------------------
        self.textbox_ch0_pulse_period_var = tk.StringVar(self.groupbox_pulse_period_settings)
        self.textbox_ch0_pulse_period_var.set('100')
        tk.Entry(self.groupbox_pulse_period_settings, textvariable=self.textbox_ch0_pulse_period_var, justify=tk.RIGHT).place(x=94, y=17, width=80, height=19)
        self.textbox_ch1_pulse_period_var = tk.StringVar(self.groupbox_pulse_period_settings)
        self.textbox_ch1_pulse_period_var.set('200')
        tk.Entry(self.groupbox_pulse_period_settings, textvariable=self.textbox_ch1_pulse_period_var, justify=tk.RIGHT).place(x=184, y=17, width=80, height=19)
        self.textbox_ch2_pulse_period_var = tk.StringVar(self.groupbox_pulse_period_settings)
        self.textbox_ch2_pulse_period_var.set('300')
        tk.Entry(self.groupbox_pulse_period_settings, textvariable=self.textbox_ch2_pulse_period_var, justify=tk.RIGHT).place(x=274, y=17, width=80, height=19)
        self.textbox_ch3_pulse_period_var = tk.StringVar(self.groupbox_pulse_period_settings)
        self.textbox_ch3_pulse_period_var.set('400')
        tk.Entry(self.groupbox_pulse_period_settings, textvariable=self.textbox_ch3_pulse_period_var, justify=tk.RIGHT).place(x=364, y=17, width=80, height=19)
        #--------------------
        # Create Combobox for Ch0 to Ch3 Pulse Period Unit
        #--------------------
        self.combobox_ch0_pulse_period_unit = ttk.Combobox(self.groupbox_pulse_period_settings)
        self.combobox_ch0_pulse_period_unit.place(x=94, y=42, width=80, height=19)
        self.combobox_ch0_pulse_period_unit['value'] = ('sec', 'msec', 'usec', 'nsec')
        self.combobox_ch0_pulse_period_unit.current(1)
        self.combobox_ch0_pulse_period_unit['state'] = 'readonly'
        self.combobox_ch1_pulse_period_unit = ttk.Combobox(self.groupbox_pulse_period_settings)
        self.combobox_ch1_pulse_period_unit.place(x=184, y=42, width=80, height=19)
        self.combobox_ch1_pulse_period_unit['value'] = ('sec', 'msec', 'usec', 'nsec')
        self.combobox_ch1_pulse_period_unit.current(1)
        self.combobox_ch1_pulse_period_unit['state'] = 'readonly'
        self.combobox_ch2_pulse_period_unit = ttk.Combobox(self.groupbox_pulse_period_settings)
        self.combobox_ch2_pulse_period_unit.place(x=274, y=42, width=80, height=19)
        self.combobox_ch2_pulse_period_unit['value'] = ('sec', 'msec', 'usec', 'nsec')
        self.combobox_ch2_pulse_period_unit.current(1)
        self.combobox_ch2_pulse_period_unit['state'] = 'readonly'
        self.combobox_ch3_pulse_period_unit = ttk.Combobox(self.groupbox_pulse_period_settings)
        self.combobox_ch3_pulse_period_unit.place(x=364, y=42, width=80, height=19)
        self.combobox_ch3_pulse_period_unit['value'] = ('sec', 'msec', 'usec', 'nsec')
        self.combobox_ch3_pulse_period_unit.current(1)
        self.combobox_ch3_pulse_period_unit['state'] = 'readonly'
        #--------------------
        # Create Button for DioSetPwmPulsePeriod
        #--------------------
        self.button_dio_set_pwm_pulse_period = tk.Button(self.groupbox_pulse_period_settings, text='DioSetPwmPulsePeriod')
        self.button_dio_set_pwm_pulse_period.place(x=501, y=22, width=130, height=24)
        #--------------------
        # Create LabelFrame for Phase Shift Settings
        #--------------------
        self.groupbox_phase_shift_settings = tk.LabelFrame(self.groupbox_pwm_operation_settings, text="<Phase Shift Settings>")
        self.groupbox_phase_shift_settings.place(x=7, y=90, width=643, height=62)
        #--------------------
        # Create Label for Phase Shift
        #--------------------
        tk.Label(self.groupbox_phase_shift_settings, text='Phase Shift:', anchor='nw').place(x=5, y=17, width=75, height=19)
        #--------------------
        # Create Label for Ch0 to Ch3
        #--------------------
        tk.Label(self.groupbox_phase_shift_settings, text='Ch 0', anchor='nw').place(x=124, y=-2, width=30, height=19)
        tk.Label(self.groupbox_phase_shift_settings, text='Ch 1', anchor='nw').place(x=214, y=-2, width=30, height=19)
        tk.Label(self.groupbox_phase_shift_settings, text='Ch 2', anchor='nw').place(x=304, y=-2, width=30, height=19)
        tk.Label(self.groupbox_phase_shift_settings, text='Ch 3', anchor='nw').place(x=394, y=-2, width=30, height=19)
        #--------------------
        # Create Combobox for Ch0 to Ch3 Phase Shift
        #--------------------
        self.combobox_ch0_phase_shift = ttk.Combobox(self.groupbox_phase_shift_settings)
        self.combobox_ch0_phase_shift.place(x=94, y=17, width=80, height=19)
        self.combobox_ch0_phase_shift['value'] = ('0', '90', '180')
        self.combobox_ch0_phase_shift.current(0)
        self.combobox_ch0_phase_shift['state'] = 'readonly'
        self.combobox_ch1_phase_shift = ttk.Combobox(self.groupbox_phase_shift_settings)
        self.combobox_ch1_phase_shift.place(x=184, y=17, width=80, height=19)
        self.combobox_ch1_phase_shift['value'] = ('0', '90', '180')
        self.combobox_ch1_phase_shift.current(0)
        self.combobox_ch1_phase_shift['state'] = 'readonly'
        self.combobox_ch2_phase_shift = ttk.Combobox(self.groupbox_phase_shift_settings)
        self.combobox_ch2_phase_shift.place(x=274, y=17, width=80, height=19)
        self.combobox_ch2_phase_shift['value'] = ('0', '90', '180')
        self.combobox_ch2_phase_shift.current(0)
        self.combobox_ch2_phase_shift['state'] = 'readonly'
        self.combobox_ch3_phase_shift = ttk.Combobox(self.groupbox_phase_shift_settings)
        self.combobox_ch3_phase_shift.place(x=364, y=17, width=80, height=19)
        self.combobox_ch3_phase_shift['value'] = ('0', '90', '180')
        self.combobox_ch3_phase_shift.current(0)
        self.combobox_ch3_phase_shift['state'] = 'readonly'
        #--------------------
        # Create Label for [Deg]
        #--------------------
        tk.Label(self.groupbox_phase_shift_settings, text='[Deg]', anchor='nw').place(x=450, y=17, width=30, height=19)
        #--------------------
        # Create Button for DioSetPwmPulsePhase
        #--------------------
        self.button_dio_set_pwm_pulse_phase = tk.Button(self.groupbox_phase_shift_settings, text='DioSetPwmPulsePhase')
        self.button_dio_set_pwm_pulse_phase.place(x=501, y=14, width=130, height=24)
        #--------------------
        # Create LabelFrame for Duty Ratio Settings
        #--------------------
        self.groupbox_duty_ratio_settings = tk.LabelFrame(self.groupbox_pwm_operation_settings, text="<Duty Ratio Settings>")
        self.groupbox_duty_ratio_settings.place(x=7, y=155, width=643, height=92)
        #--------------------
        # Create Label for Can be changed even during PWM operation
        #--------------------
        tk.Label(self.groupbox_duty_ratio_settings, text='Can be changed even during PWM operation.', anchor='nw').place(x=5, y=7, width=245, height=19)
        #--------------------
        # Create Label for Duty Ratio
        #--------------------
        tk.Label(self.groupbox_duty_ratio_settings, text='Duty Ratio:', anchor='nw').place(x=5, y=47, width=85, height=19)
        #--------------------
        # Create Label for Ch0 to Ch3
        #--------------------
        tk.Label(self.groupbox_duty_ratio_settings, text='Ch 0', anchor='nw').place(x=124, y=28, width=30, height=19)
        tk.Label(self.groupbox_duty_ratio_settings, text='Ch 1', anchor='nw').place(x=214, y=28, width=30, height=19)
        tk.Label(self.groupbox_duty_ratio_settings, text='Ch 2', anchor='nw').place(x=304, y=28, width=30, height=19)
        tk.Label(self.groupbox_duty_ratio_settings, text='Ch 3', anchor='nw').place(x=394, y=28, width=30, height=19)
        #--------------------
        # Create Entry for Ch0 to Ch3 Duty Ratio
        #--------------------
        self.textbox_ch0_duty_ratio_var = tk.StringVar(self.groupbox_duty_ratio_settings)
        self.textbox_ch0_duty_ratio_var.set('32767')
        tk.Entry(self.groupbox_duty_ratio_settings, textvariable=self.textbox_ch0_duty_ratio_var, justify=tk.RIGHT).place(x=94, y=47, width=80, height=19)
        self.textbox_ch1_duty_ratio_var = tk.StringVar(self.groupbox_duty_ratio_settings)
        self.textbox_ch1_duty_ratio_var.set('32767')
        tk.Entry(self.groupbox_duty_ratio_settings, textvariable=self.textbox_ch1_duty_ratio_var, justify=tk.RIGHT).place(x=184, y=47, width=80, height=19)
        self.textbox_ch2_duty_ratio_var = tk.StringVar(self.groupbox_duty_ratio_settings)
        self.textbox_ch2_duty_ratio_var.set('32767')
        tk.Entry(self.groupbox_duty_ratio_settings, textvariable=self.textbox_ch2_duty_ratio_var, justify=tk.RIGHT).place(x=274, y=47, width=80, height=19)
        self.textbox_ch3_duty_ratio_var = tk.StringVar(self.groupbox_duty_ratio_settings)
        self.textbox_ch3_duty_ratio_var.set('32767')
        tk.Entry(self.groupbox_duty_ratio_settings, textvariable=self.textbox_ch3_duty_ratio_var, justify=tk.RIGHT).place(x=364, y=47, width=80, height=19)
        #--------------------
        # Create Button for DioSetPwmPulseDuty
        #--------------------
        self.button_dio_set_pwm_pulse_duty = tk.Button(self.groupbox_duty_ratio_settings, text='DioSetPwmPulseDuty')
        self.button_dio_set_pwm_pulse_duty.place(x=501, y=44, width=130, height=24)
        #----------------------------------------
        # Create LabelFrame for PWM Operation
        #----------------------------------------
        self.groupbox_pwm_operation = tk.LabelFrame(self.root, text="<PWM Operation>")
        self.groupbox_pwm_operation.place(x=15, y=393, width=663, height=125)
        #--------------------
        # Create Label for Status
        #--------------------
        tk.Label(self.groupbox_pwm_operation, text='Status:', anchor='nw').place(x=14, y=27, width=75, height=19)
        #--------------------
        # Create Label for Error
        #--------------------
        tk.Label(self.groupbox_pwm_operation, text='Error:', anchor='nw').place(x=14, y=52, width=75, height=19)
        #--------------------
        # Create Label for Ch0 to Ch3
        #--------------------
        tk.Label(self.groupbox_pwm_operation, text='Ch 0', anchor='nw').place(x=133, y=8, width=30, height=19)
        tk.Label(self.groupbox_pwm_operation, text='Ch 1', anchor='nw').place(x=223, y=8, width=30, height=19)
        tk.Label(self.groupbox_pwm_operation, text='Ch 2', anchor='nw').place(x=313, y=8, width=30, height=19)
        tk.Label(self.groupbox_pwm_operation, text='Ch 3', anchor='nw').place(x=403, y=8, width=30, height=19)
        #--------------------
        # Create Entry for Ch0 to Ch3 Status
        #--------------------
        self.textbox_ch0_pwm_status_var = tk.StringVar(self.groupbox_pwm_operation)
        self.textbox_ch0_pwm_status_var.set('')
        tk.Entry(self.groupbox_pwm_operation, textvariable=self.textbox_ch0_pwm_status_var).place(x=103, y=27, width=80, height=19)
        self.textbox_ch1_pwm_status_var = tk.StringVar(self.groupbox_pwm_operation)
        self.textbox_ch1_pwm_status_var.set('')
        tk.Entry(self.groupbox_pwm_operation, textvariable=self.textbox_ch1_pwm_status_var).place(x=193, y=27, width=80, height=19)
        self.textbox_ch2_pwm_status_var = tk.StringVar(self.groupbox_pwm_operation)
        self.textbox_ch2_pwm_status_var.set('')
        tk.Entry(self.groupbox_pwm_operation, textvariable=self.textbox_ch2_pwm_status_var).place(x=283, y=27, width=80, height=19)
        self.textbox_ch3_pwm_status_var = tk.StringVar(self.groupbox_pwm_operation)
        self.textbox_ch3_pwm_status_var.set('')
        tk.Entry(self.groupbox_pwm_operation, textvariable=self.textbox_ch3_pwm_status_var).place(x=373, y=27, width=80, height=19)
        #--------------------
        # Create Entry for Ch0 to Ch3 Error
        #--------------------
        self.textbox_ch0_pwm_error_var = tk.StringVar(self.groupbox_pwm_operation)
        self.textbox_ch0_pwm_error_var.set('')
        tk.Entry(self.groupbox_pwm_operation, textvariable=self.textbox_ch0_pwm_error_var).place(x=103, y=52, width=80, height=19)
        self.textbox_ch1_pwm_error_var = tk.StringVar(self.groupbox_pwm_operation)
        self.textbox_ch1_pwm_error_var.set('')
        tk.Entry(self.groupbox_pwm_operation, textvariable=self.textbox_ch1_pwm_error_var).place(x=193, y=52, width=80, height=19)
        self.textbox_ch2_pwm_error_var = tk.StringVar(self.groupbox_pwm_operation)
        self.textbox_ch2_pwm_error_var.set('')
        tk.Entry(self.groupbox_pwm_operation, textvariable=self.textbox_ch2_pwm_error_var).place(x=283, y=52, width=80, height=19)
        self.textbox_ch3_pwm_error_var = tk.StringVar(self.groupbox_pwm_operation)
        self.textbox_ch3_pwm_error_var.set('')
        tk.Entry(self.groupbox_pwm_operation, textvariable=self.textbox_ch3_pwm_error_var).place(x=373, y=52, width=80, height=19)
        #--------------------
        # Create Label for [Hex]
        #--------------------
        tk.Label(self.groupbox_pwm_operation, text='[Hex]', anchor='nw').place(x=459, y=27, width=85, height=19)
        #--------------------
        # Create Label for [Hex]
        #--------------------
        tk.Label(self.groupbox_pwm_operation, text='[Hex]', anchor='nw').place(x=459, y=52, width=85, height=19)
        #--------------------
        # Create Button for DioStartPwm
        #--------------------
        self.button_dio_start_pwm = tk.Button(self.groupbox_pwm_operation, text='DioStartPwm')
        self.button_dio_start_pwm.place(x=510, y=5, width=130, height=24)
        #--------------------
        # Create Button for DioGetPwmStatus
        #--------------------
        self.button_dio_get_pwm_status = tk.Button(self.groupbox_pwm_operation, text='DioGetPwmStatus')
        self.button_dio_get_pwm_status.place(x=510, y=38, width=130, height=24)
        #--------------------
        # Create Button for DioStopPwm
        #--------------------
        self.button_dio_stop_pwm = tk.Button(self.groupbox_pwm_operation, text='DioStopPwm')
        self.button_dio_stop_pwm.place(x=510, y=71, width=130, height=24)
        #----------------------------------------
        # Create LabelFrame for Exit
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="<Exit>")
        self.groupbox_exit.place(x=15, y=521, width=663, 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=130, height=24)
        #----------------------------------------
        # Create Label for Return Code
        #----------------------------------------
        tk.Label(self.root, text='Return Code:', anchor='nw').place(x=22, y=586, 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=120, y=586, width=350, height=19)
        #----------------------------------------
        # Create Button for Close
        #----------------------------------------
        self.button_close = tk.Button(self.root, text='Close')
        self.button_close.place(x=527, y=596, width=130, height=24)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_dio_init,
                       command_dio_set_pwm_pulse_period,
                       command_dio_set_pwm_pulse_phase,
                       command_dio_set_pwm_pulse_duty,
                       command_dio_start_pwm,
                       command_dio_get_pwm_status,
                       command_dio_stop_pwm,
                       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 [DioSetPwmPulsePeriod] button
        #----------------------------------------
        self.button_dio_set_pwm_pulse_period.configure(command=command_dio_set_pwm_pulse_period)
        #----------------------------------------
        # Set the callback function for the [DioSetPwmPulsePhase] button
        #----------------------------------------
        self.button_dio_set_pwm_pulse_phase.configure(command=command_dio_set_pwm_pulse_phase)
        #----------------------------------------
        # Set the callback function for the [DioSetPwmPulseDuty] button
        #----------------------------------------
        self.button_dio_set_pwm_pulse_duty.configure(command=command_dio_set_pwm_pulse_duty)
        #----------------------------------------
        # Set the callback function for the [DioStartPwm] button
        #----------------------------------------
        self.button_dio_start_pwm.configure(command=command_dio_start_pwm)
        #----------------------------------------
        # Set the callback function for the [DioGetPwmStatus] button
        #----------------------------------------
        self.button_dio_get_pwm_status.configure(command=command_dio_get_pwm_status)
        #----------------------------------------
        # Set the callback function for the [DioStopPwm] button
        #----------------------------------------
        self.button_dio_stop_pwm.configure(command=command_dio_stop_pwm)
        #----------------------------------------
        # 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()