﻿#================================================================
#================================================================
# API-AIO(WDM)
# デバイス内同期(ジェネレーティング・サンプリング)サンプル
# main UIファイル
#                                                 CONTEC Co.,Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# InternalSyncGeneratingSamplingUI クラス
#========================================
class InternalSyncGeneratingSamplingUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title('デバイス内同期(ジェネレーティング・サンプリング)')
        self.root.minsize(1116,715)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        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=1086, height=98)
        #--------------------
        # デバイス名のLabelを作成
        #--------------------
        tk.Label(self.groupbox_init, text='デバイス名 : ', anchor='nw').place(x=15, y=3, 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=180, y=3, width=140, height=19)
        #--------------------
        # AioInitのButtonを作成
        #--------------------
        self.button_aio_init = tk.Button(self.groupbox_init, text='AioInit')
        self.button_aio_init.place(x=910, y=3, width=160, height=30)
        #--------------------
        # AioResetDeviceのButtonを作成
        #--------------------
        self.button_aio_reset_device = tk.Button(self.groupbox_init, text='AioResetDevice')
        self.button_aio_reset_device.place(x=910, y=43, width=160, height=30)
        #----------------------------------------
        # 同期動作処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_sync = tk.LabelFrame(self.root, text="同期動作処理")
        self.groupbox_sync.place(x=15, y=118, width=1086, height=450)
        #--------------------
        # ジェネレーティング(マスター)のLabelFrameを作成
        #--------------------
        self.groupbox_sync_master = tk.LabelFrame(self.groupbox_sync, text="ジェネレーティング(マスター)")
        self.groupbox_sync_master.place(x=10, y=3, width=440, height=425)
        #--------------------
        # アナログ出力設定のLabelFrameを作成
        #--------------------
        self.groupbox_sync_master_ao = tk.LabelFrame(self.groupbox_sync_master, text="アナログ出力設定")
        self.groupbox_sync_master_ao.place(x=10, y=3, width=417, height=70)
        #--------------------
        # レンジのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_master_ao, text='レンジ : ', anchor='nw').place(x=5, y=3, width=90, height=19)
        #--------------------
        # レンジのComboboxを作成
        #--------------------
        self.combobox_ao_range = ttk.Combobox(self.groupbox_sync_master_ao)
        self.combobox_ao_range.place(x=158, y=3, width=140, height=20)
        self.combobox_ao_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_ao_range['state'] = 'readonly'
        self.combobox_ao_range.current(0)
        #--------------------
        # ジェネレーティング設定のLabelFrameを作成
        #--------------------
        self.groupbox_sync_master_generating = tk.LabelFrame(self.groupbox_sync_master, text="ジェネレーティング設定")
        self.groupbox_sync_master_generating.place(x=10, y=78, width=417, height=120)
        #--------------------
        # チャネル数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_master_generating, text='チャネル数 : ', anchor='nw').place(x=5, y=3, width=90, height=19)
        #--------------------
        # チャネル数のEntryを作成
        #--------------------
        self.textbox_ao_channels_var = tk.StringVar(self.groupbox_sync_master_generating)
        self.textbox_ao_channels_var.set('1')
        tk.Entry(self.groupbox_sync_master_generating, textvariable=self.textbox_ao_channels_var, justify=tk.RIGHT).place(x=208, y=3, width=140, height=19)
        #--------------------
        # 開始条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_master_generating, text='開始条件 : ', anchor='nw').place(x=5, y=28, width=90, height=19)
        #--------------------
        # ソフトウェアのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_master_generating, text='ソフトウェア', anchor='nw').place(x=206, y=28, width=90, height=19)
        #--------------------
        # ジェネレーティングクロック周期のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_master_generating, text='ジェネレーティングクロック周期 : ', anchor='nw').place(x=5, y=53, width=200, height=19)
        #--------------------
        # ジェネレーティングクロック周期のEntryを作成
        #--------------------
        self.textbox_ao_generating_clock_var = tk.StringVar(self.groupbox_sync_master_generating)
        self.textbox_ao_generating_clock_var.set('1000')
        tk.Entry(self.groupbox_sync_master_generating, textvariable=self.textbox_ao_generating_clock_var, justify=tk.RIGHT).place(x=208, y=53, width=140, height=19)
        #--------------------
        # ジェネレーティングクロック周期単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_master_generating, text='[μsec]', anchor='nw').place(x=350, y=53, width=50, height=19)
        #--------------------
        # ジェネレーティング回数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_master_generating, text='ジェネレーティング回数 : ', anchor='nw').place(x=5, y=78, width=150, height=19)
        #--------------------
        # ジェネレーティング回数のEntryを作成
        #--------------------
        self.textbox_ao_generating_times_var = tk.StringVar(self.groupbox_sync_master_generating)
        self.textbox_ao_generating_times_var.set('1000')
        tk.Entry(self.groupbox_sync_master_generating, textvariable=self.textbox_ao_generating_times_var, justify=tk.RIGHT).place(x=208, y=78, width=140, height=19)
        #--------------------
        # ジェネレーティングステータスのLabelFrameを作成
        #--------------------
        self.groupbox_sync_master_status = tk.LabelFrame(self.groupbox_sync_master, text="ジェネレーティングステータス")
        self.groupbox_sync_master_status.place(x=10, y=203, width=417, height=200)
        #--------------------
        # ジェネレーティング回数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_master_status, text='ジェネレーティング回数 : ', anchor='nw').place(x=5, y=130, width=150, height=19)
        #--------------------
        # ジェネレーティング回数のEntryを作成
        #--------------------
        self.textbox_ao_generating_count_var = tk.StringVar(self.groupbox_sync_master_status)
        tk.Entry(self.groupbox_sync_master_status, textvariable=self.textbox_ao_generating_count_var, justify=tk.RIGHT).place(x=158, y=130, width=247, height=19)
        #--------------------
        # ステータスのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_master_status, text='ステータス : ', anchor='nw').place(x=5, y=155, width=120, height=19)
        #--------------------
        # ステータスのEntryを作成
        #--------------------
        self.textbox_ao_status_var = tk.StringVar(self.groupbox_sync_master_status)
        tk.Entry(self.groupbox_sync_master_status, textvariable=self.textbox_ao_status_var).place(x=158, y=155, width=247, height=19)
        #--------------------
        # サンプリング(スレーブ)のLabelFrameを作成
        #--------------------
        self.groupbox_sync_slave = tk.LabelFrame(self.groupbox_sync, text="サンプリング(スレーブ)")
        self.groupbox_sync_slave.place(x=460, y=3, width=440, height=425)
        #--------------------
        # アナログ入力設定のLabelFrameを作成
        #--------------------
        self.groupbox_sync_slave_ai = tk.LabelFrame(self.groupbox_sync_slave, text="アナログ入力設定")
        self.groupbox_sync_slave_ai.place(x=10, y=3, width=417, height=70)
        #--------------------
        # レンジのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_ai, text='レンジ : ', anchor='nw').place(x=5, y=3, width=90, height=19)
        #--------------------
        # レンジのComboboxを作成
        #--------------------
        self.combobox_ai_range = ttk.Combobox(self.groupbox_sync_slave_ai)
        self.combobox_ai_range.place(x=158, y=3, width=140, height=20)
        self.combobox_ai_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_ai_range['state'] = 'readonly'
        self.combobox_ai_range.current(0)
        #--------------------
        # 入力方式のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_ai, text='入力方式 : ', anchor='nw').place(x=5, y=28, width=90, height=19)
        #--------------------
        # 入力方式のComboboxを作成
        #--------------------
        self.combobox_ai_input_method = ttk.Combobox(self.groupbox_sync_slave_ai)
        self.combobox_ai_input_method.place(x=158, y=28, width=140, height=20)
        self.combobox_ai_input_method['value'] = ('シングルエンド入力', '差動入力')
        self.combobox_ai_input_method['state'] = 'readonly'
        self.combobox_ai_input_method.current(0)
        #--------------------
        # サンプリング設定のLabelFrameを作成
        #--------------------
        self.groupbox_sync_slave_sampling = tk.LabelFrame(self.groupbox_sync_slave, text="サンプリング設定")
        self.groupbox_sync_slave_sampling.place(x=10, y=78, width=417, height=120)
        #--------------------
        # チャネル数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='チャネル数 : ', anchor='nw').place(x=5, y=3, width=90, height=19)
        #--------------------
        # チャネル数のEntryを作成
        #--------------------
        self.textbox_ai_channels_var = tk.StringVar(self.groupbox_sync_slave_sampling)
        self.textbox_ai_channels_var.set('1')
        tk.Entry(self.groupbox_sync_slave_sampling, textvariable=self.textbox_ai_channels_var, justify=tk.RIGHT).place(x=158, y=3, width=140, height=19)
        #--------------------
        # 開始条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='開始条件 : ', anchor='nw').place(x=5, y=28, width=90, height=19)
        #--------------------
        # ジェネレーティング開始のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='ジェネレーティング開始', anchor='nw').place(x=156, y=28, width=250, height=19)
        #--------------------
        # クロック条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='クロック条件 : ', anchor='nw').place(x=5, y=53, width=100, height=19)
        #--------------------
        # ジェネレーティング内部クロックのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='ジェネレーティング内部クロック', anchor='nw').place(x=156, y=53, width=250, height=19)
        #--------------------
        # 停止条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='停止条件 : ', anchor='nw').place(x=5, y=78, width=90, height=19)
        #--------------------
        # ジェネレーティング停止のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='ジェネレーティング停止', anchor='nw').place(x=156, y=78, width=250, height=19)
        #--------------------
        # サンプリングステータスのLabelFrameを作成
        #--------------------
        self.groupbox_sync_slave_status = tk.LabelFrame(self.groupbox_sync_slave, text="サンプリングステータス")
        self.groupbox_sync_slave_status.place(x=10, y=203, width=417, height=200)
        #--------------------
        # カウントのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_status, text='カウント', anchor='nw').place(x=156, y=3, width=90, height=19)
        #--------------------
        # 電圧値[V]/電流値[mA]のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_status, text='電圧値[V]/電流値[mA]', anchor='nw').place(x=246, y=3, width=120, height=19)
        #--------------------
        # サンプリングデータのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_status, text='サンプリングデータ : ', anchor='nw').place(x=5, y=23, width=120, height=19)
        #--------------------
        # サンプリングデータのListboxを作成
        #--------------------
        self.listbox_ai_sampling_data_var = tk.StringVar(self.groupbox_sync_slave_status)
        self.listbox_ai_sampling_data = tk.Listbox(self.groupbox_sync_slave_status, listvariable=self.listbox_ai_sampling_data_var)
        self.listbox_ai_sampling_data.place(x=158, y=23, width=227, height=80)
        self.scrollbar_slave_v = ttk.Scrollbar(self.groupbox_sync_slave_status, orient=tk.VERTICAL, command=self.listbox_ai_sampling_data.yview)
        self.scrollbar_slave_v.place(x=385, y=23, width=20, height=80)
        self.scrollbar_slave_h = ttk.Scrollbar(self.groupbox_sync_slave_status, orient=tk.HORIZONTAL, command=self.listbox_ai_sampling_data.xview)
        self.scrollbar_slave_h.place(x=158, y=103, width=227, height=20)
        self.listbox_ai_sampling_data.config(yscrollcommand=self.scrollbar_slave_v.set, xscrollcommand=self.scrollbar_slave_h.set)
        #--------------------
        # サンプリング回数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_status, text='サンプリング回数 : ', anchor='nw').place(x=5, y=130, width=120, height=19)
        #--------------------
        # サンプリング回数のEntryを作成
        #--------------------
        self.textbox_ai_sampling_count_var = tk.StringVar(self.groupbox_sync_slave_status)
        tk.Entry(self.groupbox_sync_slave_status, textvariable=self.textbox_ai_sampling_count_var, justify=tk.RIGHT).place(x=158, y=130, width=247, height=19)
        #--------------------
        # ステータスのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_status, text='ステータス : ', anchor='nw').place(x=5, y=155, width=120, height=19)
        #--------------------
        # ステータスのEntryを作成
        #--------------------
        self.textbox_ai_status_var = tk.StringVar(self.groupbox_sync_slave_status)
        tk.Entry(self.groupbox_sync_slave_status, textvariable=self.textbox_ai_status_var).place(x=158, y=155, width=247, height=19)
        #--------------------
        # アナログ入出力設定のButtonを作成
        #--------------------
        self.button_ai_ao_setting = tk.Button(self.groupbox_sync, text='アナログ入出力設定')
        self.button_ai_ao_setting.place(x=910, y=20, width=160, height=30)
        #--------------------
        # ジェネレーティング/サンプリング設定のButtonを作成
        #--------------------
        self.button_generating_sampling_setting = tk.Button(self.groupbox_sync, text='ジェネレーティング/\nサンプリング設定')
        self.button_generating_sampling_setting.place(x=910, y=70, width=160, height=30)
        #--------------------
        # 同期動作設定のButtonを作成
        #--------------------
        self.button_sync_setting = tk.Button(self.groupbox_sync, text='同期動作設定')
        self.button_sync_setting.place(x=910, y=120, width=160, height=30)
        #--------------------
        # 出力データ設定のButtonを作成
        #--------------------
        self.button_output_data_setting = tk.Button(self.groupbox_sync, text='出力データ設定...')
        self.button_output_data_setting.place(x=910, y=170, width=160, height=30)
        #--------------------
        # ジェネレーティング/サンプリング開始のButtonを作成
        #--------------------
        self.button_generating_sampling_start = tk.Button(self.groupbox_sync, text='ジェネレーティング/\nサンプリング開始')
        self.button_generating_sampling_start.place(x=910, y=220, width=160, height=30)
        #--------------------
        # 状態取得のButtonを作成
        #--------------------
        self.button_get_status = tk.Button(self.groupbox_sync, text='状態取得')
        self.button_get_status.place(x=910, y=270, width=160, height=30)
        #--------------------
        # ジェネレーティング/サンプリング停止のButtonを作成
        #--------------------
        self.button_generating_sampling_stop = tk.Button(self.groupbox_sync, text='ジェネレーティング/\nサンプリング停止')
        self.button_generating_sampling_stop.place(x=910, y=320, width=160, height=30)
        #--------------------
        # 取得データファイル出力のButtonを作成
        #--------------------
        self.button_output_sampling_data = tk.Button(self.groupbox_sync, text='取得データファイル出力...')
        self.button_output_sampling_data.place(x=910, y=370, width=160, height=30)
        #----------------------------------------
        # 終了処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="終了処理")
        self.groupbox_exit.place(x=15, y=578, width=1086, height=60)
        #--------------------
        # AioExitのButtonを作成
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=910, y=3, width=160, height=30)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=35, y=660, width=120, height=19)
        #--------------------
        # 戻り値のEntryを作成
        #--------------------
        self.textbox_return_string_var = tk.StringVar(self.root)
        tk.Entry(self.root, textvariable=self.textbox_return_string_var).place(x=200, y=660, width=435, height=19)
        #--------------------
        # 閉じるのButtonを作成
        #--------------------
        self.button_close = tk.Button(self.root, text='閉じる')
        self.button_close.place(x=927, y=665, width=160, height=30)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_aio_init,
                       command_aio_reset_device,
                       command_ai_ao_setting,
                       command_generating_sampling_setting,
                       command_sync_setting,
                       command_output_data_setting,
                       command_generating_sampling_start,
                       command_get_status,
                       command_generating_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)
        #----------------------------------------
        # 「アナログ入出力設定」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_ai_ao_setting.configure(command=command_ai_ao_setting)
        #----------------------------------------
        # 「ジェネレーティング/サンプリング設定」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_generating_sampling_setting.configure(command=command_generating_sampling_setting)
        #----------------------------------------
        # 「同期動作設定」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_sync_setting.configure(command=command_sync_setting)
        #----------------------------------------
        # 「出力データ設定」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_output_data_setting.configure(command=command_output_data_setting)
        #----------------------------------------
        # 「ジェネレーティング/サンプリング開始」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_generating_sampling_start.configure(command=command_generating_sampling_start)
        #----------------------------------------
        # 「状態取得」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_get_status.configure(command=command_get_status)
        #----------------------------------------
        # 「ジェネレーティング/サンプリング停止」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_generating_sampling_stop.configure(command=command_generating_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()