/************************************************************************/
/* Copyright 2009-2012 MBARI						*/
/************************************************************************/
/* Summary  : Definitions for interfacing to libSensorNode		*/
/* Filename : sensorNode.h						*/
/* Author   : Robert Herlien (rah)					*/
/* Project  : xFOCE							*/
/* Revision: 1.0							*/
/* Created  : 04/22/2013						*/
/*									    */
/* 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							*/
/************************************************************************/

#ifndef INCsensorNodeh
#define INCsensorNodeh	1

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


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

#define SENSOR_NODES	16		/* Max SensorNodes supported	*/
#define SN_SER_PORTS	3		/* Number ser ports on SensorNode*/
#define SN_PWR_PORTS	4		/* Num pwr ports on SensorNode  */
#define SN_AD_CHANS	8		/* Num A/D channels on SensorNode*/

#ifndef SNLIB
typedef void	*SensorNodeHandle;
typedef void	*RemSerHandle;
#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

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

    
/****************************************/
/* Declarations for Analog Channels	*/
/* (chan parameter for snAnalogChan<s>)	*/
/****************************************/

#define AD_INSTR_CURR	0	/* Total current to all 4 instrument ports	*/
#define AD_INSTR_VOLT	1	/* Voltage to the instrument ports		*/
#define AD_INSTR_1_CURR	2	/* Current to instrument port 1			*/
#define AD_INSTR_2_CURR	3	/* Current to instrument port 2			*/
#define AD_INSTR_3_CURR	4	/* Current to instrument port 3			*/
#define AD_INSTR_4_CURR	5	/* Current to instrument port 4			*/
#define AD_4_20_MA_VOLT	6	/* Current from the 4-20 mA current loop	*/

/* snAnalogChan<s>() return, in the *val parameter, the resulting A/D count in	*/
/* the range of 0 - 65535.  Conversion to engineering units is a 2-step process */
/* First, convert to an A/D voltage in the range of 0.0 to 3.0 volts, via	*/
/* adVolts = 3.0 * cnt / 65536							*/
/* Next, the conversion from adVolts to engineering units is channel-specific	*/
/* Chan 0: amps = 2.0 * adVolts							*/
/* Chan 1: instrVolts = 16.0 * adVolts						*/
/* Chan 2: amps = 0.667 * adVolts						*/
/* Chan 3: amps = 0.667 * adVolts						*/
/* Chan 4: amps = 0.667 * adVolts						*/
/* Chan 5: amps = 2.0 * adVolts							*/
/* Chan 6: milliamps = 6.1 * adVolts + 4.0					*/
/* For chan 6, the max valid reading is 57235 (20 ma).  Higher readings indicate*/
/* an error in the measurement.							*/


/****************************************/
/* Serial Error Bits			*/
/****************************************/
#define PERR_BIT        0x10
#define FERR_BIT        0x08
#define OERR_BIT        0x04
#define XERR_BIT        0x02
#define YERR_BIT        0x01


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

/* Library-wide functions	*/
void		initSensorNodeLib(void);
void		snSetDebug(MBool onoff);
SensorNodeHandle openSensorNode(char *dev, int baud, int slaveID);
Errno		closeSensorNode(SensorNodeHandle snh);

/* libmodbus functions made available to sensorNode library */
void		modbusSetDebug(SensorNodeHandle snh, MBool onoff);
void		modbusGetResponseTimeout(SensorNodeHandle snh, struct timeval *timeout);
void		modbusSetResponseTimeout(SensorNodeHandle snh, struct timeval *timeout);
void		modbusGetByteTimeout(SensorNodeHandle snh, struct timeval *timeout);
void		modbusSetByteTimeout(SensorNodeHandle snh, struct timeval *timeout);

/* Remote Serial Functions	*/
RemSerHandle	snOpenSerial(SensorNodeHandle snh, uint16_t port);
Errno		snSerInit(RemSerHandle rsh, int baud, char parity, int stop);
Errno		snCloseSerial(RemSerHandle rsh);

uint32_t	snSerRead(RemSerHandle rsh, char *buffer, uint32_t len);
uint32_t	snSerWrite(RemSerHandle rsh, const char *buffer, uint32_t len);
Errno		snSerDrain(RemSerHandle rsh, uint32_t tmout);
Errno		snSerIFlush(RemSerHandle rsh);
Errno		snSerOFlush(RemSerHandle rsh);
int16_t		snSerRxCount(RemSerHandle rsh);
Errno		snSerBreak(RemSerHandle rsh, uint16_t breakTime);
Errno		snSerSetTxEnbl(RemSerHandle rsh, MBool enbl);
Errno		snSerGetErrFlags(RemSerHandle rsh, uint16_t *flags);
Errno		snSerClearErrFlags(RemSerHandle rsh, uint16_t flags);

/* Remote Power Functions	*/
Errno		snPower(SensorNodeHandle snh, uint16_t port, MBool onoff);
int16_t		snPowerIsOn(SensorNodeHandle snh, uint16_t port);
Errno		snGetPowerVector(SensorNodeHandle snh, uint16_t *vect);

/* Remote A/D Functions		*/
Errno		snAnalogChan(SensorNodeHandle snh, uint16_t chan, uint16_t *val);
Errno		snAnalogChans(SensorNodeHandle snh, uint16_t chan, uint16_t nchans, uint16_t *val);
Errno		snSetAnalogParms(SensorNodeHandle snh, uint16_t chan, uint16_t nchans, uint16_t *parms);
Errno		snGetAnalogParms(SensorNodeHandle snh, uint16_t chan, uint16_t nchans, uint16_t *parms);

/* Misc		*/
Errno		snGetSerialNum(SensorNodeHandle snh, uint16_t *num);
Errno		snGetSoftwareRev(SensorNodeHandle snh, uint16_t *num);
Errno		snGetBoardConfig(SensorNodeHandle snh, uint32_t *cfg);
Errno		snGetHwConfig(SensorNodeHandle snh, uint16_t *cfg);
Errno		snBlinky(SensorNodeHandle snh, BlinkyState state);
Errno		snSetDCAlwaysOn(SensorNodeHandle snh, MBool on);
Errno		snResetConfigMode(SensorNodeHandle snh);

#endif	/* INCsensorNodeh */
