Option Explicit On
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim wRtn As UInt16
    Public wTotalBoards As UInt16
    
    Friend sCardInfo(32) As IXUD_CARD_INFO
    Friend sDevInfo(32) As IXUD_DEVICE_INFO
    Dim wBoardNo As UInt16
    Public sModelName As New String(" "c, 20)

    Dim dwDataNum As UInt32 = 40
    Dim fBuf(dwDataNum - 1) As Single

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim wBoardIndex As UInt16
        '//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)
        cbxChannel.Items.Clear()

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

        Me.Text = cbxSelBoard.Text + " Analog Input Polling Demo"

    End Sub

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

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

        wBoardNo = CUShort(cbxSelBoard.SelectedIndex)

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

            '//Config the Analog Input Setting
            wRtn = UniDAQ.Ixud_ConfigAI(wBoardNo, 2, 2048, 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

            Dim gr As Graphics = pbxDisplay.CreateGraphics()
            While (btnActive.Text <> "&ACTIVE")

                '//Work on Analog Input Polling and Get Data
                wRtn = UniDAQ.Ixud_PollingAI(wBoardNo, CUShort(cbxChannel.SelectedIndex), CUShort(cbxConfig.SelectedIndex), dwDataNum, fBuf)

                PlotData(gr)
                '//Application.DoEvents();
                '//this.pbxDisplay_Paint(pbxDisplay, new PaintEventArgs(pbxDisplay.CreateGraphics(), Rectangle.Empty));               
                Application.DoEvents()
            End While
            gr.Dispose()


        Else

            '//bDisplay = false;
            btnActive.Text = "&ACTIVE" '
            '//this.pbxDisplay_Paint(pbxDisplay, new PaintEventArgs(pbxDisplay.CreateGraphics(), Rectangle.Empty));                
            btnExit.Enabled = True

        End If


    End Sub

    '//Draw the Graphics for Analog Input Data
    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 PointArray(dwDataNum - 1) As PointF
        Dim LPointArray() As PointF
        Dim LBaseArray(1) As PointF
        Dim i As UInt16

        For i = PointArray.GetLowerBound(0) To PointArray.GetUpperBound(0)

            PointArray(i).X = i
            PointArray(i).Y = fBuf(i)
        Next

        ReDim LPointArray(PointArray.GetUpperBound(0))

        fScaleX = pbxDisplay.Width / PointArray.Length
        fScaleY = CSng((pbxDisplay.Height * 0.97)) / (CSng(Val(tbxYup.Text)) - CSng(Val(tbxYdown.Text)))
        For i = PointArray.GetLowerBound(0) To PointArray.GetUpperBound(0)

            LPointArray(i).X = PointArray(i).X * fScaleX

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

        RedPen.Dispose()
        'g.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
End Class
