Exemplo n.º 1
0
OSC_ERR StateControl( void)
{
	OSC_ERR err;
	MainState mainState;
	uint8 *pCurRawImg = NULL;

	/* Setup main state machine. Start with idle mode. */
	MainStateConstruct(&mainState);
	HsmOnStart((Hsm *)&mainState);

	/*----------- infinite main loop */
	while( TRUE)
	{
		/*----------- wait for captured picture */
		while (TRUE)
		{
		  /*----------- Alternating 	a) check for new connections
		   *                            b) check for commands (and do process) 
		   * 				c) check for available picture */
			err = Comm_AcceptConnections(&data.comm, ACCEPT_CONNS_TIMEOUT);
			if(err != SUCCESS && err != -ETIMEOUT)
			{
				OscLog(ERROR, "%s: Error accepting new connections (%d)!\n",
				       __func__, err);
			}

			err = Comm_HandleCommands(&data.comm, &mainState, GET_CMDS_TIMEOUT);
			if(err != SUCCESS && err != -ETIMEOUT)
			{
				OscLog(ERROR, "%s: Error handling commands (%d)!\n",
				       __func__, err);
			} else if(err == SUCCESS)
			{
				OscLog(INFO, "Command received.\n");		
			}
			
			 
			err = OscCamReadPicture(OSC_CAM_MULTI_BUFFER, &pCurRawImg, 0, CAMERA_TIMEOUT);
			if ((err != -ETIMEOUT) ||(err != -ENO_CAPTURE_STARTED) )
			{
				/* Anything other than a timeout or no pending capture  means that we should
				* stop trying and analyze the situation. */
				break;
			}
		}		
		if( err == SUCCESS) /* only if breaked due to CamReadPic() */
		{
		    data.pCurRawImg = pCurRawImg;
		    OscLog(DEBUG, "---image available\n");
		}
		else
		{
		    pCurRawImg = NULL;
		}
		
		/*----------- process frame by state engine (pre-setup) Sequentially with next capture */
		if( pCurRawImg)
		{
		    ThrowEvent(&mainState, FRAMESEQ_EVT);
		}
		
		/*----------- prepare next capture */
		if( pCurRawImg)
		{
		    err = OscCamSetupCapture( OSC_CAM_MULTI_BUFFER);
		    if (err != SUCCESS)
			{
				OscLog(ERROR, "%s: Unable to setup capture (%d)!\n", __func__, err);
				break;
			}	
		}	

		/*----------- do self-triggering (if required) */
		ThrowEvent(&mainState, TRIGGER_EVT);
	
	
		/*----------- process frame by state engine (post-setup) Parallel with next capture */
		if( pCurRawImg)
		{
			ThrowEvent(&mainState, FRAMEPAR_EVT);
		}
	
	} /* end while ever */
	
	return SUCCESS;
}
Exemplo n.º 2
0
OSC_ERR StateControl( void)
{
	OSC_ERR camErr, err;
	MainState mainState;
	uint8 *pCurRawImg = NULL;
	
	/* Setup main state machine */
	MainStateConstruct(&mainState);
	HsmOnStart((Hsm *)&mainState);
	
	OscSimInitialize();
	
	/*----------- initial capture preparation*/
	camErr = OscCamSetupCapture( OSC_CAM_MULTI_BUFFER);
	if (camErr != SUCCESS)
	{
		OscLog(ERROR, "%s: Unable to setup initial capture (%d)!\n", __func__, camErr);
		return camErr;
	}
	err = OscGpioTriggerImage();
	if (err != SUCCESS)
	{
	OscLog(ERROR, "%s: Unable to trigger initial capture (%d)!\n", __func__, err);
	}
	
	/*----------- infinite main loop */
	while (TRUE)
	{
		/*----------- wait for captured picture */
		while (TRUE)
		{
			err = HandleIpcRequests(&mainState);
			if (err != SUCCESS)
			{
				OscLog(ERROR, "%s: IPC error! (%d)\n", __func__, err);
				Unload();
				return err;
			}
			
			camErr = OscCamReadPicture(OSC_CAM_MULTI_BUFFER, &pCurRawImg, 0, CAMERA_TIMEOUT);
			if (camErr != -ETIMEOUT)
			{
				/* Anything other than a timeout means that we should
				 * stop trying and analyze the situation. */
				break;
			}
			else
			{
				/*----------- procress CGI request
				 * Check for CGI request only if ReadPicture generated a
				 * time out. Process request directely or involve state
				 * engine with event */
				
				/* Read request. */
				err = HandleIpcRequests(&mainState);
				if (err != SUCCESS)
				{
					OscLog(ERROR, "%s: IPC error! (%d)\n", __func__, err);
					Unload();
					return err;
				}
			}
		}
		
		if (camErr == -EPICTURE_TOO_OLD)
		{
			/* We have a picture, but it already has been laying
			 * around for a while. Most likely we won't be able to
			 * make the deadline for this picture, so we better just
			 * give it up and don't portrude our delay to the next
			 * frame. */
			OscLog(WARN, "%s: Captured picture too old!\n", __func__);
			
			/*----------- prepare next capture */
			camErr = OscCamSetupCapture( OSC_CAM_MULTI_BUFFER);
			if (camErr != SUCCESS)
			{
				OscLog(ERROR, "%s: Unable to setup capture (%d)!\n", __func__, camErr);
				break;
			}
			err = OscGpioTriggerImage();
			if (err != SUCCESS)
			{
				OscLog(ERROR, "%s: Unable to trigger capture (%d)!\n", __func__, err);
				break;
			}
			continue;
		}
		else if (camErr != SUCCESS)
		{
			/* Fatal error, giving up. */
			OscLog(ERROR, "%s: Unable to read picture from cam!\n", __func__);
			break;
		}
		
		data.pCurRawImg = pCurRawImg;
		
		/*----------- process frame by state engine (pre-setup) Sequentially with next capture */
		ThrowEvent(&mainState, FRAMESEQ_EVT);
		
		/*----------- prepare next capture */
		camErr = OscCamSetupCapture( OSC_CAM_MULTI_BUFFER);
		if (camErr != SUCCESS)
		{
			OscLog(ERROR, "%s: Unable to setup capture (%d)!\n", __func__, camErr);
			break;
		}
	err = OscGpioTriggerImage();
	if (err != SUCCESS)
	{
		OscLog(ERROR, "%s: Unable to trigger capture (%d)!\n", __func__, err);
		break;
	}
	
	
	/*----------- process frame by state engine (post-setup) Parallel with next capture */
	ThrowEvent(&mainState, FRAMEPAR_EVT);
	
	/* Advance the simulation step counter. */
	OscSimStep();
	} /* end while ever */
	
	return SUCCESS;
}