﻿Imports System.Text

Public Class frmMSmpMode
    Dim StartComp As Integer
    Dim StopComp As Integer

    Dim InternalClock As Integer
    Dim StopNum As Integer
    Public Shared M_NotifyCount As Integer
    '===================================================
    ' Dialog initialization
    '===================================================
    Private Sub frmMSmpMode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '--------------------------------
        ' Initialize dialog display
        '--------------------------------
        ' Start
        With cboStart
            .Items.Clear()
            .Items.Insert(0, "Software Start")
            .Items.Insert(1, "External Signal Rising")
            .Items.Insert(2, "External Signal Falling")
            .Items.Insert(3, "Count-match CH0 Compare-register 0")
            .Items.Insert(4, "Count-match CH1 Compare-register 0")
            .Items.Insert(5, "Count-match CH2 Compare-register 0")
            .Items.Insert(6, "Count-match CH3 Compare-register 0")
            .Items.Insert(7, "Count-match CH4 Compare-register 0")
            .Items.Insert(8, "Count-match CH5 Compare-register 0")
            .Items.Insert(9, "Count-match CH6 Compare-register 0")
            .Items.Insert(10, "Count-match CH7 Compare-register 0")
            .Items.Insert(11, "Count-match CH0 Compare-register 1")
            .Items.Insert(12, "Count-match CH1 Compare-register 1")
            .Items.Insert(13, "Count-match CH2 Compare-register 1")
            .Items.Insert(14, "Count-match CH3 Compare-register 1")
            .Items.Insert(15, "Count-match CH4 Compare-register 1")
            .Items.Insert(16, "Count-match CH5 Compare-register 1")
            .Items.Insert(17, "Count-match CH6 Compare-register 1")
            .Items.Insert(18, "Count-match CH7 Compare-register 1")
        End With
        ' Start with
        With cboStartWith
            .Items.Clear()
            .Items.Insert(0, "Start counter regardless of sampling")
            .Items.Insert(1, "Start counter when sampling starts")
        End With
        ' Clock
        With cboClock
            .Items.Clear()
            .Items.Insert(0, "Internal Clock")
            .Items.Insert(1, "External Clock")
        End With
        ' Stop
        With cboStop
            .Items.Clear()
            .Items.Insert(0, "Software Stop")
            .Items.Insert(1, "External Signal Rising")
            .Items.Insert(2, "External Signal Falling")
            .Items.Insert(3, "Stop Transfer by Specified Times")
            .Items.Insert(4, "Count-match CH0 Compare-register 0")
            .Items.Insert(5, "Count-match CH1 Compare-register 0")
            .Items.Insert(6, "Count-match CH2 Compare-register 0")
            .Items.Insert(7, "Count-match CH3 Compare-register 0")
            .Items.Insert(8, "Count-match CH4 Compare-register 0")
            .Items.Insert(9, "Count-match CH5 Compare-register 0")
            .Items.Insert(10, "Count-match CH6 Compare-register 0")
            .Items.Insert(11, "Count-match CH7 Compare-register 0")
            .Items.Insert(12, "Count-match CH0 Compare-register 1")
            .Items.Insert(13, "Count-match CH1 Compare-register 1")
            .Items.Insert(14, "Count-match CH2 Compare-register 1")
            .Items.Insert(15, "Count-match CH3 Compare-register 1")
            .Items.Insert(16, "Count-match CH4 Compare-register 1")
            .Items.Insert(17, "Count-match CH5 Compare-register 1")
            .Items.Insert(18, "Count-match CH6 Compare-register 1")
            .Items.Insert(19, "Count-match CH7 Compare-register 1")
        End With
        ' Stop with
        With cboStopWith
            .Items.Clear()
            .Items.Insert(0, "Stop counter regardless of sampling")
            .Items.Insert(1, "Stop counter when sampling stops")
        End With

        cboStart.SelectedIndex = 0
        cboStartWith.SelectedIndex = 0
        cboClock.SelectedIndex = 0
        cboStop.SelectedIndex = 0
        cboStopWith.SelectedIndex = 0

        StartComp = &H100S
        InternalClock = 1000
        StopComp = &H200S
        StopNum = 1000
        M_NotifyCount = 500
        Form1.M_IsRing = CNTS_WRITE_ONCE


        txtCompStart.Text = Hex(StartComp)
        ' In order to display the internal clock to unsigned, convert it once through hexadecimal characters
        txtInternalClock.Text = Convert.ToUInt32(Hex(InternalClock), 16)
        txtCompStop.Text = Hex(StopComp)
        ' In order to display the stop number to unsigned, convert it once through hexadecimal characters
        txtStopNumber.Text = Convert.ToUInt32(Hex(StopNum), 16)
        ' In order to display the number of sampling cycles for notification to unsigned, convert it once through hexadecimal characters
        txtNumberData.Text = Convert.ToUInt32(Hex(M_NotifyCount), 16)

        If Form1.M_IsRing = CNTS_WRITE_ONCE Then
            rdoOnce.Checked = True
            rdoRing.Checked = False
        Else
            rdoOnce.Checked = False
            rdoRing.Checked = True
        End If

        cboStart_SelectedIndexChanged(cboStart, New System.EventArgs())
        cboClock_SelectedIndexChanged(cboClock, New System.EventArgs())
        cboStop_SelectedIndexChanged(cboStop, New System.EventArgs())
    End Sub

    '===================================================
    ' Change start condition setting
    '===================================================
    Private Sub cboStart_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboStart.SelectedIndexChanged
        Dim szBuf As New String("", 100)

        szBuf = cboStart.Text
        If InStr(szBuf, "Count-match") > 0 Then
            txtCompStart.Enabled = True
        Else
            txtCompStart.Enabled = False
        End If
    End Sub

    '===================================================
    ' Change clock condition setting
    '===================================================
    Private Sub cboClock_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboClock.SelectedIndexChanged
        Dim szBuf As New String("", 100)

        szBuf = cboClock.Text
        If StrComp(szBuf, "Internal Clock") = 0 Then
            txtInternalClock.Enabled = True
        Else
            txtInternalClock.Enabled = False
        End If
    End Sub

    '===================================================
    ' Change stop condition setting
    '===================================================
    Private Sub cboStop_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboStop.SelectedIndexChanged
        Dim szBuf As New String("", 100)

        szBuf = cboStop.Text
        If InStr(szBuf, "Count-match") > 0 Then
            txtCompStop.Enabled = True
        Else
            txtCompStop.Enabled = False
        End If
        If StrComp(szBuf, "Stop Transfer by Specified Times") = 0 Then
            txtStopNumber.Enabled = True
        Else
            txtStopNumber.Enabled = False
        End If
    End Sub

    '===================================================
    ' OK processing
    '===================================================
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim szBuf As New String("", 100)
        Dim Checked As Boolean
        Dim dwStart As Short
        Dim ExtSig1, ExtSig2, ExtSig3 As Short
        Dim wStartWith As Short
        Dim dwClock As Short
        Dim dwStop As Short
        Dim wStopWith As Short
        Dim ret As Integer
        Dim strMsg As String
        Dim CompChNo As Integer
        Dim CompRegNo As Integer
        Dim error_string As New StringBuilder("", 256)

        '--------------------------------
        ' Get displaying data
        '--------------------------------
        StartComp = Val("&H" & txtCompStart.Text)
        ' In order to set the internal clock to signed, convert it once through hexadecimal characters
        InternalClock = Convert.ToInt32(Hex(Val(txtInternalClock.Text)), 16)
        StopComp = Val("&H" & txtCompStart.Text)
        ' In order to set the stop number to signed, convert it once through hexadecimal characters
        StopNum = Convert.ToInt32(Hex(Val(txtStopNumber.Text)), 16)
        ' In order to set the number of sampling cycles for notification to signed, convert it once through hexadecimal characters
        M_NotifyCount = Convert.ToInt32(Hex(Val(txtNumberData.Text)), 16)
        Checked = rdoOnce.Checked

        Select Case cboStart.SelectedIndex
            Case 0
                dwStart = CNTS_START_SOFT
                ExtSig2 = CNTS_EXT_START_SOFT
            Case 1
                dwStart = CNTS_START_EXT_RISE
                ExtSig2 = CNTS_EXT_START_EXT_RISE
            Case 2
                dwStart = CNTS_START_EXT_FALL
                ExtSig2 = CNTS_EXT_START_EXT_FALL
            Case 3
                dwStart = CNTS_START_CNTMATCH_CH0_REG0
                ExtSig2 = CNTS_EXT_CNTMATCH_CH0_REG0
            Case 4
                dwStart = CNTS_START_CNTMATCH_CH1_REG0
                ExtSig2 = CNTS_EXT_CNTMATCH_CH1_REG0
            Case 5
                dwStart = CNTS_START_CNTMATCH_CH2_REG0
                ExtSig2 = CNTS_EXT_CNTMATCH_CH2_REG0
            Case 6
                dwStart = CNTS_START_CNTMATCH_CH3_REG0
                ExtSig2 = CNTS_EXT_CNTMATCH_CH3_REG0
            Case 7
                dwStart = CNTS_START_CNTMATCH_CH4_REG0
                ExtSig2 = CNTS_EXT_CNTMATCH_CH4_REG0
            Case 8
                dwStart = CNTS_START_CNTMATCH_CH5_REG0
                ExtSig2 = CNTS_EXT_CNTMATCH_CH5_REG0
            Case 9
                dwStart = CNTS_START_CNTMATCH_CH6_REG0
                ExtSig2 = CNTS_EXT_CNTMATCH_CH6_REG0
            Case 10
                dwStart = CNTS_START_CNTMATCH_CH7_REG0
                ExtSig2 = CNTS_EXT_CNTMATCH_CH7_REG0
            Case 11
                dwStart = CNTS_START_CNTMATCH_CH0_REG1
                ExtSig2 = CNTS_EXT_CNTMATCH_CH0_REG1
            Case 12
                dwStart = CNTS_START_CNTMATCH_CH1_REG1
                ExtSig2 = CNTS_EXT_CNTMATCH_CH1_REG1
            Case 12
                dwStart = CNTS_START_CNTMATCH_CH2_REG1
                ExtSig2 = CNTS_EXT_CNTMATCH_CH2_REG1
            Case 12
                dwStart = CNTS_START_CNTMATCH_CH3_REG1
                ExtSig2 = CNTS_EXT_CNTMATCH_CH3_REG1
            Case 12
                dwStart = CNTS_START_CNTMATCH_CH4_REG1
                ExtSig2 = CNTS_EXT_CNTMATCH_CH4_REG1
            Case 12
                dwStart = CNTS_START_CNTMATCH_CH5_REG1
                ExtSig2 = CNTS_EXT_CNTMATCH_CH5_REG1
            Case 12
                dwStart = CNTS_START_CNTMATCH_CH6_REG1
                ExtSig2 = CNTS_EXT_CNTMATCH_CH6_REG1
            Case 12
                dwStart = CNTS_START_CNTMATCH_CH7_REG1
                ExtSig2 = CNTS_EXT_CNTMATCH_CH7_REG1
        End Select

        Select Case cboStartWith.SelectedIndex
            Case 0
                wStartWith = CNTS_START_CNT_NOT_WITH_SAMP
            Case 1
                wStartWith = CNTS_START_CNT_WITH_SAMP
        End Select
        Select Case cboClock.SelectedIndex
            Case 0
                dwClock = CNTS_CLOCK_CLOCK
                ExtSig1 = CNTS_EXT_CLOCK
            Case 1
                dwClock = CNTS_CLOCK_EXT_TRG
                ExtSig1 = CNTS_EXT_EXT_TRG
        End Select

        Select Case cboStop.SelectedIndex
            Case 0
                dwStop = CNTS_STOP_SOFT
                ExtSig3 = CNTS_STOP_EXT_RISE
            Case 1
                dwStop = CNTS_CLOCK_EXT_TRG
                ExtSig3 = CNTS_EXT_STOP_EXT_RISE
            Case 2
                dwStop = CNTS_STOP_EXT_FALL
                ExtSig3 = CNTS_EXT_STOP_EXT_FALL
            Case 3
                dwStop = CNTS_STOP_NUM
                ExtSig3 = CNTS_EXT_TRNSNUM
            Case 4
                dwStop = CNTS_STOP_CNTMATCH_CH0_REG0
                ExtSig3 = CNTS_EXT_CNTMATCH_CH0_REG0
            Case 5
                dwStop = CNTS_STOP_CNTMATCH_CH1_REG0
                ExtSig3 = CNTS_EXT_CNTMATCH_CH1_REG0
            Case 6
                dwStop = CNTS_STOP_CNTMATCH_CH2_REG0
                ExtSig3 = CNTS_EXT_CNTMATCH_CH2_REG0
            Case 7
                dwStop = CNTS_STOP_CNTMATCH_CH3_REG0
                ExtSig3 = CNTS_EXT_CNTMATCH_CH3_REG0
            Case 8
                dwStop = CNTS_STOP_CNTMATCH_CH4_REG0
                ExtSig3 = CNTS_EXT_CNTMATCH_CH4_REG0
            Case 9
                dwStop = CNTS_STOP_CNTMATCH_CH5_REG0
                ExtSig3 = CNTS_EXT_CNTMATCH_CH5_REG0
            Case 10
                dwStop = CNTS_STOP_CNTMATCH_CH6_REG0
                ExtSig3 = CNTS_EXT_CNTMATCH_CH6_REG0
            Case 11
                dwStop = CNTS_STOP_CNTMATCH_CH7_REG0
                ExtSig3 = CNTS_EXT_CNTMATCH_CH7_REG0
            Case 12
                dwStop = CNTS_STOP_CNTMATCH_CH0_REG1
                ExtSig3 = CNTS_EXT_CNTMATCH_CH0_REG1
            Case 13
                dwStop = CNTS_STOP_CNTMATCH_CH1_REG1
                ExtSig3 = CNTS_EXT_CNTMATCH_CH1_REG1
            Case 14
                dwStop = CNTS_STOP_CNTMATCH_CH2_REG1
                ExtSig3 = CNTS_EXT_CNTMATCH_CH2_REG1
            Case 15
                dwStop = CNTS_STOP_CNTMATCH_CH3_REG1
                ExtSig3 = CNTS_EXT_CNTMATCH_CH3_REG1
            Case 16
                dwStop = CNTS_STOP_CNTMATCH_CH4_REG1
                ExtSig3 = CNTS_EXT_CNTMATCH_CH4_REG1
            Case 17
                dwStop = CNTS_STOP_CNTMATCH_CH5_REG1
                ExtSig3 = CNTS_EXT_CNTMATCH_CH5_REG1
            Case 18
                dwStop = CNTS_STOP_CNTMATCH_CH6_REG1
                ExtSig3 = CNTS_EXT_CNTMATCH_CH6_REG1
            Case 19
                dwStop = CNTS_STOP_CNTMATCH_CH7_REG1
                ExtSig3 = CNTS_EXT_CNTMATCH_CH7_REG1
        End Select

        Select Case cboStopWith.SelectedIndex
            Case 0
                wStopWith = CNTS_STOP_CNT_NOT_WITH_SAMP
            Case 1
                wStopWith = CNTS_STOP_CNT_WITH_SAMP
        End Select

        If Checked = True Then
            Form1.M_IsRing = CNTS_WRITE_ONCE
        Else
            Form1.M_IsRing = CNTS_WRITE_RING
        End If

        '--------------------------------
        ' Set sampling condition
        '--------------------------------
        ret = CntSetMaster(Form1.M_Id, ExtSig1, ExtSig2, ExtSig3, 1, 1)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetMaster ret = {0:d}: {1}", ret, error_string)
            MsgBox(strMsg, MsgBoxStyle.OkOnly, Nothing)
        End If
        ret = CntSetSamplingStartTrigger(Form1.M_Id, dwStart, wStartWith, CHANNEL_NUM)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetSamplingStartTrigger ret = {0:d}: {1}", ret, error_string)
            MsgBox(strMsg, MsgBoxStyle.OkOnly, Nothing)
        End If
        ret = CntSetSamplingClockTrigger(Form1.M_Id, dwClock)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetSamplingClockTrigger ret = {0:d}: {1}", ret, error_string)
            MsgBox(strMsg, MsgBoxStyle.OkOnly, Nothing)
        End If
        ret = CntSetSamplingStopTrigger(Form1.M_Id, dwStop, wStopWith, CHANNEL_NUM)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetSamplingStopTrigger ret = {0:d}: {1}", ret, error_string)
            MsgBox(strMsg, MsgBoxStyle.OkOnly, Nothing)
        End If
        ret = CntSetSamplingInternalClock(Form1.M_Id, InternalClock, CNTS_TIM_UNIT_NS)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetSamplingInternalClock ret = {0:d}: {1}", ret, error_string)
            MsgBox(strMsg, MsgBoxStyle.OkOnly, Nothing)
        End If
        ret = CntSetSamplingStopNumber(Form1.M_Id, StopNum)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetSamplingStopNumber ret = {0:d}: {1}", ret, error_string)
            MsgBox(strMsg, MsgBoxStyle.OkOnly, Nothing)
        End If

        Select Case dwStart
            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 0
                CompChNo = &HFFFFS
                CompRegNo = &HFFFFS
        End Select
        If (CompChNo <> &HFFFFS) And (CompRegNo <> &HFFFFS) Then
            ret = CntNotifyCountUp(Form1.M_Id, CompChNo, CompRegNo, StartComp, 0)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                strMsg = String.Format("CntNotifyCountUp ret = {0:d}: {1}", ret, error_string)
                MsgBox(strMsg, MsgBoxStyle.OkOnly, Nothing)
            End If
        End If
        Select Case dwStop
            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 0
                CompChNo = &HFFFFS
                CompRegNo = &HFFFFS
        End Select
        If (CompChNo <> &HFFFFS) And (CompRegNo <> &HFFFFS) Then
            ret = CntNotifyCountUp(Form1.M_Id, CompChNo, CompRegNo, StopComp, 0)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                strMsg = String.Format("CntNotifyCountUp ret = {0:d}: {1}", ret, error_string)
                MsgBox(strMsg, MsgBoxStyle.OkOnly, Nothing)
            End If
        End If
        Me.Hide()
    End Sub

    '===================================================
    ' Cancel processing
    '===================================================
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Hide()
    End Sub

    Private Sub frmMSmpMode_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub
End Class