Option Explicit On
Public Class Form1
    Inherits System.Windows.Forms.Form

    Public wTotalBoards As UInt16
    
    Friend sCardInfo(32) As IXUD_CARD_INFO
    Friend sDevInfo(32) As IXUD_DEVICE_INFO

    Public sModelName As New String(" "c, 20)
    Dim hEvent As Integer 'CreatEvent Value
    Dim MyFuncAddr As CallBackFun

    Dim dwApcCount As UInt32
    Dim bProcessing As Boolean
    Dim dwDataNum As UInt32 = 1000
    Dim wRtn As UInt16
    Dim wBoardNo As UInt16
    Dim wChannels As UInt16 = 3
    Dim fRecoderBuf(dwDataNum * wChannels - 1) As Single
    Dim fRecoderBufFlag
    Dim wFrontFlag As Integer = -1
    Dim wRearFlag As Integer = -1
    Dim wChannelList(wChannels) As UInt16
    Dim wConfigList(wChannels) As UInt16
    Dim dwBufDataNum As UInt32
    Dim wBufStatus As UInt16

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim wBoardIndex As UInt16

        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.cbxSelBoard.Items.Add(Str(wBoardIndex) & " " & sModelName)

        Next

        Me.cbxSelBoard.SelectedIndex = 0

    End Sub


    Private Sub cbxSelBoard_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxSelBoard.SelectedIndexChanged
        Dim wChannelIndex As UInt16

        wBoardNo = CUShort(cbxSelBoard.SelectedIndex)
        cbxChannel0.Items.Clear()
        cbxChannel1.Items.Clear()
        cbxChannel2.Items.Clear()

        If (sCardInfo(wBoardNo).wAIChannels > 0) Then
            For wChannelIndex = 0 To sCardInfo(wBoardNo).wAIChannels - 1
                cbxChannel0.Items.Add("Channel " + wChannelIndex.ToString())
                cbxChannel1.Items.Add("Channel " + wChannelIndex.ToString())
                cbxChannel2.Items.Add("Channel " + wChannelIndex.ToString())
            Next
            cbxChannel0.SelectedIndex = 0
            cbxChannel1.SelectedIndex = 0
            cbxChannel2.SelectedIndex = 0
            cbxCardType.SelectedIndex = 0
            cbxConfig.SelectedIndex = 1
        End If

        Me.Text = cbxSelBoard.Text + " Analog Input Pacer Scan Continuous Callback Demo"

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        wRtn = Ixud_DriverClose()

        Close()

    End Sub

    Public Sub btnActive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnActive.Click

        Dim dwTempAPC As UInt32 = 0
        wBoardNo = CUShort(cbxSelBoard.SelectedIndex)

        If (btnActive.Text = "&ACTIVE") Then

            '//Config the Analog Input Setting
            wRtn = UniDAQ.Ixud_ConfigAI(wBoardNo, 2, CUInt(tbxBufSizeK.Text), CUShort(cbxCardType.SelectedIndex), 0)
            btnActive.Text = "&STOP"
            btnExit.Enabled = False
            '//bDisplay = true;

            If (wRtn > 0) Then

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

            hEvent = 0

            wRtn = Ixud_SetEventCallback(wBoardNo, 3, 1, hEvent, MyFuncAddr, dwDataNum * wChannels)

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

            wFrontFlag = -1 '//Initial Buf Flag
            wRearFlag = -1 '//Initial Buf Flag

            wChannelList(0) = cbxChannel0.SelectedIndex
            wChannelList(1) = cbxChannel1.SelectedIndex
            wChannelList(2) = cbxChannel2.SelectedIndex
            '//Set Scan channel's configcode
            wConfigList(0) = cbxConfig.SelectedIndex
            wConfigList(1) = cbxConfig.SelectedIndex
            wConfigList(2) = cbxConfig.SelectedIndex

            Me.lblPoint.Text = CStr(dwDataNum) + " Point"

            '//Work on Analog Input Pacer
            wRtn = Ixud_StartAIScan(wBoardNo, wChannels, wChannelList, wConfigList, CSng(tbxSRate.Text), 0)
            If (wRtn <> 0) Then
                MsgBox("Start AI Error!!Error Code:" + wRtn.ToString())
                btnActive_Click(sender, e)
            End If
        Else
            '//bDisplay = false;
            btnActive.Text = "&ACTIVE" '
            '//this.pbxDisplay_Paint(pbxDisplay, new PaintEventArgs(pbxDisplay.CreateGraphics(), Rectangle.Empty));                
            btnExit.Enabled = True

            '//Stop Analog Input Pacer
            wRtn = Ixud_StopAI(wBoardNo)
            If (wRtn <> 0) Then
                MsgBox("Stop AI Error!!Error Code:" + wRtn.ToString())
                btnActive_Click(sender, e)
            End If
            wRtn = Ixud_RemoveEventCallback(wBoardNo, 1)
        End If

        Dim gr As Graphics = pbxDisplay.CreateGraphics()

        While (btnActive.Text <> "&ACTIVE")
            If (dwApcCount > dwTempAPC) Then
                tbxAPC.Text = CStr(dwApcCount)
                PlotData(gr)
                dwTempAPC = dwApcCount
            End If


            Application.DoEvents()
        End While

        gr.Dispose()

    End Sub



    Private Sub cbxConfig_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxConfig.SelectedIndexChanged


        Dim fRangeMax As Single = 0, fRangeMin As Single = 0
        Select Case cbxConfig.SelectedIndex
            Case UniDAQ.IXUD_BI_20V
                fRangeMax = 20.0F
                fRangeMin = -20.0F
                Exit Select

            Case UniDAQ.IXUD_BI_10V
                fRangeMax = 10.0F
                fRangeMin = -10.0F
                Exit Select
            Case UniDAQ.IXUD_BI_5V

                '1 //Bipolar +/- 5V 
                fRangeMax = 5.0F
                fRangeMin = -5.0F
                Exit Select
            Case UniDAQ.IXUD_BI_2V5

                '2 //Bipolar +/- 2.5V 
                fRangeMax = 2.5F
                fRangeMin = -2.5F
                Exit Select
            Case UniDAQ.IXUD_BI_1V25

                '3 //Bipolar +/- 1.25V 
                fRangeMax = 1.25F
                fRangeMin = -1.25F
                Exit Select
            Case UniDAQ.IXUD_BI_0V625

                '4 //Bipolar +/- 0.625V 
                fRangeMax = 0.625F
                fRangeMin = -0.625F
                Exit Select
            Case UniDAQ.IXUD_BI_0V3125

                '5 //Bipolar +/- 0.3125V 
                fRangeMax = 0.3125F
                fRangeMin = -0.3125F
                Exit Select
            Case UniDAQ.IXUD_BI_0V5

                '6 //Bipolar +/- 0.5V 
                fRangeMax = 0.5F
                fRangeMin = -0.5F
                Exit Select
            Case UniDAQ.IXUD_BI_0V05

                '7 //Bipolar +/- 0.05V 
                fRangeMax = 0.05F
                fRangeMin = -0.05F
                Exit Select
            Case UniDAQ.IXUD_BI_0V005

                '8 //Bipolar +/- 0.005V 
                fRangeMax = 0.005F
                fRangeMin = -0.005F
                Exit Select
            Case UniDAQ.IXUD_BI_1V

                '9 //Bipolar +/- 1V 
                fRangeMax = 1.0F
                fRangeMin = -1.0F
                Exit Select
            Case UniDAQ.IXUD_BI_0V1

                '10 //Bipolar +/- 0.1V 
                fRangeMax = 0.1F
                fRangeMin = -0.1F
                Exit Select
            Case UniDAQ.IXUD_BI_0V01

                '11 //Bipolar +/- 0.01V 
                fRangeMax = 0.01F
                fRangeMin = -0.01F
                Exit Select
            Case UniDAQ.IXUD_BI_0V001

                '12 //Bipolar +/- 0.001V 
                fRangeMax = 0.001F
                fRangeMin = -0.001F
                Exit Select
            Case UniDAQ.IXUD_UNI_20V

                '13 //Unipolar 0 ~ 20V 
                fRangeMax = 20.0F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_10V

                '14 //Unipolar 0 ~ 10V 
                fRangeMax = 10.0F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_5V

                '15 //Unipolar 0 ~ 5V 
                fRangeMax = 5.0F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_2V5

                '16 //Unipolar 0 ~ 2.5V 
                fRangeMax = 2.5F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_1V25

                '17 //Unipolar 0 ~ 1.25V 
                fRangeMax = 1.25F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_0V625

                '18 //Unipolar 0 ~ 0.625V 
                fRangeMax = 0.625F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_1V

                '19 //Unipolar 0 ~ 1V 
                fRangeMax = 1.0F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_0V1

                '20 //Unipolar 0 ~ 0.1V 
                fRangeMax = 0.1F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_0V01

                '21 //Unipolar 0 ~ 0.01V 
                fRangeMax = 0.01F
                fRangeMin = 0.0F
                Exit Select
            Case UniDAQ.IXUD_UNI_0V001

                '22 //Unipolar 0 ~ 0.001V 
                fRangeMax = 0.001F
                fRangeMin = 0.0F
                Exit Select
        End Select

        tbxYup.Text = Convert.ToString(fRangeMax)
        tbxYdown.Text = Convert.ToString(fRangeMin)




    End Sub

    Private Sub PlotData(ByVal g As Graphics)
        'float Yup = 20.0F, Ydown = -20.0F;
        Dim fScaleX As Single, fScaleY As Single
        Dim RedPen As New Pen(Color.Red)
        Dim BlackPen As New Pen(Color.Black)
        Dim BluePen As New Pen(Color.Blue)

        Dim PointArray(dwDataNum * wChannels - 1) As PointF
        Dim LPointArray() As PointF
        Dim LBaseArray(1) As PointF
        Dim i As UInt16
        'lstADData.Items.Clear()
        For i = PointArray.GetLowerBound(0) To PointArray.GetUpperBound(0)
            PointArray(i).X = i
            PointArray(i).Y = fRecoderBuf(i)

        Next

        ReDim LPointArray(PointArray.GetUpperBound(0) / wChannels - 1)

        fScaleX = pbxDisplay.Width / PointArray.Length
        fScaleY = CSng((pbxDisplay.Height * 0.97)) / (CSng(tbxYup.Text) - CSng(tbxYdown.Text))

        For i = PointArray.GetLowerBound(0) To (PointArray.GetUpperBound(0) / wChannels) - 1

            LPointArray(i).X = PointArray(i * wChannels + 0).X * fScaleX

            LPointArray(i).Y = ((Convert.ToSingle(tbxYup.Text) - PointArray(i * wChannels + 0).Y) * fScaleY)
        Next
        pbxDisplay.Refresh()
        g.DrawLines(RedPen, LPointArray)


        For i = PointArray.GetLowerBound(0) To (PointArray.GetUpperBound(0) / wChannels) - 1

            LPointArray(i).X = PointArray(i * wChannels + 1).X * fScaleX

            LPointArray(i).Y = ((Convert.ToSingle(tbxYup.Text) - PointArray(i * wChannels + 1).Y) * fScaleY)
        Next
        g.DrawLines(BlackPen, LPointArray)

        For i = PointArray.GetLowerBound(0) To (PointArray.GetUpperBound(0) / wChannels) - 1

            LPointArray(i).X = PointArray(i * wChannels + 2).X * fScaleX

            LPointArray(i).Y = ((Convert.ToSingle(tbxYup.Text) - PointArray(i * wChannels + 2).Y) * fScaleY)
        Next
        g.DrawLines(BluePen, LPointArray)
        'pbxDisplay.Refresh();

        RedPen.Dispose()
        BlackPen.Dispose()
        BluePen.Dispose()
        'g.Dispose();

    End Sub
    Public Sub MyApcFunction(ByVal Param As UInt32)

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

            '//Get Data for AI Buffer
            wRtn = UniDAQ.Ixud_GetAIBuffer(wBoardNo, dwDataNum * wChannels, fRecoderBuf)

            If (wRtn <> 0) Then
                MsgBox("Get AI Buffer Error!!Error Code:" + wRtn.ToString())
                'Form1.btnActive_Click(Form1.sender, e)
            End If
            '//Get Buffer Status
            wRtn = UniDAQ.Ixud_GetBufferStatus(wBoardNo, wBufStatus, dwBufDataNum)
            If (wRtn <> 0) Then
                MsgBox("Get AI Buffer Error!!Error Code:" + wRtn.ToString())
                'Form1.btnActive_Click(Form1.sender, e)
            End If

            If (wBufStatus <> 1) Then

                MsgBox("Buffer have error!!!")
                Me.btnActive.Text = "&STOP"
                Exit Sub

            End If

            dwApcCount = dwApcCount + 1
            bProcessing = False


        End If

    End Sub
End Class
