﻿Imports System.Text

Public Class pwm_output
    '----------------------------------------
    ' Macro definition
    '----------------------------------------
    Public Const MAX_PWM_CH As Integer = 4               ' Maximum number of PWM channels

    '----------------------------------------
    ' Global variable
    '----------------------------------------
    Dim dev_id As Short                                  ' Device ID
    Dim m_PeriodUnit(MAX_PWM_CH - 1) As ComboBox         ' Pulse period unit control
    Dim m_PulsePhase(MAX_PWM_CH - 1) As ComboBox         ' Pulse phase shift control
    Dim m_PulsePeriodValue(MAX_PWM_CH - 1) As TextBox    ' PWM pulse period value control
    Dim m_PulseDutyValue(MAX_PWM_CH - 1) As TextBox      ' PWM pulse duty ratio control
    Dim m_PwmStatus(MAX_PWM_CH - 1) As TextBox           ' PWM status control
    Dim m_PwmError(MAX_PWM_CH - 1) As TextBox            ' PWM error control

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

        '----------------------------------------
        ' Set control array
        '----------------------------------------
        '--------------------
        ' Pulse period control
        '--------------------
        m_PulsePeriodValue(0) = TextBox_PulsePeriod0
        m_PulsePeriodValue(1) = TextBox_PulsePeriod1
        m_PulsePeriodValue(2) = TextBox_PulsePeriod2
        m_PulsePeriodValue(3) = TextBox_PulsePeriod3
        '--------------------
        ' Pulse period unit control
        '--------------------
        m_PeriodUnit(0) = ComboBox_PeriodUnit0
        m_PeriodUnit(1) = ComboBox_PeriodUnit1
        m_PeriodUnit(2) = ComboBox_PeriodUnit2
        m_PeriodUnit(3) = ComboBox_PeriodUnit3
        '--------------------
        ' Pulse duty ratio control
        '--------------------
        m_PulseDutyValue(0) = TextBox_PulseDuty0
        m_PulseDutyValue(1) = TextBox_PulseDuty1
        m_PulseDutyValue(2) = TextBox_PulseDuty2
        m_PulseDutyValue(3) = TextBox_PulseDuty3
        '--------------------
        ' Pulse phase shift control
        '--------------------
        m_PulsePhase(0) = ComboBox_PulsePhase0
        m_PulsePhase(1) = ComboBox_PulsePhase1
        m_PulsePhase(2) = ComboBox_PulsePhase2
        m_PulsePhase(3) = ComboBox_PulsePhase3
        '--------------------
        ' PWM status control
        '--------------------
        m_PwmStatus(0) = TextBox_PwmStatus0
        m_PwmStatus(1) = TextBox_PwmStatus1
        m_PwmStatus(2) = TextBox_PwmStatus2
        m_PwmStatus(3) = TextBox_PwmStatus3
        '--------------------
        ' PWM error control
        '--------------------
        m_PwmError(0) = TextBox_PwmError0
        m_PwmError(1) = TextBox_PwmError1
        m_PwmError(2) = TextBox_PwmError2
        m_PwmError(3) = TextBox_PwmError3
        '----------------------------------------
        ' 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_PWM_CH - 1)
            '--------------------
            ' Pulse period control
            '--------------------
            m_PulsePeriodValue(ch_no).Text = ((ch_no + 1) * 100).ToString()
            '--------------------
            ' Pulse period unit control
            '--------------------
            m_PeriodUnit(ch_no).Items.Add("sec")
            m_PeriodUnit(ch_no).Items.Add("msec")
            m_PeriodUnit(ch_no).Items.Add("usec")
            m_PeriodUnit(ch_no).Items.Add("nsec")
            m_PeriodUnit(ch_no).SelectedItem = "msec"
            '--------------------
            ' Pulse duty ratio control
            '--------------------
            m_PulseDutyValue(ch_no).Text = "32767"
            '--------------------
            ' Pulse phase shift control
            '--------------------
            m_PulsePhase(ch_no).Items.Add("0")
            m_PulsePhase(ch_no).Items.Add("90")
            m_PulsePhase(ch_no).Items.Add("180")
            m_PulsePhase(ch_no).SelectedItem = "0"
            '--------------------
            ' PWM status control
            '--------------------
            m_PwmStatus(ch_no).Text = ""
            '--------------------
            ' PWM error control
            '--------------------
            m_PwmError(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 DioSetPwmPulsePeriod button is clicked
    ' Set the PWM pulse period of the device
    '=========================================================
    Private Sub Button_DioSetPwmPulsePeriod_Click(sender As Object, e As EventArgs) Handles Button_DioSetPwmPulsePeriod.Click
        Dim str_tmp As String
        Dim error_string As New StringBuilder("", 256)
        Dim pulse_period_value(MAX_PWM_CH - 1) As ULong
        Dim pulse_period_unit(MAX_PWM_CH - 1) As UShort
        Dim ch_no_array(MAX_PWM_CH - 1) As Short
        Dim ret As Integer
        Dim ch_no As Short
        Dim pwm_ch_num As Short

        '----------------------------------------
        ' Get number of channels
        '----------------------------------------
        Short.TryParse(ComboBox_ChannelNum.SelectedItem.ToString(), pwm_ch_num)
        '----------------------------------------
        ' For each channel
        '----------------------------------------
        For ch_no = 0 To (pwm_ch_num - 1)
            '----------------------------------------
            ' Store channel number
            '----------------------------------------
            ch_no_array(ch_no) = ch_no
            '----------------------------------------
            ' Get specified value of PWM pulse period
            '----------------------------------------
            If (ULong.TryParse(m_PulsePeriodValue(ch_no).Text, pulse_period_value(ch_no)) = False) Then
                pulse_period_value(ch_no) = (ch_no + 1) * 100
            End If
            '----------------------------------------
            ' Get specified value of pulse period unit
            '----------------------------------------
            str_tmp = m_PeriodUnit(ch_no).SelectedItem.ToString()
            If (String.Equals(str_tmp, "sec")) Then
                pulse_period_unit(ch_no) = DIO_PWM_PULSE_UNIT_S
            ElseIf (String.Equals(str_tmp, "msec")) Then
                pulse_period_unit(ch_no) = DIO_PWM_PULSE_UNIT_MS
            ElseIf (String.Equals(str_tmp, "usec")) Then
                pulse_period_unit(ch_no) = DIO_PWM_PULSE_UNIT_US
            Else
                pulse_period_unit(ch_no) = DIO_PWM_PULSE_UNIT_NS
            End If
        Next ch_no
        '----------------------------------------
        ' PWM pulse period setting
        '----------------------------------------
        ret = DioSetPwmPulsePeriod(dev_id, ch_no_array, pwm_ch_num, pulse_period_value, pulse_period_unit)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        DioGetErrorString(ret, error_string)
        TextBox_ReturnCode.Text = String.Format("DioSetPwmPulsePeriod = {0} : {1}", ret, error_string)
    End Sub

    '=========================================================
    ' It is the process when the DioSetPwmPulsePhase button is clicked
    ' Set the PWM pulse phase shift of the device
    '=========================================================
    Private Sub Button_DioSetPwmPulsePhase_Click(sender As Object, e As EventArgs) Handles Button_DioSetPwmPulsePhase.Click
        Dim str_tmp As String
        Dim error_string As New StringBuilder("", 256)
        Dim pulse_phase(MAX_PWM_CH - 1) As Short
        Dim ch_no_array(MAX_PWM_CH - 1) As Short
        Dim ret As Integer
        Dim ch_no As Short
        Dim pwm_ch_num As Short

        '----------------------------------------
        ' Get number of channels
        '----------------------------------------
        Short.TryParse(ComboBox_ChannelNum.SelectedItem.ToString(), pwm_ch_num)
        '----------------------------------------
        ' For each channel
        '----------------------------------------
        For ch_no = 0 To (pwm_ch_num - 1)
            '----------------------------------------
            ' Store channel number
            '----------------------------------------
            ch_no_array(ch_no) = ch_no
            '----------------------------------------
            ' Get specified value of PWM pulse phase shift
            '----------------------------------------
            str_tmp = m_PulsePhase(ch_no).SelectedItem.ToString()
            If (String.Equals(str_tmp, "0")) Then
                pulse_phase(ch_no) = DIO_PWM_PULSE_PHASE_0_DEGREES
            ElseIf (String.Equals(str_tmp, "90")) Then
                pulse_phase(ch_no) = DIO_PWM_PULSE_PHASE_90_DEGREES
            Else
                pulse_phase(ch_no) = DIO_PWM_PULSE_PHASE_180_DEGREES
            End If
        Next ch_no
        '----------------------------------------
        ' PWM pulse phase shift setting
        '----------------------------------------
        ret = DioSetPwmPulsePhase(dev_id, ch_no_array, pwm_ch_num, pulse_phase)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        DioGetErrorString(ret, error_string)
        TextBox_ReturnCode.Text = String.Format("DioSetPwmPulsePhase = {0} : {1}", ret, error_string)
    End Sub

    '=========================================================
    ' It is the process when the DioSetPwmPulseDuty button is clicked
    ' Set the PWM pulse duty ratio of the device
    '=========================================================
    Private Sub Button_DioSetPwmPulseDuty_Click(sender As Object, e As EventArgs) Handles Button_DioSetPwmPulseDuty.Click
        Dim error_string As New StringBuilder("", 256)
        Dim pulse_duty(MAX_PWM_CH - 1) As UShort
        Dim ch_no_array(MAX_PWM_CH - 1) As Short
        Dim ret As Integer
        Dim ch_no As Short
        Dim pwm_ch_num As Short

        '----------------------------------------
        ' Get number of channels
        '----------------------------------------
        Short.TryParse(ComboBox_ChannelNum.SelectedItem.ToString(), pwm_ch_num)
        '----------------------------------------
        ' For each channel
        '----------------------------------------
        For ch_no = 0 To (pwm_ch_num - 1)
            '----------------------------------------
            ' Store channel number
            '----------------------------------------
            ch_no_array(ch_no) = ch_no
            '----------------------------------------
            ' Get specified value of PWM pulse duty ratio
            '----------------------------------------
            If (UShort.TryParse(m_PulseDutyValue(ch_no).Text, pulse_duty(ch_no)) = False) Then
                pulse_duty(ch_no) = 32767
            End If
        Next ch_no
        '----------------------------------------
        ' PWM pulse duty ratio setting
        '----------------------------------------
        ret = DioSetPwmPulseDuty(dev_id, ch_no_array, pwm_ch_num, pulse_duty)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        DioGetErrorString(ret, error_string)
        TextBox_ReturnCode.Text = String.Format("DioSetPwmPulseDuty = {0} : {1}", ret, error_string)
    End Sub

    '=========================================================
    ' It is the process when the DioStartPwm button is clicked
    ' Start the PWM pulse output
    '=========================================================
    Private Sub Button_DioStartPwm_Click(sender As Object, e As EventArgs) Handles Button_DioStartPwm.Click
        Dim error_string As New StringBuilder("", 256)
        Dim ch_no_array(MAX_PWM_CH - 1) As Short
        Dim ret As Integer
        Dim ch_no As Short
        Dim pwm_ch_num As Short

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

    '=========================================================
    ' It is the process when the DioGetPwmStatus button is clicked
    ' Get the PWM status
    '=========================================================
    Private Sub Button_DioGetPwmStatus_Click(sender As Object, e As EventArgs) Handles Button_DioGetPwmStatus.Click
        Dim error_string As New StringBuilder("", 256)
        Dim pwm_status(MAX_PWM_CH - 1) As UShort
        Dim pwm_error(MAX_PWM_CH - 1) As UShort
        Dim ch_no_array(MAX_PWM_CH - 1) As Short
        Dim ret As Integer
        Dim ch_no As Short
        Dim pwm_ch_num As Short

        '----------------------------------------
        ' Get number of channels
        '----------------------------------------
        Short.TryParse(ComboBox_ChannelNum.SelectedItem.ToString(), pwm_ch_num)
        '----------------------------------------
        ' For each channel
        '----------------------------------------
        For ch_no = 0 To (pwm_ch_num - 1)
            '----------------------------------------
            ' Store channel number
            '----------------------------------------
            ch_no_array(ch_no) = ch_no
        Next ch_no
        '----------------------------------------
        ' Get PWM status
        '----------------------------------------
        ret = DioGetPwmStatus(dev_id, ch_no_array, pwm_ch_num, pwm_status, pwm_error)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        DioGetErrorString(ret, error_string)
        TextBox_ReturnCode.Text = String.Format("DioGetPwmStatus = {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 (pwm_ch_num - 1)
            '----------------------------------------
            ' Update the display of PWM status
            '----------------------------------------
            m_PwmStatus(ch_no).Text = pwm_status(ch_no).ToString("X")
            '----------------------------------------
            ' Update the display of PWM error
            '----------------------------------------
            m_PwmError(ch_no).Text = pwm_error(ch_no).ToString("X")
        Next ch_no
    End Sub

    '=========================================================
    ' It is the process when the DioStopPwm button is clicked
    ' Stop the PWM pulse output
    '=========================================================
    Private Sub Button_DioStopPwm_Click(sender As Object, e As EventArgs) Handles Button_DioStopPwm.Click
        Dim error_string As New StringBuilder("", 256)
        Dim ch_no_array(MAX_PWM_CH - 1) As Short
        Dim ret As Integer
        Dim ch_no As Short
        Dim pwm_ch_num As Short

        '----------------------------------------
        ' Get number of channels
        '----------------------------------------
        Short.TryParse(ComboBox_ChannelNum.SelectedItem.ToString(), pwm_ch_num)
        '----------------------------------------
        ' For each channel
        '----------------------------------------
        For ch_no = 0 To (pwm_ch_num - 1)
            '----------------------------------------
            ' Store channel number
            '----------------------------------------
            ch_no_array(ch_no) = ch_no
        Next ch_no
        '----------------------------------------
        ' Stop PWM pulse output
        '----------------------------------------
        ret = DioStopPwm(dev_id, ch_no_array, pwm_ch_num)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        DioGetErrorString(ret, error_string)
        TextBox_ReturnCode.Text = String.Format("DioStopPwm = {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
