﻿Imports System.Text

Public Class Form1
    '----------------------------------------
    ' Macro definition
    '----------------------------------------
    Public Const MAX_PULSE_MEASUREMENT_CH As Integer = 4        ' Maximum number of pulse measurement channels

    '----------------------------------------
    ' Global variable
    '----------------------------------------
    Dim dev_id As Short                                         ' Device ID
    Dim m_Logic(MAX_PULSE_MEASUREMENT_CH - 1) As ComboBox       ' Pulse measurement logic value control
    Dim m_TimeUnit(MAX_PULSE_MEASUREMENT_CH - 1) As ComboBox    ' Pulse measurement time unit value control
    Dim m_Period(MAX_PULSE_MEASUREMENT_CH - 1) As TextBox       ' Pulse measurement period value control
    Dim m_TimeValue(MAX_PULSE_MEASUREMENT_CH - 1) As TextBox    ' Pulse measurement time value control
    Dim m_Status(MAX_PULSE_MEASUREMENT_CH - 1) As TextBox       ' Pulse measurement status value control
    Dim m_Error(MAX_PULSE_MEASUREMENT_CH - 1) As TextBox        ' Pulse measurement error value control

    '=========================================================
    ' Dialog initialization
    '=========================================================
    Private Sub pulse_measurement_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ch_no As Short

        '----------------------------------------
        ' Set control array
        '----------------------------------------
        '--------------------
        ' Pulse measurement logic control
        '--------------------
        m_Logic(0) = ComboBox_Logic0
        m_Logic(1) = ComboBox_Logic1
        m_Logic(2) = ComboBox_Logic2
        m_Logic(3) = ComboBox_Logic3
        '--------------------
        ' Pulse measurement period control
        '--------------------
        m_Period(0) = TextBox_Period0
        m_Period(1) = TextBox_Period1
        m_Period(2) = TextBox_Period2
        m_Period(3) = TextBox_Period3
        '--------------------
        ' Pulse measurement time control
        '--------------------
        m_TimeValue(0) = TextBox_TimeValue0
        m_TimeValue(1) = TextBox_TimeValue1
        m_TimeValue(2) = TextBox_TimeValue2
        m_TimeValue(3) = TextBox_TimeValue3
        '--------------------
        ' Pulse measurement time unit control
        '--------------------
        m_TimeUnit(0) = ComboBox_TimeUnit0
        m_TimeUnit(1) = ComboBox_TimeUnit1
        m_TimeUnit(2) = ComboBox_TimeUnit2
        m_TimeUnit(3) = ComboBox_TimeUnit3
        '--------------------
        ' Pulse measurement status control
        '--------------------
        m_Status(0) = TextBox_Status0
        m_Status(1) = TextBox_Status1
        m_Status(2) = TextBox_Status2
        m_Status(3) = TextBox_Status3
        '--------------------
        ' Pulse measurement error control
        '--------------------
        m_Error(0) = TextBox_Error0
        m_Error(1) = TextBox_Error1
        m_Error(2) = TextBox_Error2
        m_Error(3) = TextBox_Error3
        '----------------------------------------
        ' Initialize the display
        '----------------------------------------
        '--------------------
        ' Device name
        '--------------------
        TextBox_DeviceName.Text = "DIO000"
        '--------------------
        ' Number of channels
        '--------------------
        ComboBox_ChannelNum.Items.Add("1")
        ComboBox_ChannelNum.Items.Add("2")
        ComboBox_ChannelNum.Items.Add("3")
        ComboBox_ChannelNum.Items.Add("4")
        ComboBox_ChannelNum.SelectedItem = "4"
        '--------------------
        ' For each channel
        '--------------------
        For ch_no = 0 To (MAX_PULSE_MEASUREMENT_CH - 1)
            '--------------------
            ' Pulse measurement logic control
            '--------------------
            m_Logic(ch_no).Items.Add("OFF Time")
            m_Logic(ch_no).Items.Add("ON Time")
            m_Logic(ch_no).SelectedItem = "OFF Time"
            '--------------------
            ' Pulse measurement period control
            '--------------------
            m_Period(ch_no).Text = "0"
            '--------------------
            ' Pulse measurement time unit control
            '--------------------
            m_TimeUnit(ch_no).Items.Add("sec")
            m_TimeUnit(ch_no).Items.Add("msec")
            m_TimeUnit(ch_no).Items.Add("usec")
            m_TimeUnit(ch_no).Items.Add("nsec")
            m_TimeUnit(ch_no).SelectedItem = "nsec"
            '--------------------
            ' Pulse measurement time control
            '--------------------
            m_TimeValue(ch_no).Text = ""
            '--------------------
            ' Pulse measurement status control
            '--------------------
            m_Status(ch_no).Text = ""
            '--------------------
            ' Pulse measurement error control
            '--------------------
            m_Error(ch_no).Text = ""
        Next ch_no
    End Sub

    '=========================================================
    ' It is the process when the DioInit button is clicked
    ' Initialize the device
    '=========================================================
    Private Sub Button_DioInit_Click(sender As Object, e As EventArgs) Handles Button_DioInit.Click
        Dim device_name As String
        Dim error_string As New StringBuilder("", 256)
        Dim ret As Integer

        '----------------------------------------
        ' Get device name
        '----------------------------------------
        device_name = TextBox_DeviceName.Text
        '----------------------------------------
        ' Initialization and get device ID
        '----------------------------------------
        ret = DioInit(device_name, dev_id)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        DioGetErrorString(ret, error_string)
        TextBox_ReturnCode.Text = String.Format("DioInit = {0} : {1}", ret, error_string)
    End Sub

    '=========================================================
    ' It is the process when the DioSetPulseMeasurementSetting button is clicked
    ' Set the pulse measurement of the device
    '=========================================================
    Private Sub Button_DioSetPulseMeasurementSetting_Click(sender As Object, e As EventArgs) Handles Button_DioSetPulseMeasurementSetting.Click
        Dim str_tmp As String
        Dim error_string As New StringBuilder("", 256)
        Dim pulse_measurement_target(MAX_PULSE_MEASUREMENT_CH - 1) As UShort
        Dim pulse_measurement_period(MAX_PULSE_MEASUREMENT_CH - 1) As UShort
        Dim ch_no_array(MAX_PULSE_MEASUREMENT_CH - 1) As Short
        Dim ret As Integer
        Dim ch_no As Short
        Dim pulse_measurement_ch_num As Short

        '----------------------------------------
        ' Get number of channels
        '----------------------------------------
        Short.TryParse(ComboBox_ChannelNum.SelectedItem.ToString(), pulse_measurement_ch_num)
        '----------------------------------------
        ' For each channel
        '----------------------------------------
        For ch_no = 0 To (pulse_measurement_ch_num - 1)
            '----------------------------------------
            ' Store channel number
            '----------------------------------------
            ch_no_array(ch_no) = ch_no
            '----------------------------------------
            ' Get specified value of pulse measurement logic
            '----------------------------------------
            str_tmp = m_Logic(ch_no).SelectedItem.ToString()
            If (String.Equals(str_tmp, "OFF Time")) Then
                pulse_measurement_target(ch_no) = DIO_PULSE_MEASUREMENT_TARGET_OFF_TIME
            Else
                pulse_measurement_target(ch_no) = DIO_PULSE_MEASUREMENT_TARGET_ON_TIME
            End If
            '----------------------------------------
            ' Get specified value of pulse measurement period
            '----------------------------------------
            If (UShort.TryParse(m_Period(ch_no).Text, pulse_measurement_period(ch_no)) = False) Then
                pulse_measurement_period(ch_no) = 0
            End If
        Next ch_no
        '----------------------------------------
        ' Set pulse measurement
        '----------------------------------------
        ret = DioSetPulseMeasurementSetting(dev_id, ch_no_array, pulse_measurement_ch_num, pulse_measurement_target, pulse_measurement_period)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        DioGetErrorString(ret, error_string)
        TextBox_ReturnCode.Text = String.Format("DioSetPulseMeasurementSetting = {0} : {1}", ret, error_string)
    End Sub

    '=========================================================
    ' It is the process when the DioStartPulseMeasurement button is clicked
    ' Start the pulse measurement
    '=========================================================
    Private Sub Button_DioStartPulseMeasurement_Click(sender As Object, e As EventArgs) Handles Button_DioStartPulseMeasurement.Click
        Dim error_string As New StringBuilder("", 256)
        Dim ch_no_array(MAX_PULSE_MEASUREMENT_CH - 1) As Short
        Dim ret As Integer
        Dim ch_no As Short
        Dim pulse_measurement_ch_num As Short

        '----------------------------------------
        ' Get number of channels
        '----------------------------------------
        Short.TryParse(ComboBox_ChannelNum.SelectedItem.ToString(), pulse_measurement_ch_num)
        '----------------------------------------
        ' For each channel
        '----------------------------------------
        For ch_no = 0 To (pulse_measurement_ch_num - 1)
            '----------------------------------------
            ' Store channel number
            '----------------------------------------
            ch_no_array(ch_no) = ch_no
        Next ch_no
        '----------------------------------------
        ' Start pulse measurement
        '----------------------------------------
        ret = DioStartPulseMeasurement(dev_id, ch_no_array, pulse_measurement_ch_num)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        DioGetErrorString(ret, error_string)
        TextBox_ReturnCode.Text = String.Format("DioStartPulseMeasurement = {0} : {1}", ret, error_string)
    End Sub

    '=========================================================
    ' It is the process when the DioReadPulseMeasurementData button is clicked
    ' Get the pulse measurement data
    '=========================================================
    Private Sub Button_DioReadPulseMeasurementData_Click(sender As Object, e As EventArgs) Handles Button_DioReadPulseMeasurementData.Click
        Dim str_tmp As String
        Dim error_string As New StringBuilder("", 256)
        Dim pulse_measurement_time_value(MAX_PULSE_MEASUREMENT_CH - 1) As ULong
        Dim pulse_measurement_status(MAX_PULSE_MEASUREMENT_CH - 1) As UShort
        Dim pulse_measurement_error(MAX_PULSE_MEASUREMENT_CH - 1) As UShort
        Dim pulse_measurement_time_unit(MAX_PULSE_MEASUREMENT_CH - 1) As UShort
        Dim ch_no_array(MAX_PULSE_MEASUREMENT_CH - 1) As Short
        Dim ret As Integer
        Dim ch_no As Short
        Dim pulse_measurement_ch_num As Short

        '----------------------------------------
        ' Get number of channels
        '----------------------------------------
        Short.TryParse(ComboBox_ChannelNum.SelectedItem.ToString(), pulse_measurement_ch_num)
        '----------------------------------------
        ' For each channel
        '----------------------------------------
        For ch_no = 0 To (pulse_measurement_ch_num - 1)
            '----------------------------------------
            ' Store channel number
            '----------------------------------------
            ch_no_array(ch_no) = ch_no
            '----------------------------------------
            ' Initialize pulse measurement time
            '----------------------------------------
            pulse_measurement_time_value(ch_no) = 0
            '----------------------------------------
            ' Get specified value of pulse measurement time unit
            '----------------------------------------
            str_tmp = m_TimeUnit(ch_no).SelectedItem.ToString()
            If (String.Equals(str_tmp, "sec")) Then
                pulse_measurement_time_unit(ch_no) = DIO_PULSE_MEASUREMENT_TIME_UNIT_S
            ElseIf (String.Equals(str_tmp, "msec")) Then
                pulse_measurement_time_unit(ch_no) = DIO_PULSE_MEASUREMENT_TIME_UNIT_MS
            ElseIf (String.Equals(str_tmp, "usec")) Then
                pulse_measurement_time_unit(ch_no) = DIO_PULSE_MEASUREMENT_TIME_UNIT_US
            Else
                pulse_measurement_time_unit(ch_no) = DIO_PULSE_MEASUREMENT_TIME_UNIT_NS
            End If
        Next ch_no
        '----------------------------------------
        ' Get pulse measurement data
        '----------------------------------------
        ret = DioReadPulseMeasurementData(dev_id, ch_no_array, pulse_measurement_ch_num, pulse_measurement_status, pulse_measurement_error, pulse_measurement_time_value, pulse_measurement_time_unit)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        DioGetErrorString(ret, error_string)
        TextBox_ReturnCode.Text = String.Format("DioReadPulseMeasurementData = {0} : {1}", ret, error_string)
        '----------------------------------------
        ' Terminate process in case of error
        '----------------------------------------
        If (ret <> DIO_ERR_SUCCESS) Then
            Return
        End If
        '----------------------------------------
        ' For each channel
        '----------------------------------------
        For ch_no = 0 To (pulse_measurement_ch_num - 1)
            '----------------------------------------
            ' Update the display of pulse measurement status
            '----------------------------------------
            m_Status(ch_no).Text = pulse_measurement_status(ch_no).ToString("X")
            '----------------------------------------
            ' Update the display of pulse measurement error
            '----------------------------------------
            m_Error(ch_no).Text = pulse_measurement_error(ch_no).ToString("X")
            '----------------------------------------
            ' Update the display of pulse measurement time
            '----------------------------------------
            m_TimeValue(ch_no).Text = pulse_measurement_time_value(ch_no).ToString()
        Next ch_no
    End Sub

    '=========================================================
    ' It is the process when the DioStopPulseMeasurement button is clicked
    ' Stop the pulse measurement
    '=========================================================
    Private Sub Button_DioStopPulseMeasurement_Click(sender As Object, e As EventArgs) Handles Button_DioStopPulseMeasurement.Click
        Dim error_string As New StringBuilder("", 256)
        Dim ch_no_array(MAX_PULSE_MEASUREMENT_CH - 1) As Short
        Dim ret As Integer
        Dim ch_no As Short
        Dim pulse_measurement_ch_num As Short

        '----------------------------------------
        ' Get number of channels
        '----------------------------------------
        Short.TryParse(ComboBox_ChannelNum.SelectedItem.ToString(), pulse_measurement_ch_num)
        '----------------------------------------
        ' For each channel
        '----------------------------------------
        For ch_no = 0 To (pulse_measurement_ch_num - 1)
            '----------------------------------------
            ' Store channel number
            '----------------------------------------
            ch_no_array(ch_no) = ch_no
        Next ch_no
        '----------------------------------------
        ' Stop pulse measurement
        '----------------------------------------
        ret = DioStopPulseMeasurement(dev_id, ch_no_array, pulse_measurement_ch_num)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        DioGetErrorString(ret, error_string)
        TextBox_ReturnCode.Text = String.Format("DioStopPulseMeasurement = {0} : {1}", ret, error_string)
    End Sub

    '=========================================================
    ' It is the process when the DioExit button is clicked
    ' Perform the exit process of device
    '=========================================================
    Private Sub Button_DioExit_Click(sender As Object, e As EventArgs) Handles Button_DioExit.Click
        Dim error_string As New StringBuilder("", 256)
        Dim ret As Integer

        '----------------------------------------
        ' Exit process
        '----------------------------------------
        ret = DioExit(dev_id)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        DioGetErrorString(ret, error_string)
        TextBox_ReturnCode.Text = String.Format("DioExit = {0} : {1}", ret, error_string)
    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_Exit_Click(sender As Object, e As EventArgs) Handles Button_Exit.Click
        '----------------------------------------
        ' Exit process
        '----------------------------------------
        DioExit(dev_id)
        '----------------------------------------
        ' Close dialog
        '----------------------------------------
        Close()
    End Sub
End Class
