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


#========================================
# AoGeneratingOnceUserUI class
#========================================
class AoGeneratingOnceUserUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Finite Generating Using User Buffer')
        self.root.minsize(790,570)
        #----------------------------------------
        # 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=100)
        #--------------------
        # 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 Label for Generating Clock Period
        #--------------------
        tk.Label(self.groupbox_generating_settings, text='Generating Clock Period : ', anchor='nw').place(x=15, y=30, width=200, height=19)
        #--------------------
        # Create Label for Generating Clock Period Unit
        #--------------------
        tk.Label(self.groupbox_generating_settings, text='[usec]', anchor='nw').place(x=385, y=30, width=200, height=19)
        #--------------------
        # Create Entry for Generating Clock Period
        #--------------------
        self.textbox_generating_clock_var = tk.StringVar(self.groupbox_generating_settings)
        self.textbox_generating_clock_var.set('1000')
        tk.Entry(self.groupbox_generating_settings, justify=tk.RIGHT, textvariable=self.textbox_generating_clock_var).place(x=230, y=30, width=150, height=19)
        #--------------------
        # Create Button for AioSetAoSamplingClock
        #--------------------
        self.button_aio_set_ao_sampling_clock = tk.Button(self.groupbox_generating_settings, text='AioSetAoSamplingClock')
        self.button_aio_set_ao_sampling_clock.place(x=580, y=30, width=170, height=19)
        #--------------------
        # Create Button for Basic Settings
        #--------------------
        self.button_basic_settings = tk.Button(self.groupbox_generating_settings, text='Basic Settings')
        self.button_basic_settings.place(x=580, y=55, 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=250, 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 User Buffer Set
        #--------------------
        self.button_user_buffer_size_settings = tk.Button(self.groupbox_output_data_settings, text='User Buffer Set...')
        self.button_user_buffer_size_settings.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=305, width=760, height=125)
        #--------------------
        # Create Label for Transfer Position
        #--------------------
        tk.Label(self.groupbox_generating_action, text='Transfer Position : ', anchor='nw').place(x=15, y=30, width=200, height=19)
        #--------------------
        # Create Entry for Transfer Position
        #--------------------
        self.textbox_transfer_count_var = tk.StringVar(self.groupbox_generating_action)
        self.textbox_transfer_count_var.set('')
        tk.Entry(self.groupbox_generating_action, justify=tk.RIGHT, textvariable=self.textbox_transfer_count_var).place(x=230, y=30, width=330, height=19)
        #--------------------
        # Create Label for Status 
        #--------------------
        tk.Label(self.groupbox_generating_action, text='Status : ', anchor='nw').place(x=15, y=55, 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=55, 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 AioGetAoTransferCount
        #--------------------
        self.button_aio_get_ao_transfer_count = tk.Button(self.groupbox_generating_action, text='AioGetAoTransferCount')
        self.button_aio_get_ao_transfer_count.place(x=580, y=30, 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=55, 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=80, 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=435, 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=500, 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=500, width=330, height=19)
        #--------------------
        # Create Button for Close
        #--------------------
        self.button_close = tk.Button(self.root, text='Close')
        self.button_close.place(x=597, y=525, 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_aio_set_ao_sampling_clock,
                       command_basic_settings,
                       command_user_buffer_size_settings,
                       command_generating_start,
                       command_aio_get_ao_transfer_count,
                       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 [AioSetAoSamplingClock] button
        #----------------------------------------
        self.button_aio_set_ao_sampling_clock.configure(command=command_aio_set_ao_sampling_clock)
        #----------------------------------------
        # Set the callback function for the [Basic Settings] button
        #----------------------------------------
        self.button_basic_settings.configure(command=command_basic_settings)
        #----------------------------------------
        # Set the callback function for the [User Buffer Set] button
        #----------------------------------------
        self.button_user_buffer_size_settings.configure(command=command_user_buffer_size_settings)
        #----------------------------------------
        # Set the callback function for the [Generating Start] button
        #----------------------------------------
        self.button_generating_start.configure(command=command_generating_start)
        #----------------------------------------
        # Set the callback function for the [AioGetAoTransferCount] button
        #----------------------------------------
        self.button_aio_get_ao_transfer_count.configure(command=command_aio_get_ao_transfer_count)
        #----------------------------------------
        # 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()