﻿Imports System.Text

Public Class ConditionDlg
    Dim Ret As Integer                          'Return code
    Dim szError As New StringBuilder("", 256)
    Public Shared NumberOfData As Long = 500

    '=========================================================
    ' Form load
    '=========================================================
    Private Sub ConditionDlg_load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        Me.Left = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2

        '--------------------------------
        ' Screen item initialization
        '--------------------------------
        NumberOfData = 500
        Edit_Pattern.Text = "55"
        Edit_Mask.Text = "FF"
        Edit_InternalClock.Text = "100000"
        Edit_StopNum.Text = "1000"
        Edit_NotifyNumber.Text = "500"
        '--------------------------------
        ' Start condition initialization
        '--------------------------------
        With Combo_StartTrg
            .Items.Clear()
            .Items.Insert(0, "Software Start")
            .Items.Insert(1, "External UP")
            .Items.Insert(2, "External Down")
            .Items.Insert(3, "Pattern")
            .SelectedIndex = 0
        End With
        '--------------------------------
        ' Clock condition initialization
        '--------------------------------
        With Combo_ClockTrg
            .Items.Clear()
            .Items.Insert(0, "Internal Clock")
            .Items.Insert(1, "External Clock")
            .Items.Insert(2, "Hand Shake")
            .SelectedIndex = 0
        End With
        '--------------------------------
        ' Stop condition initialization
        '--------------------------------
        With Combo_StopTrg
            .Items.Clear()
            .Items.Insert(0, "Software Stop")
            .Items.Insert(1, "External UP")
            .Items.Insert(2, "External Down")
            .Items.Insert(3, "Number")
            .SelectedIndex = 0
        End With
    End Sub

    '=========================================================
    ' Modify start condition
    '=========================================================
    Private Sub COMBO_START_TRG_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Combo_StartTrg.SelectedIndexChanged
        '--------------------------------
        ' Enable/disable pattern
        '--------------------------------
        With Edit_Pattern
            If Combo_StartTrg.Text = "Pattern" Then                     'Enable
                .BackColor = System.Drawing.SystemColors.Window         '&H80000005
                .Enabled = True
            Else                                                        'Disable
                .BackColor = System.Drawing.SystemColors.InactiveBorder '&H8000000B
                .Enabled = False
            End If
        End With
        '--------------------------------
        ' Enable/disable mask
        '--------------------------------
        With Edit_Mask
            If Combo_StartTrg.Text = "Pattern" Then                     'Enable
                .BackColor = System.Drawing.SystemColors.Window
                .Enabled = True
            Else                                                        'Disable
                .BackColor = System.Drawing.SystemColors.InactiveBorder
                .Enabled = False
            End If
        End With
    End Sub

    '=========================================================
    ' Modify clock condition
    '=========================================================
    Private Sub COMBO_CLOCK_TRG_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Combo_ClockTrg.SelectedIndexChanged
        '--------------------------------
        ' Enable/disable internal clock
        '--------------------------------
        With Edit_InternalClock
            If Combo_ClockTrg.Text = "Internal Clock" Then              'Enable
                .BackColor = System.Drawing.SystemColors.Window
                .Enabled = True
            Else                                                        'Disable
                .BackColor = System.Drawing.SystemColors.InactiveBorder
                .Enabled = False
            End If
        End With
    End Sub

    '=========================================================
    ' Modify stop condition
    '=========================================================
    Private Sub COMBO_STOP_TRG_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Combo_StopTrg.SelectedIndexChanged
        '--------------------------------
        ' Enable/disable stop number
        '--------------------------------
        With Edit_StopNum
            If Combo_StopTrg.Text = "Number" Then                       'Enable
                .BackColor = System.Drawing.SystemColors.Window
                .Enabled = True
            Else                                                        'Disable
                .BackColor = System.Drawing.SystemColors.InactiveBorder
                .Enabled = False
            End If
        End With
    End Sub

    '=========================================================
    ' OK button
    '=========================================================
    Private Sub BUTTON_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Ok.Click
        Dim StartTrg As Short
        Dim ClockTrg As Short
        Dim StopTrg As Short
        Dim Direction As Short = DIODM_DIR_IN

        '--------------------------------
        ' parameter exchange
        '--------------------------------
        '--------------------------------
        ' Start condition
        '--------------------------------
        StartTrg = 0
        If Combo_StartTrg.Text = "Software Start" Then StartTrg = DIODM_START_SOFT
        If Combo_StartTrg.Text = "External UP" Then StartTrg = DIODM_START_EXT_RISE
        If Combo_StartTrg.Text = "External Down" Then StartTrg = DIODM_START_EXT_FALL
        If Combo_StartTrg.Text = "Pattern" Then StartTrg = DIODM_START_PATTERN
        '--------------------------------
        ' Clock condition
        '--------------------------------
        ClockTrg = 0
        If Combo_ClockTrg.Text = "Internal Clock" Then ClockTrg = DIODM_CLK_CLOCK
        If Combo_ClockTrg.Text = "External Clock" Then ClockTrg = DIODM_CLK_EXT_TRG
        If Combo_ClockTrg.Text = "Hand Shake" Then ClockTrg = DIODM_CLK_HANDSHAKE
        '--------------------------------
        ' Stop condition
        '--------------------------------
        StopTrg = 0
        If Combo_StopTrg.Text = "Software Stop" Then StopTrg = DIODM_STOP_SOFT
        If Combo_StopTrg.Text = "External UP" Then StopTrg = DIODM_STOP_EXT_RISE
        If Combo_StopTrg.Text = "External Down" Then StopTrg = DIODM_STOP_EXT_FALL
        If Combo_StopTrg.Text = "Number" Then StopTrg = DIODM_STOP_NUM
        '--------------------------------
        ' Save notify number
        '--------------------------------
        NumberOfData = Val(Edit_NotifyNumber.Text)
        '--------------------------------
        ' Set direction(input)
        '--------------------------------
        Ret = DioDmSetDirection(Form1.Id, PI_32)
        If Ret <> DIO_ERR_SUCCESS Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetDirection Ret = " & Ret & ":" & szError.ToString(), 0, "Infinite")
            Exit Sub
        End If
        '--------------------------------
        ' StanAlone
        '--------------------------------
        Ret = DioDmSetStandAlone(Form1.Id)
        If Ret <> DIO_ERR_SUCCESS Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetStandAlone Ret = " & Ret & ":" & szError.ToString(), 0, "Infinite")
            Exit Sub
        End If
        '--------------------------------
        ' Set start condition
        '--------------------------------
        Ret = DioDmSetStartTrigger(Form1.Id, Direction, StartTrg)
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetStartTrigger Ret = " & Ret & ":" & szError.ToString(), 0, "Infinite")
            Exit Sub
        End If
        '--------------------------------
        ' Set clock condition
        '--------------------------------
        Ret = DioDmSetClockTrigger(Form1.Id, Direction, ClockTrg)
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetClockTrigger Ret = " & Ret & ":" & szError.ToString(), 0, "Infinite")
            Exit Sub
        End If
        '--------------------------------
        ' Set stop condition
        '--------------------------------
        Ret = DioDmSetStopTrigger(Form1.Id, Direction, StopTrg)
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetStopTrigger Ret = " & Ret & ":" & szError.ToString(), 0, "Infinite")
            Exit Sub
        End If
        '--------------------------------
        ' Pattern/Mask
        '--------------------------------
        Ret = DioDmSetStartPattern(Form1.Id, CInt("&H" & Edit_Pattern.Text), CInt("&H" & Edit_Mask.Text))
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetStartPattern Ret = " & Ret & ":" & szError.ToString(), 0, "Infinite")
            Exit Sub
        End If
        '--------------------------------
        ' Set internal clock
        '--------------------------------
        Ret = DioDmSetInternalClock(Form1.Id, Direction, CInt(Edit_InternalClock.Text), DIODM_TIM_UNIT_NS)
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetInternalClock Ret = " & Ret & ":" & szError.ToString(), 0, "Infinite")
            Exit Sub
        End If
        '--------------------------------
        ' Set stop number
        '--------------------------------
        Ret = DioDmSetStopNumber(Form1.Id, Direction, CInt(Edit_StopNum.Text))
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetStopNumber Ret = " & Ret & ":" & szError.ToString(), 0, "Infinite")
            Exit Sub
        End If
        Me.Hide()
    End Sub

    '=========================================================
    ' Cancel button
    '=========================================================
    Private Sub BUTTON_CANCEL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Cancel.Click
        Me.Hide()
    End Sub
End Class