/****************************************************************************/
/* Copyright 1995-2016 MBARI												*/
/****************************************************************************/
/* $Header$		*/
/* Summary	: Driver Routines for Seabird serial microCat on OASIS mooring	*/
/* Filename : microcat.c													*/
/* Author	: Robert Herlien (rah)											*/
/* Project	: OASIS Mooring													*/
/* $Revision: 1.1 $															*/
/* Created	: 09/01/98 from ctd.c											*/
/*																			*/
/* 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:													*/
/* 01sep98 rah - created from ctd.c											*/
/* 18aug2016 rah - ported to Oasis5											*/
/* $Log$
*/
/****************************************************************************/

#include <mbariTypes.h>					/* MBARI type definitions			*/
#include <mbariConst.h>					/* MBARI constants					*/
#include <oasis.h>						/* OASIS controller definitions		*/
#include <log.h>						/* Log record definitions			*/
#include <drvr.h>						/* OASIS driver functions			*/
#include <custom.h>						/* CTD_TS definition				*/
#include <utils.h>						/* OASIS Utility routines			*/
#include <stdio.h>						/* Standard I/O functions			*/

#define CTD_BUFSIZE		80				/* Orig 64; add for rec number		*/


/********************************/
/*		Function Prototypes		*/
/********************************/

Void	microcat_drv(Driver *dp);


/****************************************************/
/*	Drive Descriptors for Driver in this module		*/
/****************************************************/

const char *microcatParmDesc[] = 
	{"Retries", NULL, NULL, NULL, NULL, NULL, NULL, NULL};

const DrvDesc microcatDrvDesc =
	{ "Microcat", microcat_drv, nullWakeFunc, LS_NULL, microcatParmDesc,
	  600, NO_SERIAL, 9600, MODE_N81NF, 0, LOG_ASCII, 0, 3, 10, 0, 0};


/************************************************************************/
/* Function	   : getMicrocat											*/
/* Purpose	   : Make one attempt to get Microcat CTD data				*/
/* Inputs	   : Buffer, Driver pointer									*/
/* Outputs	   : Number of bytes received from CTD, or ERROR			*/
/************************************************************************/
MLocal Int16 getMicrocat(char *ctd_buf, Driver *dp, Int16 *tmp, Int16 *cond)
{
	Int16		c, len, n;
	Nat16		tmout;
	Flt32		result;
	char		*p;

	xputc('\r');						/* Get CTD's attention			*/
	tmout = dp->drv_parms[TIMEOUT];		/* Get timeout					*/

	while ((c = xgetc_tmout(tmout)) != '>')
		if (c == ERROR)					/* Look for prompt				*/
			return(ERROR);				/* If timed out, return no data */

	xputs("SL\r");						/* Send Last Sample command		*/

	while((len = xgets_tmout(ctd_buf, CTD_BUFSIZE, tmout)) >= 0)
	{
		if (sscanf(ctd_buf, " %f%n", &result, &n) > 0)
		{
			*tmp = (Int16)(result * 1000);
			p = ctd_buf + n;
			if (*p == ',')
				p++;

			if (sscanf(p, " %f", &result) > 0)
			{
				*cond = (Int16)(result * 10000);
				return(len);
			}
		}
	}

	return(ERROR);

} /* getMicrocat() */


/************************************************************************/
/* Function	   : microcat_drv											*/
/* Purpose	   : Driver for microCat CTD								*/
/* Inputs	   : Driver Pointer											*/
/* Outputs	   : None													*/
/************************************************************************/
void	microcat_drv(Driver *dp)
{
	Int16		i, len, temp, conduct;
	char		ctd_buf[CTD_BUFSIZE];	/* Buffer for CTD data			*/
	
	drv_ser_port(dp);					/* Get serial port				*/

	for ( i = 0; i < dp->drv_parms[PARM0]; i++ )
		if ((len = getMicrocat(ctd_buf, dp, &temp, &conduct)) > 0)
			break;
	
	xputs("QS\r");						/* Turn off microCat			*/
	drv_ser_release(dp);				/* Release serial port			*/

	if (len > 0)
	{
		drvLog(dp, (Byte *)ctd_buf, len);		/* Log CTD data			*/
#ifdef ARGOS_CTD
		argosCTDSample(temp, conduct);			/* Send to ARGOS		*/
#endif
	}

	drv_pwroff(dp);						/* Turn off power				*/

} /* microcat_drv() */

