#================================================================
#================================================================
# API-AIO(WDM)
# RINGメモリを使用した無限ジェネレーティング
# main UIファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# AoGeneratingRingMemoryUI クラス
#========================================
class AoGeneratingRingMemoryUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title("RINGメモリを使用した無限ジェネレーティング")
        self.root.minsize(790,640)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        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=760, height=75)
        #--------------------
        # デバイス名のLabelを作成
        #--------------------
        tk.Label(self.groupbox_init, text='デバイス名 : ', anchor='nw').place(x=15, y=5, width=200, 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=230, y=5, width=150, height=19)
        #--------------------
        # AioInitのButtonを作成
        #--------------------
        self.button_aio_init = tk.Button(self.groupbox_init, text='AioInit')
        self.button_aio_init.place(x=580, 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=580, y=30, width=170, height=19)
        #----------------------------------------
        # アナログ出力設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_output_settings = tk.LabelFrame(self.root, text="アナログ出力設定")
        self.groupbox_output_settings.place(x=15, y=90, width=760, height=50)
        #--------------------
        # レンジのLabelを作成
        #--------------------
        tk.Label(self.groupbox_output_settings, text='レンジ : ', anchor='nw').place(x=15, y=5, width=200, height=19)
        #--------------------
        # レンジのComboboxを作成
        #--------------------
        self.combobox_range = ttk.Combobox(self.groupbox_output_settings)
        self.combobox_range.place(x=230, y=5, width=150, 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', '1 - +5V')
        self.combobox_range.current(0)
        self.combobox_range['state'] = 'readonly'
        #--------------------
        # AioSetAoRangeAllのButtonを作成
        #--------------------
        self.button_aio_set_ao_range_all = tk.Button(self.groupbox_output_settings, text='AioSetAoRangeAll')
        self.button_aio_set_ao_range_all.place(x=580, y=5, width=170, height=19)
        #----------------------------------------
        # ジェネレーティング設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_generating_settings = tk.LabelFrame(self.root, text="ジェネレーティング設定")
        self.groupbox_generating_settings.place(x=15, y=145, width=760, height=125)
        #--------------------
        # チャネル数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_generating_settings, text='チャネル数 : ', anchor='nw').place(x=15, y=5, width=200, height=19)
        #--------------------
        # チャネル数のEntryを作成
        #--------------------
        self.textbox_channels_num_var = tk.StringVar(self.groupbox_generating_settings)
        self.textbox_channels_num_var.set('1')
        tk.Entry(self.groupbox_generating_settings, justify=tk.RIGHT, textvariable=self.textbox_channels_num_var).place(x=230, y=5, width=150, height=19)
        #--------------------
        # AioSetAoChannelsのButtonを作成
        #--------------------
        self.button_aio_set_ao_channels = tk.Button(self.groupbox_generating_settings, text='AioSetAoChannels')
        self.button_aio_set_ao_channels.place(x=580, y=5, width=170, height=19)
        #--------------------
        # ジェネレーティングクロック周期のLabelを作成
        #--------------------
        tk.Label(self.groupbox_generating_settings, text='ジェネレーティングクロック周期 : ', anchor='nw').place(x=15, y=30, width=200, height=19)
        #--------------------
        # ジェネレーティングクロック周期単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_generating_settings, text='[μsec]', anchor='nw').place(x=385, y=30, width=200, height=19)
        #--------------------
        # ジェネレーティングクロック周期のEntryを作成
        #--------------------
        self.textbox_generating_clock_var = tk.StringVar(self.groupbox_generating_settings)
        self.textbox_generating_clock_var.set('1000')
        tk.Entry(self.groupbox_generating_settings, justify=tk.RIGHT, textvariable=self.textbox_generating_clock_var).place(x=230, y=30, width=150, height=19)
        #--------------------
        # リピート回数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_generating_settings, text='リピート回数 : ', anchor='nw').place(x=15, y=80, width=200, height=19)
        #--------------------
        # リピート回数のEntryを作成
        #--------------------
        self.textbox_repeat_times_var = tk.StringVar(self.groupbox_generating_settings)
        self.textbox_repeat_times_var.set('0')
        tk.Entry(self.groupbox_generating_settings, justify=tk.RIGHT, textvariable=self.textbox_repeat_times_var).place(x=230, y=80, width=150, height=19)
        #--------------------
        # AioSetAoSamplingClockのButtonを作成
        #--------------------
        self.button_aio_set_ao_sampling_clock = tk.Button(self.groupbox_generating_settings, text='AioSetAoSamplingClock')
        self.button_aio_set_ao_sampling_clock.place(x=580, y=30, width=170, height=19)
        #--------------------
        # 基本設定のButtonを作成
        #--------------------
        self.button_basic_settings = tk.Button(self.groupbox_generating_settings, text='基本設定')
        self.button_basic_settings.place(x=580, y=55, width=170, height=19)
        #--------------------
        # AioSetAoRepeatTimesのButtonを作成
        #--------------------
        self.button_aio_set_ao_repeat_times = tk.Button(self.groupbox_generating_settings, text='AioSetAoRepeatTimes')
        self.button_aio_set_ao_repeat_times.place(x=580, y=80, width=170, height=19)
        #----------------------------------------
        # 出力データ設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_output_data_settings = tk.LabelFrame(self.root, text="出力データ設定")
        self.groupbox_output_data_settings.place(x=15, y=275, width=760, height=75)
        #--------------------
        # ジェネレーティング回数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_output_data_settings, text='ジェネレーティング回数 : ', anchor='nw').place(x=15, y=5, width=200, height=19)
        #--------------------
        # ジェネレーティング回数のEntryを作成
        #--------------------
        self.textbox_generating_times_var = tk.StringVar(self.groupbox_output_data_settings)
        self.textbox_generating_times_var.set("1000")
        tk.Entry(self.groupbox_output_data_settings, justify=tk.RIGHT, textvariable=self.textbox_generating_times_var).place(x=230, y=5, width=150, height=19)
        #--------------------
        # 出力データ生成のButtonを作成
        #--------------------
        self.button_output_data_generate = tk.Button(self.groupbox_output_data_settings, text='出力データ生成...')
        self.button_output_data_generate.place(x=580, y=5, width=170, height=19)
        #--------------------
        # AioSetAoSamplingDataExのButtonを作成
        #--------------------
        self.button_aio_set_ao_sampling_data_ex = tk.Button(self.groupbox_output_data_settings, text='AioSetAoSamplingDataEx')
        self.button_aio_set_ao_sampling_data_ex.place(x=580, y=30, width=170, height=19)
        #----------------------------------------
        # ジェネレーティング動作のLabelFrameを作成
        #----------------------------------------
        self.groupbox_generating_action = tk.LabelFrame(self.root, text="ジェネレーティング動作")
        self.groupbox_generating_action.place(x=15, y=350, width=760, height=150)
        #--------------------
        # 設定データ数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_generating_action, text='設定データ数 : ', anchor='nw').place(x=15, y=30, width=200, height=19)
        #--------------------
        # 総ジェネレーティング回数のEntryを作成
        #--------------------
        self.textbox_generating_count_var = tk.StringVar(self.groupbox_generating_action)
        self.textbox_generating_count_var.set('')
        tk.Entry(self.groupbox_generating_action, justify=tk.RIGHT, textvariable=self.textbox_generating_count_var).place(x=230, y=30, width=330, height=19)
        #--------------------
        # リピート回数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_generating_action, text='リピート回数 : ', anchor='nw').place(x=15, y=55, width=200, height=19)
        #--------------------
        # リピート回数のEntryを作成
        #--------------------
        self.textbox_repeat_count_var = tk.StringVar(self.groupbox_generating_action)
        self.textbox_repeat_count_var.set('')
        tk.Entry(self.groupbox_generating_action, justify=tk.RIGHT, textvariable=self.textbox_repeat_count_var).place(x=230, y=55, width=330, height=19)
        #--------------------
        # ステータスのLabelを作成
        #--------------------
        tk.Label(self.groupbox_generating_action, text='ステータス : ', anchor='nw').place(x=15, y=80, width=200, height=19)
        #--------------------
        # ステータスのEntryを作成
        #--------------------
        self.textbox_status_var = tk.StringVar(self.groupbox_generating_action)
        self.textbox_status_var.set('')
        tk.Entry(self.groupbox_generating_action, textvariable=self.textbox_status_var).place(x=230, y=80, width=330, height=19)
        #--------------------
        # ジェネレーティング開始のButtonを作成
        #--------------------
        self.button_generating_start = tk.Button(self.groupbox_generating_action, text='ジェネレーティング開始')
        self.button_generating_start.place(x=580, y=5, width=170, height=19)
        #--------------------
        # AioGetAoSamplingTimesのButtonを作成
        #--------------------
        self.button_aio_get_ao_sampling_times = tk.Button(self.groupbox_generating_action, text='AioGetAoSamplingTimes')
        self.button_aio_get_ao_sampling_times.place(x=580, y=30, width=170, height=19)
        #--------------------
        # AioGetAoRepeatCountのButtonを作成
        #--------------------
        self.button_aio_get_ao_repeat_count = tk.Button(self.groupbox_generating_action, text='AioGetAoRepeatCount')
        self.button_aio_get_ao_repeat_count .place(x=580, y=55, width=170, height=19)
        #--------------------
        # AioGetAoStatusのButtonを作成
        #--------------------
        self.button_aio_get_ao_status = tk.Button(self.groupbox_generating_action, text='AioGetAoStatus')
        self.button_aio_get_ao_status.place(x=580, y=80, width=170, height=19)
        #--------------------
        # ジェネレーティング停止のButtonを作成
        #--------------------
        self.button_generating_stop = tk.Button(self.groupbox_generating_action, text='ジェネレーティング停止')
        self.button_generating_stop.place(x=580, y=105, width=170, height=19)
        #----------------------------------------
        # 終了処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="終了処理")
        self.groupbox_exit.place(x=15, y=505, width=760, height=50)
        #--------------------
        # AioExitのButtonを作成
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=580, y=5, width=170, height=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=33, y=570, width=200, 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=247, y=570, width=330, height=19)
        #--------------------
        # 閉じるのButtonを作成
        #--------------------
        self.button_close = tk.Button(self.root, text='閉じる')
        self.button_close.place(x=597, y=595, width=170, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_aio_init,
                       command_aio_reset_device,
                       command_aio_set_ao_range_all,
                       command_aio_set_ao_channels,
                       command_aio_set_ao_sampling_clock,
                       command_basic_settings,
                       command_aio_set_ao_repeat_times,
                       command_output_data_generate,
                       command_aio_set_ao_sampling_data_ex,
                       command_generating_start,
                       command_aio_get_ao_sampling_times,
                       command_aio_get_ao_repeat_count,
                       command_aio_get_ao_status,
                       command_generating_stop,
                       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)
        #----------------------------------------
        # 「AioSetAoRangeAll」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_ao_range_all.configure(command=command_aio_set_ao_range_all)
        #----------------------------------------
        # 「AioSetAoChannels」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_ao_channels.configure(command=command_aio_set_ao_channels)
        #----------------------------------------
        # 「AioSetAoSamplingClock」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_ao_sampling_clock.configure(command=command_aio_set_ao_sampling_clock)
        #----------------------------------------
        # 「基本設定」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_basic_settings.configure(command=command_basic_settings)
        #----------------------------------------
        # 「AioSetAoRepeatTimes」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_ao_repeat_times.configure(command=command_aio_set_ao_repeat_times)
        #----------------------------------------
        # 「出力データ生成」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_output_data_generate.configure(command=command_output_data_generate)
        #----------------------------------------
        # 「AioSetAoSamplingDataEx」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_ao_sampling_data_ex.configure(command=command_aio_set_ao_sampling_data_ex)
        #----------------------------------------
        # 「ジェネレーティング開始」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_generating_start.configure(command=command_generating_start)
        #----------------------------------------
        # 「AioGetAoSamplingTimes」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_get_ao_sampling_times.configure(command=command_aio_get_ao_sampling_times)
        #----------------------------------------
        # 「AioGetAoRepeatCount」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_get_ao_repeat_count.configure(command=command_aio_get_ao_repeat_count)
        #----------------------------------------
        # 「AioGetAoStatus」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_get_ao_status.configure(command=command_aio_get_ao_status)
        #----------------------------------------
        # 「ジェネレーティング停止」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_generating_stop.configure(command=command_generating_stop)
        #----------------------------------------
        # 「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()