﻿Imports System.Globalization
Imports System.Text

Public Class ConditionDlg
    Dim StartComp As Integer
    Dim StopComp As Integer

    Dim InternalClock As Integer
    Dim StopNumber As Integer
    Public Shared NumberOfData As Integer
    Dim StartCountMode As Short
    Dim StopCountMode As Short

    '===================================================
    ' Dialog initialization
    '===================================================
    Private Sub ConditionDlg_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '--------------------------------
        ' Initialize display
        '--------------------------------
        cboStartTrg.Items.Clear()
        cboStartTrg.Items.Insert(0, "Software start")
        cboStartTrg.Items.Insert(1, "External signal rising")
        cboStartTrg.Items.Insert(2, "External signal falling")
        cboStartTrg.Items.Insert(3, "Count-match CH0 Compare-register 0")
        cboStartTrg.Items.Insert(4, "Count-match CH1 Compare-register 0")
        cboStartTrg.Items.Insert(5, "Count-match CH2 Compare-register 0")
        cboStartTrg.Items.Insert(6, "Count-match CH3 Compare-register 0")
        cboStartTrg.Items.Insert(7, "Count-match CH0 Compare-register 1")
        cboStartTrg.Items.Insert(8, "Count-match CH1 Compare-register 1")
        cboStartTrg.Items.Insert(9, "Count-match CH2 Compare-register 1")
        cboStartTrg.Items.Insert(10, "Count-match CH3 Compare-register 1")

        cboClockTrg.Items.Clear()
        cboClockTrg.Items.Insert(0, "Internal clock")
        cboClockTrg.Items.Insert(1, "External clock")

        cboStopTrg.Items.Clear()
        cboStopTrg.Items.Insert(0, "Software stop")
        cboStopTrg.Items.Insert(1, "External signal rising")
        cboStopTrg.Items.Insert(2, "External signal falling")
        cboStopTrg.Items.Insert(3, "Stop transfer by specified times")
        cboStopTrg.Items.Insert(4, "Count-match CH0 Compare-register 0")
        cboStopTrg.Items.Insert(5, "Count-match CH1 Compare-register 0")
        cboStopTrg.Items.Insert(6, "Count-match CH2 Compare-register 0")
        cboStopTrg.Items.Insert(7, "Count-match CH3 Compare-register 0")
        cboStopTrg.Items.Insert(8, "Count-match CH0 Compare-register 1")
        cboStopTrg.Items.Insert(9, "Count-match CH1 Compare-register 1")
        cboStopTrg.Items.Insert(10, "Count-match CH2 Compare-register 1")
        cboStopTrg.Items.Insert(11, "Count-match CH3 Compare-register 1")


        cboStartTrg.Text = "Software start"
        cboClockTrg.Text = "Internal clock"
        cboStopTrg.Text = "Software stop"

        If StartCountMode = CNTS_START_CNT_NOT_WITH_SAMP Then
            rdoStartCntNotWithSamp.Checked = True
            rdoStartCntWithSamp.Checked = False
        Else
            rdoStartCntNotWithSamp.Checked = False
            rdoStartCntWithSamp.Checked = True
        End If
        If StopCountMode = CNTS_STOP_CNT_NOT_WITH_SAMP Then
            rdoStopCntNotWithSamp.Checked = True
            rdoStopCntWithSamp.Checked = False
        Else
            rdoStopCntNotWithSamp.Checked = False
            rdoStopCntWithSamp.Checked = True
        End If

        txtCompStart.Text = 100
        txtInternalClock.Text = 100000
        txtStopNum.Text = 1000
        txtNumEvent.Text = 500
        txtCompStop.Text = 200

        cboStartTrg_SelectedIndexChanged(cboStartTrg, New System.EventArgs())
        cboClockTrg_SelectedIndexChanged(cboClockTrg, New System.EventArgs())
        cboStopTrg_SelectedIndexChanged(cboStopTrg, New System.EventArgs())

    End Sub

    '===================================================
    ' Start condition setting is changed
    '===================================================
    Private Sub cboStartTrg_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboStartTrg.SelectedIndexChanged

        Dim szBuf As New String("", 100)

        szBuf = cboStartTrg.Text
        If szBuf.StartsWith("Count-match") Then
            txtCompStart.Enabled = True
        Else
            txtCompStart.Enabled = False
        End If

    End Sub

    '===================================================
    ' Clock condition setting is changed
    '===================================================
    Private Sub cboClockTrg_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboClockTrg.SelectedIndexChanged

        Dim szBuf As New String("", 100)

        '--------------------------------
        ' Enable/Disable internal clock value
        '--------------------------------
        szBuf = cboClockTrg.Text
        If String.Compare(szBuf, "Internal clock") = 0 Then
            txtInternalClock.Enabled = True
        Else
            txtInternalClock.Enabled = False
        End If

    End Sub

    '===================================================
    ' Stop condition setting is changed
    '===================================================
    Private Sub cboStopTrg_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboStopTrg.SelectedIndexChanged

        Dim szBuf As New String("", 100)

        szBuf = cboStopTrg.Text
        If szBuf.StartsWith("Count-match") Then
            txtCompStop.Enabled = True
        Else
            txtCompStop.Enabled = False
        End If
        If String.Compare(szBuf, "Stop transfer by specified times") = 0 Then
            txtStopNum.Enabled = True
        Else
            txtStopNum.Enabled = False
        End If

    End Sub

    '===================================================
    ' OK button processing
    '===================================================
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Dim error_string As New StringBuilder("", 256)
        Dim szTmp As New String("", 100)
        Dim StartTrg As Short
        Dim ClockTrg As Short
        Dim StopTrg As Short
        Dim ret As Integer
        Dim strMsg As String
        Dim CompChNo As Short
        Dim CompRegNo As Short

        '--------------------------------
        ' Get displayed data
        '--------------------------------
        If rdoStartCntNotWithSamp.Checked = True Then
            StartCountMode = CNTS_START_CNT_NOT_WITH_SAMP
        Else
            StartCountMode = CNTS_START_CNT_WITH_SAMP
        End If
        If rdoStopCntNotWithSamp.Checked = True Then
            StopCountMode = CNTS_STOP_CNT_NOT_WITH_SAMP
        Else
            StopCountMode = CNTS_STOP_CNT_WITH_SAMP
        End If

        szTmp = txtCompStart.Text
        Try
            StartComp = Integer.Parse(szTmp, NumberStyles.HexNumber)
        Catch ex As Exception
            StartComp = &H100
        End Try

        szTmp = txtCompStop.Text
        Try
            StopComp = Integer.Parse(szTmp, NumberStyles.HexNumber)
        Catch ex As Exception
            StopComp = &H200
        End Try

        ' In order to set the internal clock to signed, convert it once through hexadecimal characters
        szTmp = Str(Convert.ToInt32(Hex(Val(txtInternalClock.Text)), 16))
        Try
            InternalClock = Integer.Parse(szTmp)
        Catch ex As Exception
            InternalClock = 100000
        End Try

        ' In order to set the stop number to signed, convert it once through hexadecimal characters
        szTmp = Str(Convert.ToInt32(Hex(Val(txtStopNum.Text)), 16))
        Try
            StopNumber = Integer.Parse(szTmp)
        Catch ex As Exception
            StopNumber = 1000
        End Try

        ' In order to set the number of sampling cycles for notification to signed, convert it once through hexadecimal characters
        szTmp = Str(Convert.ToInt32(Hex(Val(txtNumEvent.Text)), 16))
        Try
            NumberOfData = Integer.Parse(szTmp)
        Catch ex As Exception
            NumberOfData = 500
        End Try

        '--------------------------------
        ' Convert setting value to a number
        '--------------------------------
        StartTrg = 0
        Select Case cboStartTrg.SelectedIndex
            Case 0
                StartTrg = CNTS_START_SOFT
            Case 1
                StartTrg = CNTS_START_EXT_RISE
            Case 2
                StartTrg = CNTS_START_EXT_FALL
            Case 3
                StartTrg = CNTS_START_CNTMATCH_CH0_REG0
            Case 4
                StartTrg = CNTS_START_CNTMATCH_CH1_REG0
            Case 5
                StartTrg = CNTS_START_CNTMATCH_CH2_REG0
            Case 6
                StartTrg = CNTS_START_CNTMATCH_CH3_REG0
            Case 7
                StartTrg = CNTS_START_CNTMATCH_CH0_REG1
            Case 8
                StartTrg = CNTS_START_CNTMATCH_CH1_REG1
            Case 9
                StartTrg = CNTS_START_CNTMATCH_CH2_REG1
            Case 10
                StartTrg = CNTS_START_CNTMATCH_CH3_REG1
        End Select

        ClockTrg = 0
        Select Case cboClockTrg.SelectedIndex
            Case 0
                ClockTrg = CNTS_CLOCK_CLOCK
            Case 1
                ClockTrg = CNTS_CLOCK_EXT_TRG
        End Select

        StopTrg = 0
        Select Case cboStopTrg.SelectedIndex
            Case 0
                StopTrg = CNTS_STOP_SOFT
            Case 1
                StopTrg = CNTS_STOP_EXT_RISE
            Case 2
                StopTrg = CNTS_STOP_EXT_FALL
            Case 3
                StopTrg = CNTS_STOP_NUM
            Case 4
                StopTrg = CNTS_STOP_CNTMATCH_CH0_REG0
            Case 5
                StopTrg = CNTS_STOP_CNTMATCH_CH1_REG0
            Case 6
                StopTrg = CNTS_STOP_CNTMATCH_CH2_REG0
            Case 7
                StopTrg = CNTS_STOP_CNTMATCH_CH3_REG0
            Case 8
                StopTrg = CNTS_STOP_CNTMATCH_CH0_REG1
            Case 9
                StopTrg = CNTS_STOP_CNTMATCH_CH1_REG1
            Case 10
                StopTrg = CNTS_STOP_CNTMATCH_CH2_REG1
            Case 11
                StopTrg = CNTS_STOP_CNTMATCH_CH3_REG1
        End Select

        '--------------------------------
        ' Set sampling condition
        '--------------------------------
        ' Stand-alone
        ret = CntSetStandAlone(Id)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetStandAlone ret = {0:d}: {1}", ret, error_string)
            MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK)
            Exit Sub
        End If
        ' Set start condition
        ret = CntSetSamplingStartTrigger(Id, StartTrg, StartCountMode, CHANNEL_NUM)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetSamplingStartTrigger ret = {0:d}: {1}", ret, error_string)
            MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK)
            Exit Sub
        End If
        Select Case StartTrg
            Case CNTS_START_CNTMATCH_CH0_REG0
                CompChNo = 0
                CompRegNo = 0
            Case CNTS_START_CNTMATCH_CH1_REG0
                CompChNo = 1
                CompRegNo = 0
            Case CNTS_START_CNTMATCH_CH2_REG0
                CompChNo = 2
                CompRegNo = 0
            Case CNTS_START_CNTMATCH_CH3_REG0
                CompChNo = 3
                CompRegNo = 0
            Case CNTS_START_CNTMATCH_CH4_REG0
                CompChNo = 4
                CompRegNo = 0
            Case CNTS_START_CNTMATCH_CH5_REG0
                CompChNo = 5
                CompRegNo = 0
            Case CNTS_START_CNTMATCH_CH6_REG0
                CompChNo = 6
                CompRegNo = 0
            Case CNTS_START_CNTMATCH_CH7_REG0
                CompChNo = 7
                CompRegNo = 0
            Case CNTS_START_CNTMATCH_CH0_REG1
                CompChNo = 0
                CompRegNo = 1
            Case CNTS_START_CNTMATCH_CH1_REG1
                CompChNo = 1
                CompRegNo = 1
            Case CNTS_START_CNTMATCH_CH2_REG1
                CompChNo = 2
                CompRegNo = 1
            Case CNTS_START_CNTMATCH_CH3_REG1
                CompChNo = 3
                CompRegNo = 1
            Case CNTS_START_CNTMATCH_CH4_REG1
                CompChNo = 4
                CompRegNo = 1
            Case CNTS_START_CNTMATCH_CH5_REG1
                CompChNo = 5
                CompRegNo = 1
            Case CNTS_START_CNTMATCH_CH6_REG1
                CompChNo = 6
                CompRegNo = 1
            Case CNTS_START_CNTMATCH_CH7_REG1
                CompChNo = 7
                CompRegNo = 1
            Case Else
                CompChNo = &HFFF
                CompRegNo = &HFFF
        End Select
        If CompChNo <> &HFFF And CompRegNo <> &HFFF Then
            ret = CntNotifyCountUp(Id, CompChNo, CompRegNo, StartComp, 0)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                strMsg = String.Format("CntNotifyCountUp ret = {0:d}: {1}", ret, error_string)
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK)
            End If
        End If
        ' Set clock condition
        ret = CntSetSamplingClockTrigger(Id, ClockTrg)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetSamplingClockTrigger ret = {0:d}: {1}", ret, error_string)
            MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK)
            Exit Sub
        End If
        ' Set internal clock value
        If ClockTrg = CNTS_CLOCK_CLOCK Then
            ret = CntSetSamplingInternalClock(Id, InternalClock, CNTS_TIM_UNIT_NS)
            If ret <> CNT_ERR_SUCCESS Then
                CntGetErrorString(ret, error_string)
                strMsg = String.Format("CntSetSamplingInternalClock ret = {0:d}: {1}", ret, error_string)
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK)
                Exit Sub
            End If
        End If

        ' Set stop condition
        ret = CntSetSamplingStopTrigger(Id, StopTrg, StopCountMode, CHANNEL_NUM)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetSamplingStopTrigger ret = {0:d}: {1}", ret, error_string)
            MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK)
            Exit Sub
        End If
        Select Case StopTrg
            Case CNTS_STOP_CNTMATCH_CH0_REG0
                CompChNo = 0
                CompRegNo = 0
            Case CNTS_STOP_CNTMATCH_CH1_REG0
                CompChNo = 1
                CompRegNo = 0
            Case CNTS_STOP_CNTMATCH_CH2_REG0
                CompChNo = 2
                CompRegNo = 0
            Case CNTS_STOP_CNTMATCH_CH3_REG0
                CompChNo = 3
                CompRegNo = 0
            Case CNTS_STOP_CNTMATCH_CH4_REG0
                CompChNo = 4
                CompRegNo = 0
            Case CNTS_STOP_CNTMATCH_CH5_REG0
                CompChNo = 5
                CompRegNo = 0
            Case CNTS_STOP_CNTMATCH_CH6_REG0
                CompChNo = 6
                CompRegNo = 0
            Case CNTS_STOP_CNTMATCH_CH7_REG0
                CompChNo = 7
                CompRegNo = 0
            Case CNTS_STOP_CNTMATCH_CH0_REG1
                CompChNo = 0
                CompRegNo = 1
            Case CNTS_STOP_CNTMATCH_CH1_REG1
                CompChNo = 1
                CompRegNo = 1
            Case CNTS_STOP_CNTMATCH_CH2_REG1
                CompChNo = 2
                CompRegNo = 1
            Case CNTS_STOP_CNTMATCH_CH3_REG1
                CompChNo = 3
                CompRegNo = 1
            Case CNTS_STOP_CNTMATCH_CH4_REG1
                CompChNo = 4
                CompRegNo = 1
            Case CNTS_STOP_CNTMATCH_CH5_REG1
                CompChNo = 5
                CompRegNo = 1
            Case CNTS_STOP_CNTMATCH_CH6_REG1
                CompChNo = 6
                CompRegNo = 1
            Case CNTS_STOP_CNTMATCH_CH7_REG1
                CompChNo = 7
                CompRegNo = 1
            Case Else
                CompChNo = &HFFF
                CompRegNo = &HFFF
        End Select
        If CompChNo <> &HFFF And CompRegNo <> &HFFF Then
            ret = CntNotifyCountUp(Id, CompChNo, CompRegNo, StopComp, 0)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                strMsg = String.Format("CntNotifyCountUp ret = {0:d}: {1}", ret, error_string)
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK)
            End If
        End If
        ' Set stop number
        If StopTrg = CNTS_STOP_NUM Then
            ret = CntSetSamplingStopNumber(Id, StopNumber)
            If ret <> CNT_ERR_SUCCESS Then
                CntGetErrorString(ret, error_string)
                strMsg = String.Format("CntSetSamplingInternalClock ret = {0:d}: {1}", ret, error_string)
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK)
                Exit Sub
            End If
        End If
        Me.Hide()

    End Sub

    '---------------------------------------------
    ' Cancel button processing
    '---------------------------------------------
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        Me.Hide()

    End Sub

    Private Sub ConditionDlg_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing

        e.Cancel = True
        Me.Hide()

    End Sub
End Class