﻿using System;
using System.Windows.Forms;
using CWaveformGenerator;

namespace MultiChannelWave
{
    public partial class Form1 : Form
    {
        CCWaveformGeneratorClass c_fgen;

        //----------------------------------------
        // Macro definition
        //----------------------------------------
        public const int CWAVEFORMGENERATOR_CHANNELS = 16;      // Upper limit number of channels for C-WaveformGenerator


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // Initial processing to call C-WaveFormGenerator with SDK
            //----------------------------------------
            try
            {
                //----------------------------------------
                // Create the class of the C-WaveformGenerator
                //----------------------------------------
                c_fgen = new CCWaveformGeneratorClass();
            }
            //----------------------------------------
            // Display error message in case of errors
            //----------------------------------------
            catch
            {
                MessageBox.Show("Unanticipated exception creating CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                this.Close();
                return;
            }

            //----------------------------------------
            // 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 OpenApplication button is clicked
        // Initialize the application
        //================================================================================
        private void button_OpenApplicaion_Click(object sender, EventArgs e)
        {
            string  device_name;        // Device name
            string  arg1;               // Reserve
            short   openmode;           // Screen size
            long    ret;                // For return code

            //----------------------------------------
            // Get device name from textbox
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // Get screen size from textbox
            //----------------------------------------
            openmode = (short)comboBox_OpenMode.SelectedIndex;
            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = "";
            //----------------------------------------
            // Perform application initialization process
            //----------------------------------------
            try
            {
                ret = c_fgen.OpenApplication(openmode, device_name, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("OpenApplication = {0} ", ret);
        }

        //================================================================================
        // 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();
            short           aochannels          = 1;    // Number of channel
            float           frequency           = 1;    // Frequency
            short           unit_window         = 0;    // Selected index of frequency unit combobox
            short           frequency_type      = 0;    // Frequency or Period
            short           frequency_unit      = 0;    // Hz(sec) or kHz(msec)
            short           oscilation_type     = 0;    // Oscilation type 
            short           oscilation_start    = 1;    // Number of oscilation waveform
            float           oscilation_stop     = 1;    // Number of oscilation stop waveform or Time of oscillation stop
            short           arg1                = 0;    // Reserve
            long            ret;                        // For return code

            //----------------------------------------
            // Leave the return code blank
            //----------------------------------------
            textBox_ReturnNum.Text = "";
            //----------------------------------------
            // Get the device information
            //----------------------------------------
            try
            {
                ret = c_fgen.GetDeviceSetting(ref aochannels, ref frequency, ref frequency_type, ref frequency_unit, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            unit_window = (short)((frequency_type << 1) + frequency_unit);
            //----------------------------------------
            // Get the oscilation
            //----------------------------------------
            try
            {
                ret = c_fgen.GetOscilation(ref oscilation_type, ref oscilation_start, ref oscilation_stop, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Go to device settings dialog
            //----------------------------------------
            deviceinfodlg.devinfo_data_set(aochannels, frequency, unit_window, oscilation_type, oscilation_start, oscilation_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 oscilation_type, ref oscilation_start, ref oscilation_stop);
                //----------------------------------------
                // Change from frequency unit to type and unit
                //----------------------------------------
                frequency_type = (short)((unit_window & 0x02) >> 1);
                frequency_unit = (short)(unit_window & 0x01);
                //----------------------------------------
                // arg1 (Reserve)
                //----------------------------------------
                arg1 = 0;
                //----------------------------------------
                // Set the device information
                //----------------------------------------
                try
                {
                    ret = c_fgen.SetDeviceSetting(aochannels, frequency, frequency_type, frequency_unit, arg1);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // Display the error code if not completed normally
                //----------------------------------------
                if (ret != 0) {
                    textBox_ReturnNum.Text = string.Format("SetDeviceSetting = {0} ", ret);
                    return;
                }
                //----------------------------------------
                // Set oscillation
                //----------------------------------------
                try
                {
                    ret = c_fgen.SetOscilation(oscilation_type, oscilation_start, oscilation_stop, arg1);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // Display the error code if not completed normally
                //----------------------------------------
                if (ret != 0) {
                    textBox_ReturnNum.Text = string.Format("SetOscilation = {0} ", ret);
                    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();
            short           aochannel           = 0;    // Channel
            float           amplitude           = 0;    // Amplitude
            float           offset              = 0;    // Offset
            short           amplitude_type      = 0;    // Amplitude type
            short           amplitude_unit      = 0;    // Amplitude unit
            float           duty                = 0;    // Duty
            float           phase               = 0;    // Phase
            short           sweep_type          = 0;    // Simple sweep
            short           oscilation_time     = 1;    // Sweep time
            float           oscilation_start    = 0;    // Before sweep change
            float           oscilation_stop     = 0;    // After sweep change
            short           arg1                = 0;    // Reserve
            long            ret;                        // For return code
            short           wave_type           = 0;    // Wave type

            //----------------------------------------
            // Leave the return code blank
            //----------------------------------------
            textBox_ReturnNum.Text = "";
            //----------------------------------------
            // Get the channel from combobox
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // Get the wave type
            //----------------------------------------
            try
            {
                ret = c_fgen.GetWaveType(aochannel, ref wave_type, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Get the channel information
            //----------------------------------------
            try
            {
                ret = c_fgen.GetChannelSetting(aochannel, ref amplitude, ref offset, ref amplitude_type, ref amplitude_unit, ref duty, ref phase, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Get the simple sweep
            //----------------------------------------
            try
            {
                ret = c_fgen.GetAoSweep(aochannel, ref sweep_type, ref oscilation_time, ref oscilation_start, ref oscilation_stop, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Go to channel settings dialog
            //----------------------------------------
            channelinfodlg.chinfo_data_set(aochannel, amplitude, offset, amplitude_type,
                                           amplitude_unit, duty, phase, sweep_type,
                                           oscilation_time, oscilation_start, oscilation_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 oscilation_time, ref oscilation_start, ref oscilation_stop);
                //----------------------------------------
                // arg1 (Reserve)
                //----------------------------------------
                arg1 = 0;
                //----------------------------------------
                // Set the channel settings
                //----------------------------------------
                try
                {
                    ret = c_fgen.SetChannelSetting(aochannel, amplitude, offset, amplitude_type, amplitude_unit, duty, phase, arg1);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // Display the error code if not completed normally
                //----------------------------------------
                if (ret != 0) {
                    textBox_ReturnNum.Text = string.Format("SetChannelSetting = {0} ", ret);
                    return;
                }
                //----------------------------------------
                // Set the simple sweep
                //----------------------------------------
                try
                {
                    ret = c_fgen.SetAoSweep(aochannel, sweep_type, oscilation_time, oscilation_start, oscilation_stop, arg1);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // Display the error code if not completed normally
                //----------------------------------------
                if (ret != 0) {
                    textBox_ReturnNum.Text = string.Format("SetAoSweep = {0} ", ret);
                    return;
                }
                //----------------------------------------
                // Display the message that condition setting is completed
                //----------------------------------------
                textBox_ReturnNum.Text = "Channel settings: Successful completion";
            }
            channelinfodlg.Dispose();
        }

        //================================================================================
        // It is the process when the SetWaveType button is clicked
        // Set the wave type
        //================================================================================
        private void button_SetWaveType_Click(object sender, EventArgs e)
        {
            short   wave_type;          // Wave type
            short   aochannel;          // Channel
            short   arg1;               // Reserve
            long    ret;                // For return code

            //----------------------------------------
            // Get the wave type from combobox
            //----------------------------------------
            wave_type = (short)comboBox_WaveType.SelectedIndex;
            //----------------------------------------
            // Get the channel from combobox
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Set the wave type
            //----------------------------------------
            try
            {
                ret = c_fgen.SetWaveType(aochannel, wave_type, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SetWaveType = {0} ", ret);
        }

        //================================================================================
        // It is the process when the StartOperation button is clicked
        // Start the output of the application
        //================================================================================
        private void button_StartOperation_Click(object sender, EventArgs e)
        {
            short   arg1;               // Reserve
            long    ret;                // For return code

            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Start the output of the application
            //----------------------------------------
            try
            {
                ret = c_fgen.StartOperation(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("StartOperation = {0} ", ret);
        }

        //================================================================================
        // It is the process when the GetApplicationStatus button is clicked
        // Get the operating status
        //================================================================================
        private void button_GetApplicationStatus_Click(object sender, EventArgs e)
        {
            int     status = -1;        // Startus
            long    ret;                // For return code

            //----------------------------------------
            // Get the operating status
            //----------------------------------------
            try
            {
                ret = c_fgen.GetApplicationStatus(ref status);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GetApplicationStatus = {0} ", ret);
            //----------------------------------------
            // 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 StopOperation button is clicked
        // Stop the output of the application
        //================================================================================
        private void button_StopOperation_Click(object sender, EventArgs e)
        {
            short   arg1;               // Reserve
            long    ret;                // For return code

            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Stop the output of the application
            //----------------------------------------
            try
            {
                ret = c_fgen.StopOperation(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("StopOperation = {0} ", ret);
        }

        //================================================================================
        // It is the process when the CloseApplication button is clicked
        // Close the application
        //================================================================================
        private void button_CloseApplicaion_Click(object sender, EventArgs e)
        {
            short   arg1;               // Reserve
            long    ret;                // For return code

            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Perform application close process
            //----------------------------------------
            try
            {
                ret = c_fgen.CloseApplication(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("CloseApplication = {0} ", ret);
        }

        //================================================================================
        // It is the process when the Exit button is clicked
        // Terminate the application
        //================================================================================
        private void button_Exit_Click(object sender, EventArgs e)
        {
            short arg1;               // Reserve

            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Perform application termination process
            //----------------------------------------
            try
            {
                c_fgen.CloseApplication(arg1);
            }
            catch
            {
            }
            //----------------------------------------
            // Close dialog
            //----------------------------------------
            this.Close();
        }
    }
}
