﻿#================================================================
#================================================================
# API-DIO(WDM) (BusMaster)
# Infinite Sampling Sample
# main UI file
#                                                 CONTEC Co.,Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import messagebox


#========================================
# InfiniteUI class
#========================================
class InfiniteUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Infinite Sampling')
        self.root.minsize(505,290)
        #----------------------------------------
        # 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):
        #----------------------------------------
        # Initialize
        #----------------------------------------
        #--------------------
        # Create Label for Device Name
        #--------------------
        tk.Label(self.root, text='Device Name : ', anchor='nw').place(x=20, y=20, width=100, height=19)
        #--------------------
        # Create Entry for Device Name
        #--------------------
        self.textbox_device_name_var = tk.StringVar(self.root)
        self.textbox_device_name_var.set('DIO000')
        tk.Entry(self.root, textvariable=self.textbox_device_name_var).place(x=125, y=20, width=100, height=19)
        #--------------------
        # Create Button for DioInit
        #--------------------
        self.button_dio_init = tk.Button(self.root, text='DioInit')
        self.button_dio_init.place(x=375, y=20, width=110, height=19)
        #----------------------------------------
        # Transfer
        #----------------------------------------
        #--------------------
        # Create Button for Set Conditions
        #--------------------
        self.button_condition = tk.Button(self.root, text='Set Conditions...')
        self.button_condition.place(x=375, y=47, width=110, height=19)
        #--------------------
        # Create Button for Start Transfer
        #--------------------
        self.button_start = tk.Button(self.root, text='Start Transfer')
        self.button_start.place(x=375, y=74, width=110, height=19)
        #--------------------
        # Create Button for Stop Transfer
        #--------------------
        self.button_stop = tk.Button(self.root, text='Stop Transfer')
        self.button_stop.place(x=375, y=101, width=110, height=19)
        #--------------------
        # Create Button for Get Data
        #--------------------
        self.button_data = tk.Button(self.root, text='Get Data...')
        self.button_data.place(x=375, y=128, width=110, height=19)
        #--------------------
        # Create Label for Transfer Count
        #--------------------
        tk.Label(self.root, text='Transfer Count : ', anchor='nw').place(x=20, y=155, width=100, height=19)
        #--------------------
        # Create Entry for Transfer Count
        #--------------------
        self.textbox_trans_count_var = tk.StringVar(self.root)
        tk.Entry(self.root, textvariable=self.textbox_trans_count_var, justify=tk.RIGHT).place(x=125, y=155, width=230, height=19)
        #--------------------
        # Create Button for Transfer Count
        #--------------------
        self.button_count = tk.Button(self.root, text='Transfer Count')
        self.button_count.place(x=375, y=155, width=110, height=19)
        #--------------------
        # Create Label for Transfer Status
        #--------------------
        tk.Label(self.root, text='Status : ', anchor='nw').place(x=20, y=182, width=100, height=19)
        #--------------------
        # Create Entry for Transfer Status
        #--------------------
        self.textbox_status_var = tk.StringVar(self.root)
        tk.Entry(self.root, textvariable=self.textbox_status_var).place(x=125, y=182, width=230, height=19)
        #--------------------
        # Create Button for Transfer Status
        #--------------------
        self.button_status = tk.Button(self.root, text='Transfer Status')
        self.button_status.place(x=375, y=182, width=110, height=19)
        #--------------------
        # Create Label for Error
        #--------------------
        tk.Label(self.root, text='Error : ', anchor='nw').place(x=20, y=209, width=100, height=19)
        #--------------------
        # Create Entry for Error
        #--------------------
        self.textbox_error_var = tk.StringVar(self.root)
        tk.Entry(self.root, textvariable=self.textbox_error_var).place(x=125, y=209, width=230, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Button for DioExit
        #--------------------
        self.button_dio_exit = tk.Button(self.root, text='DioExit')
        self.button_dio_exit.place(x=375, y=209, width=110, height=19)
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.root, text='Return Code : ', anchor='nw').place(x=20, y=236, width=100, 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=125, y=236, width=230, height=19)
        #--------------------
        # Create Button for Exit
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=375, y=236, width=110, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_dio_init,
                       command_condition,
                       command_start,
                       command_stop,
                       command_data,
                       command_count,
                       command_status,
                       command_dio_exit,
                       command_exit):
        #----------------------------------------
        # Set the callback function for the [DioInit] button
        #----------------------------------------
        self.button_dio_init.configure(command=command_dio_init)
        #----------------------------------------
        # Set the callback function for the [Set Conditions...] button
        #----------------------------------------
        self.button_condition.configure(command=command_condition)
        #----------------------------------------
        # Set the callback function for the [Start Transfer] button
        #----------------------------------------
        self.button_start.configure(command=command_start)
        #----------------------------------------
        # Set the callback function for the [Stop Transfer] button
        #----------------------------------------
        self.button_stop.configure(command=command_stop)
        #----------------------------------------
        # Set the callback function for the [Get Data...] button
        #----------------------------------------
        self.button_data.configure(command=command_data)
        #----------------------------------------
        # Set the callback function for the [Transfer Count] button
        #----------------------------------------
        self.button_count.configure(command=command_count)
        #----------------------------------------
        # Set the callback function for the [Transfer Status] button
        #----------------------------------------
        self.button_status.configure(command=command_status)
        #----------------------------------------
        # Set the callback function for the [DioExit] button
        #----------------------------------------
        self.button_dio_exit.configure(command=command_dio_exit)
        #----------------------------------------
        # Set the callback function for the [Exit] button
        #----------------------------------------
        self.button_exit.configure(command=command_exit)
 
    #========================================
    # 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()