﻿Option Strict Off
Option Explicit On
Imports System.Text

Public Class Form1
    Dim Id As Short ' Device ID
    Dim Data() As Single   ' Array of retrieving data
    Dim Records As Integer ' Number of retrieved record
    Dim RecordLength As Integer ' Record length
    Dim Channels As Short ' Number of channels
    Dim TriggerChannel As Short ' Post-trigger channel
    Dim Range As Single ' Input range
    Dim bmp As Bitmap    ' Bitmap

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        bmp = New Bitmap(Graph.Size.Width, Graph.Size.Height)    ' Create the bitmap which is compatible with the graph
    End Sub

    '==================================
    ' Init handling
    '==================================
    Private Sub CommandInit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandInit.Click
        Dim Ret As Integer    ' Return value
        Dim Ret2 As Integer   ' Return value2
        Dim ErrorString As New StringBuilder("", 256)     ' Error string

        '----------------------------------
        ' Init handling
        '----------------------------------
        Ret = DigInit(TextDevice.Text, Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigInit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Reset device
        '----------------------------------
        Ret = DigResetDevice(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigResetDevice = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "Init : Normality completion"
    End Sub

    '==================================
    ' Acquisition start
    '==================================
    Private Sub CommandStart_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandStart.Click
        Dim Ret As Integer    ' Return value
        Dim Ret2 As Integer   ' Return value2
        Dim ErrorString As New StringBuilder("", 256)     ' Error string

        '----------------------------------
        ' Auto setup
        '----------------------------------
        Ret = DigAutoSetup(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigAutoSetup = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' Get the setup information and display
        '----------------------------------
        CommandInfo_Click(CommandInfo, New System.EventArgs())

        '----------------------------------
        ' Insure the array of retrieving data
        '----------------------------------
        ReDim Data(RecordLength * Channels)

        '----------------------------------
        ' Operation start
        '----------------------------------
        Ret = DigStart(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigStart = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextInfo.Text = "Acquisition start : Normality completion"

        '----------------------------------
        ' Polling until the operation status is changed to be OFF
        '----------------------------------
        Dim Status As Integer

        Do
            System.Windows.Forms.Application.DoEvents()    ' In order to enable the dialog operation
            '----------------------------------
            ' Get the number of records which are being stored in driver memory
            '----------------------------------
            Ret = DigGetDriverMemoryRecords(Id, Records)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigGetDriverMemoryRecords = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If

            '----------------------------------
            ' If there are records in driver memory, retrieve the data from the new side
            '----------------------------------
            If Records > 0 Or Records = -1 Then
                Ret = DigReadLatestRecords(Id, DIG_RECORD_DATA, 1, Data, Nothing, Nothing)
                If Ret <> 0 Then
                    Ret2 = DigGetErrorString(Ret, ErrorString)
                    TextInfo.Text = "DigReadLatestRecords = " & Ret & " : " & ErrorString.ToString()
                    Exit Sub
                End If
                '----------------------------------
                ' Draw graph
                '----------------------------------
                DrawGraph()
            End If
            '----------------------------------
            ' Get the operation status
            '----------------------------------
            Ret = DigGetStatus(Id, Status)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigGetStatus = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            LabelStatus.Text = Hex(Status) & "H"
        Loop While (Status And DIG_STATUS_BUSY) = DIG_STATUS_BUSY

        '----------------------------------
        ' Get the number of records which are being stored in driver memory
        '----------------------------------
        Ret = DigGetDriverMemoryRecords(Id, Records)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetDriverMemoryRecords = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' If there are records in driver memory, retrieve the data
        '----------------------------------
        If Records > 0 Or Records - -1 Then
            Ret = DigReadLatestRecords(Id, DIG_RECORD_DATA, 1, Data, Nothing, Nothing)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigReadLatestRecords = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            '----------------------------------
            ' Draw graph
            '----------------------------------
            DrawGraph()
        End If

    End Sub

    '==================================
    ' Show information
    '==================================
    Private Sub CommandInfo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandInfo.Click
        Dim Ret As Integer    ' Return Value
        Dim Ret2 As Integer   ' Return Value2
        Dim ErrorString As New StringBuilder("", 256)     ' Error string
        Dim Status As Integer    ' Status
        Dim SampleRate As Single    ' Sample rate
        Dim Duty As Single  ' Duty ratio
        Dim Level As Single   ' Trigger level
        Dim Slope As Short    ' Trigger direction
        Dim Count As Integer  ' Trigger count

        '----------------------------------
        ' Get the operation status
        '----------------------------------
        Ret = DigGetStatus(Id, Status)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetStatus = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        LabelStatus.Text = Hex(Status) & "H"
        '-------------------------------------
        ' Get the information set by DigAutoSetup function automatically
        '-------------------------------------
        '-------------------------------
        ' Get the sample rate
        '-------------------------------
        Ret = DigGetSampleRate(Id, SampleRate)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetSampleRate = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextDeviceInfo.Text = "Sample rate :" & SampleRate & "Hz" & vbCrLf
        '-------------------------------
        ' Get the record length
        '-------------------------------
        Ret = DigGetRecordLength(Id, RecordLength)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetSampleRate = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextDeviceInfo.Text = TextDeviceInfo.Text & "Record length:" & RecordLength & vbCrLf
        '-------------------------------
        ' Get duty ratio
        '-------------------------------
        Ret = DigGetDutyCycle(Id, Duty)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetDutyCycle = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextDeviceInfo.Text = TextDeviceInfo.Text & "Duty ratio:" & Duty & "%" & vbCrLf
        '-------------------------------
        ' Get the number of effective channels
        '-------------------------------
        Ret = DigGetChannels(Id, Channels)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetChannels = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        '-------------------------------
        ' Get the trigger information
        '-------------------------------
        Ret = DigGetPostTriggerAiEdge(Id, TriggerChannel, Level, Slope, Count)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetPostTriggerAiEdge = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextDeviceInfo.Text = TextDeviceInfo.Text & "Channel:" & TriggerChannel & vbCrLf
        TextDeviceInfo.Text = TextDeviceInfo.Text & "Trigger level:" & Level & "V" & vbCrLf
        '-------------------------------
        ' Get the range information
        '-------------------------------
        Ret = DigGetRange(Id, TriggerChannel, Range)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetRange = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextDeviceInfo.Text = TextDeviceInfo.Text & "Input range:" & Range & "VPP" & vbCrLf

        TextInfo.Text = "Show information : Normality completion"
    End Sub

    '==================================
    ' Acquisition stop
    '==================================
    Private Sub CommandStop_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandStop.Click
        Dim Ret As Integer    ' Return value
        Dim Ret2 As Integer   ' Return value2
        Dim ErrorString As New StringBuilder("", 256)     ' Error string

        Ret = DigStop(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigExit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "Acquisition stop : Normality completion"
    End Sub


    '==================================
    ' Exit handling
    '==================================
    Private Sub CommandExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandExit.Click
        Dim Ret As Integer    ' Return value
        Dim Ret2 As Integer   ' Return value2
        Dim ErrorString As New StringBuilder("", 256)     ' Error string

        Ret = DigExit(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigExit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "Exit : Normality completion"
    End Sub

    '==================================
    ' Form unload
    '==================================
    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        Dim Ret As Integer    ' Return value
        Ret = DigExit(Id)
    End Sub

    '==================================
    ' Draw graph
    '==================================
    Private Sub DrawGraph()
        Dim XUnit As Single   ' Drawing unit of X axis
        Dim PosX As Single    ' Drawing coordinate of X axis
        Dim PosY As Single    ' Drawing coordinate of Y axis
        Dim OldX As Single    ' For saving the drawing coordinate of X axis
        Dim OldY As Single    ' For saving the drawing coordinate of Y axis
        Dim i As Integer

        '-----------------------------------------
        ' Draw the current data in graph
        '-----------------------------------------
        Dim pen As New Pen(System.Drawing.Color.Red, 1)  ' Create red pen
        Graph.Image = bmp
        Dim grp As Graphics = Graphics.FromImage(Graph.Image)  ' Create a new graphics object from the specified image object
        grp.Clear(Graph.BackColor)                             ' Clear the graph
        '----------------------------------------------------
        ' Repeat the drawing handling only for the times which is equal to the numer of acquisition data
        ' In the effective channels, only the data of the least channel number will be shown
        '----------------------------------------------------
        XUnit = Graph.Size.Width() / CSng(RecordLength)  ' Calculate the moving pixel number of X axis for data 1
        OldY = Graph.Height * ((Range / 2) - Data(0)) / Range  ' Calculate the coordinate of Y axis
        If OldY >= Graph.Height Then
            OldY = Graph.Height - 1   ' If the initial value of Y axis exceeds the drawing rect of graph, modify the position
        End If
        If OldY < 0 Then
            OldY = 0     ' If the coordinate of Y axis is lower than the drawing rect,modify the drawing position
        End If
        OldX = 0
        For i = Channels To RecordLength * Channels Step Channels
            PosX = OldX + XUnit      ' Calculate the coordinate of X axis
            PosY = Graph.Height() * ((Range / 2) - Data(i)) / Range    ' Calculate the coordinate of Y axis
            If PosY >= Graph.Height() Then
                ' If the coordinate of Y axis exceeds the drawing rect
                PosY = Graph.Height() - 1    ' Modify the drawing position

            ElseIf PosY < 0 Then
                ' If the coordinate of Y axis is lower than the drawing rect
                PosY = 0    ' Modify the drawing position
            End If
            grp.DrawLine(pen, OldX, OldY, PosX, PosY)     ' Draw

            OldX = PosX   ' For saving the current coordinate of X axis
            OldY = PosY   ' For saving the current coordinate of Y axis
        Next

        pen.Dispose()   ' Reales all resoures used by this pen object
        grp.Dispose()   ' Reales all resoures used by this graphics object
    End Sub
End Class
