Option Strict Off
Option Explicit On
Public Class frm_AO
    Inherits System.Windows.Forms.Form
    Dim wRtn As UInt16
    Public wTotalBoards As UInt16
    Public Const MAX_DATA_NUM As UInt32 = 512
    'Dim sGetCardInfo As IXUD_CARD_INFO
    'Dim sGetDevInfo As IXUD_DEVICE_INFO
    Dim sCardInfo(MAX_BOARD_NUMBER) As IXUD_CARD_INFO
    Dim sDevInfo(MAX_BOARD_NUMBER) As IXUD_DEVICE_INFO
    Dim fDABuf(MAX_DATA_NUM) As Single
    Dim wBoardNo As UInt16
    Dim fRangeMax, fRangeMin As Single
    Public sModelName As New String(" "c, 20)

    Private Sub cbxBoardNo_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cbxBoardNo.SelectedIndexChanged
        Dim ChIndex As Short

        wBoardNo = CUShort(Me.cbxBoardNo.SelectedIndex)

        cbxAOCh.Items.Clear()

        Me.Text = Me.cbxBoardNo.Text & " Analog Output Cont. Sample Program"
        If sCardInfo(wBoardNo).wAOChannels > 0 Then
            For ChIndex = 0 To sCardInfo(wBoardNo).wAOChannels - 1
                Me.cbxAOCh.Items.Add("Channel " & Str(ChIndex))
            Next ChIndex

            cbxAOCh.SelectedIndex = 0
            Me.Text = Me.cbxBoardNo.Text + " Analog Output Cont. Sample Program"
        Else
            MsgBox("None AO Channel")
            Ixud_DriverClose()
        End If
    End Sub

    Private Sub cmdActive_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdActive.Click

        Dim wIndex As UInt32

        wBoardNo = Val(CStr(Me.cbxBoardNo.SelectedIndex))

        If cmdActive.Text = "&ACTIVE" Then
            cmdActive.Text = "&STOP"
            '//Setting Analog Output Pattern
            For wIndex = 0 To MAX_DATA_NUM
                fDABuf(wIndex) = CSng(Me.tbxAOAMP.Text) * Math.Sin((2 * 3.14159 / MAX_DATA_NUM) * wIndex) + CSng(Me.tbxOffset.Text)
            Next

            '//Set AO Config
            wRtn = Ixud_ConfigAO(wBoardNo, Me.cbxAOCh.SelectedIndex, Me.cbxAOCfg.SelectedIndex)
            '//Write Analog Output Value
            wRtn = Ixud_StartAOVoltage(wBoardNo, Me.cbxAOCh.SelectedIndex, Me.cbxAOCfg.SelectedIndex, CSng(Me.tbxRate.Text) * 1000.0, MAX_DATA_NUM, 0, fDABuf)

        Else
            cmdActive.Text = "&ACTIVE"

            wRtn = Ixud_StopAO(wBoardNo, Me.cbxAOCh.SelectedIndex)

        End If


    End Sub

    Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
        '//Driver Close
        Call Ixud_DriverClose()
        End
    End Sub

    Private Sub frm_AO_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        Dim wBoardIndex As Short


        '//Driver Initial
        wRtn = Ixud_DriverInit(wTotalBoards)
        If (wRtn) Then
            MsgBox("Driver Initial Error!!Error Code:" & Str(wRtn))
            End
        End If

        '//Get Card Information
        For wBoardIndex = 0 To CUShort(wTotalBoards - 1)

            wRtn = Ixud_GetCardInfo(wBoardIndex, sDevInfo(wBoardIndex), sCardInfo(wBoardIndex), sModelName)

            If (wRtn) Then
                MsgBox("Get Card Information Err")
            End If

            Me.cbxBoardNo.Items.Add(Str(wBoardIndex) & " " & sModelName)

        Next


        Me.cbxBoardNo.SelectedIndex = 0

    End Sub

    Private Sub cbxAOCfg_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxAOCfg.SelectedIndexChanged


        Select Case cbxAOCfg.SelectedIndex

            Case IXUD_AO_UNI_5V    '0 //Unipolar 0 ~ 5V
                fRangeMax = 5
                fRangeMin = 0.0#

            Case IXUD_AO_BI_5V     '1  //Bipolar กำ5V
                fRangeMax = 5.0#
                fRangeMin = -5.0#

            Case IXUD_AO_UNI_10V   '2 //Unipolar 0 ~ 10V
                fRangeMax = 10
                fRangeMin = 0

            Case IXUD_AO_BI_10V    '3 //Bipolar กำ10V
                fRangeMax = 10
                fRangeMin = -10

            Case IXUD_AO_UNI_20V '4 //Unipolar 0 ~ 20V
                fRangeMax = 20.0
                fRangeMin = 0.0

            Case IXUD_AO_BI_20V '5 //Unipolar กำ20V
                fRangeMax = 20.0
                fRangeMin = -20.0

        End Select

        Me.tbxAOAMP.Text = (fRangeMax - fRangeMin) / 2.0#
        Me.tbxOffset.Text = (fRangeMax + fRangeMin) / 2.0#

    End Sub
End Class