﻿Imports System.Text

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

    '=========================================================
    ' フォームロード
    '=========================================================
    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

        '--------------------------------
        ' 画面項目の初期化
        '--------------------------------
        NumberOfData = 500
        Edit_Pattern.Text = "55"
        Edit_Mask.Text = "FF"
        Edit_InternalClock.Text = "100000"
        Edit_StopNum.Text = "1000"
        Edit_NotifyNumber.Text = "500"
        '--------------------------------
        ' スタート条件初期化
        '--------------------------------
        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
        '--------------------------------
        ' クロック条件初期化
        '--------------------------------
        With Combo_ClockTrg
            .Items.Clear()
            .Items.Insert(0, "Internal Clock")
            .Items.Insert(1, "External Clock")
            .Items.Insert(2, "Hand Shake")
            .SelectedIndex = 0
        End With
        '--------------------------------
        ' ストップ条件初期化
        '--------------------------------
        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

    '=========================================================
    ' スタート条件設定変更
    '=========================================================
    Private Sub COMBO_START_TRG_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Combo_StartTrg.SelectedIndexChanged
        '--------------------------------
        ' 比較パターンの有効、無効を切り替え
        '--------------------------------
        With Edit_Pattern
            If Combo_StartTrg.Text = "Pattern" Then                     '有効
                .BackColor = System.Drawing.SystemColors.Window         '&H80000005
                .Enabled = True
            Else                                                        '無効
                .BackColor = System.Drawing.SystemColors.InactiveBorder '&H8000000B
                .Enabled = False
            End If
        End With
        '--------------------------------
        ' パターンマスタの有効、無効を切り替え
        '--------------------------------
        With Edit_Mask
            If Combo_StartTrg.Text = "Pattern" Then                     '有効
                .BackColor = System.Drawing.SystemColors.Window
                .Enabled = True
            Else                                                        '無効
                .BackColor = System.Drawing.SystemColors.InactiveBorder
                .Enabled = False
            End If
        End With
    End Sub

    '=========================================================
    ' クロック条件設定変更
    '=========================================================
    Private Sub COMBO_CLOCK_TRG_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Combo_ClockTrg.SelectedIndexChanged
        '--------------------------------
        ' 内部クロックの有効、無効を切り替え
        '--------------------------------
        With Edit_InternalClock
            If Combo_ClockTrg.Text = "Internal Clock" Then              '有効
                .BackColor = System.Drawing.SystemColors.Window
                .Enabled = True
            Else                                                        '無効
                .BackColor = System.Drawing.SystemColors.InactiveBorder
                .Enabled = False
            End If
        End With
    End Sub

    '=========================================================
    ' ストップ条件設定変更
    '=========================================================
    Private Sub COMBO_STOP_TRG_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Combo_StopTrg.SelectedIndexChanged
        '--------------------------------
        ' 停止個数の有効、無効を切り替え
        '--------------------------------
        With Edit_StopNum
            If Combo_StopTrg.Text = "Number" Then                       '有効
                .BackColor = System.Drawing.SystemColors.Window
                .Enabled = True
            Else                                                        '無効
                .BackColor = System.Drawing.SystemColors.InactiveBorder
                .Enabled = False
            End If
        End With
    End Sub

    '=========================================================
    ' OKボタン
    '=========================================================
    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
        '--------------------------------
        '--------------------------------
        ' スタート条件
        '--------------------------------
        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
        '--------------------------------
        ' クロック条件
        '--------------------------------
        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
        '--------------------------------
        ' ストップ条件
        '--------------------------------
        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
        '--------------------------------
        ' 指定個数通知個数保存
        '--------------------------------
        NumberOfData = Val(Edit_NotifyNumber.Text)
        '--------------------------------
        ' 入出力方向設定(全点入力)
        '--------------------------------
        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
        '--------------------------------
        ' スタンドアローン
        '--------------------------------
        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
        '--------------------------------
        ' スタート条件設定
        '--------------------------------
        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
        '--------------------------------
        ' クロック条件設定
        '--------------------------------
        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
        '--------------------------------
        ' ストップ条件設定
        '--------------------------------
        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
        '--------------------------------
        ' パターン/マスク
        '--------------------------------
        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
        '--------------------------------
        ' 内部クロック値設定
        '--------------------------------
        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
        '--------------------------------
        ' 停止個数設定
        '--------------------------------
        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

    '=========================================================
    ' キャンセルボタン
    '=========================================================
    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