/******************************************************************************
//                                                                           //
//  Project: Software Interface for HVPSU6 Devices                           //
//                                                                           //
//  CGC Instruments, (c) 2010-2022, Version 1.20. All rights reserved.       //
//                                                                           //
//  Definition for COM-HVPSU6 DLL Routines                                   //
//                                                                           //
******************************************************************************/

#ifndef __COM_HVPSU6_H__
#define __COM_HVPSU6_H__

#include <math.h>
#include <windows.h>

#ifdef __cplusplus
extern "C" {
#endif

/*
	The communication channel must be opened before the first usage.

	If necessary, the channel may be closed and reopened again.
	The channel is closed automatically at the end of the program, the main program does not need to take care about it.

	The return values of the routines are the following defines COM_HVPSU6_ERR_XXX
		 0: routine finished without any error (COM_HVPSU6_NOERR)
		<0: errors COM_HVPSU6_ERR_XXX

	The last error code can be obtained by calling COM_HVPSU6_State.
	The routine COM_HVPSU6_ErrorMessage returns a pointer to a zero-terminated character string or NULL in case of a failure.

	If a communication error occurred, the error code can be red again by COM_HVPSU6_IO_State,
	The routine COM_HVPSU6_IO_ErrorMessage provides the corresponding error message.
*/

#define COM_HVPSU6_NOERR                 0 /* No error occurred                           */
#define COM_HVPSU6_ERR_OPEN             -2 /* Error opening the port                      */
#define COM_HVPSU6_ERR_CLOSE            -3 /* Error closing the port                      */
#define COM_HVPSU6_ERR_PURGE            -4 /* Error purging the port                      */
#define COM_HVPSU6_ERR_CONTROL          -5 /* Error setting the port control lines        */
#define COM_HVPSU6_ERR_STATUS           -6 /* Error reading the port status lines         */
#define COM_HVPSU6_ERR_COMMAND_SEND     -7 /* Error sending command                       */
#define COM_HVPSU6_ERR_DATA_SEND        -8 /* Error sending data                          */
#define COM_HVPSU6_ERR_TERM_SEND        -9 /* Error sending termination character         */
#define COM_HVPSU6_ERR_COMMAND_RECEIVE -10 /* Error receiving command                     */
#define COM_HVPSU6_ERR_DATA_RECEIVE    -11 /* Error receiving data                        */
#define COM_HVPSU6_ERR_TERM_RECEIVE    -12 /* Error receiving termination character       */
#define COM_HVPSU6_ERR_COMMAND_WRONG   -13 /* Wrong command received                      */
#define COM_HVPSU6_ERR_ARGUMENT_WRONG  -14 /* Wrong argument received                     */
#define COM_HVPSU6_ERR_ARGUMENT        -15 /* Wrong argument passed to the function       */

#define COM_HVPSU6_ERR_NOT_CONNECTED  -100 /* Device not connected                        */
#define COM_HVPSU6_ERR_NOT_READY      -101 /* Device not ready                            */
#define COM_HVPSU6_ERR_READY          -102 /* Device state could not be set to not ready  */


/* Communication */
int FAR _export COM_HVPSU6_Open (BYTE COMNumber); /* Open the port  */
int FAR _export COM_HVPSU6_Close();               /* Close the port */

int FAR _export COM_HVPSU6_Purge();                     /* Clear data buffers for the port                                                 */
int FAR _export COM_HVPSU6_Buffer_State (BOOL & empty); /* Return true if the input data buffer of the device is empty                     */
int FAR _export COM_HVPSU6_Device_Purge (BOOL & empty); /* Clear output data buffer of the device, return value as COM_HVPSU6_Buffer_State */


/* Voltage control */
#define COM_HVPSU6_CH_V1N_LIMIT  0
#define COM_HVPSU6_CH_V2N_LIMIT  1
#define COM_HVPSU6_CH_V2P_LIMIT  2
#define COM_HVPSU6_CH_V3N_LIMIT  3
#define COM_HVPSU6_CH_V4N_LIMIT  4
#define COM_HVPSU6_CH_V5P_LIMIT  5
#define COM_HVPSU6_CH_V6P_LIMIT  6
#define COM_HVPSU6_CH_LIMIT_NUM  7
int FAR _export COM_HVPSU6_SetVoltageLimit (unsigned int Channel, unsigned int   Limit); /* Set the voltage limit */
int FAR _export COM_HVPSU6_GetVoltageLimit (unsigned int Channel, unsigned int & Limit); /* Get the voltage limit */

#define COM_HVPSU6_CH_V1     0
#define COM_HVPSU6_CH_V2     1
#define COM_HVPSU6_CH_V3     2
#define COM_HVPSU6_CH_V4     3
#define COM_HVPSU6_CH_V5     4
#define COM_HVPSU6_CH_V6     5
#define COM_HVPSU6_VOLT_NUM  6
#define COM_HVPSU6_LENS_NUM  4
int FAR _export COM_HVPSU6_SetVoltageSlope (unsigned int Channel, unsigned int   Slope); /* Set the voltage slope */
int FAR _export COM_HVPSU6_GetVoltageSlope (unsigned int Channel, unsigned int & Slope); /* Get the voltage slope */

int FAR _export COM_HVPSU6_SetVoltage (unsigned int Channel, double & Voltage); /* Set the voltage and return the set value */
int FAR _export COM_HVPSU6_GetVoltage (unsigned int Channel, double & Voltage); /* Get the voltage */

int FAR _export COM_HVPSU6_SetAmplitude (unsigned int Channel, double & Amplitude); /* Set the amplitude and return the set value */
int FAR _export COM_HVPSU6_GetAmplitude (unsigned int Channel, double & Amplitude); /* Get the amplitude */

#define COM_HVPSU6_MASK_V1   (1UL<< 0)
#define COM_HVPSU6_MASK_V2   (1UL<< 1)
#define COM_HVPSU6_MASK_V3   (1UL<< 2)
#define COM_HVPSU6_MASK_V4   (1UL<< 3)
#define COM_HVPSU6_MASK_V5   (1UL<< 4)
#define COM_HVPSU6_MASK_V6   (1UL<< 5)
int FAR _export COM_HVPSU6_GetScanMask (unsigned int & Mask); /* Get the scan mask */


/* Lock-in control */
#define COM_HVPSU6_TM_DEL_TRIG     0
#define COM_HVPSU6_TM_DEL_FRAME    1
#define COM_HVPSU6_TM_TOUT_TRIG    2
#define COM_HVPSU6_TM_TOUT_FRAME   3
#define COM_HVPSU6_TM_NUM          4
int FAR _export COM_HVPSU6_SetLockinTiming (unsigned int Item, double & Timing); /* Set the lock-in timing and return the set value */
int FAR _export COM_HVPSU6_GetLockinTiming (unsigned int Item, double & Timing); /* Get the lock-in timing */

int FAR _export COM_HVPSU6_SetTargetFrames (unsigned int & TargetFrames); /* Set the target frames of a measurement and return the set value */
int FAR _export COM_HVPSU6_GetTargetFrames (unsigned int & TargetFrames); /* Get the target frames of a measurement */

// Measurement state
#define COM_HVPSU6_MS_IDLE      0 // device idle
#define COM_HVPSU6_MS_RUN       1 // acquisition running
#define COM_HVPSU6_MS_STOPPING  2 // acquisition stopping
#define COM_HVPSU6_MS_ERROR     3 // error

// Measurement step
#define COM_HVPSU6_MP_START      0 // starting the cycle
#define COM_HVPSU6_MP_VOLT_CHK   1 // output voltage check
#define COM_HVPSU6_MP_READY_CHK  2 // check for ready
#define COM_HVPSU6_MP_SETUP      3 // setup output voltages
#define COM_HVPSU6_MP_VOLT_SET   4 // output voltage set
#define COM_HVPSU6_MP_TRIGGER    5 // camera triggered
#define COM_HVPSU6_MP_ACQUIRE    6 // camera acquiring data
#define COM_HVPSU6_MP_TRANSFER   7 // camera transferring data

int FAR _export COM_HVPSU6_GetMeasState (unsigned int & MeasState, unsigned int & MeasStep, BOOL & MeasPhase, unsigned int & Frames); /* Get the measurement state */

#define COM_HVPSU6_MC_START  0 // start measurement
#define COM_HVPSU6_MC_STOP   1 // stop measurement
#define COM_HVPSU6_MC_BREAK  2 // stop measurement immediately
int FAR _export COM_HVPSU6_SetMeasState (unsigned int MeasCommand); /* Set the measurement state */


/* Composite functions */
int FAR _export COM_HVPSU6_SetVoltages      (double Voltage   [COM_HVPSU6_LENS_NUM]); /* Set the output voltages V1-V4 and return the set values */
int FAR _export COM_HVPSU6_SetAmplitudes    (double Amplitude [COM_HVPSU6_LENS_NUM]); /* Set the amplitudes of the output voltages V1-V4 and return the set values */
int FAR _export COM_HVPSU6_SetLockinTimings (double Timing    [COM_HVPSU6_TM_NUM]);   /* Set the lock-in timings and return the set values */

/* Hardware control */
#define COM_HVPSU6_LED_ILOCK    (1UL<< 0) /* LED "Interlock" active                            */
#define COM_HVPSU6_LED_ACT      (1UL<< 1) /* LED "Activated" active                            */
#define COM_HVPSU6_LED_FAIL     (1UL<< 2) /* LED "Failure" active                              */
#define COM_HVPSU6_ENB_ILIM     (1UL<< 8) /* Inrush current limiter shorted, i.e. inactive     */
#define COM_HVPSU6_ENB_PSU      (1UL<< 9) /* High-voltage output enabled                       */
#define COM_HVPSU6_ILOCK_REAR   (1UL<<14) /* state of the rear  interlock, 1 = active = closed */
#define COM_HVPSU6_ILOCK_FRONT  (1UL<<15) /* state of the front interlock, 1 = active = closed */
int FAR _export COM_HVPSU6_SetDeviceState (bool HVEnable, unsigned int & DeviceState); /* Enable/disable the High-voltage output and get the device state */
int FAR _export COM_HVPSU6_GetDeviceState                (unsigned int & DeviceState); /* Get the device state                                            */

int FAR _export COM_HVPSU6_Housekeeping     (double & Volt12V, double & Volt5V0, double & Volt3V3, double & VoltAgnd, double & TempCPU); /* Get the housekeeping data         */
int FAR _export COM_HVPSU6_HousekeepingSMPS (double & Volt12V, double & Volt5V0, double & Volt3V3,                    double & TempCPU); /* Get the HV-SMPS housekeeping data */

#define COM_HVPSU6_VAL_INVALID  HUGE_VAL
int FAR _export COM_HVPSU6_GetMeasuredVoltages (double Voltage [COM_HVPSU6_VOLT_NUM]); /* Get the measered high-voltage values in V */

																	/* Temperature values             */
#define COM_HVPSU6_TEMP_ADC     0 /* ADC                            */
#define COM_HVPSU6_TEMP_AVPSU   2 /* AV-PSU                         */
#define COM_HVPSU6_TEMP_HVNPSU  3 /* HV- PSU                        */
#define COM_HVPSU6_TEMP_HVPPSU  4 /* HV+ PSU                        */
#define COM_HVPSU6_TEMP_AMPV4   6 /* Amplifier V4                   */
#define COM_HVPSU6_TEMP_AMPV2   7 /* Amplifier V2                   */
#define COM_HVPSU6_TEMP_AMPV13  8 /* Amplifiers V1 & V3             */
#define COM_HVPSU6_TEMP_NUM     9 /* Number of temperature channels */
int FAR _export COM_HVPSU6_GetMeasuredTemperatures (double Temperature [COM_HVPSU6_TEMP_NUM]); /* Get the measered temperature values in degC */


/* General */
WORD FAR _export COM_HVPSU6_SW_Version(); /* Get the COM-HVPSU6 software version */

int FAR _export COM_HVPSU6_Restart();                                                         /* Restart the controller                     */
int FAR _export COM_HVPSU6_GetCPUdata     (double & Load, double & Frequency);                /* Get CPU load (0-1) and frequency (Hz)      */
int FAR _export COM_HVPSU6_GetSMPSCPUdata (double & Load, double & Frequency);                /* Get SMPS CPU load (0-1) and frequency (Hz) */
int FAR _export COM_HVPSU6_GetUptime  (DWORD & Seconds, WORD & Milliseconds, DWORD & Optime); /* Get device uptime and optime               */
int FAR _export COM_HVPSU6_GetTotalTime (DWORD & Uptime, DWORD & Optime);                     /* Get total device uptime and optime         */

int FAR _export COM_HVPSU6_HW_Version (WORD & Version); /* Get the hardware version */

int FAR _export COM_HVPSU6_FW_Version (WORD & Version);            /* Get the firmware version                                                          */
int FAR _export COM_HVPSU6_FW_Date    (char * FAR DateString);     /* Get the firmware date, the buffer DateString should be 16 bytes long              */
int FAR _export COM_HVPSU6_Product_ID (char * FAR Identification); /* Get the product identification, the buffer Identification should be 81 bytes long */
int FAR _export COM_HVPSU6_Product_No (DWORD & Number);            /* Get the product number                                                            */

int          FAR _export COM_HVPSU6_State();           /* Get software interface state                                           */
int          FAR _export COM_HVPSU6_IO_State();        /* Get serial port interface state                                        */
#ifdef _MSC_VER
_export const char * FAR COM_HVPSU6_ErrorMessage();    /* Get the error message corresponding to the software interface state    */
_export const char * FAR COM_HVPSU6_IO_ErrorMessage(); /* Get the error message corresponding to the serial port interface state */
#else
const char * FAR _export COM_HVPSU6_ErrorMessage();    /* Get the error message corresponding to the software interface state    */
const char * FAR _export COM_HVPSU6_IO_ErrorMessage(); /* Get the error message corresponding to the serial port interface state */
#endif

#ifdef __cplusplus
}
#endif

#endif//__COM_HVPSU6_H__
