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


#========================================
# CounterUI class
#========================================
class CounterUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Counter')
        self.root.minsize(630,465)
        #----------------------------------------
        # 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=600, height=75)
        #--------------------
        # Create Label for Device Name
        #--------------------
        tk.Label(self.groupbox_init, text='Device Name : ', anchor='nw').place(x=15, y=5, width=90, 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=150, y=5, width=100, height=19)
        #--------------------
        # Create Button for AioInit
        #--------------------
        self.button_aio_init = tk.Button(self.groupbox_init, text='AioInit')
        self.button_aio_init.place(x=470, y=5, width=120, 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=470, y=30, width=120, height=19)
        #----------------------------------------
        # Create LabelFrame for Counter Set
        #----------------------------------------
        self.groupbox_counter_settings = tk.LabelFrame(self.root, text="Counter Set")
        self.groupbox_counter_settings.place(x=15, y=90, width=600, height=50)
        #--------------------
        # Create Label for ChannelNo
        #--------------------
        tk.Label(self.groupbox_counter_settings, text='ChannelNo : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # Create Entry for ChannelNo
        #--------------------
        self.textbox_channel_no_var = tk.StringVar(self.groupbox_counter_settings)
        self.textbox_channel_no_var.set("0")
        self.textbox_channel_no = tk.Entry(self.groupbox_counter_settings, textvariable=self.textbox_channel_no_var, justify=tk.RIGHT)
        self.textbox_channel_no.place(x=150, y=5, width=100, height=19)
        #--------------------
        # Create Button for Set
        #--------------------
        self.button_setting = tk.Button(self.groupbox_counter_settings, text='Set...')
        self.button_setting.place(x=470, y=5, width=120, height=19)
        #----------------------------------------
        # Create LabelFrame for Counter Preset
        #----------------------------------------
        self.groupbox_counter_preset = tk.LabelFrame(self.root, text="Counter Preset")
        self.groupbox_counter_preset.place(x=15, y=145, width=600, height=50)
        #--------------------
        # Create Label for Preset Data
        #--------------------
        tk.Label(self.groupbox_counter_preset, text='Preset Data : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Preset Data
        #--------------------
        self.textbox_preset_data_var = tk.StringVar(self.groupbox_counter_preset)
        self.textbox_preset_data_var.set("0")
        tk.Entry(self.groupbox_counter_preset, textvariable=self.textbox_preset_data_var, justify=tk.RIGHT).place(x=150, y=5, width=100, height=19)
        #--------------------
        # Create Button for AioPresetCnt
        #--------------------
        self.button_aio_preset_cnt = tk.Button(self.groupbox_counter_preset, text='AioPresetCnt')
        self.button_aio_preset_cnt.place(x=470, y=5, width=120, height=19)
        #----------------------------------------
        # Create LabelFrame for Counter Operation
        #----------------------------------------
        self.groupbox_counter_operation = tk.LabelFrame(self.root, text="Counter Operation")
        self.groupbox_counter_operation.place(x=15, y=200, width=600, height=125)
        #--------------------
        # Create Label for Count Value
        #--------------------
        tk.Label(self.groupbox_counter_operation, text='Count Value : ', anchor='nw').place(x=15, y=20, width=90, height=19)
        #--------------------
        # Create Entry for Count Value
        #--------------------
        self.textbox_count_var = tk.StringVar(self.groupbox_counter_operation)
        self.textbox_count_var.set("0")
        tk.Entry(self.groupbox_counter_operation, textvariable=self.textbox_count_var, justify=tk.RIGHT).place(x=150, y=20, width=285, height=19)
        #--------------------
        # Create Label for Status
        #--------------------
        tk.Label(self.groupbox_counter_operation, text='Status : ', anchor='nw').place(x=15, y=45, width=90, height=19)
        #--------------------
        # Create Entry for Status
        #--------------------
        self.textbox_status_var = tk.StringVar(self.groupbox_counter_operation)
        tk.Entry(self.groupbox_counter_operation, textvariable=self.textbox_status_var).place(x=150, y=45, width=285, height=19)
        #--------------------
        # Create Label for Compare Match Count
        #--------------------
        tk.Label(self.groupbox_counter_operation, text='Compare Match Count : ', anchor='nw').place(x=15, y=70, width=130, height=19)
        #--------------------
        # Create Entry for Compare Match Count
        #--------------------
        self.textbox_event_count_var = tk.StringVar(self.groupbox_counter_operation)
        self.textbox_event_count_var.set("0")
        tk.Entry(self.groupbox_counter_operation, textvariable=self.textbox_event_count_var, justify=tk.RIGHT).place(x=150, y=70, width=285, height=19)
        #--------------------
        # Create Button for Count Start
        #--------------------
        self.button_count_start = tk.Button(self.groupbox_counter_operation, text='Count Start')
        self.button_count_start.place(x=470, y=5, width=120, height=19)
        #--------------------
        # Create Button for AioGetCntCount
        #--------------------
        self.button_aio_get_cnt_count = tk.Button(self.groupbox_counter_operation, text='AioGetCntCount')
        self.button_aio_get_cnt_count.place(x=470, y=30, width=120, height=19)
        #--------------------
        # Create Button for AioGetCntStatus
        #--------------------
        self.button_aio_get_cnt_status = tk.Button(self.groupbox_counter_operation, text='AioGetCntStatus')
        self.button_aio_get_cnt_status.place(x=470, y=55, width=120, height=19)
        #--------------------
        # Create Button for Count Stop
        #--------------------
        self.button_count_stop = tk.Button(self.groupbox_counter_operation, text='Count Stop')
        self.button_count_stop.place(x=470, y=80, width=120, height=19)
        #----------------------------------------
        # Create LabelFrame for Exit process
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="Exit")
        self.groupbox_exit.place(x=15, y=330, width=600, height=50)
        #--------------------
        # Create Button for AioExit
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=470, y=5, width=120, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.root, text='Return Code : ', anchor='nw').place(x=33, y=395, width=90, height=19)
        #--------------------
        # Create Entry for Return Code
        #--------------------
        self.textbox_return_string_var = tk.StringVar(self.root)
        tk.Entry(self.root, textvariable=self.textbox_return_string_var).place(x=167, y=395, width=285, height=19)
        #--------------------
        # Create Button for Close
        #--------------------
        self.button_close = tk.Button(self.root, text='Close')
        self.button_close.place(x=487, y=420, width=120, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_aio_init,
                       command_aio_reset_device,
                       command_channel_no,
                       command_settings,
                       command_aio_preset_cnt,
                       command_start,
                       command_aio_get_cnt_count,
                       command_aio_get_cnt_status,
                       command_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 [ChannelNo] entry
        #----------------------------------------
        self.textbox_channel_no.bind("<KeyRelease>", command_channel_no)
        #----------------------------------------
        # Set the callback function for the [Set] button
        #----------------------------------------
        self.button_setting.configure(command=command_settings)
        #----------------------------------------
        # Set the callback function for the [AioPresetCnt] button
        #----------------------------------------
        self.button_aio_preset_cnt.configure(command=command_aio_preset_cnt)
        #----------------------------------------
        # Set the callback function for the [Count Start] button
        #----------------------------------------
        self.button_count_start.configure(command=command_start)
        #----------------------------------------
        # Set the callback function for the [AioGetCntCount] button
        #----------------------------------------
        self.button_aio_get_cnt_count.configure(command=command_aio_get_cnt_count)
        #----------------------------------------
        # Set the callback function for the [AioGetCntStatus] button
        #----------------------------------------
        self.button_aio_get_cnt_status.configure(command=command_aio_get_cnt_status)
        #----------------------------------------
        # Set the callback function for the [Count Stop] button
        #----------------------------------------
        self.button_count_stop.configure(command=command_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()