#================================================================
#================================================================
# API-AIO(WDM)
# ユーザーバッファを使用した有限サンプリングサンプル
# main UIファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# AiSamplingOnceUserUI クラス
#========================================
class AiSamplingOnceUserUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title('ユーザーバッファを使用した有限サンプリング')
        self.root.minsize(740,790)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        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=710, height=75)
        #--------------------
        # デバイス名のLabelを作成
        #--------------------
        tk.Label(self.groupbox_init, text='デバイス名 : ', anchor='nw').place(x=15, y=5, width=75, 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=180, y=5, width=140, height=19)
        #--------------------
        # AioInitのButtonを作成
        #--------------------
        self.button_aio_init = tk.Button(self.groupbox_init, text='AioInit')
        self.button_aio_init.place(x=530, y=5, width=170, height=19)
        #--------------------
        # AioResetDeviceのButtonを作成
        #--------------------
        self.button_aio_reset_device = tk.Button(self.groupbox_init, text='AioResetDevice')
        self.button_aio_reset_device.place(x=530, y=30, width=170, height=19)
        #----------------------------------------
        # アナログ入力設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_input_settings = tk.LabelFrame(self.root, text="アナログ入力設定")
        self.groupbox_input_settings.place(x=15, y=90, width=710, height=75)
        #--------------------
        # レンジのLabelを作成
        #--------------------
        tk.Label(self.groupbox_input_settings, text='レンジ : ', anchor='nw').place(x=15, y=5, width=75, height=19)
        #--------------------
        # レンジのComboboxを作成
        #--------------------
        self.combobox_range = ttk.Combobox(self.groupbox_input_settings)
        self.combobox_range.place(x=180, y=5, width=140, height=19)
        self.combobox_range['value'] = ('-10 - +10V', '-5 - +5V', '-2.5 - +2.5V', '-1.25 - +1.25V', '0 - +10V', '0 - +5V', '0 - +2.5V', '0 - +1.25V', '0 - 20mA', '4 - 20mA', '-20 - +20mA', '1 - +5V')
        self.combobox_range.current(0)
        self.combobox_range['state'] = 'readonly'
        #--------------------
        # AioSetAiRangeAllのButtonを作成
        #--------------------
        self.button_aio_set_ai_range_all = tk.Button(self.groupbox_input_settings, text='AioSetAiRangeAll')
        self.button_aio_set_ai_range_all.place(x=530, y=5, width=170, height=19)
        #--------------------
        # 入力方式のLabelを作成
        #--------------------
        tk.Label(self.groupbox_input_settings, text='入力方式 : ', anchor='nw').place(x=15, y=30, width=75, height=19)
        #--------------------
        # 入力方式のComboboxを作成
        #--------------------
        self.combobox_input_method = ttk.Combobox(self.groupbox_input_settings)
        self.combobox_input_method.place(x=180, y=30, width=140, height=19)
        self.combobox_input_method['value'] = ('シングルエンド入力', '差動入力')
        self.combobox_input_method.current(0)
        self.combobox_input_method['state'] = 'readonly'
        #--------------------
        # AioSetAiInputMethodのButtonを作成
        #--------------------
        self.button_aio_set_ai_input_method = tk.Button(self.groupbox_input_settings, text='AioSetAiInputMethod')
        self.button_aio_set_ai_input_method.place(x=530, y=30, width=170, height=19)
        #----------------------------------------
        # サンプリング設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_sampling_settings = tk.LabelFrame(self.root, text="サンプリング設定")
        self.groupbox_sampling_settings.place(x=15, y=170, width=710, height=150)
        #--------------------
        # チャネル数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_settings, text='チャネル数 : ', anchor='nw').place(x=15, y=5, width=75, height=19)
        #--------------------
        # チャネル数のEntryを作成
        #--------------------
        self.textbox_channels_var = tk.StringVar(self.groupbox_sampling_settings)
        self.textbox_channels_var.set('1')
        tk.Entry(self.groupbox_sampling_settings, justify=tk.RIGHT, textvariable=self.textbox_channels_var).place(x=180, y=5, width=140, height=19)
        #--------------------
        # AioSetAiChannelsのButtonを作成
        #--------------------
        self.button_aio_set_ai_channels = tk.Button(self.groupbox_sampling_settings, text='AioSetAiChannels')
        self.button_aio_set_ai_channels.place(x=530, y=5, width=170, height=19)
        #--------------------
        # サンプリングクロック周期のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_settings, text='サンプリングクロック周期 : ', anchor='nw').place(x=15, y=30, width=160, height=19)
        #--------------------
        # サンプリングクロック周期単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_settings, text='[μsec]', anchor='nw').place(x=325, y=30, width=50, height=19)
        #--------------------
        # サンプリングクロック周期のEntryを作成
        #--------------------
        self.textbox_sampling_clock_var = tk.StringVar(self.groupbox_sampling_settings)
        self.textbox_sampling_clock_var.set('1000')
        tk.Entry(self.groupbox_sampling_settings, justify=tk.RIGHT, textvariable=self.textbox_sampling_clock_var).place(x=180, y=30, width=140, height=19)
        #--------------------
        # AioSetAiSamplingClockのButtonを作成
        #--------------------
        self.button_aio_set_ai_sampling_clock = tk.Button(self.groupbox_sampling_settings, text='AioSetAiSamplingClock')
        self.button_aio_set_ai_sampling_clock.place(x=530, y=30, width=170, height=19)
        #--------------------
        # 変換速度のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_settings, text='変換速度 : ', anchor='nw').place(x=15, y=55, width=160, height=19)
        #--------------------
        # 変換速度単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_settings, text='[μsec]', anchor='nw').place(x=325, y=55, width=50, height=19)
        #--------------------
        # 変換速度のEntryを作成
        #--------------------
        self.textbox_scan_clock_var = tk.StringVar(self.groupbox_sampling_settings)
        self.textbox_scan_clock_var.set('10')
        tk.Entry(self.groupbox_sampling_settings, justify=tk.RIGHT, textvariable=self.textbox_scan_clock_var).place(x=180, y=55, width=140, height=19)
        #--------------------
        # AioSetAiScanClockのButtonを作成
        #--------------------
        self.button_aio_set_ai_scan_clock = tk.Button(self.groupbox_sampling_settings, text='AioSetAiScanClock')
        self.button_aio_set_ai_scan_clock.place(x=530, y=55, width=170, height=19)
        #--------------------
        # 停止回数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_settings, text='停止回数 : ', anchor='nw').place(x=15, y=80, width=160, height=19)
        #--------------------
        # 停止回数のEntryを作成
        #--------------------
        self.textbox_stop_times_var = tk.StringVar(self.groupbox_sampling_settings)
        self.textbox_stop_times_var.set('1000')
        tk.Entry(self.groupbox_sampling_settings, justify=tk.RIGHT, textvariable=self.textbox_stop_times_var).place(x=180, y=80, width=140, height=19)
        #--------------------
        # AioSetAiStopTimesのButtonを作成
        #--------------------
        self.button_aio_set_ai_stop_times = tk.Button(self.groupbox_sampling_settings, text='AioSetAiStopTimes')
        self.button_aio_set_ai_stop_times.place(x=530, y=80, width=170, height=19)
        #--------------------
        # 基本設定のButtonを作成
        #--------------------
        self.button_basic_settings = tk.Button(self.groupbox_sampling_settings, text='基本設定')
        self.button_basic_settings.place(x=530, y=105, width=170, height=19)
        #----------------------------------------
        # ユーザーバッファ設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_user_buffer_settings = tk.LabelFrame(self.root, text="ユーザーバッファ設定")
        self.groupbox_user_buffer_settings.place(x=15, y=325, width=710, height=50)
        #--------------------
        # サンプリング回数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_user_buffer_settings, text='サンプリング回数 : ', anchor='nw').place(x=15, y=5, width=160, height=19)
        #--------------------
        # サンプリング回数のEntryを作成
        #--------------------
        self.textbox_sampling_times_var = tk.StringVar(self.groupbox_user_buffer_settings)
        self.textbox_sampling_times_var.set('2000')
        tk.Entry(self.groupbox_user_buffer_settings, justify=tk.RIGHT, textvariable=self.textbox_sampling_times_var).place(x=180, y=5, width=140, height=19)
        #--------------------
        # ユーザーバッファサイズ設定のButtonを作成
        #--------------------
        self.button_user_buffer_size_settings = tk.Button(self.groupbox_user_buffer_settings, text='ユーザーバッファサイズ設定')
        self.button_user_buffer_size_settings.place(x=530, y=5, width=170, height=19)
        #----------------------------------------
        # サンプリング動作のLabelFrameを作成
        #----------------------------------------
        self.groupbox_sampling_action = tk.LabelFrame(self.root, text="サンプリング動作")
        self.groupbox_sampling_action.place(x=15, y=380, width=710, height=280)
        #--------------------
        # サンプリングデータのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='サンプリングデータ : ', anchor='nw').place(x=15, y=45, width=160, height=19)
        #--------------------
        # カウントのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='カウント', anchor='nw').place(x=180, y=5, width=160, height=19)
        #--------------------
        # パケットデータ(バイナリ)のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='パケットデータ(バイナリ)', anchor='nw').place(x=350, y=5, width=160, height=19)
        #--------------------
        # (パケット番号)のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='(パケット番号)', anchor='nw').place(x=180, y=25, width=160, height=19)
        #--------------------
        # 下位データ/上位データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='下位データ    上位データ', anchor='nw').place(x=350, y=25, width=160, height=19)
        #--------------------
        # サンプリングデータのListboxを作成
        #--------------------
        self.listbox_sampling_data_var = tk.StringVar(self.groupbox_sampling_action)
        self.listbox_sampling_data = tk.Listbox(self.groupbox_sampling_action, listvariable=self.listbox_sampling_data_var)
        self.listbox_y_scrollbar = tk.Scrollbar(self.groupbox_sampling_action, orient="vertical", command=self.listbox_sampling_data.yview)
        self.listbox_x_scrollbar = tk.Scrollbar(self.groupbox_sampling_action, orient="horizontal", command=self.listbox_sampling_data.xview)
        self.listbox_sampling_data.config(yscrollcommand=self.listbox_y_scrollbar.set)
        self.listbox_sampling_data.config(xscrollcommand=self.listbox_x_scrollbar.set)
        self.listbox_sampling_data.place(x=180, y=45, width=330, height=150)
        self.listbox_y_scrollbar.place(x=510, y=45, width=10, height=150)
        self.listbox_x_scrollbar.place(x=180, y=195, width=330, height=10)
        #--------------------
        # 転送位置のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='転送位置 : ', anchor='nw').place(x=15, y=210, width=160, height=19)
        #--------------------
        # 転送位置のEntryを作成
        #--------------------
        self.textbox_transfer_count_var = tk.StringVar(self.groupbox_sampling_action)
        self.textbox_transfer_count_var.set('')
        tk.Entry(self.groupbox_sampling_action, justify=tk.RIGHT, textvariable=self.textbox_transfer_count_var).place(x=180, y=210, width=330, height=19)
        #--------------------
        # ステータスのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='ステータス : ', anchor='nw').place(x=15, y=235, width=160, height=19)
        #--------------------
        # ステータスのEntryを作成
        #--------------------
        self.textbox_status_var = tk.StringVar(self.groupbox_sampling_action)
        self.textbox_status_var.set('')
        tk.Entry(self.groupbox_sampling_action, textvariable=self.textbox_status_var).place(x=180, y=235, width=330, height=19)
        #--------------------
        # サンプリング開始のButtonを作成
        #--------------------
        self.button_sampling_start = tk.Button(self.groupbox_sampling_action, text='サンプリング開始')
        self.button_sampling_start.place(x=530, y=45, width=170, height=19)
        #--------------------
        # AioGetAiTransferCountのButtonを作成
        #--------------------
        self.button_aio_get_ai_transfer_count = tk.Button(self.groupbox_sampling_action, text='AioGetAiTransferCount')
        self.button_aio_get_ai_transfer_count.place(x=530, y=85, width=170, height=19)
        #--------------------
        # AioGetAiStatusのButtonを作成
        #--------------------
        self.button_aio_get_ai_status = tk.Button(self.groupbox_sampling_action, text='AioGetAiStatus')
        self.button_aio_get_ai_status.place(x=530, y=125, width=170, height=19)
        #--------------------
        # サンプリング停止のButtonを作成
        #--------------------
        self.button_sampling_stop = tk.Button(self.groupbox_sampling_action, text='サンプリング停止')
        self.button_sampling_stop.place(x=530, y=165, width=170, height=19)
        #--------------------
        # 取得データファイル出力のButtonを作成
        #--------------------
        self.button_output_sampling_data = tk.Button(self.groupbox_sampling_action, text='取得データファイル出力...')
        self.button_output_sampling_data.place(x=530, y=205, width=170, height=19)
        #----------------------------------------
        # 終了処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="終了処理")
        self.groupbox_exit.place(x=15, y=665, width=710, height=50)
        #--------------------
        # AioExitのButtonを作成
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=530, y=5, width=170, height=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=33, y=730, width=160, 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=198, y=730, width=330, height=19)
        #--------------------
        # 閉じるのButtonを作成
        #--------------------
        self.button_close = tk.Button(self.root, text='閉じる')
        self.button_close.place(x=547, y=755, width=170, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_aio_init,
                       command_aio_reset_device,
                       command_aio_set_ai_range_all,
                       command_aio_set_ai_input_method,
                       command_aio_set_ai_channels,
                       command_aio_set_ai_sampling_clock,
                       command_aio_set_ai_scan_clock,
                       command_aio_set_ai_stop_times,
                       command_basic_settings,
                       command_user_buffer_size_settings,
                       command_sampling_start,
                       command_aio_get_ai_transfer_count,
                       command_aio_get_ai_status,
                       command_sampling_stop,
                       command_output_sampling_data,
                       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)
        #----------------------------------------
        # 「AioSetAiRangeAll」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_ai_range_all.configure(command=command_aio_set_ai_range_all)
        #----------------------------------------
        # 「AioSetAiInputMethod」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_ai_input_method.configure(command=command_aio_set_ai_input_method)
        #----------------------------------------
        # 「AioSetAiChannels」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_ai_channels.configure(command=command_aio_set_ai_channels)
        #----------------------------------------
        # 「AioSetAiSamplingClock」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_ai_sampling_clock.configure(command=command_aio_set_ai_sampling_clock)
        #----------------------------------------
        # 「AioSetAiScanClock」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_ai_scan_clock.configure(command=command_aio_set_ai_scan_clock)
        #----------------------------------------
        # 「AioSetAiStopTimes」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_ai_stop_times.configure(command=command_aio_set_ai_stop_times)
        #----------------------------------------
        # 「基本設定」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_basic_settings.configure(command=command_basic_settings)
        #----------------------------------------
        # 「ユーザーバッファサイズ設定」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_user_buffer_size_settings.configure(command=command_user_buffer_size_settings)
        #----------------------------------------
        # 「サンプリング開始」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_sampling_start.configure(command=command_sampling_start)
        #----------------------------------------
        # 「AioGetAiTransferCount」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_get_ai_transfer_count.configure(command=command_aio_get_ai_transfer_count)
        #----------------------------------------
        # 「AioGetAiStatus」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_get_ai_status.configure(command=command_aio_get_ai_status)
        #----------------------------------------
        # 「サンプリング停止」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_sampling_stop.configure(command=command_sampling_stop)
        #----------------------------------------
        # 「サンプリングデータ出力」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_output_sampling_data.configure(command=command_output_sampling_data)
        #----------------------------------------
        # 「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()