﻿Imports System.Text

Public Class Form1
    '----------------------------------------
    ' Macro definition
    '----------------------------------------
    Public CWAVEFORMGENERATOR_CHANNELS As Integer = 16    ' Max channels
    '----------------------------------------
    ' 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 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 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 GenSetDeviceName button is clicked
    ' Set the device name
    '================================================================================
    Private Sub button_GenSetDeviceName_Click(sender As Object, e As EventArgs) Handles button_GenSetDeviceName.Click
        Dim device_name As String                         ' Device name
        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
        '----------------------------------------
        ' Set device name
        '----------------------------------------
        ret = GenSetDeviceName(g_appid, device_name)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenSetDeviceName = {0} : {1} ", ret, error_string)
    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 GenExportSettingFile button is clicked
    ' Save the settings to ini file
    '================================================================================
    Private Sub button_GenExportSettingFile_Click(sender As Object, e As EventArgs) Handles button_GenExportSettingFile.Click
        Dim filepath As String                            ' File path
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Get setting file path from text box
        '----------------------------------------
        filepath = textBox_SettingFile.Text
        '----------------------------------------
        ' Save the settings to ini file
        '----------------------------------------
        ret = GenExportSettingFile(g_appid, filepath)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenExportSettingFile = {0} : {1} ", ret, error_string)
    End Sub

    '================================================================================
    ' It is the process when the GenImportSettingFile button is clicked
    ' Load the settings from ini file.
    '================================================================================
    Private Sub button_GenImportSettingFile_Click(sender As Object, e As EventArgs) Handles button_GenImportSettingFile.Click
        Dim filepath As String                            ' File path
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Get setting file path from text box
        '----------------------------------------
        filepath = textBox_SettingFile.Text
        '----------------------------------------
        ' Load the settings from ini file.
        '----------------------------------------
        ret = GenImportSettingFile(g_appid, filepath)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenImportSettingFile = {0} : {1} ", ret, error_string)
    End Sub

    '================================================================================
    ' It is the process when the GenSetWaveType button is clicked
    ' Set the waveform type
    '================================================================================
    Private Sub button_GenSetWaveType_Click(sender As Object, e As EventArgs) Handles button_GenSetWaveType.Click
        Dim wavetype As UShort                            ' Waveform 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
        '----------------------------------------
        wavetype = comboBox_WaveType.SelectedIndex
        '----------------------------------------
        ' Get the channel from combobox
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' Set the wave type
        '----------------------------------------
        ret = GenSetWaveType(g_appid, aochannel, wavetype)
        '----------------------------------------
        ' 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 ... 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 GenSetArbitraryDataFile button is clicked
    ' Set the data file (csv file) path
    '================================================================================
    Private Sub button_GenSetArbitraryDataFile_Click(sender As Object, e As EventArgs) Handles button_GenSetArbitraryDataFile.Click
        Dim filepath As String                            ' File path
        Dim aochannel As UShort                           ' Channel
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Get data file path from text box
        '----------------------------------------
        filepath = textBox_ArbitraryDataFile.Text
        '----------------------------------------
        ' Get the channel from combobox
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' Set the data file path
        '----------------------------------------
        ret = GenSetArbitraryDataFile(g_appid, aochannel, filepath)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenSetArbitraryDataFile = {0} : {1} ", ret, error_string)
    End Sub

    '================================================================================
    ' It is the process when the GenGetArbitraryDataFile button is clicked
    ' Get the setting data file (csv file) path
    '================================================================================
    Private Sub button_GenGetArbitraryDataFile_Click(sender As Object, e As EventArgs) Handles button_GenGetArbitraryDataFile.Click
        Dim filepath As New StringBuilder("", 256)        ' File path
        Dim aochannel As UShort                           ' Channel
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' Get the channel from combobox
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' Get the data file path
        '----------------------------------------
        ret = GenGetArbitraryDataFile(g_appid, aochannel, filepath)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        GenGetErrorString(ret, 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.ToString()
    End Sub

    '================================================================================
    ' It is the process when the GenSaveDataFile button is clicked
    ' Save the specified channel data to a csv file
    '================================================================================
    Private Sub button_GenSaveDataFile_Click(sender As Object, e As EventArgs) Handles button_GenSaveDataFile.Click
        Dim filepath As String                            ' File path
        Dim aochannel As Short                            ' Channel
        Dim param As Short                                ' Single or Multiple
        Dim ret As Integer                                ' For return code
        Dim error_string As New StringBuilder("", 256)    ' Error code string

        '----------------------------------------
        ' 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 fourth argument to the channel
        '-------------------
        If param = 0 Then
            ret = GenSaveDataFile(g_appid, filepath, param, aochannel)
            '-------------------
            ' For the multiple channels, set the fourth argument to 0
            '-------------------
        Else
            ret = GenSaveDataFile(g_appid, filepath, param, 0)
        End If
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display error code and message
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenSaveDataFile = {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 = String.Format("{0} ", status)
        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 = String.Format("{0} ", status)
        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
