#================================================================
# cgen_api.py
# module file for C-WaveformGenerator
#                                                CONTEC.Co., Ltd.
#================================================================
import ctypes
import ctypes.wintypes

cgen_api_dll = ctypes.windll.LoadLibrary('cgen_api.dll')

#================================================================
# Prototype definition
#================================================================
# C Prototype: long WINAPI GenOpenApplication(char* DeviceName, short* AppId, unsigned short OpenMode);
GenOpenApplication = cgen_api_dll.GenOpenApplication
GenOpenApplication.restype = ctypes.c_long
GenOpenApplication.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_short), ctypes.c_ushort]

# C Prototype: long WINAPI GenCloseApplication(short AppId, unsigned long* Status);
GenCloseApplication = cgen_api_dll.GenCloseApplication
GenCloseApplication.restype = ctypes.c_long
GenCloseApplication.argtypes = [ctypes.c_short, ctypes.POINTER(ctypes.c_ulong)]

# C Prototype: long WINAPI GenStartOperation(short AppId);
GenStartOperation = cgen_api_dll.GenStartOperation
GenStartOperation.restype = ctypes.c_long
GenStartOperation.argtypes = [ctypes.c_short]

# C Prototype: long WINAPI GenStopOperation(short AppId);
GenStopOperation = cgen_api_dll.GenStopOperation
GenStopOperation.restype = ctypes.c_long
GenStopOperation.argtypes = [ctypes.c_short]

# C Prototype: long WINAPI GenGetApplicationStatus(short AppId, unsigned long* Status);
GenGetApplicationStatus = cgen_api_dll.GenGetApplicationStatus
GenGetApplicationStatus.restype = ctypes.c_long
GenGetApplicationStatus.argtypes = [ctypes.c_short, ctypes.POINTER(ctypes.c_ulong)]

# C Prototype: long WINAPI GenExportSettingFile(short AppId, char* FilePath);
GenExportSettingFile = cgen_api_dll.GenExportSettingFile
GenExportSettingFile.restype = ctypes.c_long
GenExportSettingFile.argtypes = [ctypes.c_short, ctypes.c_char_p]

# C Prototype: long WINAPI GenImportSettingFile(short AppId, char* FilePath);
GenImportSettingFile = cgen_api_dll.GenImportSettingFile
GenImportSettingFile.restype = ctypes.c_long
GenImportSettingFile.argtypes = [ctypes.c_short, ctypes.c_char_p]

# C Prototype: long WINAPI GenSaveDataFile(short AppId, char* FilePath, short Param1, short Param2);
GenSaveDataFile = cgen_api_dll.GenSaveDataFile
GenSaveDataFile.restype = ctypes.c_long
GenSaveDataFile.argtypes = [ctypes.c_short, ctypes.c_char_p, ctypes.c_short, ctypes.c_short]

# C Prototype: long WINAPI GenSetDeviceName(short AppId, char* DeviceName);
GenSetDeviceName = cgen_api_dll.GenSetDeviceName
GenSetDeviceName.restype = ctypes.c_long
GenSetDeviceName.argtypes = [ctypes.c_short, ctypes.c_char_p]

# C Prototype: long WINAPI GenGetDeviceName(short AppId, char* DeviceName);
GenGetDeviceName = cgen_api_dll.GenGetDeviceName
GenGetDeviceName.restype = ctypes.c_long
GenGetDeviceName.argtypes = [ctypes.c_short, ctypes.c_char_p]

# C Prototype: long WINAPI GenSetDeviceSetting(short AppId, unsigned short AoChannels, float Frequency, unsigned short FrequencyType, unsigned short FrequencyUnit);
GenSetDeviceSetting = cgen_api_dll.GenSetDeviceSetting
GenSetDeviceSetting.restype = ctypes.c_long
GenSetDeviceSetting.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_float, ctypes.c_ushort, ctypes.c_ushort]

# C Prototype: long WINAPI GenGetDeviceSetting(short AppId, unsigned short* AoChannels, float* Frequency, unsigned short* FrequencyType, unsigned short* FrequencyUnit);
GenGetDeviceSetting = cgen_api_dll.GenGetDeviceSetting
GenGetDeviceSetting.restype = ctypes.c_long
GenGetDeviceSetting.argtypes = [ctypes.c_short, ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ushort)]

