﻿Imports System.Text

Public Class Condition
    '----------------------------------------
    ' Declaration of global variables
    '----------------------------------------
    Dim g_start_trigger() As Short = {0,    ' Software
                                      1,    ' External trigger rising edge
                                      2}    ' External trigger falling edge
    Dim g_clock_type() As Short = {0,       ' Internal clock
                                   1}       ' External clock
    Dim g_clock_edge() As Short = {0,       ' External input falling edge
                                   1}       ' External input rising edge
    Dim g_stop_trigger() As Short = {1,     ' External trigger rising edge
                                     2}     ' External trigger falling edge

    Private Sub Condition_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim message_string As String                      ' For displaying message
        Dim ret As Integer                                ' For return code
        Dim index As Integer                              ' Index of combobox
        Dim ao_start_trigger As Short                     ' Conversion start condition
        Dim ao_stop_trigger As Short                      ' Conversion stop condition
        Dim ao_clock_type As Short                        ' Clock type
        Dim ao_generating_clock As Single                 ' Generating clock
        Dim ao_clock_edge As Short                        ' Timing of the external clock input
        Dim ao_auto_mode As Short                         ' Set mode of transfer size
        Dim ao_transfer_size As Integer                   ' Transfer size
        Dim err_flag As Short                             ' Function execution error flag

        '----------------------------------------
        ' Initialize parameters
        '----------------------------------------
        err_flag = 0
        message_string = ""
        '----------------------------------------
        ' Create the combobox for analog output conversion start condition
        '----------------------------------------
        ComboBox_StartCondition.Items.Add("Software")
        ComboBox_StartCondition.Items.Add("External trigger rising edge")
        ComboBox_StartCondition.Items.Add("External trigger falling edge")
        '----------------------------------------
        ' Get the analog output conversion start condition
        '----------------------------------------
        ret = AioGetAoStartTrigger(Form1.g_id, ao_start_trigger)
        '----------------------------------------
        ' Abnormal termination process
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Generate error message
                '--------------------
                message_string = "AioGetAoStartTrigger = " & ret & " : " & error_string.ToString() + vbCrLf + vbCrLf + "*The display could not be updated since an error occurred."
                err_flag = 1
            Else
                '--------------------
                ' Disable control
                '--------------------
                ComboBox_StartCondition.Enabled = False
            End If
            '--------------------
            ' Change the selection of analog output conversion start condition combobox to 0
            '--------------------
            ComboBox_StartCondition.SelectedIndex = 0
        '----------------------------------------
        ' Normal termination process
        '----------------------------------------
        Else
            '--------------------
            ' Convert from conversion start condition to index
            '--------------------
            index = 0
            Select Case ao_start_trigger
                Case 0
                    index = 0
                Case 1
                    index = 1
                Case 2
                    index = 2
                Case Else
                    index = 0
            End Select
            '--------------------
            ' Change the selection of the analog output conversion start condition combobox
            '--------------------
            ComboBox_StartCondition.SelectedIndex = index
        End If
        '----------------------------------------
        ' Create the combobox for analog output clock type
        '----------------------------------------
        ComboBox_ClockType.Items.Add("Internal clock")
        ComboBox_ClockType.Items.Add("External clock")
        '----------------------------------------
        ' Get clock type
        '----------------------------------------
        ret = AioGetAoClockType(Form1.g_id, ao_clock_type)
        '----------------------------------------
        ' Abnormal termination process
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Generate error message
                '--------------------
                message_string = "AioGetAoClockType = " & ret & " : " & error_string.ToString() + vbCrLf + vbCrLf + "*The display could not be updated since an error occurred."
                err_flag = 1
            Else
                '--------------------
                ' Disable control
                '--------------------
                ComboBox_ClockType.Enabled = False
            End If
            '--------------------
            ' Change the selection of the analog output clock type combobox to 0
            '--------------------
            ComboBox_ClockType.SelectedIndex = 0
            ComboBox_ClockType_SelectedIndexChanged(New Object(), New EventArgs())
        '----------------------------------------
        ' Normal termination process
        '----------------------------------------
        Else
            '--------------------
            ' Convert from clock type to index
            '--------------------
            index = 0
            Select Case ao_clock_type
                Case 0
                    index = 0
                Case 1
                    index = 1
                Case Else
                    index = 0
            End Select
            '--------------------
            ' Change the selection of the analog output clock type combobox
            '--------------------
            ComboBox_ClockType.SelectedIndex = index
            ComboBox_ClockType_SelectedIndexChanged(New Object(), New EventArgs())
        End If
        '----------------------------------------
        ' Get generating clock
        '----------------------------------------
        ret = AioGetAoSamplingClock(Form1.g_id, ao_generating_clock)
        '----------------------------------------
        ' Abnormal termination process
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Generate error message
                '--------------------
                message_string = "AioGetAoSamplingClock = " & ret & " : " & error_string.ToString() + vbCrLf + vbCrLf + "*The display could not be updated since an error occurred."
                err_flag = 1
            Else
                '--------------------
                ' Disable control
                '--------------------
                TextBox_GeneratingClock.Enabled = False
            End If
            '--------------------
            ' Update the initial display of generating clock
            '--------------------
            TextBox_GeneratingClock.Text = "1000"
        '----------------------------------------
        ' Normal termination process
        '----------------------------------------
        Else
            '--------------------
            ' Update generating clock
            '--------------------
            TextBox_GeneratingClock.Text = Convert.ToDouble(ao_generating_clock).ToString("F6")
        End If
        '----------------------------------------
        ' Create the combobox for analog output external clock input timing
        '----------------------------------------
        ComboBox_ExternalClockInputTiming.Items.Add("External input falling edge")
        ComboBox_ExternalClockInputTiming.Items.Add("External input rising edge")
        '----------------------------------------
        ' For External clock
        '----------------------------------------
        If (ao_clock_type = 1) Then
            '----------------------------------------
            ' Get the timing of external clock input
            '----------------------------------------
            ret = AioGetAoClockEdge(Form1.g_id, ao_clock_edge)
            '----------------------------------------
            ' Abnormal termination process
            '----------------------------------------
            If (ret <> 0) Then
                If (ret <> 20001) Then
                    '--------------------
                    ' Get error string
                    '--------------------
                    AioGetErrorString(ret, error_string)
                    '--------------------
                    ' Generate error message
                    '--------------------
                    message_string = "AioGetAoClockEdge = " & ret & " : " & error_string.ToString() + vbCrLf + vbCrLf + "*The display could not be updated since an error occurred."
                    err_flag = 1
                Else
                    '--------------------
                    ' Disable control
                    '--------------------
                    ComboBox_ExternalClockInputTiming.Enabled = False
                End If
                '----------------------------------------
                ' Change the selection of the external clock input timing combobox to 0
                '----------------------------------------
                ComboBox_ExternalClockInputTiming.SelectedIndex = 0
            '----------------------------------------
            ' Normal termination process
            '----------------------------------------
            Else
                '--------------------
                ' Convert from input timing to index
                '--------------------
                index = 0
                Select Case ao_clock_edge
                    Case 0
                        index = 0
                    Case 1
                        index = 1
                    Case Else
                        index = 0
                End Select
                '----------------------------------------
                ' Change the selection of the external clock input timing combobox
                '----------------------------------------
                ComboBox_ExternalClockInputTiming.SelectedIndex = index
            End If
        '----------------------------------------
        ' Other than External clock
        '----------------------------------------
        Else
            '----------------------------------------
            ' Change the selection of the external clock input timing combobox to 0
            '----------------------------------------
            ComboBox_ExternalClockInputTiming.SelectedIndex = 0
        End If
        '----------------------------------------
        ' Get transfer size
        '----------------------------------------
        ret = AioGetAoTransferSize(Form1.g_id, ao_auto_mode, ao_transfer_size)
        '----------------------------------------
        ' Abnormal termination process
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Generate error message
                '--------------------
                message_string = "AioGetAoTransferSize = " & ret & " : " & error_string.ToString() + vbCrLf + vbCrLf + "*The display could not be updated since an error occurred."
                err_flag = 1
            Else
                '--------------------
                ' Disable control
                '--------------------
                TextBox_TransferSize.Enabled = False
            End If
            '--------------------
            ' Update the initial display of transfer size
            '--------------------
            TextBox_TransferSize.Text = "0"
        '----------------------------------------
        ' Normal termination process
        '----------------------------------------
        Else
            '--------------------
            ' Update transfer size
            '--------------------
            TextBox_TransferSize.Text = ao_transfer_size.ToString()
        End If
        '----------------------------------------
        ' Create the combobox for analog output conversion stop condition
        '----------------------------------------
        ComboBox_StopCondition.Items.Add("External trigger rising edge")
        ComboBox_StopCondition.Items.Add("External trigger falling edge")
        '----------------------------------------
        ' Get the analog output conversion stop condition
        '----------------------------------------
        ret = AioGetAoStopTrigger(Form1.g_id, ao_stop_trigger)
        '----------------------------------------
        ' Abnormal termination process
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Generate error message
                '--------------------
                message_string = "AioGetAoStopTrigger = " & ret & " : " & error_string.ToString() + vbCrLf + vbCrLf + "*The display could not be updated since an error occurred."
                err_flag = 1
            Else
                '--------------------
                ' Disable control
                '--------------------
                ComboBox_StopCondition.Enabled = False
            End If
            '--------------------
            ' Change the selection in the analog output conversion stop condition combobox to 0
            '--------------------
            ComboBox_StopCondition.SelectedIndex = 0
        '----------------------------------------
        ' Normal termination process
        '----------------------------------------
        Else
            '--------------------
            ' Convert from conversion stop condition to index
            '--------------------
            index = 0
            Select Case ao_stop_trigger
                Case 0
                    index = 0
                Case 1
                    index = 0
                Case 2
                    index = 1
                Case Else
                    index = 0
            End Select
            '--------------------
            ' Change the selection in the analog output conversion stop condition combobox
            '--------------------
            ComboBox_StopCondition.SelectedIndex = index
        End If
        '----------------------------------------
        ' Display message if an error occurs
        '----------------------------------------
        If (err_flag <> 0) Then
            MessageBox.Show(message_string, "Error", MessageBoxButtons.OK)
        End If
    End Sub

    '================================================================================
    ' It is the process when the Clock Type combobox is changed
    ' Update available controls
    '================================================================================
    Private Sub ComboBox_ClockType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox_ClockType.SelectedIndexChanged
        Dim index As Integer            ' Index of combobox
        Dim ret As Integer              ' For return code
        Dim auto_mode As Short          ' Set mode of transfer size
        Dim transfer_size As Integer    ' Transfer size

        '----------------------------------------
        ' Get the index of clock type from combobox
        '----------------------------------------
        index = ComboBox_ClockType.SelectedIndex
        '----------------------------------------
        ' Branch processing by clock type
        '----------------------------------------
        Select Case g_clock_type(index)
            Case 0    ' Internal clock
                TextBox_GeneratingClock.Enabled = True
                ComboBox_ExternalClockInputTiming.Enabled = False
                TextBox_TransferSize.Enabled = False
            Case 1    ' External clock
                TextBox_GeneratingClock.Enabled = False
                ComboBox_ExternalClockInputTiming.Enabled = True
                '----------------------------------------
                ' Get transfer size
                '----------------------------------------
                ret = AioGetAoTransferSize(Form1.g_id, auto_mode, transfer_size)
                '----------------------------------------
                ' Abnormal termination process
                '----------------------------------------
                If (ret <> 0) Then
                    TextBox_TransferSize.Enabled = False
                '----------------------------------------
                ' Normal termination process
                '----------------------------------------
                Else
                    TextBox_TransferSize.Enabled = True
                End If
            Case Else
                TextBox_GeneratingClock.Enabled = True
                ComboBox_ExternalClockInputTiming.Enabled = False
                TextBox_TransferSize.Enabled = False
        End Select
    End Sub

    '================================================================================
    ' It is the process when the OK button is clicked
    ' Set generating condition
    '================================================================================
    Private Sub Button_OK_Click(sender As Object, e As EventArgs) Handles Button_OK.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim text_string As String                         ' For updating the textbox
        Dim ret As Integer                                ' For return code
        Dim index As Integer                              ' Index of combobox
        Dim ao_generating_clock As Single                 ' Generating clock
        Dim ao_transfer_size As Integer                   ' Transfer size

        '----------------------------------------
        ' Set the analog output conversion data transfer mode to user buffer mode
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAoTransferMode(Form1.g_id, 1)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            text_string = "AioSetAoTransferMode = " & ret & " : " & error_string.ToString()
            MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' Set the data storage memory type to Repeat output
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAoMemoryType(Form1.g_id, 1)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            text_string = "AioSetAoMemoryType = " & ret & " : " & error_string.ToString()
            MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' Get the index of the conversion start condition setting value from combobox
        '----------------------------------------
        index = ComboBox_StartCondition.SelectedIndex
        '----------------------------------------
        ' Set analog output conversion start condition
        '----------------------------------------
        ret = AioSetAoStartTrigger(Form1.g_id, g_start_trigger(index))
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            text_string = "AioSetAoStartTrigger = " & ret & " : " & error_string.ToString()
            MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' Get the index of the clock type setting value from combobox
        '----------------------------------------
        index = ComboBox_ClockType.SelectedIndex
        '----------------------------------------
        ' Set analog output clock type
        '----------------------------------------
        ret = AioSetAoClockType(Form1.g_id, g_clock_type(index))
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            text_string = "AioSetAoClockType = " & ret & " : " & error_string.ToString()
            MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' For Internal clock
        '----------------------------------------
        If (g_clock_type(index) = 0) Then
            '----------------------------------------
            ' Get the generating clock from textbox
            '----------------------------------------
            If (Single.TryParse(TextBox_GeneratingClock.Text, ao_generating_clock) = False) Then
                ao_generating_clock = 1000
            End If
            '----------------------------------------
            ' Set analog output generating clock
            '----------------------------------------
            ret = AioSetAoSamplingClock(Form1.g_id, ao_generating_clock)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = "AioSetAoSamplingClock = " & ret & " : " & error_string.ToString()
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
            '----------------------------------------
            ' Set analog output transfer size
            '----------------------------------------
            ret = AioSetAoTransferSize(Form1.g_id, AO_TRANSFERSIZE_AUTO, 0)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If ((ret <> 0) And (ret <> 20001)) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = "AioSetAoTransferSize = " & ret & " : " & error_string.ToString()
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
        '----------------------------------------
        ' For External clock
        '----------------------------------------
        ElseIf (g_clock_type(index) = 1) Then
            '----------------------------------------
            ' Get the index of the external clock input timing setting value from combobox
            '----------------------------------------
            index = ComboBox_ExternalClockInputTiming.SelectedIndex
            '----------------------------------------
            ' Set the timing of external clock input
            '----------------------------------------
            ret = AioSetAoClockEdge(Form1.g_id, g_clock_edge(index))
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If ((ret <> 0) And (ret <> 20001)) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = "AioSetAoClockEdge = " & ret & " : " & error_string.ToString()
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
            '----------------------------------------
            ' Get the transfer size from textbox
            '----------------------------------------
            If (Integer.TryParse(TextBox_TransferSize.Text, ao_transfer_size) = False) Then
                ao_transfer_size = 1
            End If
            '----------------------------------------
            ' Set analog output transfer size
            '----------------------------------------
            ret = AioSetAoTransferSize(Form1.g_id, AO_TRANSFERSIZE_MANUAL, ao_transfer_size)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If ((ret <> 0) And (ret <> 20001)) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = "AioSetAoTransferSize = " & ret & " : " & error_string.ToString()
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
        End If
        '----------------------------------------
        ' Get the index of the conversion stop condition setting value from combobox
        '----------------------------------------
        index = ComboBox_StopCondition.SelectedIndex
        '----------------------------------------
        ' Set analog output conversion stop condition
        '----------------------------------------
        ret = AioSetAoStopTrigger(Form1.g_id, g_stop_trigger(index))
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            text_string = "AioSetAoStopTrigger = " & ret & " : " & error_string.ToString()
            MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' Repeat times: Infinite
        '----------------------------------------
        ret = AioSetAoRepeatTimes(Form1.g_id, 0)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            text_string = "AioSetAoRepeatTimes = " & ret & " : " & error_string.ToString()
            MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' OK process
        '----------------------------------------
        Me.Close()
    End Sub

    '================================================================================
    ' It is the process when the Close button is clicked
    ' Terminate the dialog
    '================================================================================
    Private Sub Button_Cancel_Click(sender As Object, e As EventArgs) Handles Button_Cancel.Click
        '----------------------------------------
        ' Cancel process
        '----------------------------------------
        Me.Close()
    End Sub
End Class