﻿#================================================================
#================================================================
# API-AIO(WDM)
# Device internal synchronization (Count Comparison Match/Sampling) sample
# main UI file
#                                                 CONTEC Co.,Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# InternalSyncCounterSamplingUI class
#========================================
class InternalSyncCounterSamplingUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Device Internal Synchronization (Count Comparison Match/Sampling)')
        self.root.minsize(1156,665)
        #----------------------------------------
        # Set the font for controls
        #----------------------------------------
        self.root.option_add("*Font", ("MS Gothic", 9))
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self._create_ui()

    #========================================
    # UI creation function
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # Create LabelFrame for Initialize
        #----------------------------------------
        self.groupbox_init = tk.LabelFrame(self.root, text="Initialize")
        self.groupbox_init.place(x=15, y=10, width=1136, height=75)
        #--------------------
        # Create Label for Device Name
        #--------------------
        tk.Label(self.groupbox_init, text='Device Name : ', anchor='nw').place(x=15, y=3, width=90, height=19)
        #--------------------
        # Create Entry for Device Name
        #--------------------
        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)
        #--------------------
        # Create Button for AioInit
        #--------------------
        self.button_aio_init = tk.Button(self.groupbox_init, text='AioInit')
        self.button_aio_init.place(x=910, y=3, width=200, height=19)
        #--------------------
        # Create Button for AioResetDevice
        #--------------------
        self.button_aio_reset_device = tk.Button(self.groupbox_init, text='AioResetDevice')
        self.button_aio_reset_device.place(x=910, y=30, width=200, height=19)
        #----------------------------------------
        # Create LabelFrame for Sync Operation
        #----------------------------------------
        self.groupbox_sync = tk.LabelFrame(self.root, text="Sync Operation")
        self.groupbox_sync.place(x=15, y=95, width=1136, height=450)
        #--------------------
        # Create LabelFrame for Counter(Master)
        #--------------------
        self.groupbox_sync_master = tk.LabelFrame(self.groupbox_sync, text="Counter(Master)")
        self.groupbox_sync_master.place(x=10, y=3, width=440, height=425)
        #--------------------
        # Create LabelFrame for Counter Set
        #--------------------
        self.groupbox_sync_master_cnt = tk.LabelFrame(self.groupbox_sync_master, text="Counter Set")
        self.groupbox_sync_master_cnt.place(x=10, y=3, width=417, height=70)
        #--------------------
        # Create Label for ChannelNo
        #--------------------
        tk.Label(self.groupbox_sync_master_cnt, text='ChannelNo : ', anchor='nw').place(x=5, y=3, width=90, height=19)
        #--------------------
        # Create Entry for ChannelNo
        #--------------------
        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)
        #--------------------
        # Create LabelFrame for Counter Match Set
        #--------------------
        self.groupbox_sync_master_cnt_match = tk.LabelFrame(self.groupbox_sync_master, text="Counter Match Set")
        self.groupbox_sync_master_cnt_match.place(x=10, y=78, width=417, height=58)
        #--------------------
        # Create Label for Comparison Count Value
        #--------------------
        tk.Label(self.groupbox_sync_master_cnt_match, text='Comparison Count Value : ', anchor='nw').place(x=5, y=3, width=150, height=19)
        #--------------------
        # Create Entry for Comparison Count Value
        #--------------------
        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)
        #--------------------
        # Create LabelFrame for Counter Preset
        #--------------------
        self.groupbox_sync_master_cnt_preset = tk.LabelFrame(self.groupbox_sync_master, text="Counter Preset")
        self.groupbox_sync_master_cnt_preset.place(x=10, y=141, width=417, height=58)
        #--------------------
        # Create Label for Preset Value
        #--------------------
        tk.Label(self.groupbox_sync_master_cnt_preset, text='Preset Value : ', anchor='nw').place(x=5, y=3, width=90, height=19)
        #--------------------
        # Create Entry for Preset Value
        #--------------------
        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)
        #--------------------
        # Create LabelFrame for Counter Status
        #--------------------
        self.groupbox_sync_master_cnt_status = tk.LabelFrame(self.groupbox_sync_master, text="Counter Status")
        self.groupbox_sync_master_cnt_status.place(x=10, y=203, width=417, height=200)
        #--------------------
        # Create Label for Count Value
        #--------------------
        tk.Label(self.groupbox_sync_master_cnt_status, text='Count Value : ', anchor='nw').place(x=5, y=130, width=150, height=19)
        #--------------------
        # Create Entry for Count Value
        #--------------------
        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)
        #--------------------
        # Create Label for Status
        #--------------------
        tk.Label(self.groupbox_sync_master_cnt_status, text='Status : ', anchor='nw').place(x=5, y=155, width=120, height=19)
        #--------------------
        # Create Entry for Status
        #--------------------
        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)
        #--------------------
        # Create LabelFrame for Sampling(Salve)
        #--------------------
        self.groupbox_sync_slave = tk.LabelFrame(self.groupbox_sync, text="Sampling(Salve)")
        self.groupbox_sync_slave.place(x=460, y=3, width=440, height=425)
        #--------------------
        # Create LabelFrame for Analog Input Set
        #--------------------
        self.groupbox_sync_slave_ai = tk.LabelFrame(self.groupbox_sync_slave, text="Analog Input Set")
        self.groupbox_sync_slave_ai.place(x=10, y=3, width=417, height=70)
        #--------------------
        # Create Label for Range
        #--------------------
        tk.Label(self.groupbox_sync_slave_ai, text='Range : ', anchor='nw').place(x=5, y=3, width=90, height=19)
        #--------------------
        # Create Combobox for Range
        #--------------------
        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)
        #--------------------
        # Create Label for Input Method
        #--------------------
        tk.Label(self.groupbox_sync_slave_ai, text='Input Method : ', anchor='nw').place(x=5, y=28, width=90, height=19)
        #--------------------
        # Create Combobox for Input Method
        #--------------------
        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'] = ('Single-end input', 'Differential input')
        self.combobox_ai_input_method['state'] = 'readonly'
        self.combobox_ai_input_method.current(0)
        #--------------------
        # Create LabelFrame for Sampling Set
        #--------------------
        self.groupbox_sync_slave_sampling = tk.LabelFrame(self.groupbox_sync_slave, text="Sampling Set")
        self.groupbox_sync_slave_sampling.place(x=10, y=78, width=417, height=120)
        #--------------------
        # Create Label for Number of Channels
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='Number of Channels : ', anchor='nw').place(x=5, y=3, width=150, height=19)
        #--------------------
        # Create Entry for Number of Channels
        #--------------------
        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)
        #--------------------
        # Create Label for Start Condition
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='Start Condition : ', anchor='nw').place(x=5, y=28, width=150, height=19)
        #--------------------
        # Create Label for Comparison count match
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='Comparison count match', anchor='nw').place(x=156, y=28, width=250, height=19)
        #--------------------
        # Create Label for Internal clock value
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='Internal clock value : ', anchor='nw').place(x=5, y=53, width=150, height=19)
        #--------------------
        # Create Entry for Internal clock value
        #--------------------
        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)
        #--------------------
        # Create Label for Internal clock value Unit
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='[usec]', anchor='nw').place(x=308, y=53, width=100, height=19)
        #--------------------
        # Create Label for Stop Times
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='Stop Times : ', anchor='nw').place(x=5, y=78, width=90, height=19)
        #--------------------
        # Create Entry for Stop Times
        #--------------------
        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)
        #--------------------
        # Create LabelFrame for Sampling Status
        #--------------------
        self.groupbox_sync_slave_status = tk.LabelFrame(self.groupbox_sync_slave, text="Sampling Status")
        self.groupbox_sync_slave_status.place(x=10, y=203, width=417, height=200)
        #--------------------
        # Create Label for Count
        #--------------------
        tk.Label(self.groupbox_sync_slave_status, text='Count', anchor='nw').place(x=156, y=3, width=90, height=19)
        #--------------------
        # Create Label for Voltage[V]/Current[mA]
        #--------------------
        tk.Label(self.groupbox_sync_slave_status, text='Voltage[V]/Current[mA]', anchor='nw').place(x=246, y=3, width=150, height=19)
        #--------------------
        # Create Label for Sampling Data
        #--------------------
        tk.Label(self.groupbox_sync_slave_status, text='Sampling Data : ', anchor='nw').place(x=5, y=23, width=120, height=19)
        #--------------------
        # Create Listbox for Sampling Data
        #--------------------
        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)
        #--------------------
        # Create Label for Sampling Count
        #--------------------
        tk.Label(self.groupbox_sync_slave_status, text='Sampling Count : ', anchor='nw').place(x=5, y=130, width=120, height=19)
        #--------------------
        # Create Entry for Sampling Count
        #--------------------
        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)
        #--------------------
        # Create Label for Status
        #--------------------
        tk.Label(self.groupbox_sync_slave_status, text='Status : ', anchor='nw').place(x=5, y=155, width=120, height=19)
        #--------------------
        # Create Entry for Status
        #--------------------
        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)
        #--------------------
        # Create Button for Counter/Analog Input Set
        #--------------------
        self.button_cnt_ai_setting = tk.Button(self.groupbox_sync, text='Counter/Analog Input Set')
        self.button_cnt_ai_setting.place(x=910, y=20, width=200, height=19)
        #--------------------
        # Create Button for Sampling Set
        #--------------------
        self.button_sampling_setting = tk.Button(self.groupbox_sync, text='Sampling Set')
        self.button_sampling_setting.place(x=910, y=60, width=200, height=19)
        #--------------------
        # Create Button for Sync Process Set
        #--------------------
        self.button_sync_setting = tk.Button(self.groupbox_sync, text='Sync Process Set')
        self.button_sync_setting.place(x=910, y=100, width=200, height=19)
        #--------------------
        # Create Button for Sampling Start
        #--------------------
        self.button_sampling_start = tk.Button(self.groupbox_sync, text='Sampling Start')
        self.button_sampling_start.place(x=910, y=140, width=200, height=19)
        #--------------------
        # Create Button for Counter Start
        #--------------------
        self.button_counter_start = tk.Button(self.groupbox_sync, text='Counter Start')
        self.button_counter_start.place(x=910, y=180, width=200, height=19)
        #--------------------
        # Create Button for AioPresetCnt
        #--------------------
        self.button_aio_preset_cnt = tk.Button(self.groupbox_sync, text='AioPresetCnt')
        self.button_aio_preset_cnt.place(x=910, y=220, width=200, height=19)
        #--------------------
        # Create Button for Get Status
        #--------------------
        self.button_get_status = tk.Button(self.groupbox_sync, text='Get Status')
        self.button_get_status.place(x=910, y=260, width=200, height=19)
        #--------------------
        # Create Button for Sampling Stop
        #--------------------
        self.button_sampling_stop = tk.Button(self.groupbox_sync, text='Sampling Stop')
        self.button_sampling_stop.place(x=910, y=300, width=200, height=19)
        #--------------------
        # Create Button for Counter Stop
        #--------------------
        self.button_counter_stop = tk.Button(self.groupbox_sync, text='Counter Stop')
        self.button_counter_stop.place(x=910, y=340, width=200, height=19)
        #--------------------
        # Create Button for Output Acquired Data to File
        #--------------------
        self.button_output_sampling_data = tk.Button(self.groupbox_sync, text='Output Acquired Data to File...')
        self.button_output_sampling_data.place(x=910, y=380, width=200, height=19)
        #----------------------------------------
        # Create LabelFrame for Exit process
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="Exit")
        self.groupbox_exit.place(x=15, y=555, width=1136, height=50)
        #--------------------
        # Create Button for AioExit
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=910, y=3, width=200, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.root, text='Return Code : ', anchor='nw').place(x=35, y=617, width=120, height=19)
        #--------------------
        # Create Entry for Return Code
        #--------------------
        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)
        #--------------------
        # Create Button for Close
        #--------------------
        self.button_close = tk.Button(self.root, text='Close')
        self.button_close.place(x=927, y=632, width=200, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    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):
        #----------------------------------------
        # Set the callback function for the [AioInit] button
        #----------------------------------------
        self.button_aio_init.configure(command=command_aio_init)
        #----------------------------------------
        # Set the callback function for the [AioResetDevice] button
        #----------------------------------------
        self.button_aio_reset_device.configure(command=command_aio_reset_device)
        #----------------------------------------
        # Set the callback function for the Counter [ChannelNo] entry
        #----------------------------------------
        self.textbox_cnt_channel.bind("<KeyRelease>", command_cnt_channel_no)
        #----------------------------------------
        # Set the callback function for the [Counter/Analog Input Set] button
        #----------------------------------------
        self.button_cnt_ai_setting.configure(command=command_cnt_ai_setting)
        #----------------------------------------
        # Set the callback function for the [Sampling Set] button
        #----------------------------------------
        self.button_sampling_setting.configure(command=command_sampling_setting)
        #----------------------------------------
        # Set the callback function for the [Sync Process Set] button
        #----------------------------------------
        self.button_sync_setting.configure(command=command_sync_setting)
        #----------------------------------------
        # Set the callback function for the [Sampling Start] button
        #----------------------------------------
        self.button_sampling_start.configure(command=command_sampling_start)
        #----------------------------------------
        # Set the callback function for the [Counter Start] button
        #----------------------------------------
        self.button_counter_start.configure(command=command_counter_start)
        #----------------------------------------
        # Set the callback function for the [AioPresetCnt] button
        #----------------------------------------
        self.button_aio_preset_cnt.configure(command=command_aio_preset_cnt)
        #----------------------------------------
        # Set the callback function for the [Get Status] button
        #----------------------------------------
        self.button_get_status.configure(command=command_get_status)
        #----------------------------------------
        # Set the callback function for the [Sampling Stop] button
        #----------------------------------------
        self.button_sampling_stop.configure(command=command_sampling_stop)
        #----------------------------------------
        # Set the callback function for the [Counter Stop] button
        #----------------------------------------
        self.button_counter_stop.configure(command=command_counter_stop)
        #----------------------------------------
        # Set the callback function for the [Output Acquired Data to File] button
        #----------------------------------------
        self.button_output_sampling_data.configure(command=command_output_sampling_data)
        #----------------------------------------
        # Set the callback function for the [AioExit] button
        #----------------------------------------
        self.button_aio_exit.configure(command=command_aio_exit)
        #----------------------------------------
        # Set the callback function for the [Close] button
        #----------------------------------------
        self.button_close.configure(command=command_close)

    #========================================
    # Display the error in a message box
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.root)

    #========================================
    # Run the main event loop
    #========================================
    def run(self):
        self.root.mainloop()