﻿using CaioCs;

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

        //----------------------------------------
        // Declaration of global variables
        //----------------------------------------
        public short[] g_start_trigger = {0,     // Software
                                          1,     // External trigger rising edge
                                          2};    // External trigger falling edge
        public short[] g_clock_type = {   0,     // Internal clock
                                          1};    // External clock
        public short[] g_clock_edge = {   0,     // External input falling edge
                                          1};    // External input rising edge
        public short[] g_stop_trigger = { 1,     // External trigger rising edge
                                          2};    // External trigger falling edge

        public Condition()
        {
            InitializeComponent();
        }

        private void Condition_Load(object sender, EventArgs e)
        {
            string error_string;           // Error code string
            string message_string;         // For displaying message
            int    ret;                    // For return code
            int    index;                  // Index of combobox
            short  ao_start_trigger;       // Conversion start condition
            short  ao_stop_trigger;        // Conversion stop condition
            short  ao_clock_type;          // Clock type
            float  ao_generating_clock;    // Generating clock
            short  ao_clock_edge;          // Timing of the external clock input
            short  ao_auto_mode;           // Set mode of transfer size
            int    ao_transfer_size;       // Transfer size
            short  err_flag;               // Function execution error flag

            //----------------------------------------
            // Initialize parameters
            //----------------------------------------
            err_flag       = 0;
            message_string = "";
            //----------------------------------------
            // Create the combobox for analog output conversion start condition
            //----------------------------------------
            comboBox_StartCondition.Items.Add("Software");
            comboBox_StartCondition.Items.Add("External trigger rising edge");
            comboBox_StartCondition.Items.Add("External trigger falling edge");
            //----------------------------------------
            // Get the analog output conversion start condition
            //----------------------------------------
            ret = aio.GetAoStartTrigger(Form1.g_id, out ao_start_trigger);
            //----------------------------------------
            // Abnormal termination process
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Generate error message
                    //--------------------
                    message_string = "aio.GetAoStartTrigger = " + ret.ToString() + " : " + error_string + "\r\n\r\n*The display could not be updated since an error occurred.";
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // Disable control
                    //--------------------
                    comboBox_StartCondition.Enabled = false;
                }
                //--------------------
                // Change the selection of analog output conversion start condition combobox to 0
                //--------------------
                comboBox_StartCondition.SelectedIndex = 0;
            }
            //----------------------------------------
            // Normal termination process
            //----------------------------------------
            else
            {
                //--------------------
                // Convert from conversion start condition to index
                //--------------------
                index = 0;
                switch (ao_start_trigger)
                {
                    case 0:
                        index = 0;
                        break;
                    case 1:
                        index = 1;
                        break;
                    case 2:
                        index = 2;
                        break;
                    default:
                        index = 0;
                        break;
                }
                //--------------------
                // Change the selection of the analog output conversion start condition combobox
                //--------------------
                comboBox_StartCondition.SelectedIndex = index;
            }
            //----------------------------------------
            // Create the combobox for analog output clock type
            //----------------------------------------
            comboBox_ClockType.Items.Add("Internal clock");
            comboBox_ClockType.Items.Add("External clock");
            //----------------------------------------
            // Get clock type
            //----------------------------------------
            ret = aio.GetAoClockType(Form1.g_id, out ao_clock_type);
            //----------------------------------------
            // Abnormal termination process
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Generate error message
                    //--------------------
                    message_string = "aio.GetAoClockType = " + ret.ToString() + " : " + error_string + "\r\n\r\n*The display could not be updated since an error occurred.";
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // Disable control
                    //--------------------
                    comboBox_ClockType.Enabled = false;
                }
                //--------------------
                // Change the selection of the analog output clock type combobox to 0
                //--------------------
                comboBox_ClockType.SelectedIndex = 0;
                comboBox_ClockType_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // Normal termination process
            //----------------------------------------
            else
            {
                //--------------------
                // Convert from clock type to index
                //--------------------
                index = 0;
                switch (ao_clock_type)
                {
                    case 0:
                        index = 0;
                        break;
                    case 1:
                        index = 1;
                        break;
                    default:
                        index = 0;
                        break;
                }
                //--------------------
                // Change the selection of the analog output clock type combobox
                //--------------------
                comboBox_ClockType.SelectedIndex = index;
                comboBox_ClockType_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // Get generating clock
            //----------------------------------------
            ret = aio.GetAoSamplingClock(Form1.g_id, out ao_generating_clock);
            //----------------------------------------
            // Abnormal termination process
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Generate error message
                    //--------------------
                    message_string = "aio.GetAoSamplingClock = " + ret.ToString() + " : " + error_string + "\r\n\r\n*The display could not be updated since an error occurred.";
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // Disable control
                    //--------------------
                    textBox_GeneratingClock.Enabled = false;
                }
                //--------------------
                // Update the initial display of generating clock
                //--------------------
                textBox_GeneratingClock.Text = "1000";
            }
            //----------------------------------------
            // Normal termination process
            //----------------------------------------
            else
            {
                //--------------------
                // Update generating clock
                //--------------------
                textBox_GeneratingClock.Text = Convert.ToDouble(ao_generating_clock).ToString("F6");
            }
            //----------------------------------------
            // Create the combobox for analog output external clock input timing
            //----------------------------------------
            comboBox_ExternalClockInputTiming.Items.Add("External input falling edge");
            comboBox_ExternalClockInputTiming.Items.Add("External input rising edge");
            //----------------------------------------
            // For External clock
            //----------------------------------------
            if (ao_clock_type == 1)
            {
                //----------------------------------------
                // Get the timing of external clock input
                //----------------------------------------
                ret = aio.GetAoClockEdge(Form1.g_id, out ao_clock_edge);
                //----------------------------------------
                // Abnormal termination process
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Generate error message
                        //--------------------
                        message_string = "aio.GetAoClockEdge = " + ret.ToString() + " : " + error_string + "\r\n\r\n*The display could not be updated since an error occurred.";
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // Disable control
                        //--------------------
                        comboBox_ExternalClockInputTiming.Enabled = false;
                    }
                    //----------------------------------------
                    // Change the selection of the external clock input timing combobox to 0
                    //----------------------------------------
                    comboBox_ExternalClockInputTiming.SelectedIndex = 0;
                }
                //----------------------------------------
                // Normal termination process
                //----------------------------------------
                else
                {
                    //--------------------
                    // Convert from input timing to index
                    //--------------------
                    index = 0;
                    switch (ao_clock_edge)
                    {
                        case 0:
                            index = 0;
                            break;
                        case 1:
                            index = 1;
                            break;
                        default:
                            index = 0;
                            break;
                    }
                    //----------------------------------------
                    // Change the selection of the external clock input timing combobox
                    //----------------------------------------
                    comboBox_ExternalClockInputTiming.SelectedIndex = index;
                }
            }
            //----------------------------------------
            // Other than External clock
            //----------------------------------------
            else
            {
                //----------------------------------------
                // Change the selection of the external clock input timing combobox to 0
                //----------------------------------------
                comboBox_ExternalClockInputTiming.SelectedIndex = 0;
            }
            //----------------------------------------
            // Get transfer size
            //----------------------------------------
            ret = aio.GetAoTransferSize(Form1.g_id, out ao_auto_mode, out ao_transfer_size);
            //----------------------------------------
            // Abnormal termination process
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Generate error message
                    //--------------------
                    message_string = "aio.GetAoTransferSize = " + ret.ToString() + " : " + error_string + "\r\n\r\n*The display could not be updated since an error occurred.";
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // Disable control
                    //--------------------
                    textBox_TransferSize.Enabled = false;
                }
                //--------------------
                // Update the initial display of transfer size
                //--------------------
                textBox_TransferSize.Text = "0";
            }
            //----------------------------------------
            // Normal termination process
            //----------------------------------------
            else
            {
                //--------------------
                // Update transfer size
                //--------------------
                textBox_TransferSize.Text = ao_transfer_size.ToString();
            }
            //----------------------------------------
            // Create the combobox for analog output conversion stop condition
            //----------------------------------------
            comboBox_StopCondition.Items.Add("External trigger rising edge");
            comboBox_StopCondition.Items.Add("External trigger falling edge");
            //----------------------------------------
            // Get the analog output conversion stop condition
            //----------------------------------------
            ret = aio.GetAoStopTrigger(Form1.g_id, out ao_stop_trigger);
            //----------------------------------------
            // Abnormal termination process
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Generate error message
                    //--------------------
                    message_string = "aio.GetAoStopTrigger = " + ret.ToString() + " : " + error_string + "\r\n\r\n*The display could not be updated since an error occurred.";
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // Disable control
                    //--------------------
                    comboBox_StopCondition.Enabled = false;
                }
                //--------------------
                // Change the selection in the analog output conversion stop condition combobox to 0
                //--------------------
                comboBox_StopCondition.SelectedIndex = 0;
            }
            //----------------------------------------
            // Normal termination process
            //----------------------------------------
            else
            {
                //--------------------
                // Convert from conversion stop condition to index
                //--------------------
                index = 0;
                switch (ao_stop_trigger)
                {
                    case 0:
                        index = 0;
                        break;
                    case 1:
                        index = 0;
                        break;
                    case 2:
                        index = 1;
                        break;
                    default:
                        index = 0;
                        break;
                }
                //--------------------
                // Change the selection in the analog output conversion stop condition combobox
                //--------------------
                comboBox_StopCondition.SelectedIndex = index;
            }
            //----------------------------------------
            // Display message if an error occurs
            //----------------------------------------
            if (err_flag != 0)
            {
                MessageBox.Show(message_string, "Error", MessageBoxButtons.OK);
            }
        }

        //================================================================================
        // It is the process when the Clock Type combobox is changed
        // Update available controls
        //================================================================================
        private void comboBox_ClockType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int     index;          // Index of combobox
            int     ret;            // For return code
            short   auto_mode;      // Set mode of transfer size
            int     transfer_size;  // Transfer size

            //----------------------------------------
            // Get the index of clock type from combobox
            //----------------------------------------
            index = comboBox_ClockType.SelectedIndex;
            //----------------------------------------
            // Branch processing by clock type
            //----------------------------------------
            switch (g_clock_type[index])
            {
                case 0:    // Internal clock
                    textBox_GeneratingClock.Enabled = true;
                    comboBox_ExternalClockInputTiming.Enabled = false;
                    textBox_TransferSize.Enabled = false;
                    break;
                case 1:    // External clock
                    textBox_GeneratingClock.Enabled = false;
                    comboBox_ExternalClockInputTiming.Enabled = true;
                    //----------------------------------------
                    // Get transfer size
                    //----------------------------------------
                    ret = aio.GetAoTransferSize(Form1.g_id, out auto_mode, out transfer_size);
                    //----------------------------------------
                    // Abnormal termination process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        textBox_TransferSize.Enabled = false;
                    }
                    //----------------------------------------
                    // Normal termination process
                    //----------------------------------------
                    else
                    {
                        textBox_TransferSize.Enabled = true;
                    }
                    break;
                default:
                    textBox_GeneratingClock.Enabled = true;
                    comboBox_ExternalClockInputTiming.Enabled = false;
                    textBox_TransferSize.Enabled = false;
                    break;
            }
        }

        //================================================================================
        // It is the process when the OK button is clicked
        // Set generating condition
        //================================================================================
        private void button_OK_Click(object sender, EventArgs e)
        {
            string error_string;           // Error code string
            string text_string;            // For updating the textbox
            int    ret;                    // For return code
            int    index;                  // Index of combobox
            float  ao_generating_clock;    // Generating clock
            int    ao_transfer_size;       // Transfer size

            //----------------------------------------
            // Set the analog output conversion data transfer mode to user buffer mode
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAoTransferMode(Form1.g_id, 1);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                text_string = "aio.SetAoTransferMode = " + ret.ToString() + " : " + error_string;
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Set the data storage memory type to Repeat output
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAoMemoryType(Form1.g_id, 1);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                text_string = "aio.SetAoMemoryType = " + ret.ToString() + " : " + error_string;
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Get the index of the conversion start condition setting value from combobox
            //----------------------------------------
            index = comboBox_StartCondition.SelectedIndex;
            //----------------------------------------
            // Set analog output conversion start condition
            //----------------------------------------
            ret = aio.SetAoStartTrigger(Form1.g_id, g_start_trigger[index]);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                text_string = "aio.SetAoStartTrigger = " + ret.ToString() + " : " + error_string;
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Get the index of the clock type setting value from combobox
            //----------------------------------------
            index = comboBox_ClockType.SelectedIndex;
            //----------------------------------------
            // Set analog output clock type
            //----------------------------------------
            ret = aio.SetAoClockType(Form1.g_id, g_clock_type[index]);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                text_string = "aio.SetAoClockType = " + ret.ToString() + " : " + error_string;
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // For Internal clock
            //----------------------------------------
            if (g_clock_type[index] == 0)
            {
                //----------------------------------------
                // Get the generating clock from textbox
                //----------------------------------------
                if (float.TryParse(textBox_GeneratingClock.Text, out ao_generating_clock) == false)
                {
                    ao_generating_clock = 1000;
                }
                //----------------------------------------
                // Set analog output generating clock
                //----------------------------------------
                ret = aio.SetAoSamplingClock(Form1.g_id, ao_generating_clock);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = "aio.SetAoSamplingClock = " + ret.ToString() + " : " + error_string;
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // Set analog output transfer size
                //----------------------------------------
                ret = aio.SetAoTransferSize(Form1.g_id, (short)CaioConst.AO_TRANSFERSIZE_AUTO, 0);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if ((ret != 0) && (ret != 20001))
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = "aio.SetAoTransferSize = " + ret.ToString() + " : " + error_string;
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // For External clock
            //----------------------------------------
            else if (g_clock_type[index] == 1)
            {
                //----------------------------------------
                // Get the index of the external clock input timing setting value from combobox
                //----------------------------------------
                index = comboBox_ExternalClockInputTiming.SelectedIndex;
                //----------------------------------------
                // Set the timing of external clock input
                //----------------------------------------
                ret = aio.SetAoClockEdge(Form1.g_id, g_clock_edge[index]);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if ((ret != 0) && (ret != 20001))
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = "aio.AioSetAoClockEdge = " + ret.ToString() + " : " + error_string;
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // Get the transfer size from textbox
                //----------------------------------------
                if (int.TryParse(textBox_TransferSize.Text, out ao_transfer_size) == false)
                {
                    ao_transfer_size = 1;
                }
                //----------------------------------------
                // Set analog output transfer size
                //----------------------------------------
                ret = aio.SetAoTransferSize(Form1.g_id, (short)CaioConst.AO_TRANSFERSIZE_MANUAL, ao_transfer_size);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if ((ret != 0) && (ret != 20001))
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = "aio.SetAoTransferSize = " + ret.ToString() + " : " + error_string;
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // Get the index of the conversion stop condition setting value from combobox
            //----------------------------------------
            index = comboBox_StopCondition.SelectedIndex;
            //----------------------------------------
            // Set analog output conversion stop condition
            //----------------------------------------
            ret = aio.SetAoStopTrigger(Form1.g_id, g_stop_trigger[index]);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                text_string = "aio.AioSetAoStopTrigger = " + ret.ToString() + " : " + error_string;
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Repeat times: Infinite
            //----------------------------------------
            ret = aio.SetAoRepeatTimes(Form1.g_id, 0);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                text_string = "aio.SetAoRepeatTimes = " + ret.ToString() + " : " + error_string;
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // OK process
            //----------------------------------------
            this.Close();
        }

        //================================================================================
        // It is the process when the Close button is clicked
        // Terminate the dialog
        //================================================================================
        private void button_Cancel_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Cancel process
            //----------------------------------------
            this.Close();
        }
    }
}
