#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 bDoneX, bDoneY; 
   WORD wStopStatusX, wStopStatusY;
   long nCmdCntX, nCmdCntY, nLatchData;
   DWORD dwSpeedX, dwSpeedY;

   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 );
			// constant speed
	nRet = i8094_set_synch( bCardID, AXIS_X, SYNC_ENABLE_FEATURE, AXIS_Y,\
				SYNC_CONDITION_EXCEED_CMP_POSITIVE, SYNC_ACTION_LOGIC_CMD_LATCH, SYNC_ACTION_FIXED_FORWARD_DRIVE,\
				CMP_SRC_LOGIC_COMMAND, 2500000L, -2500000L );
	printf(" i8094_set_synch returns = %d\n", nRet );
	if( nRet != SUCCESS_NO_ERROR )
	    goto Close_Device;


	nRet = i8094_synch_t_move_cfg( bCardID, AXIS_Y, 10000, 50000, 100000, 100000, 300000 );
	printf (" i8094_synch_t_move_cfg returns = %d\n", nRet );
	if( nRet != SUCCESS_NO_ERROR )
	    goto Disable_Synch_Action;

	nRet = i8094_t_move( bCardID, AXIS_X, 50000, 500000, 1000000, 1000000, 5000000, 8 );
	printf(" i8094_t_move returns = %d\n", nRet );
	if( nRet != SUCCESS_NO_ERROR )
	    goto Disable_Synch_Action;


	do
	{
	    nRet = i8094_motion_done( bCardID, AXIS_X, &bDoneX, &wStopStatusX );
	    nRet = i8094_motion_done( bCardID, AXIS_Y, &bDoneY, &wStopStatusY );

	    usleep( 500000 ); // 500ms

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

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


	}while( (bDoneX == MOTION_NOT_DONE) || (bDoneY == MOTION_NOT_DONE) );

	nRet = i8094_get_latch( bCardID, AXIS_X, &nLatchData );
	printf (" Motion Done!! wStopStatus = 0x%X Latched Data = %ld\n", wStopStatusX, nLatchData );


Disable_Synch_Action:
	i8094_set_synch( bCardID, AXIS_X, SYNC_DISABLE_FEATURE, AXIS_Y, SYNC_CONDITION_NONE, SYNC_ACTION_NONE, SYNC_ACTION_NONE, 0x00, 0x00, 0x00 );

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

   return 0;
}

