//============================================================-
// cgen_api.cs
// Class file for C-WaveformGenerator
//                                             CONTEC.Co.,Ltd.
//============================================================-
using System;
using System.Runtime.InteropServices;


//================================================================
// Prototype definition
//================================================================
namespace CWaveformGeneratorSDKCs
{
    public class CWaveformGeneratorSDK
    {
        [DllImport("cgen_api.dll")] static extern int GenOpenApplication(string DeviceName, ref short AppId, ushort OpenMode);
        [DllImport("cgen_api.dll")] static extern int GenCloseApplication(short AppId, ref uint Status);
        [DllImport("cgen_api.dll")] static extern int GenStartOperation(short AppId);
        [DllImport("cgen_api.dll")] static extern int GenStopOperation(short AppId);
        [DllImport("cgen_api.dll")] static extern int GenGetApplicationStatus(short AppId, ref uint Status);
        [DllImport("cgen_api.dll")] static extern int GenExportSettingFile(short AppId, string FilePath);
        [DllImport("cgen_api.dll")] static extern int GenImportSettingFile(short AppId, string FilePath);
        [DllImport("cgen_api.dll")] static extern int GenSaveDataFile(short AppId, string FilePath, short Param1, short Param2);
        [DllImport("cgen_api.dll")] static extern int GenSetDeviceName(short AppId, string DeviceName);
        [DllImport("cgen_api.dll")] static extern int GenGetDeviceName(short AppId, System.Text.StringBuilder DeviceName);
        [DllImport("cgen_api.dll")] static extern int GenSetDeviceSetting(short AppId, ushort AoChannels, float Frequency, ushort FrequencyType, ushort FrequencyUnit);
        [DllImport("cgen_api.dll")] static extern int GenGetDeviceSetting(short AppId, ref ushort AoChannels, ref float Frequency, ref ushort FrequencyType, ref ushort FrequencyUnit);
        [DllImport("cgen_api.dll")] static extern int GenSetOscillation(short AppId, ushort OscillationType, ushort OscillationStart, float OscillationStop);
        [DllImport("cgen_api.dll")] static extern int GenGetOscillation(short AppId, ref ushort OscillationType, ref ushort OscillationStart, ref float OscillationStop);
        [DllImport("cgen_api.dll")] static extern int GenSetChannelSetting(short AppId, ushort AoChannel, float Amplitude, float Offset, ushort AmplitudeType, ushort AmplitudeUnit, float Duty, float Phase);
        [DllImport("cgen_api.dll")] static extern int GenGetChannelSetting(short AppId, ushort AoChannel, ref float Amplitude, ref float Offset, ref ushort AmplitudeType, ref ushort AmplitudeUnit, ref float Duty, ref float Phase);
        [DllImport("cgen_api.dll")] static extern int GenSetWaveType(short AppId, ushort AoChannel, ushort WaveType);
        [DllImport("cgen_api.dll")] static extern int GenGetWaveType(short AppId, ushort AoChannel, ref ushort WaveType);
        [DllImport("cgen_api.dll")] static extern int GenSetArbitraryDataFile(short AppId, ushort AoChannel, string FilePath);
        [DllImport("cgen_api.dll")] static extern int GenGetArbitraryDataFile(short AppId, ushort AoChannel, System.Text.StringBuilder FilePath);
        [DllImport("cgen_api.dll")] static extern int GenSetAoSweep(short AppId, ushort AoChannel, ushort SweepType, ushort SweepTime, float SweepStart, float SweepStop);
        [DllImport("cgen_api.dll")] static extern int GenGetAoSweep(short AppId, ushort AoChannel, ref ushort SweepType, ref ushort SweepTime, ref float SweepStart, ref float SweepStop);
        [DllImport("cgen_api.dll")] static extern int GenGetErrorString(int ErrorCode, System.Text.StringBuilder ErrorString);
        
        // Constructor
        public CWaveformGeneratorSDK()
        {
        }

        // Description of common functions
        public int OpenApplication(string DeviceName, out short AppId, ushort OpenMode)
        {
            AppId = 0;
            int ret = GenOpenApplication(DeviceName, ref AppId, OpenMode);
            return ret;
        }

        public int CloseApplication(short AppId, out uint Status)
        {
            Status = 0;
            int ret = GenCloseApplication(AppId, ref Status);
            return ret;
        }

        public int StartOperation(short AppId)
        {
            int ret = GenStartOperation(AppId);
            return ret;
        }

        public int StopOperation(short AppId)
        {
            int ret = GenStopOperation(AppId);
            return ret;
        }

        public int GetApplicationStatus(short AppId, out uint Status)
        {
            Status = 0;
            int ret = GenGetApplicationStatus(AppId, ref Status);
            return ret;
        }

