﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CaioCs;

namespace Timer
{
    public partial class Form1 : Form
    {
        //----------------------------------------
        // Create instance Caio class
        //----------------------------------------
        Caio aio = new Caio();

        //----------------------------------------
        // Declaration of global variables
        //----------------------------------------
        public short g_id;             // ID for device access
        public short g_timer_id;       // Timer ID
        public int   g_event_count;    // Interval elapsed event occurrence count

        public Form1()
        {
            InitializeComponent();
        }

        private void Timer_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // Set the accept button
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // Update the initial display of device name
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // Update the initial display of interval
            //----------------------------------------
            textBox_Interval.Text = "1000";
            //----------------------------------------
            // Update the initial display of notification count
            //----------------------------------------
            textBox_EventCount.Text = "0";
            //----------------------------------------
            // Update the initial display of elapsed time
            //----------------------------------------
            textBox_LapTime.Text = "0";
            //----------------------------------------
            // Initialize global variables
            //----------------------------------------
            g_id          = 0;
            g_timer_id    = 0;
            g_event_count = 0;
        }

        //================================================================================
        // It is the process when the AioInit button is clicked
        // Initialize the device
        //================================================================================
        private void button_AioInit_Click(object sender, EventArgs e)
        {
            string device_name;     // Device name
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Get device name from textbox
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // Device initialization
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.Init(device_name, out g_id);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.Init = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the AioResetDevice button is clicked
        // Reset the device
        //================================================================================
        private void button_AioResetDevice_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Reset device
            //----------------------------------------
            ret = aio.ResetDevice(g_id);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.ResetDevice = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the Operation Start (Interval Timer) button is clicked
        // Start operation
        //================================================================================
        private void button_IntervalStart_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code
            float  interval;        // Interval

            //----------------------------------------
            // Set the event factor to 'Event that interval elapses'
            //----------------------------------------
            ret = aio.SetTmEvent(g_id, g_timer_id, Handle, (int)CaioConst.TME_INT);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Operation Start: aio.SetTmEvent = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Initialize notification count
            //----------------------------------------
            g_event_count = 0;
            //----------------------------------------
            // Get the interval from textbox
            //----------------------------------------
            if (float.TryParse(textBox_Interval.Text, out interval) == false)
            {
                interval = 1000;
            }
            //----------------------------------------
            // Start operation
            //----------------------------------------
            ret = aio.StartTmTimer(g_id, g_timer_id, interval);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Operation Start: aio.StartTmTimer = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // message	Display the message that operation start is completed
            //----------------------------------------
            textBox_ReturnCode.Text = "Operation Start: Successful completion";
        }

        //================================================================================
        // It is the process when the Operation Stop (Interval Timer) button is clicked
        // Stop operation
        //================================================================================
        private void button_IntervalStop_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Stop operation
            //----------------------------------------
            ret = aio.StopTmTimer(g_id, g_timer_id);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Operation Stop: aio.StopTmTimer = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Display the message that operation stop is completed
            //----------------------------------------
            textBox_ReturnCode.Text = "Operation Stop: Successful completion";
        }

        //================================================================================
        // It is the process when the Operation Start (Stop Watch Timer) button is clicked
        // Start measurement 
        //================================================================================
        private void button_WatchStart_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Start measurement
            //----------------------------------------
            ret = aio.StartTmCount(g_id, g_timer_id);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Operation Start: aio.StartTmCount = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Display the message that measurement start is completed
            //----------------------------------------
            textBox_ReturnCode.Text = "Operation Start: Successful completion";
        }

        //================================================================================
        // It is the process when the AioLapTmCount button is clicked
        // Get the elapsed time
        //================================================================================
        private void button_LapTmCount_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code
            int    lap;             // Elapsed time

            //----------------------------------------
            // Get the elapsed time
            //----------------------------------------
            ret = aio.LapTmCount(g_id, g_timer_id, out lap);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.LapTmCount = " + ret.ToString() + " : " + error_string;
            //----------------------------------------
            // When the elapsed time acquisition function is completed successfully
            //----------------------------------------
            if (ret == 0)
            {
                //----------------------------------------
                // Display the elapsed time
                //----------------------------------------
                textBox_LapTime.Text = lap.ToString();
            }
        }

        //================================================================================
        // It is the process when the AioResetTmCount button is clicked
        // Reset the elapsed time
        //================================================================================
        private void button_ResetTmCount_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Reset
            //----------------------------------------
            ret = aio.ResetTmCount(g_id, g_timer_id);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.ResetTmCount = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the Operation Stop (Stop Watch Timer) button is clicked
        // Stop measurement
        //================================================================================
        private void button_WatchStop_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Stop measurement
            //----------------------------------------
            ret = aio.StopTmCount(g_id, g_timer_id);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Operation Stop: aio.StopTmCount = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Display the message that measurement stop is completed
            //----------------------------------------
            textBox_ReturnCode.Text = "Operation Stop: Successful completion";
        }

        //================================================================================
        // It is the process when the AioExit button is clicked
        // Perform the exit process of device
        //================================================================================
        private void button_AioExit_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Exit process
            //----------------------------------------
            ret = aio.Exit(g_id);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.Exit = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the Close button is clicked
        // Perform the exit process of device and terminate the application
        //================================================================================
        private void button_Close_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Exit process
            //----------------------------------------
            aio.Exit(g_id);
            //----------------------------------------
            // Close dialog
            //----------------------------------------
            this.Close();
        }

        //================================================================================
        // Event message routine
        //================================================================================
        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void WndProc(ref Message m)
        {
            //----------------------------------------
            // Process of Event that interval elapses
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_TME_INT)
            {
                //----------------------------------------
                // Display the interval elapsed event occurrence count
                //----------------------------------------
                g_event_count++;
                textBox_EventCount.Text = g_event_count.ToString();
                return;
            }
            base.WndProc(ref m);
        }
    }
}
