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

int main(void)
{
  BYTE bCardID = 0;//bCardID=(bSlotNum-1), if the I-8094 plug-in Slot 2 of LinPAC, bCardID=2-1=1 
	short nRet;
	BYTE bDone; 
	WORD wStopStatus, wIntStatus;
	long nCmdCnt, nPosCnt, nTargetCnt;
	DWORD dwSpeed, dwAccel;
	nTargetCnt = 1000000;
	wIntStatus = 0xFFFF;
	
	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_AB, 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 );
		
		nRet = i8094_set_int_factor( bCardID, AXIS_X, INT_FACTOR_END_CONST_SPEED_MOVE);
		if( nRet != SUCCESS_NO_ERROR )
			printf(" i8094_set_int_factor returns = %d\n", nRet );
		
		nRet = i8094_const_move( bCardID, AXIS_X, 40000, nTargetCnt);
		if( nRet != SUCCESS_NO_ERROR )
			printf(" i8094_const_move returns = %d\n", nRet );		
				
		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 			
		}
		while( (nRet == SUCCESS_NO_ERROR) && (bDone == MOTION_NOT_DONE) );
		
		nRet = i8094_get_int_status( bCardID, AXIS_X, &wIntStatus);
		if( nRet != SUCCESS_NO_ERROR )
		{
			printf(" i8094_get_int_status = %d\n", nRet );
		}
		else
		{
			printf(" wIntStatus = 0x%4X\n", wIntStatus);
			printf(" nRet = %d  bDone = %d  wStopStatus = 0x%X\n", nRet, bDone, wStopStatus );
		}
		
		nRet = i8094_set_int_factor( bCardID, AXIS_X, INT_FACTOR_DISABLE);
		if( nRet != SUCCESS_NO_ERROR )
			printf(" i8094_set_int_factor returns = %d\n", nRet );
	
		i8094_close( bCardID );		
	}
	else
	{
		printf(" i8094_open() return %d !\n", nRet);
	}
	
	return 0;
}