        public int ExportSettingFile(short AppId, string FilePath)
        {
            int ret = GenExportSettingFile(AppId, FilePath);
            return ret;
        }

        public int ImportSettingFile(short AppId, string FilePath)
        {
            int ret = GenImportSettingFile(AppId, FilePath);
            return ret;
        }

        public int SaveDataFile(short AppId, string FilePath, short Param1, short Param2)
        {
            int ret = GenSaveDataFile(AppId, FilePath, Param1, Param2);
            return ret;
        }

        public int SetDeviceName(short AppId, string DeviceName)
        {
            int ret = GenSetDeviceName(AppId, DeviceName);
            return ret;
        }

        public int GetDeviceName(short AppId, string DeviceName)
        {
            DeviceName = new String('0', 1);
            System.Text.StringBuilder devicename = new System.Text.StringBuilder(256);
            int ret = GenGetDeviceName(AppId, devicename);
            if(ret == 0)
            {
                DeviceName = devicename.ToString();
            }
            else
            {
                DeviceName = "";
            }
            return ret;
        }

        public int SetDeviceSetting(short AppId, ushort AoChannels, float Frequency, ushort FrequencyType, ushort FrequencyUnit)
        {
            int ret = GenSetDeviceSetting(AppId, AoChannels, Frequency, FrequencyType, FrequencyUnit);
            return ret;
        }

        public int GetDeviceSetting(short AppId, out ushort AoChannels, out float Frequency, out ushort FrequencyType, out ushort FrequencyUnit)
        {
            AoChannels = 0;
            Frequency = 0;
            FrequencyType = 0;
            FrequencyUnit = 0;
            int ret = GenGetDeviceSetting(AppId, ref AoChannels, ref Frequency, ref FrequencyType, ref FrequencyUnit);
            return ret;
        }

        public int SetOscillation(short AppId, ushort OscillationType, ushort OscillationStart, float OscillationStop)
        {
            int ret = GenSetOscillation(AppId, OscillationType, OscillationStart, OscillationStop);
            return ret;
        }

        public int GetOscillation(short AppId, out ushort OscillationType, out ushort OscillationStart, out float OscillationStop)
        {
            OscillationType = 0;
            OscillationStart = 0;
            OscillationStop = 0;
            int ret = GenGetOscillation(AppId, ref OscillationType, ref OscillationStart, ref OscillationStop);
            return ret;
        }

        public int SetChannelSetting(short AppId, ushort AoChannel, float Amplitude, float Offset, ushort AmplitudeType, ushort AmplitudeUnit, float Duty, float Phase)
        {
            int ret = GenSetChannelSetting(AppId, AoChannel, Amplitude, Offset, AmplitudeType, AmplitudeUnit, Duty, Phase);
            return ret;
        }

        public int GetChannelSetting(short AppId, ushort AoChannel, out float Amplitude, out float Offset, out ushort AmplitudeType, out ushort AmplitudeUnit, out float Duty, out float Phase)
        {
            Amplitude = 0;
            Offset = 0;
            AmplitudeType = 0;
            AmplitudeUnit = 0;
            Duty = 0;
            Phase = 0;
            int ret = GenGetChannelSetting(AppId, AoChannel, ref Amplitude, ref Offset, ref AmplitudeType, ref AmplitudeUnit, ref Duty, ref Phase);
            return ret;
        }

        public int SetWaveType(short AppId, ushort AoChannel, ushort WaveType)
        {
            int ret = GenSetWaveType(AppId, AoChannel, WaveType);
            return ret;
        }

        public int GetWaveType(short AppId, ushort AoChannel, out ushort WaveType)
        {
            WaveType = 0;
            int ret = GenGetWaveType(AppId, AoChannel, ref WaveType);
            return ret;
        }

        public int SetArbitraryDataFile(short AppId, ushort AoChannel, string FilePath)
        {
            int ret = GenSetArbitraryDataFile(AppId, AoChannel, FilePath);
            return ret;
        }

        public int GetArbitraryDataFile(short AppId, ushort AoChannel, out string FilePath)
        {
            FilePath = new String('0', 1);
            System.Text.StringBuilder filepath = new System.Text.StringBuilder(256);
            int ret = GenGetArbitraryDataFile(AppId, AoChannel, filepath);
            if(ret == 0)
            {
                FilePath = filepath.ToString();
            }
            else
            {
                FilePath = "";
            }
            return ret;
        }

        public int SetAoSweep(short AppId, ushort AoChannel, ushort SweepType, ushort SweepTime, float SweepStart, float SweepStop)
        {
            int ret = GenSetAoSweep(AppId, AoChannel, SweepType, SweepTime, SweepStart, SweepStop);
            return ret;
        }

