﻿using CaioCs;

namespace DigitalIO
{
    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 Form1()
        {
            InitializeComponent();
        }

        private void DigitalIO_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 I/O direction of digital I/O ports
            //----------------------------------------
            textBox_IoDirection.Text = "0";
            //----------------------------------------
            // Update the initial display of the specified digital input port No.
            //----------------------------------------
            textBox_InputByteNo.Text = "0";
            //----------------------------------------
            // Update the initial display of the specified digital input port data
            //----------------------------------------
            textBox_InputByteData.Text = "00";
            //----------------------------------------
            // Update the initial display of the specified digital input bit No.
            //----------------------------------------
            textBox_InputBitNo.Text = "0";
            //----------------------------------------
            // Update the initial display of the specified digital input bit data
            //----------------------------------------
            textBox_InputBitData.Text = "0";
            //----------------------------------------
            // Update the initial display of the specified digital output port No.
            //----------------------------------------
            textBox_OutputByteNo.Text = "0";
            //----------------------------------------
            // Update the initial display of the specified digital output port data
            //----------------------------------------
            textBox_OutputByteData.Text = "00";
            //----------------------------------------
            // Update the initial display of the specified digital output bit No.
            //----------------------------------------
            textBox_OutputBitNo.Text = "0";
            //----------------------------------------
            // Update the initial display of the specified digital output bit data
            //----------------------------------------
            textBox_OutputBitData.Text = "0";
            //----------------------------------------
            // Initialize global variables
            //----------------------------------------
            g_id = 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 AioSetDioDirection button is clicked
        // Set I/O direction of digital I/O ports
        //================================================================================
        private void button_SetDioDirection_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code
            int    io_direction;    // I/O direction

            //----------------------------------------
            // Get I/O direction from textbox
            //----------------------------------------
            if (int.TryParse(textBox_IoDirection.Text, out io_direction) == false)
            {
                io_direction = 0;
            }
            //----------------------------------------
            // Set I/O direction of digital I/O ports
            //----------------------------------------
            ret = aio.SetDioDirection(g_id, io_direction);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetDioDirection = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the AioInputDiByte button is clicked
        // Input data in byte from the specified digital input port
        //================================================================================
        private void button_InputDiByte_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            string text_string;     // For updating the textbox
            int    ret;             // For return code
            short  di_port;         // Digital input port No.
            short  di_data;         // Input data

            //----------------------------------------
            // Get port No. from textbox
            //----------------------------------------
            if (short.TryParse(textBox_InputByteNo.Text, out di_port) == false)
            {
                di_port = 0;
            }
            //----------------------------------------
            // Get specified port data
            //----------------------------------------
            ret = aio.InputDiByte(g_id, di_port, out di_data);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Get error string
            //--------------------
            aio.GetErrorString(ret, out error_string);
            //--------------------
            // Display message
            //--------------------
            textBox_ReturnCode.Text = "aio.InputDiByte = " + ret.ToString() + " : " + error_string;
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                return;
            }
            //----------------------------------------
            // Display specified port data
            //----------------------------------------
            text_string = string.Format("{0:x2}", di_data);
            //--------------------
            // Update the display of conversion data
            //--------------------
            textBox_InputByteData.Text = text_string;
        }

        //================================================================================
        // It is the process when the AioInputDiBit button is clicked
        // Input data from the specified digital input bit
        //================================================================================
        private void button_InputDiBit_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            string text_string;     // For updating the textbox
            int    ret;             // For return code
            short  di_bit;          // Digital input bit No.
            short  di_data;         // Input data

            //----------------------------------------
            // Get bit No. from textbox
            //----------------------------------------
            if (short.TryParse(textBox_InputBitNo.Text, out di_bit) == false)
            {
                di_bit = 0;
            }
            //----------------------------------------
            // Get specified bit data
            //----------------------------------------
            ret = aio.InputDiBit(g_id, di_bit, out di_data);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Get error string
            //--------------------
            aio.GetErrorString(ret, out error_string);
            //--------------------
            // Display message
            //--------------------
            textBox_ReturnCode.Text = "aio.InputDiBit = " + ret.ToString() + " : " + error_string;
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                return;
            }
            //----------------------------------------
            // Display specified bit data
            //----------------------------------------
            text_string = string.Format("{0:d}", di_data);
            //--------------------
            // Update the display of conversion data
            //--------------------
            textBox_InputBitData.Text = text_string;
        }

        //================================================================================
        // It is the process when the AioOutputDoByte button is clicked
        // Output data to specified digital output byte
        //================================================================================
        private void button_OutputDoByte_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code
            short  do_port;         // Digital output port No.
            short  do_data;         // Output data

            //----------------------------------------
            // Get port No. from textbox
            //----------------------------------------
            if (short.TryParse(textBox_OutputByteNo.Text, out do_port) == false)
            {
                do_port = 0;
            }
            //----------------------------------------
            // Get output data from textbox
            //----------------------------------------
            try
            {
                do_data = Convert.ToInt16(textBox_OutputByteData.Text, 16);
            }
            catch
            {
                do_data = 0;
            }
            //----------------------------------------
            // Output data to specified digital output byte
            //----------------------------------------
            ret = aio.OutputDoByte(g_id, do_port, do_data);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.OutputDoByte = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the AioOutputDoBit button is clicked
        // Output data to specified digital output bit
        //================================================================================
        private void button_OutputDoBit_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code
            short  do_bit;          // Digital output bit No.
            short  do_data;         // Output data

            //----------------------------------------
            // Get bit No. from textbox
            //----------------------------------------
            if (short.TryParse(textBox_OutputBitNo.Text, out do_bit) == false)
            {
                do_bit = 0;
            }
            //----------------------------------------
            // Get output data from textbox
            //----------------------------------------
            if (short.TryParse(textBox_OutputBitData.Text, out do_data) == false)
            {
                do_data = 0;
            }
            //----------------------------------------
            // Output data to specified digital output bit
            //----------------------------------------
            ret = aio.OutputDoBit(g_id, do_bit, do_data);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.OutputDoBit = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // 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();
        }
    }
}