﻿Imports System.Text

Public Class frmCntMode
    Public Shared Id As Short
    '---------------------------------------------------
    ' InitDialog
    '---------------------------------------------------
    Private Sub frmCntMode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '-------------------------------
        ' Initialize ComboBox
        '-------------------------------
        ' Z Phase Mode
        With cboZPhaseMode
            .Items.Clear()
            .Items.Insert(0, "Not Use")
            .Items.Insert(1, "Next One")
            .Items.Insert(2, "Every Time")
        End With
        ' Z Phase Logic
        With cboZPhaseLogic
            .Items.Clear()
            .Items.Insert(0, "Positive")
            .Items.Insert(1, "Negative")
        End With
        ' Signal Source
        With cboSignalSource
            .Items.Clear()
            .Items.Insert(0, "Line Receiver")
            .Items.Insert(1, "TTL")
        End With
        ' Count Direction
        With cboCountDirection
            .Items.Clear()
            .Items.Insert(0, "Down count")
            .Items.Insert(1, "Up count")
        End With
        ' 1Phase/2Phase
        With cboPhase
            .Items.Clear()
            .Items.Insert(0, "1 Phase")
            .Items.Insert(1, "2 Phase")
            .Items.Insert(2, "Gate Control")
        End With
        ' Mul
        With cboMul
            .Items.Clear()
            .Items.Insert(0, "X1")
            .Items.Insert(1, "X2")
            .Items.Insert(2, "X4")
        End With
        ' Sync Clear
        With cboSyncClear
            .Items.Clear()
            .Items.Insert(0, "Async")
            .Items.Insert(1, "Sync")
        End With
        With cboFilter
            .Items.Clear()
            .Items.Insert(&H0S, "0")
            .Items.Insert(&H1S, "1")
            .Items.Insert(&H2S, "2")
            .Items.Insert(&H3S, "3")
            .Items.Insert(&H4S, "4")
            .Items.Insert(&H5S, "5")
            .Items.Insert(&H6S, "6")
            .Items.Insert(&H7S, "7")
            .Items.Insert(&H8S, "8")
            .Items.Insert(&H9S, "9")
            .Items.Insert(&HAS, "A")
            .Items.Insert(&HBS, "B")
            .Items.Insert(&HCS, "C")
            .Items.Insert(&HDS, "D")
            .Items.Insert(&HES, "E")
            .Items.Insert(&HFS, "F")
        End With
        ' Counter Mode
        With cboCounterMode
            .Items.Clear()
            .Items.Insert(0, "Normal")
            .Items.Insert(1, "Differencial")
        End With
        '-------------------------------
        ' Initialize ComboBox Data
        '-------------------------------
        cboZPhaseMode.SelectedIndex = 0
        cboZPhaseLogic.SelectedIndex = 1
        cboSignalSource.SelectedIndex = 0
        cboCountDirection.SelectedIndex = 1
        cboPhase.SelectedIndex = 1
        cboMul.SelectedIndex = 0
        cboSyncClear.SelectedIndex = 0
        cboFilter.SelectedIndex = 0
        cboCounterMode.SelectedIndex = 0
    End Sub

    '---------------------------------------------------
    ' OK Button
    '---------------------------------------------------
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim ChNo As Short
        Dim ret As Integer
        Dim str As String
        Dim error_string As New StringBuilder("", 256)

        Dim wZPhaseMode As Short
        Dim wZPhaseLogic As Short
        Dim wSignalSource As Short
        Dim wCountDirection As Short
        Dim wPhase As Short
        Dim wMul As Short
        Dim wSyncClear As Short
        Dim wFilter As Short
        Dim wCounterMode As Short

        '-------------------------------
        ' Get the set information
        '-------------------------------
        '--------------------------------
        ' Z Phase Mode
        '--------------------------------
        wZPhaseMode = 0
        If cboZPhaseMode.Text = "Not Use" Then wZPhaseMode = CNT_ZPHASE_NOT_USE
        If cboZPhaseMode.Text = "Next One" Then wZPhaseMode = CNT_ZPHASE_NEXT_ONE
        If cboZPhaseMode.Text = "Every Time" Then wZPhaseMode = CNT_ZPHASE_EVERY_TIME
        '--------------------------------
        ' Z Phase Logic
        '--------------------------------
        wZPhaseLogic = 0
        If cboZPhaseLogic.Text = "Positive" Then wZPhaseLogic = CNT_ZLOGIC_POSITIVE
        If cboZPhaseLogic.Text = "Negative" Then wZPhaseLogic = CNT_ZLOGIC_NEGATIVE
        '--------------------------------
        ' External Signal Source
        '--------------------------------
        wSignalSource = 0
        If cboSignalSource.Text = "TTL" Then wSignalSource = CNT_SIGTYPE_TTL
        If cboSignalSource.Text = "Line Receiver" Then wSignalSource = CNT_SIGTYPE_LINERECEIVER
        '--------------------------------
        ' Count Direction
        '--------------------------------
        wCountDirection = 0
        If cboCountDirection.Text = "Down count" Then wCountDirection = CNT_DIR_DOWN
        If cboCountDirection.Text = "Up count" Then wCountDirection = CNT_DIR_UP
        '--------------------------------
        ' Operation mode
        '--------------------------------
        wPhase = 0
        If cboPhase.Text = "1 Phase" Then wPhase = CNT_MODE_1PHASE
        If cboPhase.Text = "2 Phase" Then wPhase = CNT_MODE_2PHASE
        If cboPhase.Text = "Gate Control" Then wPhase = CNT_MODE_GATECONTROL
        '--------------------------------
        ' Multiple
        '--------------------------------
        wMul = 0
        If cboMul.Text = "X1" Then wMul = CNT_MUL_X1
        If cboMul.Text = "X2" Then wMul = CNT_MUL_X2
        If cboMul.Text = "X4" Then wMul = CNT_MUL_X4
        '--------------------------------
        ' Synd/Async clear
        '--------------------------------
        wSyncClear = 0
        If cboSyncClear.Text = "Async" Then wSyncClear = CNT_CLR_ASYNC
        If cboSyncClear.Text = "Sync" Then wSyncClear = CNT_CLR_SYNC
        '--------------------------------
        ' Digital filter value
        '--------------------------------
        wFilter = cboFilter.SelectedIndex
        '--------------------------------
        ' Counter Mode
        '--------------------------------
        wCounterMode = 0
        If cboCounterMode.Text = "Normal" Then wCounterMode = CNTS_MODE_NORMAL
        If cboCounterMode.Text = "Differencial" Then wCounterMode = CNTS_MODE_DIFF
        '-------------------------------
        ' Set the set information to device
        '-------------------------------
        For ChNo = 0 To CHANNEL_NUM - 1
            '---------------------------------------------
            ' Set Z phase mode
            '---------------------------------------------
            ret = CntSetZMode(Id, ChNo, wZPhaseMode)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetZMode : ret = {0:X} h" & vbCrLf & error_string.ToString(), ret)
                MessageBox.Show(str, "Error", MessageBoxButtons.OK)
                Exit For
            End If
            '---------------------------------------------
            ' Set Z phase logic
            '---------------------------------------------
            ret = CntSetZLogic(Id, ChNo, wZPhaseLogic)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetZLogic : ret = {0:X} h" & vbCrLf & error_string.ToString(), ret)
                MessageBox.Show(str, "Error", MessageBoxButtons.OK)
                Exit For
            End If
            '---------------------------------------------
            ' Set external signal source
            '---------------------------------------------
            ret = CntSelectChannelSignal(Id, ChNo, wSignalSource)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSelectChannelSignal : ret = {0:X} h" & vbCrLf & error_string.ToString(), ret)
                MessageBox.Show(str, "Error", MessageBoxButtons.OK)
                Exit For
            End If
            '---------------------------------------------
            ' Set count direction
            '---------------------------------------------
            ret = CntSetCountDirection(Id, ChNo, wCountDirection)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetCountDirection : ret = {0:X} h" & vbCrLf & error_string.ToString(), ret)
                MessageBox.Show(str, "Error", MessageBoxButtons.OK)
                Exit For
            End If
            '---------------------------------------------
            ' Set operation mode
            '---------------------------------------------
            ret = CntSetOperationMode(Id, ChNo, wPhase, wMul, wSyncClear)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetOperationMode : ret = {0:X} h" & vbCrLf & error_string.ToString(), ret)
                MessageBox.Show(str, "Error", MessageBoxButtons.OK)
                Exit For
            End If
            '---------------------------------------------
            ' Set digital filter value
            '---------------------------------------------
            ret = CntSetDigitalFilter(Id, ChNo, wFilter)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetOperationMode : ret = {0:X} h" & vbCrLf & error_string.ToString(), ret)
                MessageBox.Show(str, "Error", MessageBoxButtons.OK)
                Exit For
            End If
            '---------------------------------------------
            ' Set counter mode
            '---------------------------------------------
            ret = CntSetCounterMode(Id, ChNo, wCounterMode)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetCounterMode : ret = {0:X} h" & vbCrLf & error_string.ToString(), ret)
                MessageBox.Show(str, "Error", MessageBoxButtons.OK)
                Exit For
            End If
        Next ChNo
        Me.Hide()
    End Sub

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

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