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


#========================================
# TimerUI class
#========================================
class TimerUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Timer')
        self.root.minsize(515,460)
        #----------------------------------------
        # 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=487, height=75)
        #--------------------
        # Create Label for Device Name
        #--------------------
        tk.Label(self.groupbox_init, text='Device Name : ', anchor='nw').place(x=10, 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=140, 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=350, 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=350, y=30, width=120, height=19)
        #----------------------------------------
        # Create LabelFrame for Interval Timer
        #----------------------------------------
        self.groupbox_interval = tk.LabelFrame(self.root, text="Interval Timer")
        self.groupbox_interval.place(x=15, y=90, width=487, height=100)
        #--------------------
        # Create Label for Interval
        #--------------------
        tk.Label(self.groupbox_interval, text='Interval : ', anchor='nw').place(x=10, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Interval
        #--------------------
        self.textbox_interval_var = tk.StringVar(self.groupbox_interval)
        self.textbox_interval_var.set("1000")
        tk.Entry(self.groupbox_interval, textvariable=self.textbox_interval_var, justify=tk.RIGHT).place(x=140, y=5, width=100, height=19)
        #--------------------
        # Create Label for Interval Unit
        #--------------------
        tk.Label(self.groupbox_interval, text='[usec]', anchor='nw').place(x=250, y=5, width=90, height=19)
        #--------------------
        # Create Label for Notification count
        #--------------------
        tk.Label(self.groupbox_interval, text='Notification count : ', anchor='nw').place(x=10, y=30, width=120, height=19)
        #--------------------
        # Create Entry for Notification count
        #--------------------
        self.textbox_event_count_var = tk.StringVar(self.groupbox_interval)
        self.textbox_event_count_var.set("0")
        tk.Entry(self.groupbox_interval, textvariable=self.textbox_event_count_var, justify=tk.RIGHT).place(x=140, y=30, width=100, height=19)
        #--------------------
        # Create Button for Operation Start
        #--------------------
        self.button_interval_start = tk.Button(self.groupbox_interval, text='Operation Start')
        self.button_interval_start.place(x=350, y=0, width=120, height=19)
        #--------------------
        # Create Button for Operation Stop
        #--------------------
        self.button_interval_stop = tk.Button(self.groupbox_interval, text='Operation Stop')
        self.button_interval_stop.place(x=350, y=55, width=120, height=19)
        #----------------------------------------
        # Create LabelFrame for Stop Watch Timer
        #----------------------------------------
        self.groupbox_stopwatch = tk.LabelFrame(self.root, text="Stop Watch Timer")
        self.groupbox_stopwatch.place(x=15, y=195, width=487, height=125)
        #--------------------
        # Create Label for Elapsed time
        #--------------------
        tk.Label(self.groupbox_stopwatch, text='Elapsed time : ', anchor='nw').place(x=10, y=30, width=90, height=19)
        #--------------------
        # Create Entry for Elapsed time
        #--------------------
        self.textbox_lap_time_var = tk.StringVar(self.groupbox_stopwatch)
        self.textbox_lap_time_var.set("0")
        tk.Entry(self.groupbox_stopwatch, textvariable=self.textbox_lap_time_var, justify=tk.RIGHT).place(x=140, y=30, width=100, height=19)
        #--------------------
        # Create Label for Elapsed time Unit
        #--------------------
        tk.Label(self.groupbox_stopwatch, text='[msec]', anchor='nw').place(x=250, y=30, width=90, height=19)
        #--------------------
        # Create Button for Operation Start
        #--------------------
        self.button_watch_start = tk.Button(self.groupbox_stopwatch, text='Operation Start')
        self.button_watch_start.place(x=350, y=5, width=120, height=19)
        #--------------------
        # Create Button for AioLapTmCount
        #--------------------
        self.button_aio_lap_tm_count = tk.Button(self.groupbox_stopwatch, text='AioLapTmCount')
        self.button_aio_lap_tm_count.place(x=350, y=30, width=120, height=19)
        #--------------------
        # Create Button for AioResetTmCount
        #--------------------
        self.button_aio_reset_tm_count = tk.Button(self.groupbox_stopwatch, text='AioResetTmCount')
        self.button_aio_reset_tm_count.place(x=350, y=55, width=120, height=19)
        #--------------------
        # Create Button for Operation Stop
        #--------------------
        self.button_watch_stop = tk.Button(self.groupbox_stopwatch, text='Operation Stop')
        self.button_watch_stop.place(x=350, 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=325, width=487, height=50)
        #--------------------
        # Create Button for AioExit
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=350, 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=390, 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=157, y=390, width=190, height=19)
        #--------------------
        # Create Button for Close
        #--------------------
        self.button_close = tk.Button(self.root, text='Close')
        self.button_close.place(x=367, y=415, 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_interval_start,
                       command_interval_stop,
                       command_watch_start,
                       command_aio_lap_tm_count,
                       command_aio_reset_tm_count,
                       command_watch_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 Interval Timer [Operation Start] button
        #----------------------------------------
        self.button_interval_start.configure(command=command_interval_start)
        #----------------------------------------
        # Set the callback function for the Interval Timer [Operation Stop] button
        #----------------------------------------
        self.button_interval_stop.configure(command=command_interval_stop)
        #----------------------------------------
        # Set the callback function for the Stop Watch Timer [Operation Start] button
        #----------------------------------------
        self.button_watch_start.configure(command=command_watch_start)
        #----------------------------------------
        # Set the callback function for the [AioLapTmCount] button
        #----------------------------------------
        self.button_aio_lap_tm_count.configure(command=command_aio_lap_tm_count)
        #----------------------------------------
        # Set the callback function for the [AioResetTmCount] button
        #----------------------------------------
        self.button_aio_reset_tm_count.configure(command=command_aio_reset_tm_count)
        #----------------------------------------
        # Set the callback function for the Stop Watch Timer [Operation Stop] button
        #----------------------------------------
        self.button_watch_stop.configure(command=command_watch_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()