#================================================================
#================================================================
# API-DIO(WDM)
# 割り込みサンプル
# main UIファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk


#========================================
# InterruptUI クラス
#========================================
class InterruptUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title("割り込み")
        self.root.minsize(510,300)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        self.root.option_add("*Font", ("MS Gothic", 9))
        #----------------------------------------
        # UIの作成を開始
        #----------------------------------------
        self._create_ui()

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # 初期化処理
        #----------------------------------------
        #--------------------
        # デバイス名のLabelを作成
        #--------------------
        tk.Label(self.root, text='デバイス名 : ', anchor='nw').place(x=20, y=20, width=90, height=19)
        #--------------------
        # デバイス名のEntryを作成
        #--------------------
        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)
        #--------------------
        # DioInitのButtonを作成
        #--------------------
        self.button_dio_init = tk.Button(self.root, text='DioInit')
        self.button_dio_init.place(x=365, y=20, width=110, height=19)
        #----------------------------------------
        # 割り込みモニターのLabelFrameを作成
        #----------------------------------------
        self.groupbox_interrupt = tk.LabelFrame(self.root, text="割り込みモニター")
        self.groupbox_interrupt.place(x=15, y=60, width=475, height=155)
        #--------------------
        # ビット0～3のLabelを作成
        #--------------------
        tk.Label(self.groupbox_interrupt, text='0', anchor='nw').place(x=122, y=1, width=10, height=19)
        tk.Label(self.groupbox_interrupt, text='1', anchor='nw').place(x=165, y=1, width=10, height=19)
        tk.Label(self.groupbox_interrupt, text='2', anchor='nw').place(x=208, y=1, width=10, height=19)
        tk.Label(self.groupbox_interrupt, text='3', anchor='nw').place(x=251, y=1, width=10, height=19)
        #--------------------
        # アップカウントのLabelを作成
        #--------------------
        tk.Label(self.groupbox_interrupt, text='アップカウント : ', anchor='nw').place(x=5, y=25, width=100, height=19)
        #--------------------
        # ビット0～3のアップカウントのEntryを作成
        #--------------------
        self.textbox_up0_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_up0_var, justify=tk.RIGHT).place(x=110, y=25, width=36, height=19)
        self.textbox_up1_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_up1_var, justify=tk.RIGHT).place(x=153, y=25, width=36, height=19)
        self.textbox_up2_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_up2_var, justify=tk.RIGHT).place(x=196, y=25, width=36, height=19)
        self.textbox_up3_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_up3_var, justify=tk.RIGHT).place(x=239, y=25, width=36, height=19)
        #--------------------
        # ダウンカウントのLabelを作成
        #--------------------
        tk.Label(self.groupbox_interrupt, text='ダウンカウント : ', anchor='nw').place(x=5, y=53, width=100, height=19)
        #--------------------
        # ビット0～3のダウンカウントのEntryを作成
        #--------------------
        self.textbox_down0_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_down0_var, justify=tk.RIGHT).place(x=110, y=53, width=36, height=19)
        self.textbox_down1_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_down1_var, justify=tk.RIGHT).place(x=153, y=53, width=36, height=19)
        self.textbox_down2_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_down2_var, justify=tk.RIGHT).place(x=196, y=53, width=36, height=19)
        self.textbox_down3_var = tk.StringVar(self.groupbox_interrupt)
        tk.Entry(self.groupbox_interrupt, textvariable=self.textbox_down3_var, justify=tk.RIGHT).place(x=239, y=53, width=36, height=19)
        #--------------------
        # 割り込み論理のLabelFrameを作成
        #--------------------
        self.groupbox_logic = tk.LabelFrame(self.groupbox_interrupt, text="割り込み論理")
        self.groupbox_logic.place(x=280, y=5, width=180, height=70)
        #--------------------
        # 割り込み論理のRadiobuttonを作成
        #--------------------
        self.radio_logic_var = tk.IntVar()
        self.radio_logic_var.set(0)
        tk.Radiobutton(self.groupbox_logic, text='0 -> 1 (立ち上がりエッジ)', variable=self.radio_logic_var, value=0).place(x=1, y=5, width=170, height=19)
        tk.Radiobutton(self.groupbox_logic, text='1 -> 0 (立ち下がりエッジ)', variable=self.radio_logic_var, value=1).place(x=1, y=25, width=170, height=19)
        #--------------------
        # 割り込み開始のButtonを作成
        #--------------------
        self.button_int_start = tk.Button(self.groupbox_interrupt, text='割り込み開始')
        self.button_int_start.place(x=348, y=80, width=110, height=19)
        #--------------------
        # 割り込み停止のButtonを作成
        #--------------------
        self.button_int_stop = tk.Button(self.groupbox_interrupt, text='割り込み停止')
        self.button_int_stop.place(x=348, y=110, width=110, height=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=20, y=230, width=90, height=19)
        #--------------------
        # 戻り値のEntryを作成
        #--------------------
        self.textbox_return_string_var= tk.StringVar(self.root)
        tk.Entry(self.root, textvariable=self.textbox_return_string_var).place(x=75, y=230, width=250, height=19)
        #--------------------
        # DioExitのButtonを作成
        #--------------------
        self.button_dio_exit = tk.Button(self.root, text='DioExit')
        self.button_dio_exit.place(x=365, y=230, width=110, height=19)
        #--------------------
        # ExitのButtonを作成
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=365, y=260, width=110, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_dio_init,
                       command_int_start,
                       command_int_stop,
                       command_dio_exit,
                       command_exit):
        #----------------------------------------
        # 「DioInit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_init.configure(command=command_dio_init)
        #----------------------------------------
        # 「割り込み開始」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_int_start.configure(command=command_int_start)
        #----------------------------------------
        # 「割り込み停止」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_int_stop.configure(command=command_int_stop)
        #----------------------------------------
        # 「DioExit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_exit.configure(command=command_dio_exit)
        #----------------------------------------
        # 「Exit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_exit.configure(command=command_exit)

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