#include <stdio.h>
#include <unistd.h>
#include "i8094_dll.h"

BYTE bCardID = 0; //bCardID=(bSlotNum-1), if the I-8094 plug-in Slot 2 of LinPAC, bCardID=2-1=1 


void Display_Runtime_Error( void )
{
   short nRet;
   WORD wErrSts;

   nRet = i8094_get_error_status( bCardID, AXIS_X, &wErrSts );
   printf(" error status of ps400_const_move() = 0x%X, nRet=%d\n", wErrSts, nRet );			
   if( wErrSts & DRIVE_ERROR_STATUS_SLMTP )
	     printf(" DRIVE_ERROR_STATUS_SLMTP" );	

   if( wErrSts & DRIVE_ERROR_STATUS_SLMTM )
	     printf(" DRIVE_ERROR_STATUS_SLMTM" );	

   if( wErrSts & DRIVE_ERROR_STATUS_LMTP )
	     printf(" DRIVE_ERROR_STATUS_LMTP" );	

   if( wErrSts & DRIVE_ERROR_STATUS_LMTM )
	     printf(" DRIVE_ERROR_STATUS_LMTM" );	

   if( wErrSts & DRIVE_ERROR_STATUS_ALARM )
	     printf(" DRIVE_ERROR_STATUS_ALARM" );	

   if( wErrSts & DRIVE_ERROR_STATUS_EMG )
	     printf(" DRIVE_ERROR_STATUS_EMG" );	

   printf("\n" );	
}

int main( void )
{
   short nRet;
   BYTE bDone; 
   WORD wStopStatus;
   long nCmdCnt, nPosCnt, nTargetCnt;
   DWORD dwSpeed, dwAccel;   
   
   nRet = i8094_open( bCardID ); 
   if( nRet == SUCCESS_NO_ERROR )
   {
	   nRet = i8094_set_pls_cfg( bCardID, AXIS_X, PULSE_MODE_CW_CCW, PULSE_LOGIC_ACTIVE_HIGH, PARAMETER_IGNORED );
   	 if( nRet != SUCCESS_NO_ERROR )
	       printf(" i8094_set_pls_cfg returns = %d\n", nRet );
			
	   nRet = i8094_set_enc_cfg( bCardID, AXIS_X, ENCODER_MODE_CW_CCW, 0 );
   	 if( nRet != SUCCESS_NO_ERROR )
	       printf(" i8094_set_enc_cfg returns = %d\n", nRet );

	   nRet = i8094_set_limit( bCardID, AXIS_X, LIMIT_LOGIC_ACTIVE_LOW, LIMIT_STOP_SUDDEN );
   	 if( nRet != SUCCESS_NO_ERROR )
	       printf(" i8094_set_limit returns = %d\n", nRet );

	   nRet = i8094_set_cmdcounter( bCardID, AXIS_X, 0);
   	 if( nRet != SUCCESS_NO_ERROR )
	       printf(" i8094_set_cmdcounter returns = %d\n", nRet );

	   nRet = i8094_set_enccounter( bCardID, AXIS_X, 0);
   	 if( nRet != SUCCESS_NO_ERROR )
	      printf(" i8094_set_enccounter returns = %d\n", nRet );

#if defined(_T_MOVE_) 
	nRet = i8094_t_move( bCardID, AXIS_X, 1000, 40000, 100000, 80000, 200000, 8 );
#elif defined(_S_MOVE_) 
	nRet = i8094_s_move( bCardID, AXIS_X, 1000, 80000, 500000, 800000, 200000, 8 );
#elif defined(_VELOCITY_MOVE_) 
	nRet = i8094_velocity_move( bCardID, AXIS_X, 5000, 40000, 200000, MOVE_DIRECTION_FORWARD );
#else
	nRet = i8094_const_move( bCardID, AXIS_X, 40000, 100000);
#endif

	if( nRet != SUCCESS_NO_ERROR )
	{
	    if( nRet == ERROR_OCCURS_IN_AXIS_X )
		    Display_Runtime_Error();
	    else
	    	printf(" i8094_xxx_move returns = %d\n", nRet );

	    goto Close_Device;
	}

	nTargetCnt = (nCmdCnt + 110000);

	i8094_get_cmdcounter( bCardID, AXIS_X, &nCmdCnt);

	do{
	    nRet = i8094_motion_done( bCardID, AXIS_X, &bDone, &wStopStatus );
	    i8094_get_cmdcounter( bCardID, AXIS_X, &nCmdCnt);
	    i8094_get_enccounter( bCardID, AXIS_X, &nPosCnt);
	    i8094_get_speed( bCardID, AXIS_X, &dwSpeed );
	    i8094_get_acc( bCardID, AXIS_X, &dwAccel );

	    printf("[Speed = %ld]  [Accel = %ld]  [CmdCounter = %ld]  [EncCounter = %ld]\n", dwSpeed, dwAccel, nCmdCnt, nPosCnt );

            usleep( 100000 ); // sleep 100ms 
#if defined(_VELOCITY_MOVE_) 
	}while( (nRet == SUCCESS_NO_ERROR) && (bDone == MOTION_NOT_DONE) && (nCmdCnt < nTargetCnt) );

	if( nCmdCnt >= nTargetCnt )
	{
	    nRet = i8094_stop_move( bCardID, AXIS_X, STOP_SUDDEN );
	    printf(" ps400_stop_move returns = %d\n", nRet );
	    nRet = i8094_motion_done( bCardID, AXIS_X, &bDone, &wStopStatus );
	}
#else
	}while( (nRet == SUCCESS_NO_ERROR) && (bDone == MOTION_NOT_DONE) );
    
#endif

	printf(" nRet = %d  bDone = %d  wStopStatus = 0x%X\n", nRet, bDone, wStopStatus );


Close_Device:
    
	i8094_close( bCardID );
    }
    else
	     printf(" i8094_open() return %d !\n", nRet ); 

    return 0;
}
