/************************************************************************/
/* Copyright 2009-2014 MBARI						*/
/************************************************************************/
/* Summary  : Definitions for I/O to MV Converter Board			*/
/* Filename : mvLib.h							*/
/* Author   : Robert Herlien (rah)					*/
/* Project  : xFOCE							*/
/* Revision: 1.0							*/
/* Created  : 09/19/2012 from Oasis4 remote.h				*/
/*									    */
/* 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:						*/
/* 19sep2012 rah - created snLib.h from remote.h in Oasis4		*/
/* 11nov2014 rah - created mvLib.h from snLib.h				*/
/************************************************************************/

#ifndef INCmvLibh
#define INCmvLibh

#include <sys/types.h>

#define MVLIB
typedef struct mvConvStruct *MVConvHandle;

#include <mvConv.h>
#include <modbus.h>


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

#define DEVNAME_LEN	256		/* Length of device name field	*/

#define MV_AD_CHANS	16

/* Message response timeout in microseconds (== 600ms) */
#define RESPONSE_TIMEOUT 600000


typedef struct mvConvStruct	/****************************************/
{				/* Modbus Connection to MV Converter Board*/
    modbus_t	*ctx;		/* libmodbus context			*/
    uint16_t	slaveID;	/* SlaveID on this connection		*/
    uint16_t	useCount;	/* Number of times opened		*/
    char	devName[DEVNAME_LEN];   /* Modbus serial port		*/
} MVConvStruct;		/****************************************/


/************************************************************************/
/* Register and Coil Definitions					*/
/* Note that the Modbus addresses below use the Modbus convention where */
/* address n is represented on Modbus by (n-1).  I would have expected	*/
/* libmodbus to make this transparent, but it doesn't.			*/
/************************************************************************/
/* Misc Holding Registers */
#define SerialNum		(0x0f)
#define SoftwareRev		(0x10)
#define SysStatusReg		(0x11)
#define GfNumSampsReg		(0x12)
#define GfSampPeriodReg		(0x13)
#define GfIgnoreSampReg		(0x14)
#define GfCtrlReg		(0x15)
#define GfSeqReg		(0x16)
#define GfChanReg		(0x17)
#define GfResultReg(n)		(0x18 + n)
#define H2OSenseReg		(0x1e)

/* ADC Registers */
#define AnalogReg(chan)		(chan + 0x1f)
#define AdcNumAvgReg		(0x2f)
#define AdcAvgMsReg		(0x30)

/* Coils  */
#define PowerBit(port)		(port)
#define BlinkyDebugCoil		(6)
#define BlinkyStateCoil		(7)
#define ConfigModeCoil		(8)

#endif	/* INCmvLibh */
