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


#========================================
# InternalSyncGeneratingSamplingUI class
#========================================
class InternalSyncGeneratingSamplingUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Device Internal Synchronization (Generating/Sampling)')
        self.root.minsize(1156,715)
        #----------------------------------------
        # 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=1126, height=98)
        #--------------------
        # 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=30)
        #--------------------
        # 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=43, width=200, height=30)
        #----------------------------------------
        # Create LabelFrame for Sync Operation
        #----------------------------------------
        self.groupbox_sync = tk.LabelFrame(self.root, text="Sync Operation")
        self.groupbox_sync.place(x=15, y=118, width=1126, height=450)
        #--------------------
        # Create LabelFrame for Generating(Master)
        #--------------------
        self.groupbox_sync_master = tk.LabelFrame(self.groupbox_sync, text="Generating(Master)")
        self.groupbox_sync_master.place(x=10, y=3, width=440, height=425)
        #--------------------
        # Create LabelFrame for Analog Output Set
        #--------------------
        self.groupbox_sync_master_ao = tk.LabelFrame(self.groupbox_sync_master, text="Analog Output Set")
        self.groupbox_sync_master_ao.place(x=10, y=3, width=417, height=70)
        #--------------------
        # Create Label for Range
        #--------------------
        tk.Label(self.groupbox_sync_master_ao, text='Range : ', anchor='nw').place(x=5, y=3, width=90, height=19)
        #--------------------
        # Create Combobox for Range
        #--------------------
        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)
        #--------------------
        # Create LabelFrame for Generating Set
        #--------------------
        self.groupbox_sync_master_generating = tk.LabelFrame(self.groupbox_sync_master, text="Generating Set")
        self.groupbox_sync_master_generating.place(x=10, y=78, width=417, height=120)
        #--------------------
        # Create Label for Number of Channels
        #--------------------
        tk.Label(self.groupbox_sync_master_generating, text='Number of Channels : ', anchor='nw').place(x=5, y=3, width=120, height=19)
        #--------------------
        # Create Entry for Number of Channels
        #--------------------
        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)
        #--------------------
        # Create Label for Start Condition
        #--------------------
        tk.Label(self.groupbox_sync_master_generating, text='Start Condition : ', anchor='nw').place(x=5, y=28, width=120, height=19)
        #--------------------
        # Create Label for Software
        #--------------------
        tk.Label(self.groupbox_sync_master_generating, text='Software', anchor='nw').place(x=206, y=28, width=90, height=19)
        #--------------------
        # Create Label for Generating Clock Period
        #--------------------
        tk.Label(self.groupbox_sync_master_generating, text='Generating Clock Period : ', anchor='nw').place(x=5, y=53, width=200, height=19)
        #--------------------
        # Create Entry for Generating Clock Period
        #--------------------
        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)
        #--------------------
        # Create Label for Generating Clock Period Unit
        #--------------------
        tk.Label(self.groupbox_sync_master_generating, text='[usec]', anchor='nw').place(x=350, y=53, width=50, height=19)
        #--------------------
        # Create Label for Generating Times
        #--------------------
        tk.Label(self.groupbox_sync_master_generating, text='Generating Times : ', anchor='nw').place(x=5, y=78, width=150, height=19)
        #--------------------
        # Create Entry for Generating Times
        #--------------------
        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)
        #--------------------
        # Create LabelFrame for Generating Status
        #--------------------
        self.groupbox_sync_master_status = tk.LabelFrame(self.groupbox_sync_master, text="Generating Status")
        self.groupbox_sync_master_status.place(x=10, y=203, width=417, height=200)
        #--------------------
        # Create Label for Generating Count
        #--------------------
        tk.Label(self.groupbox_sync_master_status, text='Generating Count : ', anchor='nw').place(x=5, y=130, width=150, height=19)
        #--------------------
        # Create Entry for Generating Count
        #--------------------
        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)
        #--------------------
        # Create Label for Status
        #--------------------
        tk.Label(self.groupbox_sync_master_status, text='Status : ', anchor='nw').place(x=5, y=155, width=120, height=19)
        #--------------------
        # Create Entry for Status
        #--------------------
        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)
        #--------------------
        # 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=120, 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=120, height=19)
        #--------------------
        # Create Label for Generating Start
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='Generating Start', anchor='nw').place(x=156, y=28, width=250, height=19)
        #--------------------
        # Create Label for Clock Condition
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='Clock Condition : ', anchor='nw').place(x=5, y=53, width=120, height=19)
        #--------------------
        # Create Label for Generating Internal Clock
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='Generating Internal Clock', anchor='nw').place(x=156, y=53, width=250, height=19)
        #--------------------
        # Create Label for Stop Condition
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='Stop Condition : ', anchor='nw').place(x=5, y=78, width=120, height=19)
        #--------------------
        # Create Label for Generating Stop
        #--------------------
        tk.Label(self.groupbox_sync_slave_sampling, text='Generating Stop', anchor='nw').place(x=156, y=78, width=250, 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 Analog I/O Set
        #--------------------
        self.button_ai_ao_setting = tk.Button(self.groupbox_sync, text='Analog I/O Set')
        self.button_ai_ao_setting.place(x=910, y=20, width=200, height=30)
        #--------------------
        # Create Button for Generating/Sampling Set
        #--------------------
        self.button_generating_sampling_setting = tk.Button(self.groupbox_sync, text='Generating/Sampling Set')
        self.button_generating_sampling_setting.place(x=910, y=70, width=200, height=30)
        #--------------------
        # Create Button for Sync Operation Set
        #--------------------
        self.button_sync_setting = tk.Button(self.groupbox_sync, text='Sync Operation Set')
        self.button_sync_setting.place(x=910, y=120, width=200, height=30)
        #--------------------
        # Create Button for Output Data Set
        #--------------------
        self.button_output_data_setting = tk.Button(self.groupbox_sync, text='Output Data Set...')
        self.button_output_data_setting.place(x=910, y=170, width=200, height=30)
        #--------------------
        # Create Button for Generating/Sampling Start
        #--------------------
        self.button_generating_sampling_start = tk.Button(self.groupbox_sync, text='Generating/Sampling Start')
        self.button_generating_sampling_start.place(x=910, y=220, width=200, height=30)
        #--------------------
        # 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=270, width=200, height=30)
        #--------------------
        # Create Button for Generating/Sampling Stop
        #--------------------
        self.button_generating_sampling_stop = tk.Button(self.groupbox_sync, text='Generating/Sampling Stop')
        self.button_generating_sampling_stop.place(x=910, y=320, width=200, height=30)
        #--------------------
        # 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=370, width=200, height=30)
        #----------------------------------------
        # Create LabelFrame for Exit process
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="Exit")
        self.groupbox_exit.place(x=15, y=578, width=1126, height=60)
        #--------------------
        # 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=30)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.root, text='Return Code : ', anchor='nw').place(x=35, y=660, 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=660, width=435, height=19)
        #--------------------
        # Create Button for Close
        #--------------------
        self.button_close = tk.Button(self.root, text='Close')
        self.button_close.place(x=927, y=665, width=200, height=30)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    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):
        #----------------------------------------
        # 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 [Analog I/O Set] button
        #----------------------------------------
        self.button_ai_ao_setting.configure(command=command_ai_ao_setting)
        #----------------------------------------
        # Set the callback function for the [Generating/Sampling Set] button
        #----------------------------------------
        self.button_generating_sampling_setting.configure(command=command_generating_sampling_setting)
        #----------------------------------------
        # Set the callback function for the [Sync Operation Set] button
        #----------------------------------------
        self.button_sync_setting.configure(command=command_sync_setting)
        #----------------------------------------
        # Set the callback function for the [Output Data Set] button
        #----------------------------------------
        self.button_output_data_setting.configure(command=command_output_data_setting)
        #----------------------------------------
        # Set the callback function for the [Generating/Sampling Start] button
        #----------------------------------------
        self.button_generating_sampling_start.configure(command=command_generating_sampling_start)
        #----------------------------------------
        # Set the callback function for the [Get Status] button
        #----------------------------------------
        self.button_get_status.configure(command=command_get_status)
        #----------------------------------------
        # Set the callback function for the [Generating/Sampling Stop] button
        #----------------------------------------
        self.button_generating_sampling_stop.configure(command=command_generating_sampling_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()