﻿Imports System.Text

Public Class Mode

    Private Sub frmMode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Initialize Screen
        Me.Left = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2

        '===================================================
        ' Initialize combo boxes
        '===================================================
        '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

        'External signal source
        With cboSignalSource
            .Items.Clear()
            .Items.Insert(0, "Photocoupler insulation")
            .Items.Insert(1, "TTL")
            .Items.Insert(2, "Line Receiver")
        End With

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

        'Operation mode
        With cboPhase
            .Items.Clear()
            .Items.Insert(0, "1 Phase")
            .Items.Insert(1, "2 Phase")
            .Items.Insert(2, "Gate Control")
        End With

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

        'Synd/Async clear
        With cboSyncClear
            .Items.Clear()
            .Items.Insert(0, "Async")
            .Items.Insert(1, "Sync")
        End With

        'Digital filter value
        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")
            .Items.Insert(&H10S, "10")
            .Items.Insert(&H11S, "11")
            .Items.Insert(&H12S, "12")
            .Items.Insert(&H13S, "13")
            .Items.Insert(&H14S, "14")
        End With

        cboZPhaseMode.SelectedIndex = 0
        cboZPhaseLogic.SelectedIndex = 1
        cboSignalSource.SelectedIndex = 2
        cboCountDirection.SelectedIndex = 1
        cboPhase.SelectedIndex = 1
        cboMul.SelectedIndex = 0
        cboSyncClear.SelectedIndex = 0
        cboFilter.SelectedIndex = 0
    End Sub

    '---------------------------------------------
    ' Setting completion 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

        '-------------------------------
        ' 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 = "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
        '--------------------------------
        ' 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

        '-------------------------------
        ' Set the set information to device
        '-------------------------------
        For ChNo = 0 To Form1.ChannelNum - 1
            '---------------------------------------------
            ' Set Z phase mode
            '---------------------------------------------
            ret = CntSetZMode(Form1.Id, ChNo, wZPhaseMode)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetZMode : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                MsgBox(str, MsgBoxStyle.OkOnly, "Error")
            End If
            '---------------------------------------------
            ' Set Z phase logic
            '---------------------------------------------
            ret = CntSetZLogic(Form1.Id, ChNo, wZPhaseLogic)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetZLogic : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                MsgBox(str, MsgBoxStyle.OkOnly, "Error")
            End If

            '---------------------------------------------
            ' Set external signal source
            '---------------------------------------------
            ret = CntSelectChannelSignal(Form1.Id, ChNo, wSignalSource)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSelectChannelSignal : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                MsgBox(str, MsgBoxStyle.OkOnly, "Error")
            End If
            '---------------------------------------------
            ' Set count direction
            '---------------------------------------------
            ret = CntSetCountDirection(Form1.Id, ChNo, wCountDirection)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetCountDirection : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                MsgBox(str, MsgBoxStyle.OkOnly, "Error")
            End If
            '---------------------------------------------
            ' Set operation mode
            '---------------------------------------------
            ret = CntSetOperationMode(Form1.Id, ChNo, wPhase, wMul, wSyncClear)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetOperationMode : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                MsgBox(str, MsgBoxStyle.OkOnly, "Error")
            End If

            '---------------------------------------------
            ' Set digital filter value
            '---------------------------------------------
            ret = CntSetDigitalFilter(Form1.Id, ChNo, wFilter)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetDigitalFilter : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                MsgBox(str, MsgBoxStyle.OkOnly, "Error")
            End If
        Next ChNo
        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 frmMode_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub
End Class