/****************************************************************************/
/* Copyright 1991-2016 MBARI												*/
/****************************************************************************/
/* $Header$ */
/* Summary	: Driver Routines for Default Sensors for OASIS Mooring			*/
/* Filename : sensors.c														*/
/* Author	: Robert Herlien (rah)											*/
/* Project	: OASIS Mooring													*/
/* Version  : 1.0															*/
/* Created	: 28 july 2016													*/
/*																			*/
/* 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:													*/
/* 15feb1991 rah - created													*/
/* 28jul2016 rah - created OASIS5 version from OASIS version				*/
/* $Log$
*/
/****************************************************************************/

#include <mbariTypes.h>					/* MBARI type definitions			*/
#include <mbariConst.h>					/* MBARI constants					*/
#include <oasis.h>						/* OASIS controller definitions		*/
#include <olist.h>						/* OASIS Linked List library		*/
#include <sem.h>						/* OASIS Semaphore library			*/
#include <otask.h>						/* OASIS task dispatcher			*/
#include <log.h>						/* OASIS data logging definitions	*/
#include <drvr.h>						/* OASIS driver functions			*/
#include <utils.h>						/* OASIS Utility routines			*/
#include <sensors.h>					/* OASIS sensor module				*/
#include <adc.h>						/* OASIS5 PIC analog definitions	*/
#include <debug.h>

#include <string.h>						/* Std C string library				*/


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

const char *analogParmDesc[] = 
	{"Start Chan", "Num Chans", "Samples to avg", NULL, NULL, NULL, NULL, NULL};

const DrvDesc analogDrvDesc =
	{ "Analog", analog_drv,nullWakeFunc, LS_NULL, analogParmDesc,
	  1800, NO_SERIAL, 0, 0, 0, LOG_BIN, 0, 60, 0, 5, 100};

const DrvDesc oasisDrvDesc =
	{ "OASIS", analog_drv,nullWakeFunc, LS_NULL, analogParmDesc,
	  1800, NO_SERIAL, 0, 0, 0, LOG_BIN, 0, 3, 0, 5, 100};

const DrvDesc onoffDrvDesc =
	{ "OnOff", onoff_drv, nullWakeFunc, LS_NULL, NULL,
	  0, NO_SERIAL, 0, 0, 0, LOG_ASCII, 0, 60, 0, 0, 0};

const DrvDesc pswitchDrvDesc =
	{ "PowerSwitch", pswitch_drv, nullWakeFunc, LS_NULL, NULL,
	  0, NO_SERIAL, 0, 0, 0, LOG_ASCII, 0, 0, 0, 0, 0};


/************************************************************************/
/* Function	   : analog_drv												*/
/* Purpose	   : Driver for General Purpose Analog Sensor				*/
/* Inputs	   : Driver Pointer											*/
/* Outputs	   : None													*/
/* Comments	   : Used for Pressure sensor, batteries, PARs				*/
/*				 Turns on power, waits for device to stabilize for		*/
/*				 TIMEOUT seconds.  Then reads channels PARM0 to			*/
/*				 PARM0 + PARM1 (total of PARM1 channels), and averages	*/
/*				 PARM2 readings, taken each tick.  Averages accumulated */
/*				 as 32 bits, then divided by PARM2 and stored as 16 bits*/
/************************************************************************/
void analog_drv(Driver *dp)
{
	Nat32			nports;
	Nat16			ad_sample[ADC_CHANS];	/* A/D samples				*/

	drv_pwron(dp);							/* Turn on device			*/

	delay_secs(dp->drv_parms[TIMEOUT]);

	nports = dp->drv_parms[PARM1];

	adcConvertChans(dp->drv_parms[PARM0], nports, dp->drv_parms[PARM2], ad_sample);

	drvLogWords(dp, ad_sample, nports);

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

} /* analog_drv() */


/************************************************************************/
/* Function	   : onoff_drv												*/
/* Purpose	   : Simple driver to turn a device on and off				*/
/* Inputs	   : Driver Pointer											*/
/* Outputs	   : None													*/
/************************************************************************/
void onoff_drv(Driver *dp)
{
  drv_pwron(dp);						/* Turn on device				*/
  delay_secs (dp->drv_parms[TIMEOUT]);
  drv_pwroff(dp);						/* Turn off device				*/

} /* onoff_drv() */


/************************************************************************/
/* Function	   : pswitch_drv											*/
/* Purpose	   : Another (non-timed) driver to turn a device on and off */
/* Inputs	   : Driver Pointer											*/
/*				 drv_usrparm=TRUE:on ELSE:off							*/
/* Outputs	   : None													*/
/************************************************************************/
void pswitch_drv(Driver *dp)
{
	if(dp->drv_usrparm == TRUE)
		drv_pwron(dp);					/* Turn on device				*/
	else
		drv_pwroff(dp);					/* Turn off device				*/

} /* pswitch_drv() */

