#================================================================
#================================================================
# API-AIO(WDM)
# Infinite generating using device buffer sample
# main UI file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# AoGeneratingInfiniteDevUI class
#========================================
class AoGeneratingInfiniteDevUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title("Infinite Generating Using Device Buffer")
        self.root.minsize(790,630)
        #----------------------------------------
        # 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=760, height=75)
        #--------------------
        # Create Label for Device Name
        #--------------------
        tk.Label(self.groupbox_init, text='Device Name : ', anchor='nw').place(x=15, y=5, width=200, 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=230, y=5, width=150, height=19)
        #--------------------
        # Create Button for AioInit
        #--------------------
        self.button_aio_init = tk.Button(self.groupbox_init, text='AioInit')
        self.button_aio_init.place(x=580, y=5, width=170, 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=580, y=30, width=170, height=19)
        #----------------------------------------
        # Create LabelFrame for Analog Output Set
        #----------------------------------------
        self.groupbox_output_settings = tk.LabelFrame(self.root, text="Analog Output Set")
        self.groupbox_output_settings.place(x=15, y=90, width=760, height=50)
        #--------------------
        # Create Label for Range
        #--------------------
        tk.Label(self.groupbox_output_settings, text='Range : ', anchor='nw').place(x=15, y=5, width=200, height=19)
        #--------------------
        # Create Combobox for Range
        #--------------------
        self.combobox_range = ttk.Combobox(self.groupbox_output_settings)
        self.combobox_range.place(x=230, y=5, width=150, height=19)
        self.combobox_range['value'] = ('-10 - +10V', '-5 - +5V', '-2.5 - +2.5V', '-1.25 - +1.25V', '0 - +10V', '0 - +5V', '0 - +2.5V', '0 - +1.25V', '0 - 20mA', '4 - 20mA', '1 - +5V')
        self.combobox_range.current(0)
        self.combobox_range['state'] = 'readonly'
        #--------------------
        # Create Button for AioSetAoRangeAll
        #--------------------
        self.button_aio_set_ao_range_all = tk.Button(self.groupbox_output_settings, text='AioSetAoRangeAll')
        self.button_aio_set_ao_range_all.place(x=580, y=5, width=170, height=19)
        #----------------------------------------
        # Create LabelFrame for Generating Set
        #----------------------------------------
        self.groupbox_generating_settings = tk.LabelFrame(self.root, text="Generating Set")
        self.groupbox_generating_settings.place(x=15, y=145, width=760, height=75)
        #--------------------
        # Create Label for Number of Channels
        #--------------------
        tk.Label(self.groupbox_generating_settings, text='Number of Channels : ', anchor='nw').place(x=15, y=5, width=200, height=19)
        #--------------------
        # Create Entry for Number of Channels
        #--------------------
        self.textbox_channels_num_var = tk.StringVar(self.groupbox_generating_settings)
        self.textbox_channels_num_var.set('1')
        tk.Entry(self.groupbox_generating_settings, justify=tk.RIGHT, textvariable=self.textbox_channels_num_var).place(x=230, y=5, width=150, height=19)
        #--------------------
        # Create Button for AioSetAoChannels
        #--------------------
        self.button_aio_set_ao_channels = tk.Button(self.groupbox_generating_settings, text='AioSetAoChannels')
        self.button_aio_set_ao_channels.place(x=580, y=5, width=170, height=19)
        #--------------------
        # Create Button for Condition Set
        #--------------------
        self.button_condition_settings = tk.Button(self.groupbox_generating_settings, text='Condition Set...')
        self.button_condition_settings.place(x=580, y=30, width=170, height=19)
        #----------------------------------------
        # Create LabelFrame for Output Data Set
        #----------------------------------------
        self.groupbox_output_data_settings = tk.LabelFrame(self.root, text="Output Data Set")
        self.groupbox_output_data_settings.place(x=15, y=225, width=760, height=50)
        #--------------------
        # Create Label for Generating Times
        #--------------------
        tk.Label(self.groupbox_output_data_settings, text='Generating Times : ', anchor='nw').place(x=15, y=5, width=200, height=19)
        #--------------------
        # Create Entry for Generating Times
        #--------------------
        self.textbox_generating_times_var = tk.StringVar(self.groupbox_output_data_settings)
        self.textbox_generating_times_var.set("1000")
        tk.Entry(self.groupbox_output_data_settings, justify=tk.RIGHT, textvariable=self.textbox_generating_times_var).place(x=230, y=5, width=150, height=19)
        #--------------------
        # Create Button for Generate Output Data
        #--------------------
        self.button_output_data_generate = tk.Button(self.groupbox_output_data_settings, text='Generate Output Data...')
        self.button_output_data_generate.place(x=580, y=5, width=170, height=19)
        #----------------------------------------
        # Create LabelFrame for Event that the specified number of data are output Set
        #----------------------------------------
        self.groupbox_event_settings = tk.LabelFrame(self.root, text="Event that the specified number of data are output Set")
        self.groupbox_event_settings.place(x=15, y=280, width=760, height=50)
        #--------------------
        # Create Label for Generating Times
        #--------------------
        tk.Label(self.groupbox_event_settings, text='Generating Times : ', anchor='nw').place(x=15, y=5, width=200, height=19)
        #--------------------
        # Create Entry for Generating Times
        #--------------------
        self.textbox_event_generating_times_var = tk.StringVar(self.groupbox_event_settings)
        self.textbox_event_generating_times_var.set("500")
        tk.Entry(self.groupbox_event_settings, justify=tk.RIGHT, textvariable=self.textbox_event_generating_times_var).place(x=230, y=5, width=150, height=19)
        #--------------------
        # Create Button for AioSetAoEventSamplingTimes
        #--------------------
        self.button_aio_set_ao_event_sampling_times = tk.Button(self.groupbox_event_settings, text='AioSetAoEventSamplingTimes')
        self.button_aio_set_ao_event_sampling_times.place(x=580, y=5, width=170, height=19)
        #----------------------------------------
        # Create LabelFrame for Generating Operation
        #----------------------------------------
        self.groupbox_generating_action = tk.LabelFrame(self.root, text="Generating Operation")
        self.groupbox_generating_action.place(x=15, y=335, width=760, height=150)
        #--------------------
        # Create Label for Total Generating Count
        #--------------------
        tk.Label(self.groupbox_generating_action, text='Total Generating Count : ', anchor='nw').place(x=15, y=30, width=200, height=19)
        #--------------------
        # Create Entry for Total Generating Count
        #--------------------
        self.textbox_generating_count_var = tk.StringVar(self.groupbox_generating_action)
        self.textbox_generating_count_var.set('')
        tk.Entry(self.groupbox_generating_action, justify=tk.RIGHT, textvariable=self.textbox_generating_count_var).place(x=230, y=30, width=330, height=19)
        #--------------------
        # Create Label for Total Setting Generating Count
        #--------------------
        tk.Label(self.groupbox_generating_action, text='Setting Generating Count : ', anchor='nw').place(x=15, y=55, width=200, height=19)
        #--------------------
        # Create Entry for Setting Generating Count
        #--------------------
        self.textbox_setting_generating_count_var = tk.StringVar(self.groupbox_generating_action)
        self.textbox_setting_generating_count_var.set('')
        tk.Entry(self.groupbox_generating_action, justify=tk.RIGHT, textvariable=self.textbox_setting_generating_count_var).place(x=230, y=55, width=330, height=19)
        #--------------------
        # Create Label for Status 
        #--------------------
        tk.Label(self.groupbox_generating_action, text='Status : ', anchor='nw').place(x=15, y=80, width=200, height=19)
        #--------------------
        # Create Entry for Status
        #--------------------
        self.textbox_status_var = tk.StringVar(self.groupbox_generating_action)
        self.textbox_status_var.set('')
        tk.Entry(self.groupbox_generating_action, textvariable=self.textbox_status_var).place(x=230, y=80, width=330, height=19)
        #--------------------
        # Create Button for Generating Start
        #--------------------
        self.button_generating_start = tk.Button(self.groupbox_generating_action, text='Generating Start')
        self.button_generating_start.place(x=580, y=5, width=170, height=19)
        #--------------------
        # Create Button for AioGetAoSamplingCount
        #--------------------
        self.button_aio_get_ao_sampling_count = tk.Button(self.groupbox_generating_action, text='AioGetAoSamplingCount')
        self.button_aio_get_ao_sampling_count.place(x=580, y=30, width=170, height=19)
        #--------------------
        # Create Button for AioGetAoSamplingTimes
        #--------------------
        self.button_aio_get_ao_sampling_times = tk.Button(self.groupbox_generating_action, text='AioGetAoSamplingTimes')
        self.button_aio_get_ao_sampling_times.place(x=580, y=55, width=170, height=19)
        #--------------------
        # Create Button for AioGetAoStatus
        #--------------------
        self.button_aio_get_ao_status = tk.Button(self.groupbox_generating_action, text='AioGetAoStatus')
        self.button_aio_get_ao_status.place(x=580, y=80, width=170, height=19)
        #--------------------
        # Create Button for Generating Stop
        #--------------------
        self.button_generating_stop = tk.Button(self.groupbox_generating_action, text='Generating Stop')
        self.button_generating_stop.place(x=580, y=105, width=170, height=19)
        #----------------------------------------
        # Create LabelFrame for Exit process
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="Exit")
        self.groupbox_exit.place(x=15, y=490, width=760, height=50)
        #--------------------
        # Create Button for AioExit
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=580, y=5, width=170, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.root, text='Return Code : ', anchor='nw').place(x=33, y=555, width=200, height=19)
        #--------------------
        # Create Entry for Return Code
        #--------------------
        self.textbox_return_string_var = tk.StringVar(self.root)
        self.textbox_return_string_var.set('')
        tk.Entry(self.root, textvariable=self.textbox_return_string_var).place(x=247, y=555, width=330, height=19)
        #--------------------
        # Create Button for Close
        #--------------------
        self.button_close = tk.Button(self.root, text='Close')
        self.button_close.place(x=597, y=580, width=170, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_aio_init,
                       command_aio_reset_device,
                       command_aio_set_ao_range_all,
                       command_aio_set_ao_channels,
                       command_condition_settings,
                       command_output_data_generate,
                       command_aio_set_ao_event_sampling_times,
                       command_generating_start,
                       command_aio_get_ao_sampling_count,
                       command_aio_get_ao_sampling_times,
                       command_aio_get_ao_status,
                       command_generating_stop,
                       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 [AioSetAoRangeAll] button
        #----------------------------------------
        self.button_aio_set_ao_range_all.configure(command=command_aio_set_ao_range_all)
        #----------------------------------------
        # Set the callback function for the [AioSetAoChannels] button
        #----------------------------------------
        self.button_aio_set_ao_channels.configure(command=command_aio_set_ao_channels)
        #----------------------------------------
        # Set the callback function for the [Condition Set] button
        #----------------------------------------
        self.button_condition_settings.configure(command=command_condition_settings)
        #----------------------------------------
        # Set the callback function for the [Generate Output Data] button
        #----------------------------------------
        self.button_output_data_generate.configure(command=command_output_data_generate)
        #----------------------------------------
        # Set the callback function for the [AioSetAoEventSamplingTimes] button
        #----------------------------------------
        self.button_aio_set_ao_event_sampling_times.configure(command=command_aio_set_ao_event_sampling_times)
        #----------------------------------------
        # Set the callback function for the [Generating Start] button
        #----------------------------------------
        self.button_generating_start.configure(command=command_generating_start)
        #----------------------------------------
        # Set the callback function for the [AioGetAoSamplingCount] button
        #----------------------------------------
        self.button_aio_get_ao_sampling_count.configure(command=command_aio_get_ao_sampling_count)
        #----------------------------------------
        # Set the callback function for the [AioGetAoSamplingTimes] button
        #----------------------------------------
        self.button_aio_get_ao_sampling_times.configure(command=command_aio_get_ao_sampling_times)
        #----------------------------------------
        # Set the callback function for the [AioGetAoStatus] button
        #----------------------------------------
        self.button_aio_get_ao_status.configure(command=command_aio_get_ao_status)
        #----------------------------------------
        # Set the callback function for the [Generating Stop] button
        #----------------------------------------
        self.button_generating_stop.configure(command=command_generating_stop)
        #----------------------------------------
        # 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()