﻿using CdioCs;

namespace SimpleIO
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// Create instance Cdio class
        /// </summary>
        Cdio dio = new Cdio();

        public short m_Id;

        public Form1()
        {
            InitializeComponent();
        }

        private void SimpleIO_Load(object sender, System.EventArgs e)
        {

        }

        private void button_DioInit_Click(object sender, System.EventArgs e)
        {
            // Initialization handling
            int Ret;
            Ret = dio.Init(textBox_DeviceName.Text, out m_Id);

            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
        }

        private void button_DioInpByte_Click(object sender, System.EventArgs e)
        {
            int Ret;
            short InpPortNo;
            byte InpPortData;
            //-----------------------------
            // Get port No. from screen
            //-----------------------------
            try
            {
                InpPortNo = System.Convert.ToInt16(textBox_1PortInput_LogicPortNo.Text, 16);
            }
            catch
            {
                InpPortNo = 0;
            }
            //-----------------------------
            // Port input
            //-----------------------------
            Ret = dio.InpByte(m_Id, InpPortNo, out InpPortData);
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Data express
            //-----------------------------
            textBox_1PortInput_InputData.Text = System.Convert.ToString(InpPortData, 16);
        }

        private void button_DioInpBit_Click(object sender, System.EventArgs e)
        {
            int Ret;
            short InpBitNo;
            byte InpBitData;
            //-----------------------------
            // Get port No. from screen
            //-----------------------------
            try
            {
                InpBitNo = System.Convert.ToInt16(textBox_1BitInput_LogicBitNo.Text);
            }
            catch
            {
                InpBitNo = 0;
            }
            //-----------------------------
            // Port input
            //-----------------------------
            Ret = dio.InpBit(m_Id, InpBitNo, out InpBitData);
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Data express
            //-----------------------------
            textBox_1BitInput_InputData.Text = System.Convert.ToString(InpBitData);
        }

        private void button_DioOutByte_Click(object sender, System.EventArgs e)
        {
            int Ret;
            short OutPortNo;
            byte OutPortData;
            //-----------------------------
            // Get port No. and output data from screen
            //-----------------------------
            try
            {
                OutPortNo = System.Convert.ToInt16(textBox_1PortOutput_LogicPortNo.Text);
                OutPortData = System.Convert.ToByte(textBox_1PortOutput_OutputData.Text, 16);
            }
            catch
            {
                OutPortNo = 0;
                OutPortData = 0;
            }
            //-----------------------------
            // Port input
            //-----------------------------
            Ret = dio.OutByte(m_Id, OutPortNo, OutPortData);
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
        }

        private void button_DioEchoBackByte_Click(object sender, System.EventArgs e)
        {
            int Ret;
            short OutPortNo;
            byte OutPortData;
            //-----------------------------
            // Get port No. from screen
            //-----------------------------
            try
            {
                OutPortNo = System.Convert.ToInt16(textBox_1PortOutput_LogicPortNo.Text);
            }
            catch
            {
                OutPortNo = 0;
            }
            //-----------------------------
            // Port input
            //-----------------------------
            Ret = dio.EchoBackByte(m_Id, OutPortNo, out OutPortData);
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Data express
            //-----------------------------
            textBox_1PortOutput_OutputData.Text = System.Convert.ToString(OutPortData, 16);
        }

        private void button_DioOutBit_Click(object sender, System.EventArgs e)
        {
            int Ret;
            short OutBitNo;
            byte OutBitData;
            //-----------------------------
            // Get bit No. and output data from screen
            //-----------------------------
            try
            {
                OutBitNo = System.Convert.ToInt16(textBox_1BitOutput_LogicBitNo.Text);
                OutBitData = System.Convert.ToByte(textBox_1BitOutput_OutputData.Text, 16);
            }
            catch
            {
                OutBitNo = 0;
                OutBitData = 0;
            }
            //-----------------------------
            // Port input
            //-----------------------------
            Ret = dio.OutBit(m_Id, OutBitNo, OutBitData);
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
        }

        private void button_DioEchoBackBit_Click(object sender, System.EventArgs e)
        {
            int Ret;
            short OutBitNo;
            byte OutBitData;
            //-----------------------------
            // Get bit No. from screen
            //-----------------------------
            try
            {
                OutBitNo = System.Convert.ToInt16(textBox_1BitOutput_LogicBitNo.Text);
            }
            catch
            {
                OutBitNo = 0;
            }
            //-----------------------------
            // Port input
            //-----------------------------
            Ret = dio.EchoBackBit(m_Id, OutBitNo, out OutBitData);
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Data express
            //-----------------------------
            textBox_1BitOutput_OutputData.Text = System.Convert.ToString(OutBitData);
        }

        private void button_DioExit_Click(object sender, System.EventArgs e)
        {
            // Exit handling of device
            int Ret;
            Ret = dio.Exit(m_Id);

            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
        }

        private void button_Exit_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void button_DioSetIoDirection_Click(object sender, System.EventArgs e)
        {
            uint dwDir;

            //-----------------------------
            // Get IoDirection
            //-----------------------------
            try
            {
                dwDir = System.Convert.ToByte(textBox_DirData.Text, 16);
            }
            catch
            {
                dwDir = 0;
            }
            //-----------------------------
            // Set IoDirection
            //-----------------------------
            int Ret;
            Ret = dio.SetIoDirection(m_Id, dwDir);
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
        }
    }
}