﻿Imports CWaveformGenerator
Public Class Form1
    Dim c_fgen As CCWaveformGeneratorClass

    '----------------------------------------
    ' Macro definition
    '----------------------------------------
    Public AO_CHANNEL As Integer = 0                        ' Analog output channel number: 0
    Public CWAVEFORMGENERATOR_CHANNELS As Integer = 16      ' Max channels
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' 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)
            Me.Close()
            Return
        End Try

        '----------------------------------------
        ' 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
        '----------------------------------------
        Dim i As Integer
        For i = 0 To CWAVEFORMGENERATOR_CHANNELS
            comboBox_AoChannel.Items.Add(i.ToString())
        Next i
        '----------------------------------------
        ' 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
    End Sub

    '================================================================================
    ' It is the process when the OpenApplication button is clicked
    ' Initialize the application
    '================================================================================
    Private Sub button_OpenApplication_Click(sender As Object, e As EventArgs) Handles button_OpenApplication.Click
        Dim device_name As String         ' Device name
        Dim arg1 As String                ' Reserve
        Dim openmode As Short             ' Screen size
        Dim ret As Long                   ' For return code

        '----------------------------------------
        ' Get device name from textbox
        '----------------------------------------
        device_name = textBox_DeviceName.Text
        '----------------------------------------
        ' Get screen size from textbox
        '----------------------------------------
        openmode = 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
        End Try
        '----------------------------------------
        ' Display the error code
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("OpenApplication = {0} ", ret)
    End Sub

    '================================================================================
    ' It is the process when the SetDeviceName button is clicked
    ' Set the device name
    '================================================================================
    Private Sub button_SetDeviceName_Click(sender As Object, e As EventArgs) Handles button_SetDeviceName.Click
        Dim device_name As String         ' Device name
        Dim ret As Long                   ' For return code

        '----------------------------------------
        ' Get setting file name from textbox
        '----------------------------------------
        device_name = textBox_DeviceName.Text
        '----------------------------------------
        ' Set device name
        '----------------------------------------
        Try
            ret = c_fgen.SetDeviceName(device_name)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' Display the error code
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("SetDeviceName = {0} ", ret)
    End Sub

    '================================================================================
    ' It is the process when the ... button is clicked (Setting file)
    ' Displays the dialog to select the ini file
    '================================================================================
    Private Sub button_SettingSelectFile_Click(sender As Object, e As EventArgs) Handles button_SettingSelectFile.Click
        '----------------------------------------
        ' 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 Then
            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 = ""
        End If
        '----------------------------------------
        ' Display the selected path
        '----------------------------------------
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            textBox_SettingFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName)
        End If
    End Sub

    '================================================================================
    ' It is the process when the ExportSettingFile button is clicked
    ' Save the settings to ini file
    '================================================================================
    Private Sub button_ExportSettingFile_Click(sender As Object, e As EventArgs) Handles button_ExportSettingFile.Click
        Dim filepath As String           ' File path
        Dim arg1 As Short                ' Reserve
        Dim ret As Long                  ' For return code

        '----------------------------------------
        ' Get setting file path from text box
        '----------------------------------------
        filepath = textBox_SettingFile.Text
        '----------------------------------------
        ' arg1 (Reserve)
        '----------------------------------------
        arg1 = 0
        '----------------------------------------
        ' Save the settings to ini file
        '----------------------------------------
        Try
            ret = c_fgen.ExportSettingFile(filepath, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' Display the error code
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("ExportSettingFile = {0} ", ret)
    End Sub

    '================================================================================
    ' It is the process when the ImportSettingFile button is clicked
    ' Load the settings from ini file.
    '================================================================================
    Private Sub button_ImportSettingFile_Click(sender As Object, e As EventArgs) Handles button_ImportSettingFile.Click
        Dim filepath As String            ' File path
        Dim arg1 As Short                 ' Reserve
        Dim ret As Long                   ' For return code

        '----------------------------------------
        ' Get setting file path from text box
        '----------------------------------------
        filepath = textBox_SettingFile.Text
        '----------------------------------------
        ' arg1 (Reserve)
        '----------------------------------------
        arg1 = 0
        '----------------------------------------
        ' Load the settings from ini file.
        '----------------------------------------
        Try
            ret = c_fgen.ImportSettingFile(filepath, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' Display the error code
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("ImportSettingFile = {0} ", ret)
    End Sub

    '================================================================================
    ' It is the process when the SetWaveType button is clicked
    ' Set the waveform type
    '================================================================================
    Private Sub button_SetWaveType_Click(sender As Object, e As EventArgs) Handles button_SetWaveType.Click
        Dim wavetype As Short            ' Waveform type
        Dim aochannel As Short           ' Channel
        Dim arg1 As Short                ' Reserve
        Dim ret As Long                  ' For return code

        '----------------------------------------
        ' Get the wave type from combobox
        '----------------------------------------
        wavetype = comboBox_WaveType.SelectedIndex
        '----------------------------------------
        ' Get the channel from combobox
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' arg1 (Reserve)
        '----------------------------------------
        arg1 = 0
        '----------------------------------------
        ' Set the wave type
        '----------------------------------------
        Try
            ret = c_fgen.SetWaveType(aochannel, wavetype, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' Display the error code
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("SetWaveType = {0} ", ret)
    End Sub

    '================================================================================
    ' It is the process when the ... button is clicked (Data file)
    ' Displays the dialog to select the csv file
    '================================================================================
    Private Sub button_ArSelectFile_Click(sender As Object, e As EventArgs) Handles button_ArSelectFile.Click
        '----------------------------------------
        ' 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 Then
            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 = ""
        End If
        '----------------------------------------
        ' Display the selected path
        '----------------------------------------
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            textBox_ArbitraryDataFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName)
        End If
    End Sub

    '================================================================================
    ' It is the process when the SetArbitraryDataFile button is clicked
    ' Set the data file (csv file) path
    '================================================================================
    Private Sub button_SetArbitraryDataFile_Click(sender As Object, e As EventArgs) Handles button_SetArbitraryDataFile.Click
        Dim filepath As String            ' File path
        Dim aochannel As Short            ' Channel
        Dim arg1 As Short                 ' Reserve
        Dim ret As Long                   ' For return code

        '----------------------------------------
        ' Get data file path from text box
        '----------------------------------------
        filepath = textBox_ArbitraryDataFile.Text
        '----------------------------------------
        ' Get the channel from combobox
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' arg1 (Reserve)
        '----------------------------------------
        arg1 = 0
        '----------------------------------------
        ' Set the data file path
        '----------------------------------------
        Try
            ret = c_fgen.SetArbitraryDataFile(aochannel, filepath, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' Display the error code
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("SetArbitraryDataFile = {0} ", ret)
    End Sub

    '================================================================================
    ' It is the process when the GetArbitraryDataFile button is clicked
    ' Get the setting data file (csv file) path
    '================================================================================
    Private Sub button_GetArbitraryDataFile_Click(sender As Object, e As EventArgs) Handles button_GetArbitraryDataFile.Click
        Dim filepath As String              ' File path
        Dim aochannel As Short              ' Channel
        Dim arg1 As Short                   ' Reserve
        Dim ret As Long                     ' For return code

        arg1 = 0
        filepath = vbNullString
        '----------------------------------------
        ' Get the channel from combobox
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' Get the data file path
        '----------------------------------------
        Try
            ret = c_fgen.GetArbitraryDataFile(aochannel, filepath, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' Display the error code
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GetArbitraryDataFile = {0} ", ret)
        '----------------------------------------
        ' Display the data file path
        '----------------------------------------
        textBox_ArbitraryDataFile.Text = filepath
    End Sub

    '================================================================================
    ' It is the process when the SaveDataFile button is clicked
    ' Save the specified channel data to a csv file
    '================================================================================
    Private Sub button_SaveDataFile_Click(sender As Object, e As EventArgs) Handles button_SaveDataFile.Click
        Dim filepath As String            ' File path
        Dim aochannel As Short            ' Channel
        Dim param As Short                ' Single or Multiple
        Dim ret As Long                   ' For return code

        '----------------------------------------
        ' Get the data file name from text box
        '----------------------------------------
        filepath = textBox_ArbitraryDataFile.Text
        '----------------------------------------
        ' Get the channel parameters (single or multiple) from combo box
        '----------------------------------------
        param = comboBox_Param.SelectedIndex
        '----------------------------------------
        ' Get the channel from combobox
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' Data file settings
        '----------------------------------------
        '-------------------
        ' For the single channel, set the third argument to the channel
        '-------------------
        If param = 0 Then
            Try
                ret = c_fgen.SaveDataFile(filepath, param, aochannel)
            Catch
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK)
                Return
            End Try
        '-------------------
        ' For the multiple channels, set the third argument to 0
        '-------------------
        Else
            Try
                ret = c_fgen.SaveDataFile(filepath, param, 0)
            Catch
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK)
                Return
            End Try
        End If
        '----------------------------------------
        ' Display the error code
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("SaveDataFile = {0} ", ret)
    End Sub

    '================================================================================
    ' It is the process when the StartOperation button is clicked
    ' Start the output of the application
    '================================================================================
    Private Sub button_StartOperation_Click(sender As Object, e As EventArgs) Handles button_StartOperation.Click
        Dim arg1 As Short                ' Reserve
        Dim ret As Long                  ' 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
        End Try
        '----------------------------------------
        ' Display the error code
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("StartOperation = {0} ", ret)
    End Sub

    '================================================================================
    ' It is the process when the GetApplicationStatus button is clicked
    ' Get the operating status
    '================================================================================
    Private Sub button_GetApplicationStatus_Click(sender As Object, e As EventArgs) Handles button_GetApplicationStatus.Click
        Dim status As Integer = -1       ' Startus
        Dim ret As Long                  ' For return code

        '----------------------------------------
        ' Get the operating status
        '----------------------------------------
        Try
            ret = c_fgen.GetApplicationStatus(status)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' Display the error code
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GetApplicationStatus = {0} ", ret)
        '----------------------------------------
        ' Display the status
        '----------------------------------------
        If status = 0 Then
            textBox_Status.Text = "00H(Stopped)"
        ElseIf status = 1 Then
            textBox_Status.Text = "01H(Running)"
        ElseIf status = 2 Then
            textBox_Status.Text = "02H(Waiting for start trigger)"
        ElseIf status = 3 Then
            textBox_Status.Text = "03H(Running, Waiting for start trigger)"
        Else
            textBox_Status.Text = String.Format("{0} ", status)
        End If
    End Sub

    '================================================================================
    ' It is the process when the StopOperation button is clicked
    ' Stop the output of the application
    '================================================================================
    Private Sub button_StopOperation_Click(sender As Object, e As EventArgs) Handles button_StopOperation.Click
        Dim arg1 As Short                ' Reserve
        Dim ret As Long                  ' 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
        End Try
        '----------------------------------------
        ' Display the error code
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("StopOperation = {0} ", ret)
    End Sub

    '================================================================================
    ' It is the process when the CloseApplication button is clicked
    ' Close the application
    '================================================================================
    Private Sub button_CloseApplicaion_Click(sender As Object, e As EventArgs) Handles button_CloseApplicaion.Click
        Dim arg1 As Short                ' Reserve
        Dim ret As Long                  ' 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
        End Try
        '----------------------------------------
        ' Display the error code
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("CloseApplication = {0} ", ret)
    End Sub

    '================================================================================
    ' It is the process when the Exit button is clicked
    ' Terminate  the application
    '================================================================================
    Private Sub button_Exit_Click(sender As Object, e As EventArgs) Handles button_Exit.Click
        Dim arg1 As Short                ' Reserve

        '----------------------------------------
        ' arg1 (Reserve)
        '----------------------------------------
        arg1 = 0
        '----------------------------------------
        ' Perform application termination process
        '----------------------------------------
        Try
            c_fgen.CloseApplication(arg1)
        Catch
        End Try
        '----------------------------------------
        ' Close dialog
        '----------------------------------------
        Me.Close()
    End Sub
End Class
