﻿Imports CWaveformGenerator

Public Class Form1
    Dim c_fgen As CCWaveformGeneratorClass

    '----------------------------------------
    ' Macro definition
    '----------------------------------------
    Public Const CWAVEFORMGENERATOR_CHANNELS As Integer = 16    ' Upper limit number of channels for C-WaveformGenerator

    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 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 i = 0 To (CWAVEFORMGENERATOR_CHANNELS - 1)
            comboBox_AoChannel.Items.Add(i.ToString())
        Next i
        '----------------------------------------
        ' Change the selection of channel combobox to 0
        '----------------------------------------
        comboBox_AoChannel.SelectedIndex = 0
    End Sub

    '================================================================================
    ' It is the process when the OpenApplication button is clicked
    ' Initialize the application
    '================================================================================
    Private Sub button_OpenApplicaion_Click(sender As Object, e As EventArgs) Handles button_OpenApplicaion.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 Device settings button is clicked
    ' Open the device settings dialog
    '================================================================================
    Private Sub button_DeviceSetting_Click(sender As Object, e As EventArgs) Handles button_DeviceSetting.Click
        Dim deviceinfodlg As New DeviceSetting
        Dim aochannels As Short = 1         ' Number of channel
        Dim frequency As Single = 1         ' Frequency
        Dim unit_window As Short = 0        ' Selected index of frequency unit combobox
        Dim frequency_type As Short = 0     ' Frequency or Period
        Dim frequency_unit As Short = 0     ' Hz(sec) or kHz(msec)
        Dim oscilation_type As Short = 0    ' Oscilation type
        Dim oscilation_start As Short = 1   ' Number of oscilation waveform
        Dim oscilation_stop As Single = 1   ' Number of oscilation stop waveform or Time of oscillation stop
        Dim arg1 As Short = 0               ' Reserve
        Dim ret As Long                     ' For return code

        '----------------------------------------
        ' Leave the return code blank
        '----------------------------------------
        textBox_ReturnNum.Text = ""
        '----------------------------------------
        ' Get the device information
        '----------------------------------------
        Try
            ret = c_fgen.GetDeviceSetting(aochannels, frequency, frequency_type, frequency_unit, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK)
            Return
        End Try
        unit_window = ((frequency_type << 1) + frequency_unit)
        '----------------------------------------
        ' Get the oscilation
        '----------------------------------------
        Try
            ret = c_fgen.GetOscilation(oscilation_type, oscilation_start, oscilation_stop, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' 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) Then
            '----------------------------------------
            ' Get data form device settings dialog
            '----------------------------------------
            deviceinfodlg.devinfo_data_get(aochannels, frequency, unit_window, oscilation_type, oscilation_start, oscilation_stop)
            '----------------------------------------
            ' Change from frequency unit to type and unit
            '----------------------------------------
            frequency_type = ((unit_window And &H2S) >> 1)
            frequency_unit = (unit_window And &H1S)
            '----------------------------------------
            ' 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
            End Try
            '----------------------------------------
            ' Display the error code if not completed normally
            '----------------------------------------
            If (ret <> 0) Then
                textBox_ReturnNum.Text = String.Format("SetDeviceSetting = {0} ", ret)
                Return
            End If
            '----------------------------------------
            ' 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
            End Try
            '----------------------------------------
            ' Display the error code if not completed normally
            '----------------------------------------
            If (ret <> 0) Then
                textBox_ReturnNum.Text = String.Format("SetOscilation = {0} ", ret)
                Return
            End If
            '----------------------------------------
            ' Display the message that device settings is completed
            '----------------------------------------
            textBox_ReturnNum.Text = "Device settings: Successful completion"
        End If
        deviceinfodlg.Dispose()
    End Sub

    '================================================================================
    ' It is the process when the Channel settings button is clicked
    ' Open the channel settings dialog
    '================================================================================
    Private Sub button_ChannelSetting_Click(sender As Object, e As EventArgs) Handles button_ChannelSetting.Click
        Dim channelinfodlg As New ChannelSetting
        Dim aochannel As Short = 0          ' Channel
        Dim amplitude As Single = 0         ' Amplitude
        Dim offset As Single = 0            ' Offset
        Dim amplitude_type As Short = 0     ' Amplitude type
        Dim amplitude_unit As Short = 0     ' Amplitude unit
        Dim duty As Single = 0              ' Duty
        Dim phase As Single = 0             ' Phase
        Dim sweep_type As Short = 0         ' Simple sweep
        Dim oscilation_time As Short = 1    ' Sweep time
        Dim oscilation_start As Single = 0  ' Before sweep change
        Dim oscilation_stop As Single = 0   ' After sweep change
        Dim arg1 As Short = 0               ' Reserve
        Dim ret As Long                     ' For return code
        Dim wave_type As Short = 0          ' Wave type

        '----------------------------------------
        ' Leave the return code blank
        '----------------------------------------
        textBox_ReturnNum.Text = ""
        '----------------------------------------
        ' Get the channel from combobox
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' Get the wave type
        '----------------------------------------
        Try
            ret = c_fgen.GetWaveType(aochannel, wave_type, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' Get the channel information
        '----------------------------------------
        Try
            ret = c_fgen.GetChannelSetting(aochannel, amplitude, offset, amplitude_type, amplitude_unit, duty, phase, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' Get the simple sweep
        '----------------------------------------
        Try
            ret = c_fgen.GetAoSweep(aochannel, sweep_type, oscilation_time, oscilation_start, oscilation_stop, arg1)
        Catch
            MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK)
            Return
        End Try
        '----------------------------------------
        ' 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) Then
            '----------------------------------------
            ' Get data from channel settings dialog
            '----------------------------------------
            channelinfodlg.chinfo_data_get(aochannel, amplitude, offset, amplitude_type,
                                           amplitude_unit, duty, phase, sweep_type,
                                           oscilation_time, oscilation_start, 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
            End Try
            '----------------------------------------
            ' Display the error code if not completed normally
            '----------------------------------------
            If (ret <> 0) Then
                textBox_ReturnNum.Text = String.Format("SetChannelSetting = {0} ", ret)
                Return
            End If
            '----------------------------------------
            ' 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
            End Try
            '----------------------------------------
            ' Display the error code if not completed normally
            '----------------------------------------
            If (ret <> 0) Then
                textBox_ReturnNum.Text = String.Format("SetAoSweep = {0} ", ret)
                Return
            End If
            '----------------------------------------
            ' Display the message that condition setting is completed
            '----------------------------------------
            textBox_ReturnNum.Text = "Channel settings: Successful completion"
        End If
        channelinfodlg.Dispose()
    End Sub

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

        '----------------------------------------
        ' Get the wave type from combobox
        '----------------------------------------
        wave_type = 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, wave_type, 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 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 = status.ToString()
        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
