/************************************************************************/
/* Copyright 2009-2014 MBARI						*/
/************************************************************************/
/* Summary  : Definitions for interfacing to mvLib, the MV Converter lib*/
/* Filename : mvConv.h							*/
/* Author   : Robert Herlien (rah)					*/
/* Project  : xFOCE							*/
/* Revision: 1.0							*/
/* Created  : 11/11/2014						*/
/*									    */
/* MBARI provides this documentation and code "as is", with no warranty,    */
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium  */
/* Research Institute to assist in its use, correction, modification, or    */
/* enhancement. This information should not be published or distributed to  */
/* third parties without specific written permission from MBARI.            */
/*									    */
/************************************************************************/
/* Modification History:						*/
/* 22apr2013, rah, created sensorNode.h					*/
/* 11nov2014, rah, created mvConv.h from sensorNode.h			*/
/************************************************************************/

#ifndef INCmvConvh
#define INCmvConvh	1

#include <sys/types.h>
#include <sys/time.h>
#include <stdint.h>

#ifndef FALSE
#define FALSE	0
#endif
#ifndef TRUE
#define	TRUE	~FALSE
#endif
#ifndef ON
#define ON	TRUE
#endif
#ifndef OFF
#define	OFF	FALSE
#endif

#define OK	 0
#define SUCCESS	 0
#define ERROR	-1
#define FOREVER	while(TRUE)


/****************************************/
/* Typedefs and definitions		*/
/****************************************/

#ifndef MVLIB
typedef void	*MVConvHandle;
#define MV_AD_CHANS	14		/* Num A/D channels on MVConv	*/
#endif

#ifndef DEF_MBOOL
typedef short int	MBool;
#define DEF_MBOOL
#endif
#ifndef DEF_ERRNO
typedef int		Errno;
#define DEF_ERRNO
#endif

#ifndef BLINKY_STATE
typedef enum {BlinkyOff=0, BlinkyOn, BlinkyDebug} BlinkyState;
#define BLINKY_STATE
#endif

/****************************************/
/* Power Channels			*/
/****************************************/
#define VICOR_01		0
#define VICOR_02		1
#define FOCE_01			2
#define FOCE_02			3
#define FAN_01			4
#define FAN_02			5
#define PWR_CHANS		(FAN_02 + 1)

/****************************************/
/* ADC Channels				*/
/****************************************/
#define AD_VICOR_01_TEMP	0
#define AD_VICOR_02_TEMP	1
#define AD_VICOR_03_TEMP	2
#define AD_HEATSINK_TEMP	3
#define AD_48V_01_CURR		4
#define AD_48V_01_VOLT		5
#define AD_48V_02_CURR		6
#define AD_48V_02_VOLT		7
#define AD_24V_CURR		8
#define AD_24V_VOLT		9
#define AD_3V_CURR		10
#define AD_3V_VOLT		11
#define AD_AMB_HUMID		12
#define AD_AMB_TEMP		13

/****************************************/
/* Status Vector Bits			*/
/****************************************/
#define FAN_01_STATE		1
#define FAN_02_STATE		2
#define VICOR_01_STATE		4
#define VICOR_02_STATE		8
#define FOCE_01_RELAY_STATE	0x10
#define FOCE_02_RELAY_STATE	0x20

/****************************************/
/* Fan Control				*/
/****************************************/
#define FAN_OFF		0
#define FAN_ON		1

/****************************************/
/* Ground Fault Channels and Control	*/
/****************************************/

/* GF Channel numbers for mvGetGFResult()	*/

typedef enum
{
    GF_48V_01=0, GF_48V_01_RET, GF_48V_02,
    GF_48V_02_RET, GF_TEST, GF_TEST_RET
} GFChan;

#define NUM_GF_CHANS	6

/* GF Control Bits for mvGet/SetGFCtrl()	*/
/* Note they start with GFC_, compared to GF_ above*/

#define GFC_48V_01	0x01
#define GFC_48V_01_RET	0x02
#define GFC_48V_02	0x04
#define GFC_48V_02_RET	0x08
#define GFC_TEST	0x10
#define GFC_TEST_RET	0x20

