﻿Option Strict Off
Option Explicit On

Public Class Mode
    Dim Ret As Int32                                    ' Return value
    Dim szError As New System.Text.StringBuilder(256)   ' Error string
    Dim szReturnCode As New String("", 256)             ' The string for error
    Dim szText As New String("", 256)                   ' Temporary string

    Dim ChNo As Short
    Dim ZMode As Short
    Dim ZLogic As Short
    Dim SigType As Short
    Dim Dir As Short
    Dim Phase As Short
    Dim Mul As Short
    Dim SyncClr As Short
    Dim FilterValue As Short

    '---------------------------------------------------------
    ' Define mode
    '---------------------------------------------------------
    Private Structure StrNum
        Public Str As String
        Public Num As Short
        Public Sub New(ByVal Str As String, ByVal Num As Short)
            Me.Str = Str
            Me.Num = Num
        End Sub
    End Structure

    Dim cboZModeL() As StrNum = {
                                                New StrNum("Not Use", CNT_ZPHASE_NOT_USE),
                                                New StrNum("Next Time", CNT_ZPHASE_NEXT_ONE),
                                                New StrNum("Everytime", CNT_ZPHASE_EVERY_TIME),
                                                New StrNum("End", 0)
    }
    Dim cboZLogicL() As StrNum = {
                                                New StrNum("Positive Logic", CNT_ZLOGIC_POSITIVE),
                                                New StrNum("Negative Logic", CNT_ZLOGIC_NEGATIVE),
                                                New StrNum("End", 0)
    }
    Dim cboChannelSignalL() As StrNum = {
                                                New StrNum("Photoisolator", CNT_SIGTYPE_ISOLATE),
                                                New StrNum("TTL", CNT_SIGTYPE_TTL),
                                                New StrNum("Differential", CNT_SIGTYPE_LINERECEIVER),
                                                New StrNum("End", 0)
    }
    Dim cboCntDirL() As StrNum = {
                                                New StrNum("Down-count", CNT_DIR_DOWN),
                                                New StrNum("Up-count", CNT_DIR_UP),
                                                New StrNum("End", 0)
    }
    Dim cboPhaseL() As StrNum = {
                                                New StrNum("1 Phase", CNT_MODE_1PHASE),
                                                New StrNum("2 Phase", CNT_MODE_2PHASE),
                                                New StrNum("Gate Control", CNT_MODE_GATECONTROL),
                                                New StrNum("End", 0)
    }
    Dim cboMulL() As StrNum = {
                                                New StrNum("x1", CNT_MUL_X1),
                                                New StrNum("x2", CNT_MUL_X2),
                                                New StrNum("x4", CNT_MUL_X4),
                                                New StrNum("End", 0)
    }
    Dim cboSyncClrL() As StrNum = {
                                                New StrNum("Asynchronous Clear", CNT_CLR_ASYNC),
                                                New StrNum("Synchronous Clear", CNT_CLR_SYNC),
                                                New StrNum("End", 0)
    }

    '---------------------------------------------------------
    ' Convert a number to a string and convert a string to a number
    '---------------------------------------------------------
    Private Function NumToStr(ByVal p() As StrNum, ByVal Num As Short) As String

        Dim i As Short
        Dim None As String = ""
        i = 0
        While (StrComp(p(i).Str, "End") <> 0)
            If p(i).Num = Num Then
                Return p(i).Str
            End If
            i += 1
        End While
        Return None

    End Function

    Private Function StrToNum(ByVal p() As StrNum, ByVal Str As String) As Short

        Dim i As Short
        i = 0
        While (StrComp(p(i).Str, "End") <> 0)
            If StrComp(p(i).Str, Str) = 0 Then
                Return p(i).Num
            End If
            i += 1
        End While
        Return 0

    End Function

    '---------------------------------------------------------
    ' Initialize dialog
    '---------------------------------------------------------
    Private Sub Mode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '-----------------------------------
        ' Initialize combobox
        '-----------------------------------
        Dim i As Short
        For i = 0 To (ChannelNum - 1)
            cboChNo.Items.Add(i)
        Next i
        cboChNo.SelectedIndex = 0
        i = 0
        While (StrComp(cboZModeL(i).Str, "End") <> 0)
            cboZMode.Items.Add(cboZModeL(i).Str)
            i += 1
        End While
        i = 0
        While (StrComp(cboZLogicL(i).Str, "End") <> 0)
            cboZLogic.Items.Add(cboZLogicL(i).Str)
            i += 1
        End While
        i = 0
        While (StrComp(cboChannelSignalL(i).Str, "End") <> 0)
            cboSource.Items.Add(cboChannelSignalL(i).Str)
            i += 1
        End While
        i = 0
        While (StrComp(cboCntDirL(i).Str, "End") <> 0)
            cboDir.Items.Add(cboCntDirL(i).Str)
            i += 1
        End While
        i = 0
        While (StrComp(cboPhaseL(i).Str, "End") <> 0)
            cboPhase.Items.Add(cboPhaseL(i).Str)
            i += 1
        End While
        i = 0
        While (StrComp(cboMulL(i).Str, "End") <> 0)
            cboMul.Items.Add(cboMulL(i).Str)
            i += 1
        End While
        i = 0
        While (StrComp(cboSyncClrL(i).Str, "End") <> 0)
            cboClr.Items.Add(cboSyncClrL(i).Str)
            i += 1
        End While
        '-----------------------------------
        ' Select channel 0
        '-----------------------------------
        cboChNo_SelectionChangeCommitted(cboChNo, New System.EventArgs())

    End Sub

    '---------------------------------------------------------
    ' Process Set button
    '---------------------------------------------------------
    Private Sub btnSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSet.Click
        '-----------------------------------
        ' Get mode from screen
        '-----------------------------------
        szText = cboZMode.Text
        ZMode = StrToNum(cboZModeL, szText)
        szText = cboZLogic.Text
        ZLogic = StrToNum(cboZLogicL, szText)
        szText = cboSource.Text
        SigType = StrToNum(cboChannelSignalL, szText)
        szText = cboDir.Text
        Dir = StrToNum(cboCntDirL, szText)
        szText = cboPhase.Text
        Phase = StrToNum(cboPhaseL, szText)
        szText = cboMul.Text
        Mul = StrToNum(cboMulL, szText)
        szText = cboClr.Text
        SyncClr = StrToNum(cboSyncClrL, szText)
        szText = txtFilter.Text
        FilterValue = CLng("&H" & szText)
        '-----------------------------------
        ' Set mode
        '-----------------------------------
        Ret = CntSetZMode(Id, ChNo, ZMode)
        If Ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(Ret, szError)
            szReturnCode = "CntSetZMode : " & szError.ToString()
            MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "Error")
        End If
        Ret = CntSetZLogic(Id, ChNo, ZLogic)
        If Ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(Ret, szError)
            szReturnCode = "CntSetZLogic : " & szError.ToString()
            MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "Error")
        End If
        Ret = CntSelectChannelSignal(Id, ChNo, SigType)
        If Ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(Ret, szError)
            szReturnCode = "CntSelectChannelSignal : " & szError.ToString()
            MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "Error")
        End If
        Ret = CntSetCountDirection(Id, ChNo, Dir)
        If Ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(Ret, szError)
            szReturnCode = "CntSetCountDirection : " & szError.ToString()
            MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "Error")
        End If
        Ret = CntSetOperationMode(Id, ChNo, Phase, Mul, SyncClr)
        If Ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(Ret, szError)
            szReturnCode = "CntSetOperationMode : " & szError.ToString()
            MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "Error")
        End If
        Ret = CntSetDigitalFilter(Id, ChNo, FilterValue)
        If Ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(Ret, szError)
            szReturnCode = "CntSetDigitalFilter : " & szError.ToString()
            MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "Error")
        End If

        Me.Close()

    End Sub

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

        Me.Close()

    End Sub

    '---------------------------------------------------------
    ' Select channel combobox
    '---------------------------------------------------------
    Private Sub cboChNo_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboChNo.SelectionChangeCommitted

        '-----------------------------------
        ' Get mode
        '-----------------------------------
        ChNo = CShort(cboChNo.Text)

        CntGetZMode(Id, ChNo, ZMode)
        CntGetZLogic(Id, ChNo, ZLogic)
        CntGetChannelSignal(Id, ChNo, SigType)
        CntGetCountDirection(Id, ChNo, Dir)
        CntGetOperationMode(Id, ChNo, Phase, Mul, SyncClr)
        CntGetDigitalFilter(Id, ChNo, FilterValue)
        '-----------------------------------
        ' Set mode
        '-----------------------------------
        cboZMode.Text = NumToStr(cboZModeL, ZMode)
        cboZLogic.Text = NumToStr(cboZLogicL, ZLogic)
        cboSource.Text = NumToStr(cboChannelSignalL, SigType)
        cboDir.Text = NumToStr(cboCntDirL, Dir)
        cboPhase.Text = NumToStr(cboPhaseL, Phase)
        cboMul.Text = NumToStr(cboMulL, Mul)
        cboClr.Text = NumToStr(cboSyncClrL, SyncClr)
        txtFilter.Text = Hex(FilterValue)

    End Sub
End Class