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

static 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 i8094_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;
   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 );
	printf(" i8094_set_pls_cfg returns = %d\n", nRet );
			
	nRet = i8094_set_enc_cfg( bCardID, AXIS_X, ENCODER_MODE_CW_CCW, 0 );
	printf(" i8094_set_enc_cfg returns = %d\n", nRet );

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

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

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


	nRet = i8094_set_softlimit( bCardID, AXIS_X, SW_LIMIT_ENABLE_FEATURE, CMP_SRC_LOGIC_COMMAND, 80000L, -80000L );
	printf(" i8094_set_limit returns = %d\n", nRet );

	nRet = i8094_const_move( bCardID, AXIS_X, 40000, 100000);
	printf(" i8094_const_move returns = %d\n", nRet );

	do{

	    nRet = i8094_motion_done( bCardID, AXIS_X, &bDone, &wStopStatus );
	    usleep( 500000 );

	    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]  [PosCounter = %ld]\n", dwSpeed, dwAccel, nCmdCnt, nPosCnt );

	}while( (nRet == SUCCESS_NO_ERROR) && (bDone == MOTION_NOT_DONE) );

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

	if( wStopStatus == DRIVE_FINISH_WITH_SW_LIMIT_POSITIVE )
	    printf(" Motion is terminated by Software-Limit\n" );

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

    return 0;
}

