﻿Imports System.Text

Public Class Form1
    '----------------------------------------
    ' Declaration of global variables
    '----------------------------------------
    Dim g_id As Short               ' ID for device access
    Dim g_timer_id As Short         ' Timer ID
    Dim g_event_count As Integer    ' Interval elapsed event occurrence count

    Private Sub Timer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' Set the default button
        '----------------------------------------
        AcceptButton = Button_Close
        '----------------------------------------
        ' Update the initial display of device name
        '----------------------------------------
        TextBox_DeviceName.Text = "AIO000"
        '----------------------------------------
        ' Update the initial display of interval
        '----------------------------------------
        TextBox_Interval.Text = "1000"
        '----------------------------------------
        ' Update the initial display of notification count
        '----------------------------------------
        TextBox_EventCount.Text = "0"
        '----------------------------------------
        ' Update the initial display of elapsed time
        '----------------------------------------
        TextBox_LapTime.Text = "0"
        '----------------------------------------
        ' Initialize global variables
        '----------------------------------------
        g_id = 0
        g_timer_id = 0
        g_event_count = 0
    End Sub

    '================================================================================
    ' It is the process when the AioInit button is clicked
    ' Initialize the device
    '================================================================================
    Private Sub Button_AioInit_Click(sender As Object, e As EventArgs) Handles Button_AioInit.Click
        Dim device_name As String                         ' Device name
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Get device name from textbox
        '----------------------------------------
        device_name = TextBox_DeviceName.Text
        '----------------------------------------
        ' Device initialization
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioInit(device_name, g_id)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioInit = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the AioResetDevice button is clicked
    ' Reset the device
    '================================================================================
    Private Sub Button_AioResetDevice_Click(sender As Object, e As EventArgs) Handles Button_AioResetDevice.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Reset device
        '----------------------------------------
        ret = AioResetDevice(g_id)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioResetDevice = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the Operation Start (Interval Timer) button is clicked
    ' Start operation
    '================================================================================
    Private Sub Button_IntervalStart_Click(sender As Object, e As EventArgs) Handles Button_IntervalStart.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim interval As Single                            ' Interval

        '----------------------------------------
        ' Set the event factor to 'Event that interval elapses'
        '----------------------------------------
        ret = AioSetTmEvent(g_id, g_timer_id, Handle, TME_INT)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Operation Start: AioSetTmEvent = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Initialize notification count
        '----------------------------------------
        g_event_count = 0
        '----------------------------------------
        ' Get the interval from textbox
        '----------------------------------------
        If (Single.TryParse(TextBox_Interval.Text, interval) = False) Then
            interval = 1000
        End If
        '----------------------------------------
        ' Start operation
        '----------------------------------------
        ret = AioStartTmTimer(g_id, g_timer_id, interval)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Operation Start: AioStartTmTimer = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display the message that operation start is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Operation Start: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the Operation Stop (Interval Timer) button is clicked
    ' Stop operation
    '================================================================================
    Private Sub Button_IntervalStop_Click(sender As Object, e As EventArgs) Handles Button_IntervalStop.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Stop operation
        '----------------------------------------
        ret = AioStopTmTimer(g_id, g_timer_id)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Operation Stop: AioStopTmTimer = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display the message that operation stop is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Operation Stop: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the Operation Start (Stop Watch Timer) button is clicked
    ' Start measurement
    '================================================================================
    Private Sub Button_WatchStart_Click(sender As Object, e As EventArgs) Handles Button_WatchStart.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Start measurement
        '----------------------------------------
        ret = AioStartTmCount(g_id, g_timer_id)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Operation Start: AioStartTmCount = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display the message that measurement start is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Operation Start: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the AioLapTmCount button is clicked
    ' Get the elapsed time
    '================================================================================
    Private Sub Button_LapTmCount_Click(sender As Object, e As EventArgs) Handles Button_LapTmCount.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim lap As Integer                                ' Elapsed time

        '----------------------------------------
        ' Get the elapsed time
        '----------------------------------------
        ret = AioLapTmCount(g_id, g_timer_id, lap)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioLapTmCount = " & ret.ToString() & " : " & error_string.ToString()
        '----------------------------------------
        ' When the elapsed time acquisition function is completed successfully
        '----------------------------------------
        If (ret = 0) Then
            '----------------------------------------
            ' Display the elapsed time
            '----------------------------------------
            TextBox_LapTime.Text = lap.ToString()
        End If
    End Sub

    '================================================================================
    ' It is the process when the AioResetTmCount button is clicked
    ' Reset the elapsed time
    '================================================================================
    Private Sub Button_ResetTmCount_Click(sender As Object, e As EventArgs) Handles Button_ResetTmCount.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Reset
        '----------------------------------------
        ret = AioResetTmCount(g_id, g_timer_id)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioResetTmCount = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the Operation Stop (Stop Watch Timer) button is clicked
    ' Stop measurement
    '================================================================================
    Private Sub Button_WatchStop_Click(sender As Object, e As EventArgs) Handles Button_WatchStop.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Stop measurement
        '----------------------------------------
        ret = AioStopTmCount(g_id, g_timer_id)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Operation Stop: AioStopTmCount = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display the message that measurement stop is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Operation Stop: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the AioExit button is clicked
    ' Perform the exit process of device
    '================================================================================
    Private Sub Button_AioExit_Click(sender As Object, e As EventArgs) Handles Button_AioExit.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Exit process
        '----------------------------------------
        ret = AioExit(g_id)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioExit = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the Close button is clicked
    ' Perform the exit process of device and terminate the application
    '================================================================================
    Private Sub Button_Close_Click(sender As Object, e As EventArgs) Handles Button_Close.Click
        '----------------------------------------
        ' Exit process
        '----------------------------------------
        AioExit(g_id)
        '----------------------------------------
        ' Close dialog
        '----------------------------------------
        Me.Close()
    End Sub

    '================================================================================
    ' Operation when event occurred
    ' Process for each event occurred
    '================================================================================
    Protected Overrides Sub WndProc(ByRef m As Message)
        '----------------------------------------
        ' Process of Event that interval elapses
        '----------------------------------------
        If (m.Msg = AIOM_TME_INT) Then
            '----------------------------------------
            ' Display the interval elapsed event occurrence count
            '----------------------------------------
            g_event_count += 1
            TextBox_EventCount.Text = g_event_count.ToString()
            Exit Sub
        End If
        MyBase.WndProc(m)
    End Sub
End Class