#define GFC_OFF		0x00
#define GFC_NORM	(GFC_48V_01 | GFC_48V_01_RET | GFC_48V_02 | GFC_48V_02_RET )
#define GFC_TEST_ONLY	(GFC_TEST | GFC_TEST_RET)
#define GFC_ALL		(GFC_NORM | GFC_TEST_ONLY)


/****************************************/
/* Water Alarm Sense			*/
/****************************************/
#define H2O_1  		1
#define H2O_2  		2


/****************************************/
/* Function Declarations		*/
/****************************************/

/* Library-wide functions	*/
void		initMVConvLib(void);
void		mvSetDebug(MBool onoff);
MVConvHandle	openMVConv(char *dev, int baud, int slaveID,
			   char parity, int data_bit, int stop_bit);
Errno		closeMVConv(MVConvHandle mvh);

/* libmodbus functions made available to mvConv library */
void		mvModbusSetDebug(MVConvHandle mvh, MBool onoff);
void		mvModbusGetResponseTimeout(MVConvHandle mvh, struct timeval *timeout);
void		mvModbusSetResponseTimeout(MVConvHandle mvh, struct timeval *timeout);
void		mvModbusGetByteTimeout(MVConvHandle mvh, struct timeval *timeout);
void		mvModbusSetByteTimeout(MVConvHandle mvh, struct timeval *timeout);

/* Remote Power Functions (includes fans)	*/
Errno		mvPower(MVConvHandle mvh, uint16_t port, MBool onoff);
int16_t		mvPowerIsOn(MVConvHandle mvh, uint16_t port);

/* Remote A/D Functions		*/
Errno		mvAnalogChan(MVConvHandle mvh, uint16_t chan, uint16_t *val);
Errno		mvAnalogChans(MVConvHandle mvh, uint16_t chan, uint16_t nchans, uint16_t *val);
Errno		mvSetAnalogParms(MVConvHandle mvh, uint16_t numAvg, uint16_t avgMs);
Errno		mvGetAnalogParms(MVConvHandle mvh, uint16_t *numAvg, uint16_t *avgMs);

/* Ground Fault			*/
/* Note - Ground faults are continuously sampled in the background, based on the 	*/
/* CFCtrl bits set in mvSetGFCtrl().  At any point in time, the sequence number (number */
/* of full sets of GF results since startup) and the current channel being sampled are	*/
/* available via mvGetGFSeq() and mvGetGFChan().  The latest result for any channel is	*/
/* available via mvGetGFResult().							*/
/* See the "Ground Fault Notes" paragraph of "Medium Voltage Converter Modbus Interface"*/

Errno		mvSetGFCtrl(MVConvHandle mvh, uint16_t gfBits);
Errno		mvGetGFCtrl(MVConvHandle mvh, uint16_t *gfBits);
Errno		mvGetGFResult(MVConvHandle mvh, uint16_t chan, uint16_t nchans, uint16_t *result);
Errno		mvGetGFSeq(MVConvHandle mvh, uint16_t *seq);
Errno		mvGetGFChan(MVConvHandle mvh, uint16_t *chan);
Errno		mvSetGFParms(MVConvHandle mvh, uint16_t numSamps, uint16_t sampPeriodMs, uint16_t ignoreSamps);
Errno		mvGetGFParms(MVConvHandle mvh, uint16_t *numSamps, uint16_t *sampPeriodMs, uint16_t *ignoreSamps);

/* Misc		*/
Errno		mvGetSerialNum(MVConvHandle mvh, uint16_t *num);
Errno		mvGetSoftwareRev(MVConvHandle mvh, uint16_t *num);
Errno		mvGetStatusVector(MVConvHandle mvh, uint16_t *vect);
Errno		mvGetWaterAlarms(MVConvHandle mvh, uint16_t *alarms);
Errno		mvBlinky(MVConvHandle mvh, BlinkyState state);
Errno		mvResetConfigMode(MVConvHandle mvh);

#endif	/* INCmvConvh */