# C Prototype: long WINAPI GenSetOscillation(short AppId, unsigned short OscillationType, unsigned short OscillationStart, float OscillationStop);
GenSetOscillation = cgen_api_dll.GenSetOscillation
GenSetOscillation.restype = ctypes.c_long
GenSetOscillation.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort, ctypes.c_float]

# C Prototype: long WINAPI GenGetOscillation(short AppId, unsigned short* OscillationType, unsigned short* OscillationStart, float* OscillationStop);
GenGetOscillation = cgen_api_dll.GenGetOscillation
GenGetOscillation.restype = ctypes.c_long
GenGetOscillation.argtypes = [ctypes.c_short, ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_float)]

# C Prototype: long WINAPI GenSetChannelSetting(short AppId, unsigned short AoChannel, float Amplitude, float Offset, unsigned short AmplitudeType, unsigned short AmplitudeUnit, float Duty, float Phase);
GenSetChannelSetting = cgen_api_dll.GenSetChannelSetting
GenSetChannelSetting.restype = ctypes.c_long
GenSetChannelSetting.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_float, ctypes.c_float, ctypes.c_ushort, ctypes.c_ushort, ctypes.c_float, ctypes.c_float]

# C Prototype: long WINAPI GenGetChannelSetting(short AppId, unsigned short AoChannel,float* Amplitude, float* Offset, unsigned short* AmplitudeType, unsigned short* AmplitudeUnit, float* Duty, float*  Phase);
GenGetChannelSetting = cgen_api_dll.GenGetChannelSetting
GenGetChannelSetting.restype = ctypes.c_long
GenGetChannelSetting.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float)]

# C Prototype: long WINAPI GenSetWaveType(short AppId, unsigned short AoChannel, unsigned short WaveType);
GenSetWaveType = cgen_api_dll.GenSetWaveType
GenSetWaveType.restype = ctypes.c_long
GenSetWaveType.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort]

# C Prototype: long WINAPI GenGetWaveType(short AppId, unsigned short AoChannel, unsigned short* WaveType);
GenGetWaveType = cgen_api_dll.GenGetWaveType
GenGetWaveType.restype = ctypes.c_long
GenGetWaveType.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_ushort)]

# C Prototype: long WINAPI GenSetArbitraryDataFile(short AppId, unsigned short AoChannel, char* FilePath);
GenSetArbitraryDataFile = cgen_api_dll.GenSetArbitraryDataFile
GenSetArbitraryDataFile.restype = ctypes.c_long
GenSetArbitraryDataFile.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_char_p]

# C Prototype: long WINAPI GenGetArbitraryDataFile(short AppId, unsigned short AoChannel, char* FilePath);
GenGetArbitraryDataFile = cgen_api_dll.GenGetArbitraryDataFile
GenGetArbitraryDataFile.restype = ctypes.c_long
GenGetArbitraryDataFile.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_char_p]

# C Prototype: long WINAPI GenSetAoSweep(short AppId, unsigned short AoChannel, unsigned short SweepType, unsigned short SweepTime, float SweepStart, float SweepStop);
GenSetAoSweep = cgen_api_dll.GenSetAoSweep
GenSetAoSweep.restype = ctypes.c_long
GenSetAoSweep.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.c_ushort, ctypes.c_ushort, ctypes.c_float, ctypes.c_float]

# C Prototype: long WINAPI GenGetAoSweep(short AppId, unsigned short AoChannel, unsigned short* SweepType, unsigned short* SweepTime, float* SweepStart, float* SweepStop);
GenGetAoSweep = cgen_api_dll.GenGetAoSweep
GenGetAoSweep.restype = ctypes.c_long
GenGetAoSweep.argtypes = [ctypes.c_short, ctypes.c_ushort, ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_ushort), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float)]

# C Prototype: long WINAPI GenGetErrorString(long ErrorCode, char* ErrorString);
GenGetErrorString = cgen_api_dll.GenGetErrorString
GenGetErrorString.restype = ctypes.c_long
GenGetErrorString.argtypes = [ctypes.c_long, ctypes.c_char_p]



#================================================================
# 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

