/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by John E. Lloyd.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

/*---------------------------------------------------------------------------
 *		
 * MODULE NAME:	rcclSysTime
 *
 * DESCRIPTION:	Routines to get and set the RCCL system time (in
 *		milliseconds) and get the system sample interval.
 *
 *		The RCCL system time is an artificial time which
 *		is simply the current control cycle number multiplied by 
 *		the nominal sample interval. In this sense if differs from 
 *		rciTime() in that it is "ideal". The system time is also 
 *		suspended when the system is paused using the rcclPause() 
 *		command.
 *
 * AUTHOR/DATE:	John Lloyd, Spring 1989
 *
 * MODIFICATION HISTORY:
 *
 *--------------------------------------------------------------------------*/

#include "../h/rcclDefs.h"

#include "../h/rciConfig.h"

#include "../rccl/setpoint.h"
#include "../rccl/rcclAutoAbort.h"

extern RCCL_GMEM *rcclGmem;
extern int rciCpu;

rcclSetSysTime (value)
float value;
{
	/* Called by the 1st call to rcclCreate() to set the system time */

	rcclGmem->sysTime[rciCpu] = value;
}

rcclIncSysTime (inc)
float inc;
{
	/* Called by trajectory generator to increment system time */

	char *fxnName = "rcclIncSysTime()";
	if (!globalMemoryCheck(fxnName)) /* validate rcclGmem */
	 { return (-1);
	 }
	rcclGmem->sysTime[rciCpu] += inc;
}

float rcclSysTime ()
{
	/* Called by anyone to get system time */

	char *fxnName = "rcclSysTime()";

	if (INSIDE_PLANNING_TASK())
	 { if (rcclGmem == NULL)
	    { return (0.0);
	    }
	   else
	    { register int k;
	      for (k=0; k<MAX_CPUS; k++)
	       { if (rcclGmem->taskActive[k])
		  { return (rcclGmem->sysTime[k]);
		  }
	       }
	      return (0.0);
	    }
	 }
	else
	 { if (!globalMemoryCheck(fxnName)) /* validate rcclGmem */
	    { return (0.0);
	    }
	   return (rcclGmem->sysTime[rciCpu]);
	 }
}

rcclGetInterval()
{
	/* Called by anyone to get the current system sample 
	   interval (milliseconds) */

	char *fxnName = "rcclGetInterval()";

	if (INSIDE_PLANNING_TASK() && rcclGmem == NULL)
	 { return (-1);
	 }
	if (!globalMemoryCheck(fxnName)) /* validate rcclGmem */
	 { return (-1);
	 }
	else
	 { return (rcclGmem->interval);
	 }
}
