//-----------------------------------------------
//TUSB-0214DAM用ドライバアクセス用API
//Visual C# 用モジュール
//
//2023_02_29　株式会社タートル工業
//Copyright (C) 2024 Turtle Industry Co.,Ltd.
//-----------------------------------------------

using System.Runtime.InteropServices;
using TU_STATUS = System.UInt32;

// 設定値定義
enum ModuleN : byte
{
    CH1 = 0,
    CH2 = 1,
    Ext = 2
}
enum GateSt : byte
{
    GOff = 0,
    GOn = 1
}
enum ChSel : byte
{
    Ch1 = 0,
    Ch2 = 1,
}
enum RangeSel : 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
}
enum TrgSel : byte
{
    TStop = 0,
    TStart = 1
}
enum TrgStat : byte
{
    TStop = 0,
    TRun = 1
}

// DAC動作設定構造体
enum SetMode : byte//動作モード
{
    DC = 0,
    Gate = 1,
    Trg = 2,
    Fifo = 3
}
enum SigSrc : byte//クロック、トリガなどのソース
{
    Internal = 0,
    External = 1,
    EXIO = 2
}
enum SigInv : byte// 信号反転
{
    NonInvert = 0,
    Invert = 1
}
enum RestartPos : byte//波形出力モードがゲートの時にゲート停止から再びゲート開始になった時に波形メモリカウンタを何処から始めるか
{
    Zero = 0,       // ゼロから開始
    Conti = 1       // 前回の続き
}
enum FinState : byte//波形出力停止時の状態選択
{
    LastValue = 0,  // 最終出力値
    SpecificValue = 1// 特定値
}
[StructLayout(LayoutKind.Sequential)]
struct stSettings
{
    public SetMode Mode;        //動作モード 0:固定値出力 1:連続波形ゲート動作 2:連続波形トリガ動作
    public SigSrc ClkSel;       //連続波形の時のクロックソース 0:内部 1:外部入力
    public SigSrc GTSel;        //連続波形の時のゲート/トリガソース 0:内部 1:外部入力
    public SigInv ExGtInv;      //外部ゲート信号論理反転 0:反転しない(Lowの時出力) 1:反転する ※ゲートの時のみ　トリガの場合は反転しない
    public RestartPos Rstc;     //波形出力モードがゲートの時にゲート停止から再びゲート開始になった時に波形メモリカウンタをリセットするか　0:する 1:しない
    public byte xOption;        //オプション　予約機能 0を設定する
    public FinState FStat;    //波形出力停止時の状態選択 0:波形メモリの最終値 1:停止時出力指定値
    public ushort FVal;       //波形停止時出力指定値
    public ushort RepCyc;       //トリガ動作波形出力回数
}

class TUSB0214DAM
{
    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)] 
    public static extern TU_STATUS TUSB0214DAM_Connect(string SerialNumber);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_DisConnect(string SerialNumber);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_Dio_Dir(string SerialNumber, byte DirData);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_Dio_Dir_R(string SerialNumber, out byte DirData);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_Dio_In(string SerialNumber, out byte InData);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_Dio_Out(string SerialNumber, byte OutData);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_Dio_Out_R(string SerialNumber, out byte OutData);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_Exs_Sel(string SerialNumber, byte OutSel0, byte OutSel1);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_Exs_Sel_R(string SerialNumber, out byte OutSel0, out byte OutSel1);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_Clock_Gen(string SerialNumber, ModuleN MSel, uint DivideN);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_DacChSet(string SerialNumber, ModuleN MSel, ref stSettings Damset);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_DacChSet_R(string SerialNumber, ModuleN MSel, out stSettings Damset);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_WavImgSet(string SerialNumber, ModuleN MSel, [In] ushort[] WImage, int Len);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_Fifo_ImgSet(string SerialNumber, ChSel Ch, [In] ushort[] WImage, int Len);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_Fifo_Clr(string SerialNumber, ChSel Ch);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_Fifo_Status(string SerialNumber, [Out] byte[] Over, [Out] byte[] Under, [Out] uint[] DataLen);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_Trigger(string SerialNumber, ModuleN MSel, TrgSel SSel);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_Trigger_Stat(string SerialNumber, ModuleN MSel, out TrgStat Stat);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_GateSet(string SerialNumber, ModuleN MSel, GateSt GateEn);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_GateSet_R(string SerialNumber, ModuleN MSel, out GateSt GateEn);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_RangeSet(string SerialNumber, ChSel Ch, RangeSel Range);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_RangeSet_R(string SerialNumber, ChSel Ch, out RangeSel Range);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_DacValSet(string SerialNumber, ChSel Ch, ushort NVal);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_DacValSet_R(string SerialNumber, ChSel Ch, out ushort NVal);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_EXIO_Sel(string SerialNumber, byte EXIOSel0, byte EXIOSel1);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_EXIO_Sel_R(string SerialNumber, out byte EXIOSel0, out byte EXIOSel1);

    [DllImport("TUSB0214DAM.DLL", CallingConvention = CallingConvention.StdCall)]
    public static extern TU_STATUS TUSB0214DAM_EXIO_In(string SerialNumber, out byte InData);

    public static string GetErrMessage(TU_STATUS retcode)
    {
        switch (retcode)
        {
            case 0:
                return "正常実行しました";
            case 1:
                return "引数が不正です";
            case 2:
                return "接続されていません";
            case 3:
                return "接続されている台数が多すぎます";
            case 4:
                return "デバイスがみつかりません";
            case 5:
                return "USB通信エラーです";
            case 6:
                return "デバイスは接続されています";
            default:
                return "不明なエラーです";
        }
    }
}

