Imports System.Runtime.InteropServices
Imports TU_STATUS = System.UInt32

Namespace TUSB0214DAMSampleApp
    ' 設定値定義
    Public Enum ModuleN As Byte
        CH1 = 0
        CH2 = 1
        Ext = 2
    End Enum

    Public Enum GateSt As Byte
        GOff = 0
        GOn = 1
    End Enum

    Public Enum ChSel As Byte
        Ch1 = 0
        Ch2 = 1
        'Ch3 = 2
        'Ch4 = 3
    End Enum

    Public Enum RangeSel As Byte
        Bipol10 = 0    '±10V
        Bipol5 = 1     '±5V
        Bipol2_5 = 2   '±2.5V
        Bipol1_25 = 3  '±1.25V
        Unipol10 = 4   '10V
        Unipol5 = 5    '5V
        Unipol2_5 = 6  '2.5V
    End Enum

    Public Enum TrgSel As Byte
        TStop = 0
        TStart = 1
    End Enum

    Public Enum TrgStat As Byte
        TStop = 0
        TRun = 1
    End Enum

    ' DAC動作設定構造体
    Public Enum SetMode As Byte '動作モード
        DC = 0
        Gate = 1
        Trg = 2
        Fifo = 3
    End Enum

    Public Enum SigSrc As Byte 'クロック、トリガなどのソース
        Internal = 0
        External = 1
        EXIO = 2
    End Enum

    Public Enum SigInv As Byte '信号反転
        NonInvert = 0
        Invert = 1
    End Enum

    Public Enum RestartPos As Byte '波形出力モードがゲートの時にゲート停止から再びゲート開始になった時に波形メモリカウンタを何処から始めるか
        Zero = 0       'ゼロから開始
        Conti = 1      '前回の続き
    End Enum

    Public Enum FinState As Byte '波形出力停止時の状態選択
        LastValue = 0  '最終出力値
        SpecificValue = 1 '特定値
    End Enum

    <StructLayout(LayoutKind.Sequential)>
    Public Structure stSettings
        Public Mode As SetMode        '動作モード 0:固定値出力 1:連続波形ゲート動作 2:連続波形トリガ動作
        Public ClkSel As SigSrc       '連続波形の時のクロックソース 0:内部 1:外部入力
        Public GTSel As SigSrc        '連続波形の時のゲート/トリガソース 0:内部 1:外部入力
        Public ExGtInv As SigInv      '外部ゲート信号論理反転 0:反転しない(Lowの時出力) 1:反転する ※ゲートの時のみ　トリガの場合は反転しない
        Public Rstc As RestartPos     '波形出力モードがゲートの時にゲート停止から再びゲート開始になった時に波形メモリカウンタをリセットするか　0:する 1:しない
        Public xOption As Byte        'オプション　予約機能 0を設定する
        Public FStat As FinState      '波形出力停止時の状態選択 0:波形メモリの最終値 1:停止時出力指定値
        Public FVal As UShort         '波形停止時出力指定値
        Public RepCyc As UShort       'トリガ動作波形出力回数
    End Structure

    Public Class TUSB0214DAM
        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_Connect(SerialNumber As String) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_DisConnect(SerialNumber As String) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_Dio_Dir(SerialNumber As String, DirData As Byte) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_Dio_Dir_R(SerialNumber As String, <Out> ByRef DirData As Byte) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_Dio_In(SerialNumber As String, <Out> ByRef InData As Byte) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_Dio_Out(SerialNumber As String, OutData As Byte) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_Dio_Out_R(SerialNumber As String, <Out> ByRef OutData As Byte) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_Exs_Sel(SerialNumber As String, OutSel0 As Byte, OutSel1 As Byte) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_Exs_Sel_R(SerialNumber As String, <Out> ByRef OutSel0 As Byte, <Out> ByRef OutSel1 As Byte) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_Clock_Gen(SerialNumber As String, MSel As ModuleN, DivideN As UInteger) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_DacChSet(SerialNumber As String, MSel As ModuleN, ByRef Damset As stSettings) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_DacChSet_R(SerialNumber As String, MSel As ModuleN, <Out> ByRef Damset As stSettings) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_WavImgSet(SerialNumber As String, MSel As ModuleN, <[In]> WImage() As UShort, Len As Integer) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_Fifo_ImgSet(SerialNumber As String, Ch As ChSel, <[In]> WImage() As UShort, Len As Integer) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_Fifo_Clr(SerialNumber As String, Ch As ChSel) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_Fifo_Status(SerialNumber As String, <Out> Over() As Byte, <Out> Under() As Byte, <Out> DataLen() As UInteger) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_Trigger(SerialNumber As String, MSel As ModuleN, SSel As TrgSel) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_Trigger_Stat(SerialNumber As String, MSel As ModuleN, <Out> ByRef Stat As TrgStat) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_GateSet(SerialNumber As String, MSel As ModuleN, GateEn As GateSt) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_GateSet_R(SerialNumber As String, MSel As ModuleN, <Out> ByRef GateEn As GateSt) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_RangeSet(SerialNumber As String, Ch As ChSel, Range As RangeSel) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_RangeSet_R(SerialNumber As String, Ch As ChSel, <Out> ByRef Range As RangeSel) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_DacValSet(SerialNumber As String, Ch As ChSel, NVal As UShort) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_DacValSet_R(SerialNumber As String, Ch As ChSel, <Out> ByRef NVal As UShort) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_EXIO_Sel(SerialNumber As String, EXIOSel0 As Byte, EXIOSel1 As Byte) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_EXIO_Sel_R(SerialNumber As String, <Out> ByRef EXIOSel0 As Byte, <Out> ByRef EXIOSel1 As Byte) As TU_STATUS
        End Function

        <DllImport("TUSB0214DAM.DLL", CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function TUSB0214DAM_EXIO_In(SerialNumber As String, <Out> ByRef InData As Byte) As TU_STATUS
        End Function

        Public Shared Function GetErrMessage(retcode As TU_STATUS) As String
            Select Case retcode
                Case 0
                    Return "正常実行しました"
                Case 1
                    Return "引数が不正です"
                Case 2
                    Return "接続されていません"
                Case 3
                    Return "接続されている台数が多すぎます"
                Case 4
                    Return "デバイスがみつかりません"
                Case 5
                    Return "USB通信エラーです"
                Case 6
                    Return "デバイスは接続されています"
                Case Else
                    Return "不明なエラーです"
            End Select
        End Function
    End Class
End Namespace