﻿using CWaveformGeneratorSDKCs;

namespace MultiChannelWave
{
    public partial class Form1 : Form
    {
        //----------------------------------------
        // Create instance CWaveformGeneratorSDK class
        //----------------------------------------
        CWaveformGeneratorSDK gen = new CWaveformGeneratorSDK();

        //----------------------------------------
        // Macro definition
        //----------------------------------------
        public const int CWAVEFORMGENERATOR_CHANNELS = 16;    // Upper limit number of channels for C-WaveformGenerator
        //----------------------------------------
        // Global variable declaration
        //----------------------------------------
        public short g_appid;                                 // Application access ID

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // Update the initial display of device name
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // Create the combobox for screen size
            //----------------------------------------
            comboBox_OpenMode.Items.Add("Minimize");
            comboBox_OpenMode.Items.Add("Normal");
            comboBox_OpenMode.Items.Add("Maximize");
            //----------------------------------------
            // Change the selection of screen size combobox to 0
            //----------------------------------------
            comboBox_OpenMode.SelectedIndex = 0;
            //----------------------------------------
            // Create the combobox for wave type
            //----------------------------------------
            comboBox_WaveType.Items.Add("Sinewave");
            comboBox_WaveType.Items.Add("Constant");
            comboBox_WaveType.Items.Add("Triangular");
            comboBox_WaveType.Items.Add("Ramp");
            comboBox_WaveType.Items.Add("Square");
            comboBox_WaveType.Items.Add("Noise");
            comboBox_WaveType.Items.Add("Arbitrary");    // For a sample of how to read a data file, see the file sample.
            //----------------------------------------
            // Change the selection of wave type combobox to 0
            //----------------------------------------
            comboBox_WaveType.SelectedIndex = 0;
            //----------------------------------------
            // Create the combobox for channel
            // Upper limit number of channels for C-WaveformGenerator
            //----------------------------------------
            for (int i = 0; i < CWAVEFORMGENERATOR_CHANNELS; i++)
            {
                comboBox_AoChannel.Items.Add(i.ToString());
            }
            //----------------------------------------
            // Change the selection of channel combobox to 0
            //----------------------------------------
            comboBox_AoChannel.SelectedIndex = 0;
        }

        //================================================================================
        // It is the process when the GenOpenApplication button is clicked
        // Initialize the application
        //================================================================================
        private void button_GenOpenApplication_Click(object sender, EventArgs e)
        {
            string device_name;     // Device name
            ushort openmode;        // Screen size
            int    ret;             // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Get device name from textbox
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // Get screen size from combobox
            //----------------------------------------
            openmode = (ushort)comboBox_OpenMode.SelectedIndex;
            //----------------------------------------
            // Perform application initialization process
            //----------------------------------------
            ret = gen.OpenApplication(device_name, out g_appid, openmode);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenOpenApplication = {0} : {1} ", ret, error_string);
        }