        public int GetAoSweep(short AppId, ushort AoChannel, out ushort SweepType, out ushort SweepTime, out float SweepStart, out float SweepStop)
        {
            SweepType = 0;
            SweepTime = 0;
            SweepStart = 0;
            SweepStop = 0;
            int ret = GenGetAoSweep(AppId, AoChannel, ref SweepType, ref SweepTime, ref SweepStart, ref SweepStop);
            return ret;
        }

        public int GetErrorString(int ErrorCode, out string ErrorString)
        {
            ErrorString = new String('0', 1);
            System.Text.StringBuilder errorstring = new System.Text.StringBuilder(256);
            int ret = GenGetErrorString(ErrorCode, errorstring);
            if(ret == 0)
            {
                ErrorString = errorstring.ToString();
            }
            else
            {
                ErrorString = "";
            }
            return ret;
        }

    }
}

public enum CWaveformGeneratorSDKConst
{
//================================================================
// Parameters
//================================================================
//----------------------------------------
// App size
//----------------------------------------
    GEN_PARAM_WINDOW_OFF                      =    0,        // Minimize
    GEN_PARAM_WINDOW_NORMAL                   =    1,        // Normal
    GEN_PARAM_WINDOW_MAX                      =    2,        // Maximize

//----------------------------------------
// Termination processing status
//----------------------------------------
    GEN_STATUS_CLOSE_SUCCESS                  =    0,        // Normal completed
    GEN_STATUS_CLOSE_FAILURE                  =    1,        // C-WaveformGenerator completed error

//----------------------------------------
// Operation status
//----------------------------------------
    GEN_STATUS_BUSY                           =    1,        // Running trigger
    GEN_STATUS_START_TRG                      =    2,        // Start trigger

//----------------------------------------
// Number of channels for data save
//----------------------------------------
    GEN_PARAM_DATASAVE_SINGLE                 =    0,        // Data save for single channel
    GEN_PARAM_DATASAVE_MULTI                  =    1,        // Data save for multi channel

//----------------------------------------
// Frequency type
//----------------------------------------
    GEN_PARAM_FREQTYPE_FREQUENCY              =    0,        // Frequency
    GEN_PARAM_FREQTYPE_PERIOD                 =    1,        // Period

//----------------------------------------
// Frequency unit
//----------------------------------------
    GEN_PARAM_FREQUNIT_HZ_SEC                 =    0,        // Frequency [ Hz ], Period [ sec ]
    GEN_PARAM_FREQUNIT_KHZ_MSEC               =    1,        // Frequency [ kHz ], Period [ msec ]

//----------------------------------------
// Oscillation type
//----------------------------------------
    GEN_PARAM_OSCILLATION_NONE                =    0,        // None
    GEN_PARAM_OSCILLATION_AUTO_BURST          =    1,        // Auto burst
    GEN_PARAM_OSCILLATION_TRIGGER_BURST_RISE  =    2,        // Trigger burst(rising edge)
    GEN_PARAM_OSCILLATION_TRIGGER_BURST_FALL  =    3,        // Trigger burst(falling edge)

//----------------------------------------
// Amplitude type
//----------------------------------------
    GEN_PARAM_AMPTYPE_AMPLITUDE               =    0,        // Amplitude
    GEN_PARAM_AMPTYPE_VPP                     =    1,        // Vp-p

//----------------------------------------
// Amplitude unit
//----------------------------------------
    GEN_PARAM_AMPUNIT_V_MA                    =    0,        // Voltage or Current
    GEN_PARAM_AMPUNIT_BINARY                  =    1,        // LSB

//----------------------------------------
// Wave type
//----------------------------------------
    GEN_PARAM_WAVE_SINE                       =    0,        // Sinewave
    GEN_PARAM_WAVE_CONSTANT                   =    1,        // Constant
    GEN_PARAM_WAVE_TRIANGULAR                 =    2,        // Triangular
    GEN_PARAM_WAVE_RAMP                       =    3,        // Ramp
    GEN_PARAM_WAVE_SQUARE                     =    4,        // Square
    GEN_PARAM_WAVE_NOISE                      =    5,        // Noise
    GEN_PARAM_WAVE_ARBITRARY                  =    6,        // Arbitrary

//----------------------------------------
// Sweep type
//----------------------------------------
    GEN_PARAM_SWEEP_NONE                      =    0,        // None
    GEN_PARAM_SWEEP_FREQUENCY                 =    1,        // Frequency sweep
    GEN_PARAM_SWEEP_AMPLITUDE                 =    2,        // Amplitude sweep
    GEN_PARAM_SWEEP_DC_OFFSET                 =    3,        // DC offset sweep
    GEN_PARAM_SWEEP_DUTY_RATIO                =    1,        // Square Duty ratio sweep

//================================================================
// Error codes
//================================================================
//----------------------------------------
// Initialize Error
//----------------------------------------
    GEN_ERR_SUCCESS                           =    0,        // Normal completed
    GEN_ERR_INI_RESOURCE                      =    1,        // Failed in the acquisition of the resource
    GEN_ERR_INI_MEMORY                        =    3,        // Failed in the allocation of the memory
    GEN_ERR_MAKE_OBJECT                       =    100,      // Failed in the creation of the object
    GEN_ERR_OPEN_APPLICATION                  =    102,      // Cannot use C-WaveformGenerator because other application is using
    GEN_ERR_CTRL_APPLICATION                  =    103,      // Failed to application control

//----------------------------------------
// Common Error
//----------------------------------------
    GEN_ERR_APPLICATION_SIZE                  =    10000,    // Unable to specify screen size
    GEN_ERR_STATUS                            =    10001,    // Unable to get status
    GEN_ERR_FILEPATH                          =    10002,    // The setting file path is invalid
    GEN_ERR_FILETYPE                          =    10003,    // The setting file type is invalid
    GEN_ERR_FILESAVE                          =    10005,    // Failed to save file
    GEN_ERR_STARTED                           =    10006,    // Cannot execute because the device is in operation
    GEN_ERR_DEVICENAME                        =    10007,    // A device name isn't stored in DeviceName
    GEN_ERR_DEVICE_START                      =    10008,    // Operation start error occurred
    GEN_ERR_DEVICE_STOP                       =    10009,    // Operation stop error occurred
    GEN_ERR_INVALID_ID                        =    10010,    // Invalid AppID was specified
    GEN_ERR_BUFF_ADDRESS                      =    10100,    // Invalid data buffer address

//----------------------------------------
// Specific Error
//----------------------------------------
//--------------------
// Device Setting Error
//--------------------
    GEN_ERR_DEVICENAME_ANALOGOUT              =    20001,    // The specified device does not have analog output function
    GEN_ERR_CHANNEL_NUM                       =    20002,    // Number of channels is outside the settable range
    GEN_ERR_FREQUENCY                         =    20003,    // Frequency is outside the settable range
    GEN_ERR_FREQUENCY_TYPE                    =    20004,    // Frequency type is outside the settable range
    GEN_ERR_FREQUENCY_UNIT                    =    20005,    // Frequency unit is outside the settable range
    GEN_ERR_OSCILLATION_TYPE                  =    20006,    // Oscillation type is outside the settable range
    GEN_ERR_OSCILLATION_WAVENUM               =    20007,    // Number of oscillations is outside the settable range
    GEN_ERR_OSCILLATION_STOP_WAVENUM          =    20008,    // Number of stops or time is outside the settable range
    GEN_ERR_OSCILLATION_DATANUM               =    20009,    // Created data is outside the settable range
    GEN_ERR_DEVICESET                         =    20010,    // There are no devices that can be configured, the SDK will close
    GEN_ERR_BASICMODE                         =    20011,    // Switched to Basic mode. Settings may change automatically.
//--------------------
// Channel Setting Error
//--------------------
    GEN_ERR_CHANNEL_NO                        =    22000,    // Channel number is outside the settable range
    GEN_ERR_AMPLITUDE                         =    22001,    // Amplitude is outside the settable range
    GEN_ERR_OFFSET                            =    22002,    // Offset is outside the settable range
    GEN_ERR_AMPLITUDE_TYPE                    =    22003,    // Amplitude type is outside the settable range
    GEN_ERR_AMPLITUDE_UNIT                    =    22004,    // Amplitude unit is outside the settable range
    GEN_ERR_DUTY                              =    22005,    // Duty ratio is outside the settable range
    GEN_ERR_PHASE                             =    22006,    // Phase is outside the settable range
    GEN_ERR_WAVE_TYPE                         =    22007,    // Waveform type is outside the settable range
    GEN_ERR_SWEEP_TYPE                        =    22008,    // Sweep type is outside the settable range
    GEN_ERR_SWEEP_RATIO                       =    22009,    // Sweep time is outside the settable range
    GEN_ERR_SWEEP1                            =    22010,    // The value before the sweep change is outside the settable range
    GEN_ERR_SWEEP2                            =    22011,    // The value after the sweep change is outside the settable range
    GEN_ERR_WAVEDATA                          =    20012,    // Data file is not set when waveform type is arbitrary

    GEN_ERR_DEVICE                            =    24001,    // Device condition error occurred
    GEN_ERR_RELAY                             =    24002,    // Relay error occurred
}