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


#========================================
# AiSamplingOnceUserUI class
#========================================
class AiSamplingOnceUserUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Finite Sampling Using User Buffer')
        self.root.minsize(740,790)
        #----------------------------------------
        # 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=710, height=75)
        #--------------------
        # Create Label for Device Name
        #--------------------
        tk.Label(self.groupbox_init, text='Device Name : ', anchor='nw').place(x=15, y=5, width=80, 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=160, y=5, 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=510, y=5, width=190, 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=510, y=30, width=190, height=19)
        #----------------------------------------
        # Create LabelFrame for Analog Input Set
        #----------------------------------------
        self.groupbox_input_settings = tk.LabelFrame(self.root, text="Analog Input Set")
        self.groupbox_input_settings.place(x=15, y=90, width=710, height=75)
        #--------------------
        # Create Label for Range
        #--------------------
        tk.Label(self.groupbox_input_settings, text='Range : ', anchor='nw').place(x=15, y=5, width=75, height=19)
        #--------------------
        # Create Combobox for Range
        #--------------------
        self.combobox_range = ttk.Combobox(self.groupbox_input_settings)
        self.combobox_range.place(x=160, y=5, width=140, 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', '-20 - +20mA', '1 - +5V')
        self.combobox_range.current(0)
        self.combobox_range['state'] = 'readonly'
        #--------------------
        # Create Button for AioSetAiRangeAll
        #--------------------
        self.button_aio_set_ai_range_all = tk.Button(self.groupbox_input_settings, text='AioSetAiRangeAll')
        self.button_aio_set_ai_range_all.place(x=510, y=5, width=190, height=19)
        #--------------------
        # Create Label for Input Method
        #-------------------
        tk.Label(self.groupbox_input_settings, text='Input Method : ', anchor='nw').place(x=15, y=30, width=85, height=19)
        #--------------------
        # Create Combobox for Input Method
        #--------------------
        self.combobox_input_method = ttk.Combobox(self.groupbox_input_settings)
        self.combobox_input_method.place(x=160, y=30, width=140, height=19)
        self.combobox_input_method['value'] = ('Single-end input', 'Differential input')
        self.combobox_input_method.current(0)
        self.combobox_input_method['state'] = 'readonly'
        #--------------------
        # Create Button for AioSetAiInputMethod
        #--------------------
        self.button_aio_set_ai_input_method = tk.Button(self.groupbox_input_settings, text='AioSetAiInputMethod')
        self.button_aio_set_ai_input_method.place(x=510, y=30, width=190, height=19)
        #----------------------------------------
        # Create LabelFrame for Sampling Set
        #----------------------------------------
        self.groupbox_sampling_settings = tk.LabelFrame(self.root, text="Sampling Set")
        self.groupbox_sampling_settings.place(x=15, y=170, width=710, height=150)
        #--------------------
        # Create Label for Number of Channels
        #--------------------
        tk.Label(self.groupbox_sampling_settings, text='Number of Channels : ', anchor='nw').place(x=15, y=5, width=120, height=19)
        #--------------------
        # Create Entry for Number of Channels
        #--------------------
        self.textbox_channels_var = tk.StringVar(self.groupbox_sampling_settings)
        self.textbox_channels_var.set('1')
        tk.Entry(self.groupbox_sampling_settings, justify=tk.RIGHT, textvariable=self.textbox_channels_var).place(x=160, y=5, width=140, height=19)
        #--------------------
        # Create Button for AioSetAiChannels
        #--------------------
        self.button_aio_set_ai_channels = tk.Button(self.groupbox_sampling_settings, text='AioSetAiChannels')
        self.button_aio_set_ai_channels.place(x=510, y=5, width=190, height=19)
        #--------------------
        # Create Label for Sampling Clock Period
        #--------------------
        tk.Label(self.groupbox_sampling_settings, text='Sampling Clock Period : ', anchor='nw').place(x=15, y=30, width=160, height=19)
        #--------------------
        # Create Label for Sampling Clock Period Unit
        #--------------------
        tk.Label(self.groupbox_sampling_settings, text='[usec]', anchor='nw').place(x=305, y=30, width=50, height=19)
        #--------------------
        # Create Entry for Sampling Clock Period
        #--------------------
        self.textbox_sampling_clock_var = tk.StringVar(self.groupbox_sampling_settings)
        self.textbox_sampling_clock_var.set('1000')
        tk.Entry(self.groupbox_sampling_settings, justify=tk.RIGHT, textvariable=self.textbox_sampling_clock_var).place(x=160, y=30, width=140, height=19)
        #--------------------
        # Create Button for AioSetAiSamplingClock
        #--------------------
        self.button_aio_set_ai_sampling_clock = tk.Button(self.groupbox_sampling_settings, text='AioSetAiSamplingClock')
        self.button_aio_set_ai_sampling_clock.place(x=510, y=30, width=190, height=19)
        #--------------------
        # Create Label for Sampling Clock Period
        #--------------------
        tk.Label(self.groupbox_sampling_settings, text='Conversion Speed : ', anchor='nw').place(x=15, y=55, width=160, height=19)
        #--------------------
        # Create Label for Conversion Speed Unit
        #--------------------
        tk.Label(self.groupbox_sampling_settings, text='[usec]', anchor='nw').place(x=305, y=55, width=50, height=19)
        #--------------------
        # Create Entry for Conversion Speed
        #--------------------
        self.textbox_scan_clock_var = tk.StringVar(self.groupbox_sampling_settings)
        self.textbox_scan_clock_var.set('10')
        tk.Entry(self.groupbox_sampling_settings, justify=tk.RIGHT, textvariable=self.textbox_scan_clock_var).place(x=160, y=55, width=140, height=19)
        #--------------------
        # Create Button for AioSetAiScanClock
        #--------------------
        self.button_aio_set_ai_scan_clock = tk.Button(self.groupbox_sampling_settings, text='AioSetAiScanClock')
        self.button_aio_set_ai_scan_clock.place(x=510, y=55, width=190, height=19)
        #--------------------
        # Create Label for Stop Times
        #--------------------
        tk.Label(self.groupbox_sampling_settings, text='Stop Times : ', anchor='nw').place(x=15, y=80, width=160, height=19)
        #--------------------
        # Create Entry for Stop Times
        #--------------------
        self.textbox_stop_times_var = tk.StringVar(self.groupbox_sampling_settings)
        self.textbox_stop_times_var.set('1000')
        tk.Entry(self.groupbox_sampling_settings, justify=tk.RIGHT, textvariable=self.textbox_stop_times_var).place(x=160, y=80, width=140, height=19)
        #--------------------
        # Create Button for AioSetAiStopTimes
        #--------------------
        self.button_aio_set_ai_stop_times = tk.Button(self.groupbox_sampling_settings, text='AioSetAiStopTimes')
        self.button_aio_set_ai_stop_times.place(x=510, y=80, width=190, height=19)
        #--------------------
        # Create Button for Basic Settings
        #--------------------
        self.button_basic_settings = tk.Button(self.groupbox_sampling_settings, text='Basic Settings')
        self.button_basic_settings.place(x=510, y=105, width=190, height=19)
        #----------------------------------------
        # Create LabelFrame for User Buffer Set
        #----------------------------------------
        self.groupbox_user_buffer_settings = tk.LabelFrame(self.root, text="User Buffer Set")
        self.groupbox_user_buffer_settings.place(x=15, y=325, width=710, height=50)
        #--------------------
        # Create Label for Sampling Times
        #--------------------
        tk.Label(self.groupbox_user_buffer_settings, text='Sampling Times : ', anchor='nw').place(x=15, y=5, width=160, height=19)
        #--------------------
        # Create Entry for Sampling Times
        #--------------------
        self.textbox_sampling_times_var = tk.StringVar(self.groupbox_user_buffer_settings)
        self.textbox_sampling_times_var.set('2000')
        tk.Entry(self.groupbox_user_buffer_settings, justify=tk.RIGHT, textvariable=self.textbox_sampling_times_var).place(x=160, y=5, width=140, height=19)
        #--------------------
        # Create Button for User Buffer Size Set
        #--------------------
        self.button_user_buffer_size_settings = tk.Button(self.groupbox_user_buffer_settings, text='User Buffer Size Set')
        self.button_user_buffer_size_settings.place(x=510, y=5, width=190, height=19)
        #----------------------------------------
        # Create LabelFrame for Sampling Operation
        #----------------------------------------
        self.groupbox_sampling_action = tk.LabelFrame(self.root, text="Sampling Operation")
        self.groupbox_sampling_action.place(x=15, y=380, width=710, height=280)
        #--------------------
        # Create Label for Sampling Data
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='Sampling Data : ', anchor='nw').place(x=15, y=45, width=160, height=19)
        #--------------------
        # Create Label for Count
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='Count', anchor='nw').place(x=160, y=5, width=160, height=19)
        #--------------------
        # Create Label for Packet Data (Binary)
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='Packet Data (Binary)', anchor='nw').place(x=330, y=5, width=160, height=19)
        #--------------------
        # Create Label for (Packet No.)
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='(Packet No.)', anchor='nw').place(x=160, y=25, width=160, height=19)
        #--------------------
        # Create Label for Lower Data / Upper Data
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='Lower Data    Upper Data', anchor='nw').place(x=330, y=25, width=160, height=19)
        #--------------------
        # Create Listbox for Sampling Data
        #--------------------
        self.listbox_sampling_data_var = tk.StringVar(self.groupbox_sampling_action)
        self.listbox_sampling_data = tk.Listbox(self.groupbox_sampling_action, listvariable=self.listbox_sampling_data_var)
        self.listbox_y_scrollbar = tk.Scrollbar(self.groupbox_sampling_action, orient="vertical", command=self.listbox_sampling_data.yview)
        self.listbox_x_scrollbar = tk.Scrollbar(self.groupbox_sampling_action, orient="horizontal", command=self.listbox_sampling_data.xview)
        self.listbox_sampling_data.config(yscrollcommand=self.listbox_y_scrollbar.set)
        self.listbox_sampling_data.config(xscrollcommand=self.listbox_x_scrollbar.set)
        self.listbox_sampling_data.place(x=160, y=45, width=330, height=150)
        self.listbox_y_scrollbar.place(x=490, y=45, width=10, height=150)
        self.listbox_x_scrollbar.place(x=160, y=195, width=330, height=10)
        #--------------------
        # Create Label for Transfer Position
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='Transfer Position : ', anchor='nw').place(x=15, y=210, width=160, height=19)
        #--------------------
        # Create Entry for Transfer Position
        #--------------------
        self.textbox_transfer_count_var = tk.StringVar(self.groupbox_sampling_action)
        self.textbox_transfer_count_var.set('')
        tk.Entry(self.groupbox_sampling_action, justify=tk.RIGHT, textvariable=self.textbox_transfer_count_var).place(x=160, y=210, width=330, height=19)
        #--------------------
        # Create Label for Status
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='Status : ', anchor='nw').place(x=15, y=235, width=160, height=19)
        #--------------------
        # Create Entry for Status
        #--------------------
        self.textbox_status_var = tk.StringVar(self.groupbox_sampling_action)
        self.textbox_status_var.set('')
        tk.Entry(self.groupbox_sampling_action, textvariable=self.textbox_status_var).place(x=160, y=235, width=330, height=19)
        #--------------------
        # Create Button for Sampling Start
        #--------------------
        self.button_sampling_start = tk.Button(self.groupbox_sampling_action, text='Sampling Start')
        self.button_sampling_start.place(x=510, y=45, width=190, height=19)
        #--------------------
        # Create Button for AioGetAiTransferCount
        #--------------------
        self.button_aio_get_ai_transfer_count = tk.Button(self.groupbox_sampling_action, text='AioGetAiTransferCount')
        self.button_aio_get_ai_transfer_count.place(x=510, y=85, width=190, height=19)
        #--------------------
        # Create Button for AioGetAiStatus
        #--------------------
        self.button_aio_get_ai_status = tk.Button(self.groupbox_sampling_action, text='AioGetAiStatus')
        self.button_aio_get_ai_status.place(x=510, y=125, width=190, height=19)
        #--------------------
        # Create Button for Sampling Stop
        #--------------------
        self.button_sampling_stop = tk.Button(self.groupbox_sampling_action, text='Sampling Stop')
        self.button_sampling_stop.place(x=510, y=165, width=190, height=19)
        #--------------------
        # Create Button for Output Acquired Data to File
        #--------------------
        self.button_output_sampling_data = tk.Button(self.groupbox_sampling_action, text='Output Acquired Data to File...')
        self.button_output_sampling_data.place(x=510, y=205, width=190, height=19)
        #----------------------------------------
        # Create LabelFrame for Exit process
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="Exit")
        self.groupbox_exit.place(x=15, y=665, width=710, height=50)
        #--------------------
        # Create Button for AioExit
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=510, y=5, width=190, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.root, text='Return Code : ', anchor='nw').place(x=33, y=730, width=160, 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=178, y=730, width=330, height=19)
        #--------------------
        # Create Button for Close
        #--------------------
        self.button_close = tk.Button(self.root, text='Close')
        self.button_close.place(x=527, y=755, width=190, 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_ai_range_all,
                       command_aio_set_ai_input_method,
                       command_aio_set_ai_channels,
                       command_aio_set_ai_sampling_clock,
                       command_aio_set_ai_scan_clock,
                       command_aio_set_ai_stop_times,
                       command_basic_settings,
                       command_user_buffer_size_settings,
                       command_sampling_start,
                       command_aio_get_ai_transfer_count,
                       command_aio_get_ai_status,
                       command_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 [AioSetAiRangeAll] button
        #----------------------------------------
        self.button_aio_set_ai_range_all.configure(command=command_aio_set_ai_range_all)
        #----------------------------------------
        # Set the callback function for the [AioSetAiInputMethod] button
        #----------------------------------------
        self.button_aio_set_ai_input_method.configure(command=command_aio_set_ai_input_method)
        #----------------------------------------
        # Set the callback function for the [AioSetAiChannels] button
        #----------------------------------------
        self.button_aio_set_ai_channels.configure(command=command_aio_set_ai_channels)
        #----------------------------------------
        # Set the callback function for the [AioSetAiSamplingClock] button
        #----------------------------------------
        self.button_aio_set_ai_sampling_clock.configure(command=command_aio_set_ai_sampling_clock)
        #----------------------------------------
        # Set the callback function for the [AioSetAiScanClock] button
        #----------------------------------------
        self.button_aio_set_ai_scan_clock.configure(command=command_aio_set_ai_scan_clock)
        #----------------------------------------
        # Set the callback function for the [AioSetAiStopTimes] button
        #----------------------------------------
        self.button_aio_set_ai_stop_times.configure(command=command_aio_set_ai_stop_times)
        #----------------------------------------
        # 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 Size Set] button
        #----------------------------------------
        self.button_user_buffer_size_settings.configure(command=command_user_buffer_size_settings)
        #----------------------------------------
        # Set the callback function for the [Sampling Start] button
        #----------------------------------------
        self.button_sampling_start.configure(command=command_sampling_start)
        #----------------------------------------
        # Set the callback function for the [AioGetAiTransferCount] button
        #----------------------------------------
        self.button_aio_get_ai_transfer_count.configure(command=command_aio_get_ai_transfer_count)
        #----------------------------------------
        # Set the callback function for the [AioGetAiStatus] button
        #----------------------------------------
        self.button_aio_get_ai_status.configure(command=command_aio_get_ai_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 [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()