﻿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 AiSimple
{
    public partial class Form1 : Form
    {
        //----------------------------------------
        // Create instance Caio class
        //----------------------------------------
        Caio aio = new Caio();

        //----------------------------------------
        // Macro definition
        //----------------------------------------
        public const int MAX_AI_RANGE_NUM = 32;    // Maximum number of elements in analog input range array

        //----------------------------------------
        // Declaration of global variables
        //----------------------------------------
        public short   g_id;                                                                  // ID for device access
        public int     g_ai_range_index;                                                      // Index of range
        public short[] g_ai_range = new short[MAX_AI_RANGE_NUM]{(short)CaioConst.PM10,        // +/-10V
                                                                (short)CaioConst.PM5,         // +/-5V
                                                                (short)CaioConst.PM25,        // +/-2.5V
                                                                (short)CaioConst.PM125,       // +/-1.25V
                                                                (short)CaioConst.PM1,         // +/-1V
                                                                (short)CaioConst.PM0625,      // +/-0.625V
                                                                (short)CaioConst.PM05,        // +/-0.5V
                                                                (short)CaioConst.PM03125,     // +/-0.3125V
                                                                (short)CaioConst.PM025,       // +/-0.25V
                                                                (short)CaioConst.PM0125,      // +/-0.125V
                                                                (short)CaioConst.PM01,        // +/-0.1V
                                                                (short)CaioConst.PM005,       // +/-0.05V
                                                                (short)CaioConst.PM0025,      // +/-0.025V
                                                                (short)CaioConst.PM00125,     // +/-0.0125V
                                                                (short)CaioConst.PM001,       // +/-0.01V
                                                                (short)CaioConst.P10,         // 0 ~ 10V
                                                                (short)CaioConst.P5,          // 0 ~ 5V
                                                                (short)CaioConst.P4095,       // 0 ~ 4.095V
                                                                (short)CaioConst.P25,         // 0 ~ 2.5V
                                                                (short)CaioConst.P125,        // 0 ~ 1.25V
                                                                (short)CaioConst.P1,          // 0 ~ 1V
                                                                (short)CaioConst.P05,         // 0 ~ 0.5V
                                                                (short)CaioConst.P025,        // 0 ~ 0.25V
                                                                (short)CaioConst.P01,         // 0 ~ 0.1V
                                                                (short)CaioConst.P005,        // 0 ~ 0.05V
                                                                (short)CaioConst.P0025,       // 0 ~ 0.025V
                                                                (short)CaioConst.P00125,      // 0 ~ 0.0125V
                                                                (short)CaioConst.P001,        // 0 ~ 0.01V
                                                                (short)CaioConst.P20MA,       // 0 ~ 20mA
                                                                (short)CaioConst.P4TO20MA,    // 4 ~ 20mA
                                                                (short)CaioConst.PM20MA,      // +/-20mA
                                                                (short)CaioConst.P1TO5};      // 1 ~ 5V

        public Form1()
        {
            InitializeComponent();
        }

        private void AiSimple_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // Set the default button
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // Update the initial display of device name
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // Update the initial display of the channel No. for specified channel input
            //----------------------------------------
            textBox_ChNo.Text = "0";
            //----------------------------------------
            // Update the initial display of number of channels for multi-channel input
            //----------------------------------------
            textBox_ChNum.Text = "1";
            //----------------------------------------
            // Create the combobox for analog input range
            //----------------------------------------
            comboBox_RangeValue.Items.Add("-10 - +10V");
            comboBox_RangeValue.Items.Add("-5 - +5V");
            comboBox_RangeValue.Items.Add("-2.5 - +2.5V");
            comboBox_RangeValue.Items.Add("-1.25 - +1.25V");
            comboBox_RangeValue.Items.Add("-1 - +1V");
            comboBox_RangeValue.Items.Add("-0.625 - +0.625V");
            comboBox_RangeValue.Items.Add("-0.5 - +0.5V");
            comboBox_RangeValue.Items.Add("-0.3125 - +0.3125V");
            comboBox_RangeValue.Items.Add("-0.25 - +0.25V");
            comboBox_RangeValue.Items.Add("-0.125 - +0.125V");
            comboBox_RangeValue.Items.Add("-0.1 - +0.1V");
            comboBox_RangeValue.Items.Add("-0.05 - +0.05V");
            comboBox_RangeValue.Items.Add("-0.025 - +0.025V");
            comboBox_RangeValue.Items.Add("-0.0125 - +0.0125V");
            comboBox_RangeValue.Items.Add("-0.01 - +0.01V");
            comboBox_RangeValue.Items.Add("0 - +10V");
            comboBox_RangeValue.Items.Add("0 - +5V");
            comboBox_RangeValue.Items.Add("0 - +4.095V");
            comboBox_RangeValue.Items.Add("0 - +2.5V");
            comboBox_RangeValue.Items.Add("0 - +1.25V");
            comboBox_RangeValue.Items.Add("0 - +1V");
            comboBox_RangeValue.Items.Add("0 - +0.5V");
            comboBox_RangeValue.Items.Add("0 - +0.25V");
            comboBox_RangeValue.Items.Add("0 - +0.1V");
            comboBox_RangeValue.Items.Add("0 - +0.05V");
            comboBox_RangeValue.Items.Add("0 - +0.025V");
            comboBox_RangeValue.Items.Add("0 - +0.0125V");
            comboBox_RangeValue.Items.Add("0 - +0.01V");
            comboBox_RangeValue.Items.Add("0 - +20mA");
            comboBox_RangeValue.Items.Add("4 - +20mA");
            comboBox_RangeValue.Items.Add("-20 - +20mA");
            comboBox_RangeValue.Items.Add("1 - +5V");
            //----------------------------------------
            // Change the selection of analog input range combobox to 0
            //----------------------------------------
            comboBox_RangeValue.SelectedIndex = 0;
            //----------------------------------------
            // Create the combobox for analog input method
            //----------------------------------------
            comboBox_InpMethod.Items.Add("Single-end input");
            comboBox_InpMethod.Items.Add("Differential input");
            //----------------------------------------
            // Change the selection of analog input method combobox to 0
            //----------------------------------------
            comboBox_InpMethod.SelectedIndex = 0;
            //----------------------------------------
            // Initialize global variables
            //----------------------------------------
            g_id             = 0;
            g_ai_range_index = 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 AioSetAiRangeAll button is clicked
        // Set the input range of analog input
        //================================================================================
        private void button_SetAiRangeAll_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Update GUI information
            //----------------------------------------
            this.Refresh();
            //----------------------------------------
            // Get the index of range setting value from combobox
            //----------------------------------------
            g_ai_range_index = comboBox_RangeValue.SelectedIndex;
            //----------------------------------------
            // Set the analog input range (all channels)
            //----------------------------------------
            ret = aio.SetAiRangeAll(g_id, g_ai_range[g_ai_range_index]);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetAiRangeAll = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the AioSetAiInputMethod button is clicked
        // Set the input method of analog input
        //================================================================================
        private void button_SetAiInpMethod_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code
            int    input_method;    // Input method

            //----------------------------------------
            // Update GUI information
            //----------------------------------------
            this.Refresh();
            //----------------------------------------
            // Get the index of input method setting value from combobox
            //----------------------------------------
            input_method = comboBox_InpMethod.SelectedIndex;
            //----------------------------------------
            // Set the analog input method
            //----------------------------------------
            ret = aio.SetAiInputMethod(g_id, (short)input_method);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetAiInputMethod = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the AioSingleAiEx button is clicked
        // Inputs the input data (voltage or current) once from the specified channel
        //================================================================================
        private void button_SingleAiEx_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            string text_string;     // For updating the textbox
            int    ret;             // For return code
            float  ai_data;         // Input data
            short  ai_channel;      // Channel No.

            //----------------------------------------
            // Get the channel No. from textbox
            //----------------------------------------
            if (short.TryParse(textBox_ChNo.Text, out ai_channel) == false)
            {
                ai_channel = 0;
            }
            //----------------------------------------
            // Get the AD conversion data of specified channel
            //----------------------------------------
            ret = aio.SingleAiEx(g_id, ai_channel, out ai_data);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Get error string
            //--------------------
            aio.GetErrorString(ret, out error_string);
            //--------------------
            // Display message
            //--------------------
            textBox_ReturnCode.Text = "aio.SingleAiEx = " + ret.ToString() + " : " + error_string;
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                return;
            }
            //----------------------------------------
            // Display the AD conversion data of specified channel
            //----------------------------------------
            if ((g_ai_range[g_ai_range_index] == (short)CaioConst.P20MA) || (g_ai_range[g_ai_range_index] == (short)CaioConst.P4TO20MA) || (g_ai_range[g_ai_range_index] == (short)CaioConst.PM20MA))
            {
                text_string = string.Format("{0:F5}mA", ai_data);
            }
            else
            {
                text_string = string.Format("{0:F5}V", ai_data);
            }
            //--------------------
            // Update conversion data display
            //--------------------
            textBox_SingleAiData.Text = text_string;
        }

        //================================================================================
        // It is the process when the AioMultiAiEx button is clicked
        // Inputs the input data (voltage or current) once from the multi-channels
        //================================================================================
        private void button_MultiAiEx_Click(object sender, EventArgs e)
        {
            string  error_string;    // Error code string
            string  text_string;     // For updating the textbox
            int     ret;             // For return code
            float[] ai_data;         // Input data
            short   i;               // For looping the input data display
            short   ai_channels;     // Number of channels

            //----------------------------------------
            // Get the number of channels from textbox
            //----------------------------------------
            if (short.TryParse(textBox_ChNum.Text, out ai_channels) == false)
            {
                ai_channels = 1;
            }
            //----------------------------------------
            // Allocate an array for storing AD conversion data
            //----------------------------------------
            try
            {
                ai_data = new float[ai_channels];
            }
            //----------------------------------------
            // When failing to allocate the AD conversion data storage array
            //----------------------------------------
            catch
            {
                textBox_ReturnCode.Text = "Failed to allocate AD conversion data storage array";
                return;
            }
            //----------------------------------------
            // Get the AD conversion data of multi-channels
            //----------------------------------------
            ret = aio.MultiAiEx(g_id, ai_channels, ai_data);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Get error string
            //--------------------
            aio.GetErrorString(ret, out error_string);
            //--------------------
            // Display message
            //--------------------
            textBox_ReturnCode.Text = "aio.MultiAiEx = " + ret.ToString() + " : " + error_string;
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Release the array for storing AD conversion data
                //--------------------
                ai_data = null;
                return;
            }
            //----------------------------------------
            // Display AD conversion data
            //----------------------------------------
            //--------------------
            // Clear the listbox for AD conversion data display
            //--------------------
            listBox_MultiInpData.Items.Clear();
            //--------------------
            // Repeat the process up to the set number of channels
            //--------------------
            for (i = 0; i < ai_channels; i++)
            {
                //--------------------
                // Generate display data
                //--------------------
                if ((g_ai_range[g_ai_range_index] == (short)CaioConst.P20MA) || (g_ai_range[g_ai_range_index] == (short)CaioConst.P4TO20MA) || (g_ai_range[g_ai_range_index] == (short)CaioConst.PM20MA))
                {
                    text_string = string.Format("{0,3}                   {1:F5}mA", i, ai_data[i]);
                }
                else
                {
                    text_string = string.Format("{0,3}                   {1:F5}V", i, ai_data[i]);
                }
                //--------------------
                // Update conversion data display
                //--------------------
                listBox_MultiInpData.Items.Add(text_string);
            }
            //----------------------------------------
            // Release the array for storing AD conversion data
            //----------------------------------------
            ai_data = null;
        }

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