#================================================================
#================================================================
# API-DIO(WDM)
# PWM パルス出力サンプル
# main UIファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# PwmOutputUI クラス
#========================================
class PwmOutputUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title('PWM パルス出力')
        self.root.minsize(688,635)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        self.root.option_add("*Font", ("MS Gothic", 9))
        #----------------------------------------
        # UIの作成を開始
        #----------------------------------------
        self._create_ui()

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # 初期化処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_init = tk.LabelFrame(self.root, text="<初期化処理>")
        self.groupbox_init.place(x=15, y=10, width=663, height=52)
        #--------------------
        # デバイス名のLabelを作成
        #--------------------
        tk.Label(self.groupbox_init, text='デバイス名 :', anchor='nw').place(x=5, y=5, width=75, height=19)
        #--------------------
        # デバイス名のEntryを作成
        #--------------------
        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=85, y=5, width=90, height=19)
        #--------------------
        # DioInitのButtonを作成
        #--------------------
        self.button_dio_init = tk.Button(self.groupbox_init, text='DioInit')
        self.button_dio_init.place(x=510, y=5, width=130, height=24)
        #----------------------------------------
        # 共通設定値のLabelFrameを作成
        #----------------------------------------
        self.groupbox_common_settings = tk.LabelFrame(self.root, text="<共通設定値>")
        self.groupbox_common_settings.place(x=15, y=65, width=663, height=52)
        #--------------------
        # チャネル数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_common_settings, text='チャネル数 :', anchor='nw').place(x=5, y=5, width=75, height=19)
        #--------------------
        # チャネル数のComboboxを作成
        #--------------------
        self.combobox_channel_num = ttk.Combobox(self.groupbox_common_settings)
        self.combobox_channel_num.place(x=85, 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'
        #----------------------------------------
        # PWM 動作設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_pwm_operation_settings = tk.LabelFrame(self.root, text="<PWM 動作設定>")
        self.groupbox_pwm_operation_settings.place(x=15, y=120, width=663, height=270)
        #--------------------
        # パルス周期設定のLabelFrameを作成
        #--------------------
        self.groupbox_pulse_period_settings = tk.LabelFrame(self.groupbox_pwm_operation_settings, text="<パルス周期設定>")
        self.groupbox_pulse_period_settings.place(x=7, y=5, width=643, height=82)
        #--------------------
        # パルス周期のLabelを作成
        #--------------------
        tk.Label(self.groupbox_pulse_period_settings, text='パルス周期 :', anchor='nw').place(x=5, y=17, width=75, height=19)
        #--------------------
        # Ch0～3のLabelを作成
        #--------------------
        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)
        #--------------------
        # Ch0～3パルス周期値のEntryを作成
        #--------------------
        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)
        #--------------------
        # Ch0～3パルス周期単位のComboboxを作成
        #--------------------
        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'
        #--------------------
        # DioSetPwmPulsePeriodのButtonを作成
        #--------------------
        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)
        #--------------------
        # 位相シフト設定のLabelFrameを作成
        #--------------------
        self.groupbox_phase_shift_settings = tk.LabelFrame(self.groupbox_pwm_operation_settings, text="<位相シフト設定>")
        self.groupbox_phase_shift_settings.place(x=7, y=90, width=643, height=62)
        #--------------------
        # 位相シフトのLabelを作成
        #--------------------
        tk.Label(self.groupbox_phase_shift_settings, text='位相シフト :', anchor='nw').place(x=5, y=17, width=75, height=19)
        #--------------------
        # Ch0～3のLabelを作成
        #--------------------
        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)
        #--------------------
        # Ch0～3位相シフトのComboboxを作成
        #--------------------
        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'
        #--------------------
        # [Deg]のLabelを作成
        #--------------------
        tk.Label(self.groupbox_phase_shift_settings, text='[Deg]', anchor='nw').place(x=450, y=17, width=30, height=19)
        #--------------------
        # DioSetPwmPulsePhaseのButtonを作成
        #--------------------
        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)
        #--------------------
        # デューティ比設定のLabelFrameを作成
        #--------------------
        self.groupbox_duty_ratio_settings = tk.LabelFrame(self.groupbox_pwm_operation_settings, text="<デューティ比設定>")
        self.groupbox_duty_ratio_settings.place(x=7, y=155, width=643, height=92)
        #--------------------
        # PWM動作中も変更可能のLabelを作成
        #--------------------
        tk.Label(self.groupbox_duty_ratio_settings, text='PWM動作中も変更可能', anchor='nw').place(x=5, y=7, width=155, height=19)
        #--------------------
        # デューティ比のLabelを作成
        #--------------------
        tk.Label(self.groupbox_duty_ratio_settings, text='デューティ比 :', anchor='nw').place(x=5, y=47, width=85, height=19)
        #--------------------
        # Ch0～3のLabelを作成
        #--------------------
        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)
        #--------------------
        # Ch0～3デューティ比のEntryを作成
        #--------------------
        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)
        #--------------------
        # DioSetPwmPulseDutyのButtonを作成
        #--------------------
        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)
        #----------------------------------------
        # PWM 動作のLabelFrameを作成
        #----------------------------------------
        self.groupbox_pwm_operation = tk.LabelFrame(self.root, text="<PWM 動作>")
        self.groupbox_pwm_operation.place(x=15, y=393, width=663, height=125)
        #--------------------
        # ステータスのLabelを作成
        #--------------------
        tk.Label(self.groupbox_pwm_operation, text='ステータス :', anchor='nw').place(x=14, y=27, width=75, height=19)
        #--------------------
        # エラーのLabelを作成
        #--------------------
        tk.Label(self.groupbox_pwm_operation, text='エラー :', anchor='nw').place(x=14, y=52, width=75, height=19)
        #--------------------
        # Ch0～3のLabelを作成
        #--------------------
        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)
        #--------------------
        # Ch0～3ステータスのEntryを作成
        #--------------------
        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)
        #--------------------
        # Ch0～3エラーのEntryを作成
        #--------------------
        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)
        #--------------------
        # [Hex]のLabelを作成
        #--------------------
        tk.Label(self.groupbox_pwm_operation, text='[Hex]', anchor='nw').place(x=459, y=27, width=85, height=19)
        #--------------------
        # [Hex]のLabelを作成
        #--------------------
        tk.Label(self.groupbox_pwm_operation, text='[Hex]', anchor='nw').place(x=459, y=52, width=85, height=19)
        #--------------------
        # DioStartPwmのButtonを作成
        #--------------------
        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)
        #--------------------
        # DioGetPwmStatusのButtonを作成
        #--------------------
        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)
        #--------------------
        # DioStopPwmのButtonを作成
        #--------------------
        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)
        #----------------------------------------
        # 終了処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="<終了処理>")
        self.groupbox_exit.place(x=15, y=521, width=663, height=52)
        #--------------------
        # DioExitのButtonを作成
        #--------------------
        self.button_dio_exit = tk.Button(self.groupbox_exit, text='DioExit')
        self.button_dio_exit.place(x=510, y=5, width=130, height=24)
        #----------------------------------------
        # 戻り値のLabelを作成
        #----------------------------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=22, y=586, width=75, height=19)
        #----------------------------------------
        # 戻り値のEntryを作成
        #----------------------------------------
        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)
        #----------------------------------------
        # 閉じるのButtonを作成
        #----------------------------------------
        self.button_close = tk.Button(self.root, text='閉じる')
        self.button_close.place(x=527, y=596, width=130, height=24)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    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):
        #----------------------------------------
        # 「DioInit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_init.configure(command=command_dio_init)
        #----------------------------------------
        # 「DioSetPwmPulsePeriod」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_set_pwm_pulse_period.configure(command=command_dio_set_pwm_pulse_period)
        #----------------------------------------
        # 「DioSetPwmPulsePhase」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_set_pwm_pulse_phase.configure(command=command_dio_set_pwm_pulse_phase)
        #----------------------------------------
        # 「DioSetPwmPulseDuty」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_set_pwm_pulse_duty.configure(command=command_dio_set_pwm_pulse_duty)
        #----------------------------------------
        # 「DioStartPwm」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_start_pwm.configure(command=command_dio_start_pwm)
        #----------------------------------------
        # 「DioGetPwmStatus」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_get_pwm_status.configure(command=command_dio_get_pwm_status)
        #----------------------------------------
        # 「DioStopPwm」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_stop_pwm.configure(command=command_dio_stop_pwm)
        #----------------------------------------
        # 「DioExit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_exit.configure(command=command_dio_exit)
        #----------------------------------------
        # 「閉じる」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_close.configure(command=command_close)

    #========================================
    # メッセージボックスにエラーを表示
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.root)

    #========================================
    # メインイベントループを実行
    #========================================
    def run(self):
        self.root.mainloop()