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


#========================================
# InternalSyncCounterSamplingUI クラス
#========================================
class InternalSyncCounterSamplingUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title('デバイス内同期(カウント比較一致・サンプリング)')
        self.root.minsize(1116,665)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        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=75)
        #--------------------
        # デバイス名の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=19)
        #--------------------
        # AioResetDeviceのButtonを作成
        #--------------------
        self.button_aio_reset_device = tk.Button(self.groupbox_init, text='AioResetDevice')
        self.button_aio_reset_device.place(x=910, y=30, width=160, height=19)
        #----------------------------------------
        # 同期動作処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_sync = tk.LabelFrame(self.root, text="同期動作処理")
        self.groupbox_sync.place(x=15, y=95, 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_cnt = tk.LabelFrame(self.groupbox_sync_master, text="カウンタ設定")
        self.groupbox_sync_master_cnt.place(x=10, y=3, width=417, height=70)
        #--------------------
        # チャネル番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_master_cnt, text='チャネル番号 : ', anchor='nw').place(x=5, y=3, width=90, height=19)
        #--------------------
        # チャネル番号のEntryを作成
        #--------------------
        self.textbox_cnt_channel_var = tk.StringVar(self.groupbox_sync_master_cnt)
        self.textbox_cnt_channel_var.set('0')
        self.textbox_cnt_channel = tk.Entry(self.groupbox_sync_master_cnt, textvariable=self.textbox_cnt_channel_var, justify=tk.RIGHT)
        self.textbox_cnt_channel.place(x=158, y=3, width=140, height=19)
        #--------------------
        # カウンタ一致設定のLabelFrameを作成
        #--------------------
        self.groupbox_sync_master_cnt_match = tk.LabelFrame(self.groupbox_sync_master, text="カウンタ一致設定")
        self.groupbox_sync_master_cnt_match.place(x=10, y=78, width=417, height=58)
        #--------------------
        # 比較カウント値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_master_cnt_match, text='比較カウント値 : ', anchor='nw').place(x=5, y=3, width=110, height=19)
        #--------------------
        # 比較カウント値のEntryを作成
        #--------------------
        self.textbox_cnt_comparison_value_var = tk.StringVar(self.groupbox_sync_master_cnt_match)
        self.textbox_cnt_comparison_value_var.set('1000')
        tk.Entry(self.groupbox_sync_master_cnt_match, textvariable=self.textbox_cnt_comparison_value_var, justify=tk.RIGHT).place(x=158, y=3, width=140, height=19)
        #--------------------
        # カウンタプリセットのLabelFrameを作成
        #--------------------
        self.groupbox_sync_master_cnt_preset = tk.LabelFrame(self.groupbox_sync_master, text="カウンタプリセット")
        self.groupbox_sync_master_cnt_preset.place(x=10, y=141, width=417, height=58)
        #--------------------
        # プリセット値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_master_cnt_preset, text='プリセット値 : ', anchor='nw').place(x=5, y=3, width=90, height=19)
        #--------------------
        # プリセット値のEntryを作成
        #--------------------
        self.textbox_cnt_preset_value_var = tk.StringVar(self.groupbox_sync_master_cnt_preset)
        self.textbox_cnt_preset_value_var.set('0')
        tk.Entry(self.groupbox_sync_master_cnt_preset, textvariable=self.textbox_cnt_preset_value_var, justify=tk.RIGHT).place(x=158, y=3, width=140, height=19)
        #--------------------
        # カウンタステータスのLabelFrameを作成
        #--------------------
        self.groupbox_sync_master_cnt_status = tk.LabelFrame(self.groupbox_sync_master, text="カウンタステータス")
        self.groupbox_sync_master_cnt_status.place(x=10, y=203, width=417, height=200)
        #--------------------
        # カウント値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_master_cnt_status, text='カウント値 : ', anchor='nw').place(x=5, y=130, width=150, height=19)
        #--------------------
        # カウント値のEntryを作成
        #--------------------
        self.textbox_cnt_count_var = tk.StringVar(self.groupbox_sync_master_cnt_status)
        tk.Entry(self.groupbox_sync_master_cnt_status, textvariable=self.textbox_cnt_count_var, justify=tk.RIGHT).place(x=158, y=130, width=247, height=19)
        #--------------------
        # ステータスのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_master_cnt_status, text='ステータス : ', anchor='nw').place(x=5, y=155, width=120, height=19)
        #--------------------
        # ステータスのEntryを作成
        #--------------------
        self.textbox_cnt_status_var = tk.StringVar(self.groupbox_sync_master_cnt_status)
        tk.Entry(self.groupbox_sync_master_cnt_status, textvariable=self.textbox_cnt_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)
        #--------------------
        # 内部クロック値のEntryを作成
        #--------------------
        self.textbox_ai_internal_clock_var = tk.StringVar(self.groupbox_sync_slave_sampling)
        self.textbox_ai_internal_clock_var.set('1000')
        tk.Entry(self.groupbox_sync_slave_sampling, textvariable=self.textbox_ai_internal_clock_var, justify=tk.RIGHT).place(x=158, y=53, width=140, height=19)
        #--------------------
        # 内部クロック値単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='[μsec]', anchor='nw').place(x=308, y=53, width=100, height=19)
        #--------------------
        # 停止回数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='停止回数 : ', anchor='nw').place(x=5, y=78, width=90, height=19)
        #--------------------
        # 停止回数のEntryを作成
        #--------------------
        self.textbox_ai_stop_times_var = tk.StringVar(self.groupbox_sync_slave_sampling)
        self.textbox_ai_stop_times_var.set('1000')
        tk.Entry(self.groupbox_sync_slave_sampling, textvariable=self.textbox_ai_stop_times_var, justify=tk.RIGHT).place(x=158, y=78, width=140, 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_cnt_ai_setting = tk.Button(self.groupbox_sync, text='カウンタ/アナログ入力設定')
        self.button_cnt_ai_setting.place(x=910, y=20, width=160, height=19)
        #--------------------
        # サンプリング設定のButtonを作成
        #--------------------
        self.button_sampling_setting = tk.Button(self.groupbox_sync, text='サンプリング設定')
        self.button_sampling_setting.place(x=910, y=60, width=160, height=19)
        #--------------------
        # 同期処理設定のButtonを作成
        #--------------------
        self.button_sync_setting = tk.Button(self.groupbox_sync, text='同期処理設定')
        self.button_sync_setting.place(x=910, y=100, width=160, height=19)
        #--------------------
        # サンプリング開始のButtonを作成
        #--------------------
        self.button_sampling_start = tk.Button(self.groupbox_sync, text='サンプリング開始')
        self.button_sampling_start.place(x=910, y=140, width=160, height=19)
        #--------------------
        # カウンタ開始のButtonを作成
        #--------------------
        self.button_counter_start = tk.Button(self.groupbox_sync, text='カウンタ開始')
        self.button_counter_start.place(x=910, y=180, width=160, height=19)
        #--------------------
        # AioPresetCntのButtonを作成
        #--------------------
        self.button_aio_preset_cnt = tk.Button(self.groupbox_sync, text='AioPresetCnt')
        self.button_aio_preset_cnt.place(x=910, y=220, width=160, height=19)
        #--------------------
        # 状態取得のButtonを作成
        #--------------------
        self.button_get_status = tk.Button(self.groupbox_sync, text='状態取得')
        self.button_get_status.place(x=910, y=260, width=160, height=19)
        #--------------------
        # サンプリング停止のButtonを作成
        #--------------------
        self.button_sampling_stop = tk.Button(self.groupbox_sync, text='サンプリング停止')
        self.button_sampling_stop.place(x=910, y=300, width=160, height=19)
        #--------------------
        # カウンタ停止のButtonを作成
        #--------------------
        self.button_counter_stop = tk.Button(self.groupbox_sync, text='カウンタ停止')
        self.button_counter_stop.place(x=910, y=340, width=160, height=19)
        #--------------------
        # 取得データファイル出力のButtonを作成
        #--------------------
        self.button_output_sampling_data = tk.Button(self.groupbox_sync, text='取得データファイル出力...')
        self.button_output_sampling_data.place(x=910, y=380, width=160, height=19)
        #----------------------------------------
        # 終了処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="終了処理")
        self.groupbox_exit.place(x=15, y=555, width=1086, height=50)
        #--------------------
        # 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=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=35, y=617, 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=617, width=435, height=19)
        #--------------------
        # 閉じるのButtonを作成
        #--------------------
        self.button_close = tk.Button(self.root, text='閉じる')
        self.button_close.place(x=927, y=632, width=160, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_aio_init,
                       command_aio_reset_device,
                       command_cnt_channel_no,
                       command_cnt_ai_setting,
                       command_sampling_setting,
                       command_sync_setting,
                       command_sampling_start,
                       command_counter_start,
                       command_aio_preset_cnt,
                       command_get_status,
                       command_sampling_stop,
                       command_counter_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.textbox_cnt_channel.bind("<KeyRelease>", command_cnt_channel_no)
        #----------------------------------------
        # 「カウンタ/アナログ入力設定」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_ai_setting.configure(command=command_cnt_ai_setting)
        #----------------------------------------
        # 「サンプリング設定」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_sampling_setting.configure(command=command_sampling_setting)
        #----------------------------------------
        # 「同期処理設定」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_sync_setting.configure(command=command_sync_setting)
        #----------------------------------------
        # 「サンプリング開始」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_sampling_start.configure(command=command_sampling_start)
        #----------------------------------------
        # 「カウンタ開始」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_counter_start.configure(command=command_counter_start)
        #----------------------------------------
        # 「AioPresetCnt」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_preset_cnt.configure(command=command_aio_preset_cnt)
        #----------------------------------------
        # 「状態取得」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_get_status.configure(command=command_get_status)
        #----------------------------------------
        # 「サンプリング停止」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_sampling_stop.configure(command=command_sampling_stop)
        #----------------------------------------
        # 「カウンタ停止」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_counter_stop.configure(command=command_counter_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()