﻿Imports System.Text

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

        '===================================================
        ' Initialize ComboBox
        '===================================================
        Dim i As Short
        ' Z phase mode
        cboZPhaseMode.Items.Clear()
        cboZPhaseMode.Items.Insert(0, "Not Use")
        cboZPhaseMode.Items.Insert(1, "Next One")
        cboZPhaseMode.Items.Insert(2, "Every Time")

        ' Z phase logic
        cboZPhaseLogic.Items.Clear()
        cboZPhaseLogic.Items.Insert(0, "Positive")
        cboZPhaseLogic.Items.Insert(1, "Negative")

        ' Signal source
        cboSignalSource.Items.Clear()
        cboSignalSource.Items.Insert(0, "Photocoupler insulation")
        cboSignalSource.Items.Insert(1, "TTL")
        cboSignalSource.Items.Insert(2, "Line Receiver")

        ' Count direction
        cboCountDirection.Items.Clear()
        cboCountDirection.Items.Insert(0, "Down Count")
        cboCountDirection.Items.Insert(1, "Up Count")

        ' 1Phase/2Phase
        cboPhase.Items.Clear()
        cboPhase.Items.Insert(0, "1 Phase")
        cboPhase.Items.Insert(1, "2 Phase")
        cboPhase.Items.Insert(2, "Gate Control")

        ' Mul
        cboMul.Items.Clear()
        cboMul.Items.Insert(0, "X1")
        cboMul.Items.Insert(1, "X2")
        cboMul.Items.Insert(2, "X4")

        ' Sync clear
        cboSyncClear.Items.Clear()
        cboSyncClear.Items.Insert(0, "Async")
        cboSyncClear.Items.Insert(1, "Sync")

        ' Filter
        cboFilter.Items.Clear()
        For i = 0 To 15
            cboFilter.Items.Insert(i, Hex(i))
        Next

        ' Counter mode
        cboCounterMode.Items.Clear()
        cboCounterMode.Items.Insert(0, "Normal")
        cboCounterMode.Items.Insert(1, "Differencial")

        '-------------------------------
        ' Set initial data
        '-------------------------------
        cboZPhaseMode.SelectedIndex = 0
        cboZPhaseLogic.SelectedIndex = 1
        cboSignalSource.SelectedIndex = 2
        cboCountDirection.SelectedIndex = 1
        cboPhase.SelectedIndex = 1
        cboMul.SelectedIndex = 0
        cboSyncClear.SelectedIndex = 0
        cboFilter.SelectedIndex = 0
        cboCounterMode.SelectedIndex = 0

    End Sub

    '---------------------------------------------
    ' OK button processing
    '---------------------------------------------
    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 setting 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
        '--------------------------------
        ' Signal source
        '--------------------------------
        wSignalSource = 0
        If cboSignalSource.Text = "Photocoupler insulation" Then wSignalSource = CNT_SIGTYPE_ISOLATE
        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
        '--------------------------------
        ' 1Phase/2Phase
        '--------------------------------
        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
        '--------------------------------
        ' Mul
        '--------------------------------
        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
        '--------------------------------
        ' Sync clear
        '--------------------------------
        wSyncClear = 0
        If cboSyncClear.Text = "Async" Then wSyncClear = CNT_CLR_ASYNC
        If cboSyncClear.Text = "Sync" Then wSyncClear = CNT_CLR_SYNC
        '--------------------------------
        ' Filter
        '--------------------------------
        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 setting 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 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 1Phase/2Phase
            '---------------------------------------------
            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 filter
            '---------------------------------------------
            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 processing
    '---------------------------------------------
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        Me.Hide()

    End Sub

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

        e.Cancel = True
        Me.Hide()

    End Sub
End Class