﻿Imports VB = Microsoft.VisualBasic
Imports System.Text
Imports System.Runtime.InteropServices

Public Class Form1
    Dim Id As Short                     'Device ID
    Dim Ret As Integer                  'Return code
    Dim szError As New StringBuilder("", 256)
    Dim UpCount(4) As Short             'Up counter
    Dim DownCount(4) As Short           'Down counter
    Dim Edit_Up(4) As TextBox
    Dim Edit_Down(4) As TextBox
    Dim gCh As GCHandle
    Dim pdelegate_func As PINTCALLBACK
    Dim pfunc As IntPtr

    Delegate Sub SetCountDelegate(ByVal BitNo As Integer, ByVal Kind As Integer)

    '=========================================================
    ' Dialog initialization
    '=========================================================
    Private Sub InterruptCallback_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

        Dim i As Short

        Me.Left = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2

        Edit_Up(0) = Edit_Up_0
        Edit_Up(1) = Edit_Up_1
        Edit_Up(2) = Edit_Up_2
        Edit_Up(3) = Edit_Up_3

        Edit_Down(0) = Edit_Down_0
        Edit_Down(1) = Edit_Down_1
        Edit_Down(2) = Edit_Down_2
        Edit_Down(3) = Edit_Down_3

        '--------------------------------------
        ' Screen item initialization
        '--------------------------------------
        Edit_DeviceName.Text = "DIO000"
        Edit_ReturnCode.Text = ""
        'Up counter
        For i = 0 To 3
            Edit_Up(i).Text = ""
        Next i
        'Down counter
        For i = 0 To 3
            Edit_Down(i).Text = ""
        Next i
        Radio_Rise.Checked = True
        Edit_ReturnCode.Text = ""
        '--------------------------------------
        ' Initialize the delegate for interrupt processing
        '--------------------------------------
        pdelegate_func = New PINTCALLBACK(AddressOf CallBackProc)
        '--------------------------------------
        ' Do not release the delegate
        '--------------------------------------
        If gCh.IsAllocated = False Then
            gCh = GCHandle.Alloc(pdelegate_func)
        End If
    End Sub

    '=========================================================
    ' DioInit()
    '=========================================================
    Private Sub Button_DioInit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_DioInit.Click

        Dim szDeviceName As String

        '--------------------------------------
        ' Get device name from screen
        '--------------------------------------
        szDeviceName = Edit_DeviceName.Text
        '--------------------------------------
        ' Execute initialization function and get ID
        '--------------------------------------
        Ret = DioInit(szDeviceName, Id)
        '--------------------------------------
        ' Error process
        '--------------------------------------
        Call DioGetErrorString(Ret, szError)
        Edit_ReturnCode.Text = "DioInit Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' Start monitoring interrupt
    '=========================================================
    Private Sub Button_IntStart_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_IntStart.Click

        Dim i As Short
        Dim BitNo As Short
        Dim Kind As Short

        '--------------------------------------
        ' Get the fixed pointer of the delegate for interrupt processing
        '--------------------------------------
        pfunc = Marshal.GetFunctionPointerForDelegate(pdelegate_func)
        '--------------------------------------
        ' Set callback routine
        '--------------------------------------
        Dim Temp As IntPtr = New IntPtr(Id)
        Ret = DioSetInterruptCallBackProc(Id, pfunc, Temp)
        '--------------------------------------
        ' Error process
        '--------------------------------------
        Call DioGetErrorString(Ret, szError)
        Edit_ReturnCode.Text = "DioSetInterruptCallBackProc Ret = " & Ret & ":" & szError.ToString()
        '--------------------------------------
        ' Parameters initialization
        '--------------------------------------
        For i = 0 To 3
            UpCount(i) = 0                      'Up counter
            DownCount(i) = 0                    'Down counter
        Next i
        If Radio_Rise.Checked = True Then
            Kind = DIO_INT_RISE
        Else
            Kind = DIO_INT_FALL
        End If
        '--------------------------------------
        ' Start interrupt
        '--------------------------------------
        For BitNo = 0 To 3
            '--------------------------------------
            ' Set interrupt event
            '--------------------------------------
            Ret = DioNotifyInterrupt(Id, BitNo, Kind, 0)
            If (Ret <> DIO_ERR_SUCCESS) Then
                Exit For
            End If
        Next BitNo
        '--------------------------------------
        ' Error process
        '--------------------------------------
        Call DioGetErrorString(Ret, szError)
        Edit_ReturnCode.Text = "DioNotifyInterrupt Ret = " & Ret & ":" & szError.ToString()

    End Sub

    '=========================================================
    ' Stop monitoring interrupt
    '=========================================================
    Private Sub Button_IntStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_IntStop.Click

        Dim BitNo As Short

        '--------------------------------------
        ' Stop interrupt
        '--------------------------------------
        For BitNo = 0 To 3
            '--------------------------------------
            ' Set interrupt event
            '--------------------------------------
            Ret = DioNotifyInterrupt(Id, BitNo, DIO_INT_NONE, 0)
            If (Ret <> DIO_ERR_SUCCESS) Then
                Exit For
            End If
        Next BitNo
        '--------------------------------------
        ' Error process
        '--------------------------------------
        Call DioGetErrorString(Ret, szError)
        Edit_ReturnCode.Text = "DioNotifyInterrupt Ret = " & Ret & ":" & szError.ToString()

    End Sub

    '=========================================================
    ' DioExit()
    '=========================================================
    Private Sub Button_DioExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_DioExit.Click

        '--------------------------------------
        ' DioExit()
        '--------------------------------------
        Ret = DioExit(Id)
        '--------------------------------------
        ' Error process
        '--------------------------------------
        Call DioGetErrorString(Ret, szError)
        Edit_ReturnCode.Text = "DioExit Ret = " & Ret & ":" & szError.ToString()

    End Sub

    '=========================================================
    ' Dialog exit
    '=========================================================
    Private Sub Button_Exit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_Exit.Click

        '--------------------------------------
        ' DioExit
        '--------------------------------------
        Ret = DioExit(Id)
        '--------------------------------------
        ' Release the garbage collection handle of the delegate for interrupt processing
        '--------------------------------------
        If gCh.IsAllocated = True Then
            gCh.Free()
        End If
        '--------------------------------------
        ' Dialog exit
        '--------------------------------------
        End
    End Sub

    '=========================================================
    ' Subroutine: Get HIWORD,LOWORD
    '=========================================================
    Function HiLoWord(ByRef Number As Integer, ByRef HiLo As Short) As Integer

        Dim Hi As Integer
        Dim Lo As Integer
        Dim StrData As String
        Dim i As Short

        StrData = Hex(Number)
        If Len(StrData) < 8 Then
            For i = 1 To 8 - Len(StrData)
                StrData = "0" & StrData
            Next i
        End If

        Lo = Val("&h" & VB.Right(StrData, 4))
        Hi = Val("&h" & VB.Left(StrData, 4))
        If HiLo = 0 Then
            HiLoWord = Lo
        ElseIf HiLo = 1 Then
            HiLoWord = Hi
        End If

        Return HiLoWord

    End Function

    '=========================================================
    ' Synchronous function for control operation
    '=========================================================
    Sub SetCountText(ByVal BitNo As Integer, ByVal Kind As Integer)
        Edit_Up(BitNo).Text = CStr(UpCount(BitNo))
        Edit_Down(BitNo).Text = CStr(DownCount(BitNo))
    End Sub

    '=========================================================
    ' Callback routine
    '=========================================================
    Protected Sub CallBackProc(ByVal Id As Short, ByVal wParam As Integer, ByVal lParam As Integer, ByVal Param As IntPtr)
        Dim BitNo As Short
        Dim Kind As Short

        BitNo = HiLoWord(lParam, 0)                      'Interrupt BitNo
        Kind = HiLoWord(lParam, 1)                       'Rise:1, Fall:2

        If (Kind And DIO_INT_RISE) Then
            UpCount(BitNo) = UpCount(BitNo) + 1          'Up counter
        End If
        If (Kind And DIO_INT_FALL) Then
            DownCount(BitNo) = DownCount(BitNo) + 1      'Down counter
        End If
        '--------------------------------------
        ' Expression
        '--------------------------------------
        Invoke(New SetCountDelegate(AddressOf SetCountText), New Object() {BitNo, Kind})
    End Sub
End Class