        //================================================================================
        // It is the process when the Device settings button is clicked
        // Open the device settings dialog
        //================================================================================
        private void button_DeviceSetting_Click(object sender, EventArgs e)
        {
            DeviceSetting deviceinfodlg     = new DeviceSetting();
            ushort        aochannels        = 1;    // Number of channel
            float         frequency         = 1;    // Frequency
            ushort        unit_window       = 0;    // Selected index of frequency unit combobox
            ushort        frequency_type    = 0;    // Frequency or Period
            ushort        frequency_unit    = 0;    // Hz(sec) or kHz(msec)
            ushort        oscillation_type  = 0;    // Oscillation type
            ushort        oscillation_start = 1;    // Number of oscillation waveform
            float         oscillation_stop  = 1;    // Number of oscillation stop waveform or Time of oscillation stop
            int           ret;                      // For return code
            string        error_string;             // Error code string

            //----------------------------------------
            // Leave the return code blank
            //----------------------------------------
            textBox_ReturnNum.Text = "";
            //----------------------------------------
            // Get the device information
            //----------------------------------------
            ret = gen.GetDeviceSetting(g_appid, out aochannels, out frequency, out frequency_type, out frequency_unit);
            unit_window = (ushort)((frequency_type << 1) + frequency_unit);
            //----------------------------------------
            // Get the oscillation
            //----------------------------------------
            ret = gen.GetOscillation(g_appid, out oscillation_type, out oscillation_start, out oscillation_stop);
            //----------------------------------------
            // Go to device settings dialog
            //----------------------------------------
            deviceinfodlg.devinfo_data_set(aochannels, frequency, unit_window, oscillation_type, oscillation_start, oscillation_stop);
            //----------------------------------------
            // If OK is pressed, process
            //----------------------------------------
            if (deviceinfodlg.ShowDialog() == DialogResult.OK)
            {
                //----------------------------------------
                // Get data form device settings dialog
                //----------------------------------------
                deviceinfodlg.devinfo_data_get(ref aochannels, ref frequency, ref unit_window, ref oscillation_type, ref oscillation_start, ref oscillation_stop);
                //----------------------------------------
                // Change from frequency unit to type and unit
                //----------------------------------------
                frequency_type = (ushort)((unit_window & 0x02) >> 1);
                frequency_unit = (ushort)(unit_window & 0x01);
                //----------------------------------------
                // Set the device information
                //----------------------------------------
                ret = gen.SetDeviceSetting(g_appid, aochannels, frequency, frequency_type, frequency_unit);
                //----------------------------------------
                // Display the error code if not completed normally
                //----------------------------------------
                if (ret != 0)
                {
                    //----------------------------------------
                    // Get error string
                    //----------------------------------------
                    gen.GetErrorString(ret, out error_string);
                    //----------------------------------------
                    // Display error code and message
                    //----------------------------------------
                    textBox_ReturnNum.Text = string.Format("GenSetDeviceSetting = {0} : {1} ", ret, error_string);
                    return;
                }
                //----------------------------------------
                // Set oscillation
                //----------------------------------------
                ret = gen.SetOscillation(g_appid, oscillation_type, oscillation_start, oscillation_stop);
                //----------------------------------------
                // Display the error code if not completed normally
                //----------------------------------------
                if (ret != 0)
                {
                    //----------------------------------------
                    // Get error string
                    //----------------------------------------
                    gen.GetErrorString(ret, out error_string);
                    //----------------------------------------
                    // Display error code and message
                    //----------------------------------------
                    textBox_ReturnNum.Text = string.Format("GenSetOscillation = {0} : {1} ", ret, error_string);
                    return;
                }
                //----------------------------------------
                // Display the message that device settings is completed
                //----------------------------------------
                textBox_ReturnNum.Text = "Device settings: Successful completion";
            }
            deviceinfodlg.Dispose();
        }

        //================================================================================
        // It is the process when the Channel settings button is clicked
        // Open the channel settings dialog
        //================================================================================
        private void button_ChannelSetting_Click(object sender, EventArgs e)
        {
            ChannelSetting channelinfodlg    = new ChannelSetting();
            ushort         aochannel         = 0;    // Channel
            float          amplitude         = 0;    // Amplitude
            float          offset            = 0;    // Offset
            ushort         amplitude_type    = 0;    // Amplitude type
            ushort         amplitude_unit    = 0;    // Amplitude unit
            float          duty              = 0;    // Duty
            float          phase             = 0;    // Phase
            ushort         sweep_type        = 0;    // Simple sweep
            ushort         oscillation_time  = 1;    // Sweep time
            float          oscillation_start = 0;    // Sweep before change
            float          oscillation_stop  = 0;    // Sweep after change
            int            ret;                      // For return code
            ushort         wave_type         = 0;    // Wave type
            string         error_string;             // Error code string

            //----------------------------------------
            // Leave the return code blank
            //----------------------------------------
            textBox_ReturnNum.Text = "";
            //----------------------------------------
            // Get the channel from combobox
            //----------------------------------------
            aochannel = (ushort)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // Get the wave type
            //----------------------------------------
            ret = gen.GetWaveType(g_appid, aochannel, out wave_type);
            //----------------------------------------
            // Get the channel information
            //----------------------------------------
            ret = gen.GetChannelSetting(g_appid, aochannel, out amplitude, out offset, out amplitude_type, out amplitude_unit, out duty, out phase);
            //----------------------------------------
            // Get the simple sweep
            //----------------------------------------
            ret = gen.GetAoSweep(g_appid, aochannel, out sweep_type, out oscillation_time, out oscillation_start, out oscillation_stop);
            //----------------------------------------
            // Go to channel settings dialog
            //----------------------------------------
            channelinfodlg.chinfo_data_set(aochannel, amplitude, offset, amplitude_type,
                                           amplitude_unit, duty, phase, sweep_type,
                                           oscillation_time, oscillation_start, oscillation_stop, wave_type);
            //----------------------------------------
            // If OK is pressed, process
            //----------------------------------------
            if (channelinfodlg.ShowDialog() == DialogResult.OK)
            {
                //----------------------------------------
                // Get data from channel settings dialog
                //----------------------------------------
                channelinfodlg.chinfo_data_get(ref aochannel, ref amplitude, ref offset, ref amplitude_type,
                                               ref amplitude_unit, ref duty, ref phase, ref sweep_type,
                                               ref oscillation_time, ref oscillation_start, ref oscillation_stop);
                //----------------------------------------
                // Set the channel settings
                //----------------------------------------
                ret = gen.SetChannelSetting(g_appid, aochannel, amplitude, offset, amplitude_type, amplitude_unit, duty, phase);
                //----------------------------------------
                // Display the error code if not completed normally
                //----------------------------------------
                if (ret != 0)
                {
                    //----------------------------------------
                    // Get error string
                    //----------------------------------------
                    gen.GetErrorString(ret, out error_string);
                    //----------------------------------------
                    // Display error code and message
                    //----------------------------------------
                    textBox_ReturnNum.Text = string.Format("GenSetChannelSetting = {0} : {1} ", ret, error_string);
                    return;
                }
                //----------------------------------------
                // Set the simple sweep
                //----------------------------------------
                ret = gen.SetAoSweep(g_appid, aochannel, sweep_type, oscillation_time, oscillation_start, oscillation_stop);
                //----------------------------------------
                // Display the error code if not completed normally
                //----------------------------------------
                if (ret != 0)
                {
                    //----------------------------------------
                    // Get error string
                    //----------------------------------------
                    gen.GetErrorString(ret, out error_string);
                    //----------------------------------------
                    // Display error code and message
                    //----------------------------------------
                    textBox_ReturnNum.Text = string.Format("GenSetAoSweep = {0} : {1} ", ret, error_string);
                    return;
                }
                //----------------------------------------
                // Display the message that channel setting is completed
                //----------------------------------------
                textBox_ReturnNum.Text = "Channel settings: Successful completion";
            }
            channelinfodlg.Dispose();
        }

