#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;
   long nCmdCntX, nCmdCntY;
   DWORD dwSpeedX;

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

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

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

	   nRet = i8094_set_pls_cfg( bCardID, AXIS_Y, PULSE_MODE_CW_CCW, PULSE_LOGIC_ACTIVE_HIGH, PARAMETER_IGNORED );
	   printf(" [AXIS_Y] i8094_set_pls_cfg returns = %d\n", nRet );
			
	   nRet = i8094_set_enc_cfg( bCardID, AXIS_Y, ENCODER_MODE_CW_CCW, 0 );
	   printf(" [AXIS_Y] i8094_set_enc_cfg returns = %d\n", nRet );

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

	   nRet = i8094_set_cmdcounter( bCardID, AXIS_Y, 0);
	   printf (" [AXIS_Y] i8094_set_position returns = %d\n", nRet );

#if defined(_T_MOVE_) 
	// trapezoidal move
	nRet = i8094_t_line2_move( bCardID, AXIS_X, AXIS_Y, 1000, 500000, 800000, 600000, 5000000, 8000000, 8, DISABLE_BLOCK_OPEARTION);
	printf (" i8094_t_line2_move returns = %d\n", nRet );
#elif defined(_S_MOVE_) 
	// s-curve move 
	nRet = i8094_s_line2_move( bCardID, AXIS_X, AXIS_Y, 10000, 500000, 800000, 600000, 5000000, 8000000, 8, DISABLE_BLOCK_OPEARTION);
	printf (" i8094_s_line2_move returns = %d\n", nRet );
#else
	// constant speed
	nRet = i8094_t_line2_move( bCardID, AXIS_X, AXIS_Y, 500000, 500000, 800000, 600000, 5000000, 8000000, 8, DISABLE_BLOCK_OPEARTION);
	printf(" i8094_t_line2_move returns = %d\n", nRet );
#endif

	if( nRet != SUCCESS_NO_ERROR )
	    goto Close_Device;

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

	    i8094_get_cmdcounter( bCardID, AXIS_X, &nCmdCntX);
	    i8094_get_cmdcounter( bCardID, AXIS_Y, &nCmdCntY);
	    i8094_get_speed( bCardID, AXIS_X, &dwSpeedX );

	    printf(" [Speed-X = %ld]  [CmdCounter-X = %ld]  [CmdCounter-Y = %ld]\n", dwSpeedX, nCmdCntX, nCmdCntY );


	}while( (nRet == SUCCESS_NO_ERROR) && (bDone == MOTION_NOT_DONE) );
	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;
}

