﻿using CWaveformGeneratorSDKCs;

namespace FileOperation
{
    public partial class Form1 : Form
    {
        //----------------------------------------
        // Create the class of the CWaveformGeneratorSDK
        //----------------------------------------
        CWaveformGeneratorSDK gen = new CWaveformGeneratorSDK();

        //----------------------------------------
        // Macro definition
        //----------------------------------------
        public const int CWAVEFORMGENERATOR_CHANNELS = 16;    // Max channels
        //----------------------------------------
        // Global variable declaration
        //----------------------------------------
        public short g_appid = 0;                             // 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 channel
            //----------------------------------------
            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;
            //----------------------------------------
            // 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");
            //----------------------------------------
            // Change the selection of wave type combobox to 0
            //----------------------------------------
            comboBox_WaveType.SelectedIndex = 0;
            //----------------------------------------
            // Create the combobox for data save
            //----------------------------------------
            comboBox_Param.Items.Add("Single channel");
            comboBox_Param.Items.Add("Multiple channels");
            //----------------------------------------
            // Change the selection of data save combobox to 0
            //----------------------------------------
            comboBox_Param.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 GenSetDeviceName button is clicked
        // Set the device name
        //================================================================================
        private void button_GenSetDeviceName_Click(object sender, EventArgs e)
        {
            string device_name;     // Device name
            int    ret;             // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Get device name from text box
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // Set device name
            //----------------------------------------
            ret = gen.SetDeviceName(g_appid, device_name);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenSetDeviceName = {0} : {1} ", ret, error_string);
        }

