Option Strict Off
Option Explicit On
Public Class frm_AO
    Inherits System.Windows.Forms.Form
    Dim wRtn As UInt16
    Public wTotalBoards As UInt16
    'Dim sGetCardInfo As IXUD_CARD_INFO
    'Dim sGetDevInfo As IXUD_DEVICE_INFO
    Dim sCardInfo(32) As IXUD_CARD_INFO
    Dim sDevInfo(32) As IXUD_DEVICE_INFO
    Dim wBoardNo As UInt16
    Public sModelName As New String(" "c, 20)
    Dim hEvent As Integer 'CreatEvent Value
    Dim MyFuncAddr As CallBackFun

    Dim dwApcCount As UInt32
    Dim dwTempAPC As UInt32
    Dim bProcessing As Boolean
    Dim dwSamplingNum As UInt16
    Dim DANUM As UInt16 = 100
    Dim fDABuf(0 To DANUM - 1) As Single


    Public Sub MyApcFunction(ByVal Param As UInt32)

        ' --------- Do your action in this function ------------
        'While (btnActive.Text <> "&ACTIVE")
        If (bProcessing = False) Then
            bProcessing = True

            '//Write Analog Output Value
            wRtn = Ixud_WriteAOVoltage(wBoardNo, Me.cbxAOCh.SelectedIndex, fDABuf(dwApcCount Mod dwSamplingNum))

            If (wRtn <> 0) Then
                MsgBox("Get AI Buffer Error!!Error Code:" + wRtn.ToString())
                'Form1.btnActive_Click(Form1.sender, e)
            End If

            dwApcCount = dwApcCount + 1
            bProcessing = False


        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

        MyFuncAddr = AddressOf MyApcFunction

        '//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 cbxBoardNo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e 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"
        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 Demo"
        Else
            MsgBox("None AO Channel")
            Ixud_DriverClose()
        End If
    End Sub

    Private Sub cmdActive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdActive.Click
        Dim i As Integer
        wBoardNo = Val(CStr(Me.cbxBoardNo.SelectedIndex))
        hEvent = 0
        dwSamplingNum = DANUM

        For i = 0 To dwSamplingNum - 1
            fDABuf(i) = 10.0 * Math.Sin(((360.0 / CSng(dwSamplingNum)) * i * 3.14159) / 180.0)

        Next
        If (cmdActive.Text = "&ACTIVE") Then
            dwTempAPC = 0
            dwApcCount = 0

            wRtn = Ixud_DisableCounter(wBoardNo, 0)
            '//Config the Analog Output Setting
            wRtn = Ixud_ConfigAO(wBoardNo, Me.cbxAOCh.SelectedIndex, Me.cbxAOCfg.SelectedIndex)
            cmdActive.Text = "&STOP"
            cmdExit.Enabled = False
            '//bDisplay = true;

            If (wRtn > 0) Then

                MsgBox("AO Config Error!!Error Code:" + wRtn.ToString())
                cmdActive_Click(sender, e)
            End If

            hEvent = 0
            '//Active Low, Set Event to Source 0
            wRtn = Ixud_SetEventCallback(wBoardNo, 9, 1, hEvent, MyFuncAddr, 0)

            If (wRtn) Then
                MsgBox("SetEventCallback Error" + wRtn.ToString())
                cmdActive_Click(sender, e)
            End If

            '//Enable source 0
            wRtn = Ixud_InstallIrq(wBoardNo, &H1)

            If (wRtn) Then
                MsgBox("Install IRQ Error" + wRtn.ToString())
                cmdActive_Click(sender, e)
            End If

            '//Enable Timer/Counter 0 to generate interrupt
            wRtn = Ixud_SetCounter(wBoardNo, 0, 3, CInt(4000000.0 / (CDbl(Me.tbxSRate.Text) * CDbl(dwSamplingNum))))

            If (wRtn) Then
                MsgBox("Set Timer/Counter Error" + wRtn.ToString())
                cmdActive_Click(sender, e)
            End If


        Else
            cmdActive.Text = "&ACTIVE" '

            cmdExit.Enabled = True

            '//Enable Timer/Counter 0 to generate interrupt
            wRtn = Ixud_DisableCounter(wBoardNo, 0)

            If (wRtn) Then
                MsgBox("Set Timer/Counter Error" + wRtn.ToString())
                cmdActive_Click(sender, e)
            End If


            '// Remove the Interrupt
            wRtn = Ixud_RemoveIrq(wBoardNo)
            If (wRtn <> 0) Then
                MsgBox("Remove IRQ Error!!Error Code:" + wRtn.ToString())
                cmdActive_Click(sender, e)
            End If
            '//Remove the Callback function
            wRtn = Ixud_RemoveEventCallback(wBoardNo, 1)
            If (wRtn <> 0) Then
                MsgBox("Remvoe Callback function Error!!Error Code:" + wRtn.ToString())
                cmdActive_Click(sender, e)
            End If

        End If


        While (cmdActive.Text = "&STOP")

            Application.DoEvents()
        End While
    End Sub
End Class