#================================================================
#================================================================
# API-AIO(WDM)
# Simple analog output sample
# main UI file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# AoSimpleUI class
#========================================
class AoSimpleUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Simple Analog Output')
        self.root.minsize(740,470)
        #----------------------------------------
        # 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=50)
        #--------------------
        # Create Label for Device Name
        #--------------------
        tk.Label(self.groupbox_init, text='Device Name : ', anchor='nw').place(x=15, y=5, width=85, 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=145, y=5, width=130, height=19)
        #--------------------
        # Create Button for AioInit
        #--------------------
        self.button_aio_init = tk.Button(self.groupbox_init, text='AioInit')
        self.button_aio_init.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # Create LabelFrame for Range Set
        #----------------------------------------
        self.groupbox_range_settings = tk.LabelFrame(self.root, text="Range Set")
        self.groupbox_range_settings.place(x=15, y=65, width=710, height=50)
        #--------------------
        # Create Label for Range
        #--------------------
        tk.Label(self.groupbox_range_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_range_settings)
        self.combobox_range.place(x=145, y=5, width=130, 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_range_settings, text='AioSetAoRangeAll')
        self.button_aio_set_ao_range_all.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # Create LabelFrame for Specify Channel Simple Output
        #----------------------------------------
        self.groupbox_single_ao_ex = tk.LabelFrame(self.root, text="Specify Channel Simple Output")
        self.groupbox_single_ao_ex.place(x=15, y=125, width=710, height=50)
        #--------------------
        # Create Label for ChannelNo
        #--------------------
        tk.Label(self.groupbox_single_ao_ex, text='ChannelNo : ', anchor='nw').place(x=15, y=5, width=85, height=19)
        #--------------------
        # Create Entry for ChannelNo
        #--------------------
        self.textbox_channel_no_var = tk.StringVar(self.groupbox_single_ao_ex)
        self.textbox_channel_no_var.set("0")
        tk.Entry(self.groupbox_single_ao_ex, justify=tk.RIGHT, textvariable=self.textbox_channel_no_var).place(x=145, y=5, width=130, height=19)
        #--------------------
        # Create Label for Output Data
        #--------------------
        tk.Label(self.groupbox_single_ao_ex, text='Output Data : ', anchor='nw').place(x=290, y=5, width=85, height=19)
        #--------------------
        # Create Entry for Output Data
        #--------------------
        self.textbox_single_ao_data_var = tk.StringVar(self.groupbox_single_ao_ex)
        self.textbox_single_ao_data_var.set("0.0")
        tk.Entry(self.groupbox_single_ao_ex, justify=tk.RIGHT, textvariable=self.textbox_single_ao_data_var).place(x=375, y=5, width=130, height=19)
        #--------------------
        # Create Button for AioSingleAoEx
        #--------------------
        self.button_aio_single_ao_ex = tk.Button(self.groupbox_single_ao_ex, text='AioSingleAoEx')
        self.button_aio_single_ao_ex.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # Create LabelFrame for Multi Channel Simple Output
        #----------------------------------------
        self.groupbox_multi_ao_ex = tk.LabelFrame(self.root, text="Multi Channel Simple Output")
        self.groupbox_multi_ao_ex.place(x=15, y=180, width=710, height=150)
        #--------------------
        # Create Label for Number of Channels
        #--------------------
        tk.Label(self.groupbox_multi_ao_ex, text='Number of Channels : ', anchor='nw').place(x=15, y=10, width=120, height=19)
        #--------------------
        # Create Entry for Number of Channels
        #--------------------
        self.textbox_channel_num_var = tk.StringVar(self.groupbox_multi_ao_ex)
        self.textbox_channel_num_var.set("1")
        tk.Entry(self.groupbox_multi_ao_ex, justify=tk.RIGHT, textvariable=self.textbox_channel_num_var).place(x=145, y=10, width=130, height=19)
        #--------------------
        # Create LabelFrame for Output Data
        #--------------------
        self.groupbox_multi_ao_ex_data = tk.LabelFrame(self.groupbox_multi_ao_ex, text="Output Data")
        self.groupbox_multi_ao_ex_data.place(x=285, y=0, width=240, height=125)
        #--------------------
        # Create Label for Output Data CH0
        #--------------------
        tk.Label(self.groupbox_multi_ao_ex_data, text='CH0 : ', anchor='nw').place(x=15, y=5, width=85, height=19)
        #--------------------
        # Create Label for Output Data CH1
        #--------------------
        tk.Label(self.groupbox_multi_ao_ex_data, text='CH1 : ', anchor='nw').place(x=15, y=30, width=85, height=19)
        #--------------------
        # Create Label for Output Data CH2
        #--------------------
        tk.Label(self.groupbox_multi_ao_ex_data, text='CH2 : ', anchor='nw').place(x=15, y=55, width=85, height=19)
        #--------------------
        # Create Label for Output Data CH3
        #--------------------
        tk.Label(self.groupbox_multi_ao_ex_data, text='CH3 : ', anchor='nw').place(x=15, y=80, width=85, height=19)
        #--------------------
        # Create Entry for Output Data CH0
        #--------------------
        self.textbox_multi_ao_ex_data1_var = tk.StringVar(self.groupbox_multi_ao_ex_data)
        self.textbox_multi_ao_ex_data1_var.set("0.0")
        tk.Entry(self.groupbox_multi_ao_ex_data, justify=tk.RIGHT, textvariable=self.textbox_multi_ao_ex_data1_var).place(x=88, y=5, width=130, height=19)
        #--------------------
        # Create Entry for Output Data CH1
        #--------------------
        self.textbox_multi_ao_ex_data2_var = tk.StringVar(self.groupbox_multi_ao_ex_data)
        self.textbox_multi_ao_ex_data2_var.set("0.0")
        tk.Entry(self.groupbox_multi_ao_ex_data, justify=tk.RIGHT, textvariable=self.textbox_multi_ao_ex_data2_var).place(x=88, y=30, width=130, height=19)
        #--------------------
        # Create Entry for Output Data CH2
        #--------------------
        self.textbox_multi_ao_ex_data3_var = tk.StringVar(self.groupbox_multi_ao_ex_data)
        self.textbox_multi_ao_ex_data3_var.set("0.0")
        tk.Entry(self.groupbox_multi_ao_ex_data, justify=tk.RIGHT, textvariable=self.textbox_multi_ao_ex_data3_var).place(x=88, y=55, width=130, height=19)
        #--------------------
        # Create Entry for Output Data CH3
        #--------------------
        self.textbox_multi_ao_ex_data4_var = tk.StringVar(self.groupbox_multi_ao_ex_data)
        self.textbox_multi_ao_ex_data4_var.set("0.0")
        tk.Entry(self.groupbox_multi_ao_ex_data, justify=tk.RIGHT, textvariable=self.textbox_multi_ao_ex_data4_var).place(x=88, y=80, width=130, height=19)
        #--------------------
        # Create Button for AioMultiAoEx
        #--------------------
        self.button_aio_multi_ao_ex = tk.Button(self.groupbox_multi_ao_ex, text='AioMultiAoEx')
        self.button_aio_multi_ao_ex.place(x=550, y=10, width=150, height=19)
        #----------------------------------------
        # Create LabelFrame for Exit process
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="Exit")
        self.groupbox_exit.place(x=15, y=335, 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=550, y=5, width=150, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.root, text='Return Code : ', anchor='nw').place(x=33, y=400, 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=162, y=400, width=330, height=19)
        #--------------------
        # Create Button for Close
        #--------------------
        self.button_close = tk.Button(self.root, text='Close')
        self.button_close.place(x=567, y=425, width=150, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_aio_init,
                       command_aio_set_ao_range_all,
                       command_aio_single_ao_ex,
                       command_aio_multi_ao_ex,
                       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 [AioSetAoRangeAll] button
        #----------------------------------------
        self.button_aio_set_ao_range_all.configure(command=command_aio_set_ao_range_all)
        #----------------------------------------
        # Set the callback function for the [AioSingleAoEx] button
        #----------------------------------------
        self.button_aio_single_ao_ex.configure(command=command_aio_single_ao_ex)
        #----------------------------------------
        # Set the callback function for the [AioMultiAoEx] button
        #----------------------------------------
        self.button_aio_multi_ao_ex.configure(command=command_aio_multi_ao_ex)
        #----------------------------------------
        # 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()