        //================================================================================
        // It is the process when the ... button is clicked (Setting file)
        // Displays the dialog to select the ini file
        //================================================================================
        private void button_SettingSelectFile_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Don't check if filename exists
            //----------------------------------------
            openFileDialog1.CheckFileExists = false;
            //----------------------------------------
            // If the path exists, set the current path to the initial path of dialog
            //----------------------------------------
            if (textBox_SettingFile.Text.Length > 0)
            {
                openFileDialog1.InitialDirectory = textBox_SettingFile.Text;
                openFileDialog1.FileName = openFileDialog1.SafeFileName;
            }
            //----------------------------------------
            // If the path not exists, set the MyDocuments path to the initial path of dialog
            //----------------------------------------
            else
            {
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFileDialog1.FileName = "";
            }
            //----------------------------------------
            // Display the selected path
            //----------------------------------------
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_SettingFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName);
            }
        }

        //================================================================================
        // It is the process when the GenExportSettingFile button is clicked
        // Save the settings to ini file
        //================================================================================
        private void button_GenExportSettingFile_Click(object sender, EventArgs e)
        {
            string filepath;        // File path
            int    ret;             // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Get setting file path from text box
            //----------------------------------------
            filepath = textBox_SettingFile.Text;
            //----------------------------------------
            // Save the settings to ini file
            //----------------------------------------
            ret = gen.ExportSettingFile(g_appid, filepath);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenExportSettingFile = {0} : {1} ", ret, error_string);
        }

        //================================================================================
        // It is the process when the GenImportSettingFile button is clicked
        // Load the settings from ini file.
        //================================================================================
        private void button_GenImportSettingFile_Click(object sender, EventArgs e)
        {
            string filepath;        // File path
            int    ret;             // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Get setting file path from text box
            //----------------------------------------
            filepath = textBox_SettingFile.Text;
            //----------------------------------------
            // Load the settings from ini file.
            //----------------------------------------
            ret = gen.ImportSettingFile(g_appid, filepath);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenImportSettingFile = {0} : {1} ", ret, error_string);
        }

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

            //----------------------------------------
            // Get the wave type from combobox
            //----------------------------------------
            wavetype = (ushort)comboBox_WaveType.SelectedIndex;
            //----------------------------------------
            // Get the channel from combobox
            //----------------------------------------
            aochannel = (ushort)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // Set the wave type
            //----------------------------------------
            ret = gen.SetWaveType(g_appid, aochannel, wavetype);
            //----------------------------------------
            // 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 ... button is clicked (Data file)
        // Displays the dialog to select the csv file
        //================================================================================
        private void button_ArSelectFile_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Don't check if filename exists
            //----------------------------------------
            openFileDialog1.CheckFileExists = false;
            //----------------------------------------
            // If the path exists, set the current path to the initial path of dialog
            //----------------------------------------
            if (textBox_ArbitraryDataFile.Text.Length > 0)
            {
                openFileDialog1.InitialDirectory = textBox_ArbitraryDataFile.Text;
                openFileDialog1.FileName = openFileDialog1.SafeFileName;
            }
            //----------------------------------------
            // If the path not exists, set the MyDocuments path to the initial path of dialog
            //----------------------------------------
            else
            {
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFileDialog1.FileName = "";
            }
            //----------------------------------------
            // Display the selected path
            //----------------------------------------
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_ArbitraryDataFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName);
            }
        }

        //================================================================================
        // It is the process when the GenSetArbitraryDataFile button is clicked
        // Set the data file (csv file) path
        //================================================================================
        private void button_GenSetArbitraryDataFile_Click(object sender, EventArgs e)
        {
            string filepath;        // File path
            ushort aochannel;       // Channel
            int    ret;             // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Get data file path from text box
            //----------------------------------------
            filepath = textBox_ArbitraryDataFile.Text;
            //----------------------------------------
            // Get the channel from combobox
            //----------------------------------------
            aochannel = (ushort)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // Set the data file path
            //----------------------------------------
            ret = gen.SetArbitraryDataFile(g_appid, aochannel, filepath);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenSetArbitraryDataFile = {0} : {1} ", ret, error_string);
        }

        //================================================================================
        // It is the process when the GenGetArbitraryDataFile button is clicked
        // Get the setting data file (csv file) path
        //================================================================================
        private void button_GenGetArbitraryDataFile_Click(object sender, EventArgs e)
        {
            string filepath;        // File path
            ushort aochannel;       // Channel
            int    ret;             // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Get the channel from combobox
            //----------------------------------------
            aochannel = (ushort)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // Get the data file path
            //----------------------------------------
            ret = gen.GetArbitraryDataFile(g_appid, aochannel, out filepath);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenGetArbitraryDataFile = {0} : {1} ", ret, error_string);
            //----------------------------------------
            // Display the data file path
            //----------------------------------------
            textBox_ArbitraryDataFile.Text = filepath;
        }

        //================================================================================
        // It is the process when the GenSaveDataFile button is clicked
        // Save the specified channel data to a csv file
        //================================================================================
        private void button_GenSaveDataFile_Click(object sender, EventArgs e)
        {
            string filepath;        // File path
            short  aochannel;       // Channel
            short  param;           // Single or Multiple
            int    ret;             // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Get the data file name from text box
            //----------------------------------------
            filepath = textBox_ArbitraryDataFile.Text;
            //----------------------------------------
            // Get the channel parameters (single or multiple) from combobox
            //----------------------------------------
            param = (short)comboBox_Param.SelectedIndex;
            //----------------------------------------
            // Get the channel from combobox
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // Data file settings
            //----------------------------------------
            //-------------------
            // For the single channel, set the fourth argument to the channel
            //-------------------
            if (param == 0)
            {
                ret = gen.SaveDataFile(g_appid, filepath, param, aochannel);
            }
            //-------------------
            // For the multiple channels, set the fourth argument to 0
            //-------------------
            else
            {
                ret = gen.SaveDataFile(g_appid, filepath, param, 0);
            }
            //----------------------------------------
            // Get error string
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenSaveDataFile = {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;      // Status
            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 = string.Format("{0} ", status);
            }
        }

        //================================================================================
        // 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 = string.Format("{0} ", status);
            }
        }

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