﻿Option Strict Off
Option Explicit On

Public Class Mode
    Dim Ret As Int32                                         ' 戻り値
    Dim szError As New System.Text.StringBuilder("", 256)    ' エラー文字列
    Dim szReturnCode As New String("", 256)                  ' 表示エラー文字列
    Dim szText As New String("", 256)                        ' テンポラリ文字列

    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

    '---------------------------------------------------------
    ' モード定義
    '---------------------------------------------------------
    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("未使用", CNT_ZPHASE_NOT_USE),
                                                New StrNum("次の１回", CNT_ZPHASE_NEXT_ONE),
                                                New StrNum("毎回", CNT_ZPHASE_EVERY_TIME),
                                                New StrNum("End", 0)
    }
    Dim cboZLogicL() As StrNum = {
                                                New StrNum("正論理", CNT_ZLOGIC_POSITIVE),
                                                New StrNum("負論理", CNT_ZLOGIC_NEGATIVE),
                                                New StrNum("End", 0)
    }
    Dim cboChannelSignalL() As StrNum = {
                                                New StrNum("フォトカプラ絶縁", CNT_SIGTYPE_ISOLATE),
                                                New StrNum("TTL", CNT_SIGTYPE_TTL),
                                                New StrNum("差動", CNT_SIGTYPE_LINERECEIVER),
                                                New StrNum("End", 0)
    }
    Dim cboCntDirL() As StrNum = {
                                                New StrNum("ダウンカウント", CNT_DIR_DOWN),
                                                New StrNum("アップカウント", CNT_DIR_UP),
                                                New StrNum("End", 0)
    }
    Dim cboPhaseL() As StrNum = {
                                                New StrNum("単相", CNT_MODE_1PHASE),
                                                New StrNum("２相", CNT_MODE_2PHASE),
                                                New StrNum("ゲートコントロール", CNT_MODE_GATECONTROL),
                                                New StrNum("End", 0)
    }
    Dim cboMulL() As StrNum = {
                                                New StrNum("１逓倍", CNT_MUL_X1),
                                                New StrNum("２逓倍", CNT_MUL_X2),
                                                New StrNum("４逓倍", CNT_MUL_X4),
                                                New StrNum("End", 0)
    }
    Dim cboSyncClrL() As StrNum = {
                                                New StrNum("非同期クリア", CNT_CLR_ASYNC),
                                                New StrNum("同期クリア", CNT_CLR_SYNC),
                                                New StrNum("End", 0)
    }

    '---------------------------------------------------------
    ' サブルーチン：番号←→文字列変換
    '---------------------------------------------------------
    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

    '---------------------------------------------------------
    ' ダイアログ初期化
    '---------------------------------------------------------
    Private Sub Mode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '-----------------------------------
        ' コンボボックスの初期化
        '-----------------------------------
        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
        '-----------------------------------
        ' チャネル0選択処理
        '-----------------------------------
        cboChNo_SelectionChangeCommitted(cboChNo, New System.EventArgs())

    End Sub

    '---------------------------------------------------------
    ' 設定ボタンの処理
    '---------------------------------------------------------
    Private Sub btnSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSet.Click
        '-----------------------------------
        ' 画面からモードを取得
        '-----------------------------------
        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 = Short.Parse(szText, System.Globalization.NumberStyles.HexNumber)
        '-----------------------------------
        ' モードの設定
        '-----------------------------------
        Ret = CntSetZMode(Id, ChNo, ZMode)
        If Ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(Ret, szError)
            szReturnCode = "CntSetZMode : " & szError.ToString()
            MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "エラー")
        End If
        Ret = CntSetZLogic(Id, ChNo, ZLogic)
        If Ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(Ret, szError)
            szReturnCode = "CntSetZLogic : " & szError.ToString()
            MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "エラー")
        End If
        Ret = CntSelectChannelSignal(Id, ChNo, SigType)
        If Ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(Ret, szError)
            szReturnCode = "CntSelectChannelSignal : " & szError.ToString()
            MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "エラー")
        End If
        Ret = CntSetCountDirection(Id, ChNo, Dir)
        If Ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(Ret, szError)
            szReturnCode = "CntSetCountDirection : " & szError.ToString()
            MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "エラー")
        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, "エラー")
        End If
        Ret = CntSetDigitalFilter(Id, ChNo, FilterValue)
        If Ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(Ret, szError)
            szReturnCode = "CntSetDigitalFilter : " & szError.ToString()
            MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "エラー")
        End If

        Me.Close()

    End Sub

    '---------------------------------------------------------
    ' キャンセルボタンの処理
    '---------------------------------------------------------
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        Me.Close()

    End Sub

    '---------------------------------------------------------
    ' チャネルコンボボックス選択処理
    '---------------------------------------------------------
    Private Sub cboChNo_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboChNo.SelectionChangeCommitted

        '-----------------------------------
        ' モードの取得
        '-----------------------------------
        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)
        '-----------------------------------
        ' モードの設定
        '-----------------------------------
        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