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


#========================================
# AiSamplingInfiniteDevUI class
#========================================
class AiSamplingInfiniteDevUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title("Infinite Sampling Using Device Buffer")
        self.root.minsize(740,540)
        #----------------------------------------
        # 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=180, 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=530, 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=530, y=30, width=170, height=19)
        #----------------------------------------
        # Create LabelFrame for Parameter Set
        #----------------------------------------
        self.groupbox_parameter_settings = tk.LabelFrame(self.root, text="Parameter Set")
        self.groupbox_parameter_settings.place(x=15, y=90, width=710, height=125)
        #--------------------
        # Create Label for Range
        #--------------------
        tk.Label(self.groupbox_parameter_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_parameter_settings)
        self.combobox_range.place(x=180, 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_parameter_settings, text='AioSetAiRangeAll')
        self.button_aio_set_ai_range_all.place(x=530, y=5, width=170, height=19)
        #--------------------
        # Create Label for Input Method
        #--------------------
        tk.Label(self.groupbox_parameter_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_parameter_settings)
        self.combobox_input_method.place(x=180, 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_parameter_settings, text='AioSetAiInputMethod')
        self.button_aio_set_ai_input_method.place(x=530, y=30, width=170, height=19)
        #--------------------
        # Create Label for Number of Channels
        #--------------------
        tk.Label(self.groupbox_parameter_settings, text='Number of Channels : ', anchor='nw').place(x=15, y=55, width=120, height=19)
        #--------------------
        # Create Entry for Number of Channels
        #--------------------
        self.textbox_channels_var = tk.StringVar(self.groupbox_parameter_settings)
        self.textbox_channels_var.set('1')
        tk.Entry(self.groupbox_parameter_settings, justify=tk.RIGHT, textvariable=self.textbox_channels_var).place(x=180, y=55, width=140, height=19)
        #--------------------
        # Create Button for AioSetAiChannels
        #--------------------
        self.button_aio_set_ai_channels = tk.Button(self.groupbox_parameter_settings, text='AioSetAiChannels')
        self.button_aio_set_ai_channels.place(x=530, y=55, width=170, height=19)
        #--------------------
        # Create Button for Condition Set
        #--------------------
        self.button_condition_settings = tk.Button(self.groupbox_parameter_settings, text='Condition Set...')
        self.button_condition_settings.place(x=530, y=80, width=170, height=19)
        #----------------------------------------
        # Create LabelFrame for Event that the specified number of data are stored Set
        #----------------------------------------
        self.groupbox_sampling_settings = tk.LabelFrame(self.root, text="Event that the specified number of data are stored Set")
        self.groupbox_sampling_settings.place(x=15, y=220, width=710, height=50)
        #--------------------
        # Create Label for Sampling Times
        #--------------------
        tk.Label(self.groupbox_sampling_settings, text='Sampling Times : ', anchor='nw').place(x=15, y=5, width=160, height=19)
        #--------------------
        # Create Entry for Sampling Times
        #--------------------
        self.textbox_event_sampling_times_var = tk.StringVar(self.groupbox_sampling_settings)
        self.textbox_event_sampling_times_var.set('1000')
        tk.Entry(self.groupbox_sampling_settings, justify=tk.RIGHT, textvariable=self.textbox_event_sampling_times_var).place(x=180, y=5, width=140, height=19)
        #--------------------
        # Create Button for AioSetAiEventSamplingTimes
        #--------------------
        self.button_aio_set_ai_event_sampling_times = tk.Button(self.groupbox_sampling_settings, text='AioSetAiEventSamplingTimes')
        self.button_aio_set_ai_event_sampling_times.place(x=530, y=5, width=170, 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=275, width=710, height=125)
        #--------------------
        # Create Label for Total Sampling Count
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='Total Sampling Count : ', anchor='nw').place(x=15, y=20, width=160, height=19)
        #--------------------
        # Create Entry for Total Sampling Count
        #--------------------
        self.textbox_total_sampling_count_var = tk.StringVar(self.groupbox_sampling_action)
        self.textbox_total_sampling_count_var.set('')
        tk.Entry(self.groupbox_sampling_action, justify=tk.RIGHT, textvariable=self.textbox_total_sampling_count_var).place(x=180, y=20, width=330, height=19)
        #--------------------
        # Create Label for Sampling Count
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='Sampling Count : ', anchor='nw').place(x=15, y=45, width=160, height=19)
        #--------------------
        # Create Entry for Sampling Times
        #--------------------
        self.textbox_sampling_count_var = tk.StringVar(self.groupbox_sampling_action)
        self.textbox_sampling_count_var.set('')
        tk.Entry(self.groupbox_sampling_action, justify=tk.RIGHT, textvariable=self.textbox_sampling_count_var).place(x=180, y=45, width=330, height=19)
        #--------------------
        # Create Label for Status
        #--------------------
        tk.Label(self.groupbox_sampling_action, text='Status : ', anchor='nw').place(x=15, y=70, 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=180, y=70, 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=530, y=5, width=170, height=19)
        #--------------------
        # Create Button for AioGetAiSamplingCount
        #--------------------
        self.button_aio_get_ai_sampling_count = tk.Button(self.groupbox_sampling_action, text='AioGetAiSamplingCount')
        self.button_aio_get_ai_sampling_count.place(x=530, y=30, width=170, 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=530, y=55, width=170, 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=530, 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=405, 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=530, 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=470, 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=198, y=470, width=330, height=19)
        #--------------------
        # Create Button for Close
        #--------------------
        self.button_close = tk.Button(self.root, text='Close')
        self.button_close.place(x=547, y=495, 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_ai_range_all,
                       command_aio_set_ai_input_method,
                       command_aio_set_ai_channels,
                       command_condition_settings,
                       command_aio_set_ai_event_sampling_times,
                       command_sampling_start,
                       command_aio_get_ai_sampling_count,
                       command_aio_get_ai_status,
                       command_sampling_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 [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 [Condition Set] button
        #----------------------------------------
        self.button_condition_settings.configure(command=command_condition_settings)
        #----------------------------------------
        # Set the callback function for the [AioSetAiEventSamplingTimes] button
        #----------------------------------------
        self.button_aio_set_ai_event_sampling_times.configure(command=command_aio_set_ai_event_sampling_times)
        #----------------------------------------
        # Set the callback function for the [Sampling Start] button
        #----------------------------------------
        self.button_sampling_start.configure(command=command_sampling_start)
        #----------------------------------------
        # Set the callback function for the [AioGetAiSamplingCount] button
        #----------------------------------------
        self.button_aio_get_ai_sampling_count.configure(command=command_aio_get_ai_sampling_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 [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()