#================================================================
#================================================================
# API-AIO(WDM)
# デジタル汎用入出力サンプル
# main UIファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import messagebox


#========================================
# DigitalIOUI クラス
#========================================
class DigitalIOUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title('デジタル汎用入出力')
        self.root.minsize(670,490)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        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=640, height=75)
        #--------------------
        # デバイス名のLabelを作成
        #--------------------
        tk.Label(self.groupbox_init, text='デバイス名 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # デバイス名のEntryを作成
        #--------------------
        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=95, y=5, width=100, height=19)
        #--------------------
        # AioInitのButtonを作成
        #--------------------
        self.button_aio_init = tk.Button(self.groupbox_init, text='AioInit')
        self.button_aio_init.place(x=510, y=5, width=120, height=19)
        #--------------------
        # AioResetDeviceのButtonを作成
        #--------------------
        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)
        #----------------------------------------
        # 入出力方向設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_dir_settings = tk.LabelFrame(self.root, text="入出力方向設定")
        self.groupbox_dir_settings.place(x=15, y=90, width=640, height=50)
        #--------------------
        # 入出力方向のLabelを作成
        #--------------------
        tk.Label(self.groupbox_dir_settings, text='入出力方向 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # 入出力方向のEntryを作成
        #--------------------
        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=95, y=5, width=100, height=19)
        #--------------------
        # 入出力方向進数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_dir_settings, text='[Dec]', anchor='nw').place(x=200, y=5, width=90, height=19)
        #--------------------
        # AioSetDioDirectionのButtonを作成
        #--------------------
        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)
        #----------------------------------------
        # 1ポート入力のLabelFrameを作成
        #----------------------------------------
        self.groupbox_inp_port = tk.LabelFrame(self.root, text="1ポート入力")
        self.groupbox_inp_port.place(x=15, y=145, width=640, height=50)
        #--------------------
        # ポート番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_inp_port, text='ポート番号 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # ポート番号のEntryを作成
        #--------------------
        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=95, y=5, width=100, height=19)
        #--------------------
        # ポート番号進数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_inp_port, text='[Dec]', anchor='nw').place(x=200, y=5, width=90, height=19)
        #--------------------
        # 入力データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_inp_port, text='入力データ : ', anchor='nw').place(x=245, y=5, width=90, height=19)
        #--------------------
        # 入力データのEntryを作成
        #--------------------
        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=325, y=5, width=100, height=19)
        #--------------------
        # 入力データ進数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_inp_port, text='[Hex]', anchor='nw').place(x=430, y=5, width=80, height=19)
        #--------------------
        # AioInputDiByteのButtonを作成
        #--------------------
        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)
        #----------------------------------------
        # 1ビット入力のLabelFrameを作成
        #----------------------------------------
        self.groupbox_inp_bit = tk.LabelFrame(self.root, text="1ビット入力")
        self.groupbox_inp_bit.place(x=15, y=200, width=640, height=50)
        #--------------------
        # ビット番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_inp_bit, text='ビット番号 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # ビット番号のEntryを作成
        #--------------------
        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=95, y=5, width=100, height=19)
        #--------------------
        # ビット番号進数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_inp_bit, text='[Dec]', anchor='nw').place(x=200, y=5, width=90, height=19)
        #--------------------
        # 入力データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_inp_bit, text='入力データ : ', anchor='nw').place(x=245, y=5, width=90, height=19)
        #--------------------
        # 入力データのEntryを作成
        #--------------------
        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=325, y=5, width=100, height=19)
        #--------------------
        # 入力データ進数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_inp_bit, text='[Dec]', anchor='nw').place(x=430, y=5, width=80, height=19)
        #--------------------
        # AioInputDiBitのButtonを作成
        #--------------------
        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)
        #----------------------------------------
        # 1ポート出力のLabelFrameを作成
        #----------------------------------------
        self.groupbox_out_port = tk.LabelFrame(self.root, text="1ポート出力")
        self.groupbox_out_port.place(x=15, y=255, width=640, height=50)
        #--------------------
        # ポート番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_out_port, text='ポート番号 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # ポート番号のEntryを作成
        #--------------------
        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=95, y=5, width=100, height=19)
        #--------------------
        # ポート番号進数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_out_port, text='[Dec]', anchor='nw').place(x=200, y=5, width=90, height=19)
        #--------------------
        # 出力データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_out_port, text='出力データ : ', anchor='nw').place(x=245, y=5, width=90, height=19)
        #--------------------
        # 出力データのEntryを作成
        #--------------------
        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=325, y=5, width=100, height=19)
        #--------------------
        # 出力データ進数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_out_port, text='[Hex]', anchor='nw').place(x=430, y=5, width=80, height=19)
        #--------------------
        # AioOutputDoByteのButtonを作成
        #--------------------
        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)
        #----------------------------------------
        # 1ビット出力のLabelFrameを作成
        #----------------------------------------
        self.groupbox_out_bit = tk.LabelFrame(self.root, text="1ビット出力")
        self.groupbox_out_bit.place(x=15, y=310, width=640, height=50)
        #--------------------
        # ビット番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_out_bit, text='ビット番号 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # ビット番号のEntryを作成
        #--------------------
        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=95, y=5, width=100, height=19)
        #--------------------
        # ビット番号進数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_out_bit, text='[Dec]', anchor='nw').place(x=200, y=5, width=90, height=19)
        #--------------------
        # 出力データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_out_bit, text='出力データ : ', anchor='nw').place(x=245, y=5, width=90, height=19)
        #--------------------
        # 出力データのEntryを作成
        #--------------------
        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=325, y=5, width=100, height=19)
        #--------------------
        # 出力データ進数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_out_bit, text='[Dec]', anchor='nw').place(x=430, y=5, width=80, height=19)
        #--------------------
        # AioOutputDoBitのButtonを作成
        #--------------------
        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)
        #----------------------------------------
        # 終了処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="終了処理")
        self.groupbox_exit.place(x=15, y=365, width=640, height=50)
        #--------------------
        # AioExitのButtonを作成
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=510, y=5, width=120, height=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=33, y=430, 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=112, y=430, width=380, height=19)
        #--------------------
        # 閉じるのButtonを作成
        #--------------------
        self.button_close = tk.Button(self.root, text='閉じる')
        self.button_close.place(x=527, y=455, width=120, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    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):
        #----------------------------------------
        # 「AioInit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_init.configure(command=command_aio_init)
        #----------------------------------------
        # 「AioResetDevice」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_reset_device.configure(command=command_aio_reset_device)
        #----------------------------------------
        # 「AioSetDioDirection」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_dio_direction.configure(command=command_aio_set_dio_direction)
        #----------------------------------------
        # 「AioInputDiByte」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_input_di_byte.configure(command=command_aio_input_di_byte)
        #----------------------------------------
        # 「AioInputDiBit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_input_di_bit.configure(command=command_aio_input_di_bit)
        #----------------------------------------
        # 「AioOutputDoByte」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_output_do_byte.configure(command=command_aio_output_do_byte)
        #----------------------------------------
        # 「AioOutputDoBit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_output_do_bit.configure(command=command_aio_output_do_bit)
        #----------------------------------------
        # 「AioExit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_exit.configure(command=command_aio_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()