/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 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:	rcclPause
 *
 * DESCRIPTION:	Primitives to pause and resume the RCCL system.
 * 
 * CAVEATS:	The pause primitive is one of the least tested of the
 *		RCCL features.
 *
 * AUTHOR/DATE:	John Lloyd, Fall 1988
 *
 * MODIFICATION HISTORY:
 *
 *--------------------------------------------------------------------------*/

#include "../h/cdefs.h"
#include "../h/errorCodes.h"
#include "../h/rcclDefs.h"

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

static RCCL_GMEM *gmem;

rcclPause (accel, time)
double accel, time;
{
	/* Pause the RCCL system. All trajectory generators will enter
	   into a pause state, putting their current motions on hold.
	 */
	char *fxnName = "rcclPause()";

	if ((gmem = globalMemoryCheck (fxnName)) == NULL)
	 { return (-1);
	 }
	if (gmem->pauseCtrl != TASK_RUNNING)
	 { errorMsg (EBusy, rcclPausedMsg, fxnName);
	   RETURN_ERROR(-1);
	 }
	if (time != F_UNDEF)
	 { if (time < 0.0)
	    { errorMsg (ERange, rcclArgNegMsg, fxnName);
	      RETURN_ERROR(-1);
	    }
	   gmem->pauseTimeLimit = time;
	 }
	else
	 { gmem->pauseTimeLimit = UNDEF;
	 }
	if (accel != F_DEFAULT)
	 { if (accel < 0.0)
	    { errorMsg (ERange, rcclArgNegMsg, fxnName);
	      RETURN_ERROR(-1);
	    }
	   gmem->pauseTransTime = accel;
	 }
	else
	 { gmem->pauseTransTime = UNDEF;
	 }

	gmem->pauseReq = YES;
#if 0
	/* this could race if the pause time is very small */
	if (INSIDE_PLANNING_TASK())
	 { while (gmem->pauseCtrl == TASK_RUNNING)
	    { delay ((float)rcclGetInterval());
	    }
	 }
#endif
	RETURN_NORMAL (1);
}

rcclResume ()
{
	/* Resume the RCCL system after it has been paused with
	   rcclPause().
	 */
	char *fxnName = "rcclResume()";

	if ((gmem = globalMemoryCheck (fxnName)) == NULL)
	 { return (-1);
	 }
	gmem->pauseReq = 0;
	if (INSIDE_PLANNING_TASK())
	 { while (gmem->pauseCtrl != TASK_RUNNING)
	    { delay ((float)rcclGetInterval());
	    }
	 }
	return (1);
}

rcclIsPaused()
{
	char *fxnName = "rcclIsPaused()";

	if ((gmem = globalMemoryCheck (fxnName)) == NULL)
	 { return (-1);
	 }
	return (gmem->pauseCtrl == TASK_RUNNING ? 0 : 1);
}

rcclWasPaused()
{
	char *fxnName = "rcclWasPaused()";

	if ((gmem = globalMemoryCheck (fxnName)) == NULL)
	 { return (-1);
	 }
	return (gmem->wasPaused);
}
