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

        //----------------------------------------
        // Macro definition
        //----------------------------------------
        public const int MAX_AO_RANGE_NUM = 31;    // Maximum number of elements in analog output range array

        //----------------------------------------
        // Declaration of global variables
        //----------------------------------------
        public short   g_id;                                                                  // ID for device access
        public short[] g_ao_range = new short[MAX_AO_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.P1TO5 };     // 1 ~ 5V

        public Form1()
        {
            InitializeComponent();
        }

        private void AoSimple_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 output
            //----------------------------------------
            textBox_ChNo.Text = "0";
            //----------------------------------------
            // Update initial display of data for specified channel output
            //----------------------------------------
            textBox_SingleAoData.Text = "0.0";
            //----------------------------------------
            // Updated initial display of number of channels for multi-channel output
            //----------------------------------------
            textBox_ChNum.Text = "1";
            //----------------------------------------
            // Update initial display of data for multi-channel output
            //----------------------------------------
            textBox_MultiAoDataCh0.Text = "0.0";
            textBox_MultiAoDataCh1.Text = "0.0";
            textBox_MultiAoDataCh2.Text = "0.0";
            textBox_MultiAoDataCh3.Text = "0.0";
            //----------------------------------------
            // Create the combobox for analog output 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("1 - +5V");
            //----------------------------------------
            // Change the selection of analog output range combobox to 0
            //----------------------------------------
            comboBox_RangeValue.SelectedIndex = 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 AioSetAoRangeAll button is clicked
        // Set the output range of analog output
        //================================================================================
        private void button_SetAoRangeAll_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code
            int    index;           // Index of combobox

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

        //================================================================================
        // It is the process when the AioSingleAoEx button is clicked
        // Outputs the output data (voltage or current) once from the specified channel
        //================================================================================
        private void button_SingleAoEx_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code
            float  ao_data;         // Output data
            short  ao_channel;      // Channel No.

            //----------------------------------------
            // Get the channel No. from textbox
            //----------------------------------------
            if (short.TryParse(textBox_ChNo.Text, out ao_channel) == false)
            {
                ao_channel = 0;
            }
            //----------------------------------------
            // Get the output data from textbox
            //----------------------------------------
            if (float.TryParse(textBox_SingleAoData.Text, out ao_data) == false)
            {
                ao_data = 0.0f;
            }
            //----------------------------------------
            // Output the output data from the specified channel
            //----------------------------------------
            ret = aio.SingleAoEx(g_id, ao_channel, ao_data);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SingleAoEx = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the AioMultiAoEx button is clicked
        // Outputs the output data (voltage or current) once from the multi-channels
        //================================================================================
        private void button_MultiAoEx_Click(object sender, EventArgs e)
        {
            string  error_string;    // Error code string
            int     ret;             // For return code
            float[] ao_data;         // Output data
            short   ao_channels;     // Number of channels

            //----------------------------------------
            // Get the number of channels from textbox
            //----------------------------------------
            if (short.TryParse(textBox_ChNum.Text, out ao_channels) == false)
            {
                ao_channels = 1;
            }
            //----------------------------------------
            // Allocate an array for storing DA conversion data
            //----------------------------------------
            try
            {
                ao_data = new float[ao_channels];
            }
            //----------------------------------------
            // When failing to allocate the DA conversion data storage array
            //----------------------------------------
            catch
            {
                textBox_ReturnCode.Text = "Failed to allocate DA conversion data storage array";
                return;
            }
            //----------------------------------------
            // Initialize DA conversion data
            //----------------------------------------
            if (0 < ao_channels)
            {
                if (float.TryParse(textBox_MultiAoDataCh0.Text, out ao_data[0]) == false)
                {
                    ao_data[0] = 0.0f;
                }
            }
            if (1 < ao_channels)
            {
                if (float.TryParse(textBox_MultiAoDataCh1.Text, out ao_data[1]) == false)
                {
                    ao_data[1] = 0.0f;
                }
            }
            if (2 < ao_channels)
            {
                if (float.TryParse(textBox_MultiAoDataCh2.Text, out ao_data[2]) == false)
                {
                    ao_data[2] = 0.0f;
                }
            }
            if (3 < ao_channels)
            {
                if (float.TryParse(textBox_MultiAoDataCh3.Text, out ao_data[3]) == false)
                {
                    ao_data[3] = 0.0f;
                }
            }
            //----------------------------------------
            // Output the output data from the multi-channels
            //----------------------------------------
            ret = aio.MultiAoEx(g_id, ao_channels, ao_data);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.MultiAoEx = " + ret.ToString() + " : " + error_string;
            //----------------------------------------
            // Release the array for storing DA conversion data
            //----------------------------------------
            ao_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();
        }
    }
}
