﻿Imports System.Text

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

    '=========================================================
    ' 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_InternalClock.Text = "1000"
        Edit_StopNum.Text = "1000"
        Edit_NotifyNumber.Text = Str(NumberOfData)
        '--------------------------------
        ' Start condition initialization
        '--------------------------------
        With Combo_StartTrg
            .Items.Clear()
            .Items.Insert(0, "Software Start")
            .Items.Insert(1, "External UP")
            .Items.Insert(2, "External Down")
            .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 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_OUT

        '--------------------------------
        ' 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
        '--------------------------------
        ' 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 transfer condition
        '--------------------------------
        If Radio_WriteOnce.Checked = True Then
            IsRing = DIODM_WRITE_ONCE
        Else
            IsRing = DIODM_WRITE_RING
        End If
        '--------------------------------
        ' Save notify number
        '--------------------------------
        NumberOfData = Val(Edit_NotifyNumber.Text)
        '--------------------------------
        ' Set direction(output)
        '--------------------------------
        Ret = DioDmSetDirection(Form1.Id, PO_32)
        If Ret <> DIO_ERR_SUCCESS Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetDirection Ret = " & Ret & ":" & szError.ToString(), 0, "Generating")
            Exit Sub
        End If
        '--------------------------------
        ' StandAlone
        '--------------------------------
        Ret = DioDmSetStandAlone(Form1.Id)
        If Ret <> DIO_ERR_SUCCESS Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetStandAlone Ret = " & Ret & ":" & szError.ToString(), 0, "Generating")
            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, "Generating")
            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, "Generating")
            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, "Generating")
            Exit Sub
        End If
        '--------------------------------
        ' Set internal clock
        '--------------------------------
        Ret = DioDmSetInternalClock(Form1.Id, Direction, CLng(Edit_InternalClock.Text), DIODM_TIM_UNIT_NS)
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetInternalClock Ret = " & Ret & ":" & szError.ToString(), 0, "Generating")
            Exit Sub
        End If
        '--------------------------------
        ' Set stop number
        '--------------------------------
        Ret = DioDmSetStopNumber(Form1.Id, Direction, CLng(Edit_StopNum.Text))
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetStopNumber Ret = " & Ret & ":" & szError.ToString(), 0, "Generating")
            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
        Hide()
    End Sub
End Class