﻿Imports System.Text

Public Class Form1
    '----------------------------------------
    ' Macro definition
    '----------------------------------------
    Public Const CWAVEFORMGENERATOR_CHANNELS As Integer = 16    ' Upper limit number of channels for C-WaveformGenerator
    '----------------------------------------
    ' Global variable declaration
    '----------------------------------------
    Public g_appid As Short                                     ' Application access ID

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' 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 GenOpenApplication button is clicked
    ' Initialize the application
    '================================================================================
    Private Sub button_GenOpenApplication_Click(sender As Object, e As EventArgs) Handles button_GenOpenApplication.Click
        Dim device_name As String                         ' Device name
        Dim openmode As UShort                            ' Screen size
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Get device name from textbox
        '----------------------------------------
        device_name = textBox_DeviceName.Text
        '----------------------------------------
        ' Get screen size from combobox
        '----------------------------------------
        openmode = comboBox_OpenMode.SelectedIndex
        '----------------------------------------
        ' Perform application initialization process
        '----------------------------------------
        ret = GenOpenApplication(device_name, g_appid, openmode)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenOpenApplication = {0} : {1} ", ret, error_string)
    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 UShort = 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 UShort = 0                  ' Frequency or Period
        Dim frequency_unit As UShort = 0                  ' Hz(sec) Or kHz(msec)
        Dim oscillation_type As UShort = 0                ' Oscillation type
        Dim oscillation_start As UShort = 1               ' Number of oscillation waveform
        Dim oscillation_stop As Single = 1                ' Number of oscillation stop waveform or Time of oscillation stop
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Leave the return code blank
        '----------------------------------------
        textBox_ReturnNum.Text = ""
        '----------------------------------------
        ' Get the device information
        '----------------------------------------
        ret = GenGetDeviceSetting(g_appid, aochannels, frequency, frequency_type, frequency_unit)
        unit_window = ((frequency_type << 1) + frequency_unit)
        '----------------------------------------
        ' Get the oscillation
        '----------------------------------------
        ret = GenGetOscillation(g_appid, oscillation_type, oscillation_start, oscillation_stop)
        '----------------------------------------
        ' Go to device settings dialog
        '----------------------------------------
        deviceinfodlg.devinfo_data_set(aochannels, frequency, unit_window, oscillation_type, oscillation_start, oscillation_stop)
        '----------------------------------------
        ' If OK is pressed, process
        '----------------------------------------
        If (deviceinfodlg.ShowDialog() = DialogResult.OK) Then
            '----------------------------------------
            ' Get data form device settings dialog
            '----------------------------------------
            deviceinfodlg.devinfo_data_get(aochannels, frequency, unit_window, oscillation_type, oscillation_start, oscillation_stop)
            '----------------------------------------
            ' Change from frequency unit to type and unit
            '----------------------------------------
            frequency_type = ((unit_window And &H2S) >> 1)
            frequency_unit = (unit_window And &H1S)
            '----------------------------------------
            ' Set the device information
            '----------------------------------------
            ret = GenSetDeviceSetting(g_appid, aochannels, frequency, frequency_type, frequency_unit)
            '----------------------------------------
            ' Display the error code if not completed normally
            '----------------------------------------
            If (ret <> 0) Then
                '----------------------------------------
                ' Get error string
                '----------------------------------------
                GenGetErrorString(ret, error_string)
                '----------------------------------------
                ' Display error code and message
                '----------------------------------------
                textBox_ReturnNum.Text = String.Format("GenSetDeviceSetting = {0} : {1} ", ret, error_string)
                Return
            End If
            '----------------------------------------
            ' Set oscillation
            '----------------------------------------
            ret = GenSetOscillation(g_appid, oscillation_type, oscillation_start, oscillation_stop)
            '----------------------------------------
            ' Display the error code if not completed normally
            '----------------------------------------
            If (ret <> 0) Then
                '----------------------------------------
                ' Get error string
                '----------------------------------------
                GenGetErrorString(ret, error_string)
                '----------------------------------------
                ' Display error code and message
                '----------------------------------------
                textBox_ReturnNum.Text = String.Format("GenSetOscillation = {0} : {1} ", ret, error_string)
                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 UShort = 0                       ' Channel
        Dim amplitude As Single = 0                       ' Amplitude
        Dim offset As Single = 0                          ' Offset
        Dim amplitude_type As UShort = 0                  ' Amplitude type
        Dim amplitude_unit As UShort = 0                  ' Amplitude unit
        Dim duty As Single = 0                            ' Duty
        Dim phase As Single = 0                           ' Phase
        Dim sweep_type As UShort = 0                      ' Simple sweep
        Dim oscillation_time As UShort = 1                ' Sweep time
        Dim oscillation_start As Single = 0               ' Sweep before change
        Dim oscillation_stop As Single = 0                ' Sweep after change
        Dim ret As Integer                                ' For return code
        Dim wave_type As UShort = 0                       ' Wave type
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Leave the return code blank
        '----------------------------------------
        textBox_ReturnNum.Text = ""
        '----------------------------------------
        ' Get the channel from combobox
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' Get the wave type
        '----------------------------------------
        ret = GenGetWaveType(g_appid, aochannel, wave_type)
        '----------------------------------------
        ' Get the channel information
        '----------------------------------------
        ret = GenGetChannelSetting(g_appid, aochannel, amplitude, offset, amplitude_type, amplitude_unit, duty, phase)
        '----------------------------------------
        ' Get the simple sweep
        '----------------------------------------
        ret = GenGetAoSweep(g_appid, aochannel, sweep_type, oscillation_time, oscillation_start, oscillation_stop)
        '----------------------------------------
        ' Go to channel settings dialog
        '----------------------------------------
        channelinfodlg.chinfo_data_set(aochannel, amplitude, offset, amplitude_type,
                                        amplitude_unit, duty, phase, sweep_type,
                                        oscillation_time, oscillation_start, oscillation_stop, wave_type)
        '----------------------------------------
        ' If OK is pressed, process
        '----------------------------------------
        If (channelinfodlg.ShowDialog() = DialogResult.OK) Then
            '----------------------------------------
            ' Get data from channel settings dialog
            '----------------------------------------
            channelinfodlg.chinfo_data_get(aochannel, amplitude, offset, amplitude_type,
                                           amplitude_unit, duty, phase, sweep_type,
                                           oscillation_time, oscillation_start, oscillation_stop)
            '----------------------------------------
            ' Set the channel settings
            '----------------------------------------
            ret = GenSetChannelSetting(g_appid, aochannel, amplitude, offset, amplitude_type, amplitude_unit, duty, phase)
            '----------------------------------------
            ' Display the error code if not completed normally
            '----------------------------------------
            If (ret <> 0) Then
                '----------------------------------------
                ' Get error string
                '----------------------------------------
                GenGetErrorString(ret, error_string)
                '----------------------------------------
                ' Display error code and message
                '----------------------------------------
                textBox_ReturnNum.Text = String.Format("GenSetChannelSetting = {0} : {1} ", ret, error_string)
                Return
            End If
            '----------------------------------------
            ' Set the simple sweep
            '----------------------------------------
            ret = GenSetAoSweep(g_appid, aochannel, sweep_type, oscillation_time, oscillation_start, oscillation_stop)
            '----------------------------------------
            ' Display the error code if not completed normally
            '----------------------------------------
            If (ret <> 0) Then
                '----------------------------------------
                ' Get error string
                '----------------------------------------
                GenGetErrorString(ret, error_string)
                '----------------------------------------
                ' Display error code and message
                '----------------------------------------
                textBox_ReturnNum.Text = String.Format("GenSetAoSweep = {0} : {1} ", ret, error_string)
                Return
            End If
            '----------------------------------------
            ' Display the message that channel setting is completed
            '----------------------------------------
            textBox_ReturnNum.Text = "Channel settings: Successful completion"
        End If
        channelinfodlg.Dispose()
    End Sub

    '================================================================================
    ' It is the process when the GenSetWaveType button is clicked
    ' Set the wave type
    '================================================================================
    Private Sub button_GenSetWaveType_Click(sender As Object, e As EventArgs) Handles button_GenSetWaveType.Click
        Dim wave_type As UShort                           ' Wave type
        Dim aochannel As UShort                           ' Channel
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Get the wave type from combobox
        '----------------------------------------
        wave_type = comboBox_WaveType.SelectedIndex
        '----------------------------------------
        ' Get the channel from combobox
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' Set the wave type
        '----------------------------------------
        ret = GenSetWaveType(g_appid, aochannel, wave_type)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenSetWaveType = {0} : {1} ", ret, error_string)
    End Sub

    '================================================================================
    ' It is the process when the GenStartOperation button is clicked
    ' Start the output of the application
    '================================================================================
    Private Sub button_GenStartOperation_Click(sender As Object, e As EventArgs) Handles button_GenStartOperation.Click
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Start the output of the application
        '----------------------------------------
        ret = GenStartOperation(g_appid)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenStartOperation = {0} : {1} ", ret, error_string)
    End Sub

    '================================================================================
    ' It is the process when the GenGetApplicationStatus button is clicked
    ' Get the operating status
    '================================================================================
    Private Sub button_GenGetApplicationStatus_Click(sender As Object, e As EventArgs) Handles button_GenGetApplicationStatus.Click
        Dim status As UInteger = 0                        ' Status
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Get the operating status
        '----------------------------------------
        ret = GenGetApplicationStatus(g_appid, status)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenGetApplicationStatus = {0} : {1} ", ret, error_string)
        '----------------------------------------
        ' 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 GenStopOperation button is clicked
    ' Stop the output of the application
    '================================================================================
    Private Sub button_GenStopOperation_Click(sender As Object, e As EventArgs) Handles button_GenStopOperation.Click
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Stop the output of the application
        '----------------------------------------
        ret = GenStopOperation(g_appid)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenStopOperation = {0} : {1} ", ret, error_string)
    End Sub

    '================================================================================
    ' It is the process when the GenCloseApplication button is clicked
    ' Close the application
    '================================================================================
    Private Sub button_GenCloseApplication_Click(sender As Object, e As EventArgs) Handles button_GenCloseApplication.Click
        Dim status As UInteger = 0                        ' Status
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Perform application close process
        '----------------------------------------
        ret = GenCloseApplication(g_appid, status)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenCloseApplication = {0} : {1} ", ret, error_string)
        '----------------------------------------
        ' Display the status
        '----------------------------------------
        If (status = 0) Then
            textBox_Status.Text = "0(Normal completed)"
        ElseIf (status = 1) Then
            textBox_Status.Text = "1(C-WaveformGenerator completed error)"
        Else
            textBox_Status.Text = status.ToString()
        End If
    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
        '----------------------------------------
        ' Close dialog
        '----------------------------------------
        Me.Close()
    End Sub
End Class