        //================================================================================
        // It is the process when the GenSetWaveType button is clicked
        // Set the wave type
        //================================================================================
        private void button_GenSetWaveType_Click(object sender, EventArgs e)
        {
            ushort wave_type;       // Wave type
            ushort aochannel;       // Channel
            int    ret;             // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Get the wave type from combobox
            //----------------------------------------
            wave_type = (ushort)comboBox_WaveType.SelectedIndex;
            //----------------------------------------
            // Get the channel from combobox
            //----------------------------------------
            aochannel = (ushort)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // Set the wave type
            //----------------------------------------
            ret = gen.SetWaveType(g_appid, aochannel, wave_type);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenSetWaveType = {0} : {1} ", ret, error_string);
        }

        //================================================================================
        // It is the process when the GenStartOperation button is clicked
        // Start the output of the application
        //================================================================================
        private void button_GenStartOperation_Click(object sender, EventArgs e)
        {
            int    ret;             // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Start the output of the application
            //----------------------------------------
            ret = gen.StartOperation(g_appid);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenStartOperation = {0} : {1} ", ret, error_string);
        }

        //================================================================================
        // It is the process when the GenGetApplicationStatus button is clicked
        // Get the operating status
        //================================================================================
        private void button_GenGetApplicationStatus_Click(object sender, EventArgs e)
        {
            uint   status = 0;      // Startus
            int    ret;             // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Get the operating status
            //----------------------------------------
            ret = gen.GetApplicationStatus(g_appid, out status);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenGetApplicationStatus = {0} : {1} ", ret, error_string);
            //----------------------------------------
            // Display the status
            //----------------------------------------
            if (status == 0)
            {
                textBox_Status.Text = "00H(Stopped)";
            }
            else if (status == 1)
            {
                textBox_Status.Text = "01H(Running)";
            }
            else if (status == 2)
            {
                textBox_Status.Text = "02H(Waiting for start trigger)";
            }
            else if (status == 3)
            {
                textBox_Status.Text = "03H(Running, Waiting for start trigger)";
            }
            else
            {
                textBox_Status.Text = status.ToString();
            }
        }

        //================================================================================
        // It is the process when the GenStopOperation button is clicked
        // Stop the output of the application
        //================================================================================
        private void button_GenStopOperation_Click(object sender, EventArgs e)
        {
            int    ret;             // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Stop the output of the application
            //----------------------------------------
            ret = gen.StopOperation(g_appid);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenStopOperation = {0} : {1} ", ret, error_string);
        }

        //================================================================================
        // It is the process when the GenCloseApplication button is clicked
        // Close the application
        //================================================================================
        private void button_GenCloseApplication_Click(object sender, EventArgs e)
        {
            uint   status = 0;      // Status
            int    ret;             // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Perform application close process
            //----------------------------------------
            ret = gen.CloseApplication(g_appid, out status);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenCloseApplication = {0} : {1} ", ret, error_string);
            //----------------------------------------
            // Display the status
            //----------------------------------------
            if (status == 0)
            {
                textBox_Status.Text = "0(Normal completed)";
            }
            else if (status == 1)
            {
                textBox_Status.Text = "1(C-WaveformGenerator completed error)";
            }
            else
            {
                textBox_Status.Text = status.ToString();
            }
        }

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