#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 

WORD wActiveSts;
BYTE bIN3Sts;

void Display_Motion_DI(void)
{
   if( (wActiveSts & DI_STATUS_ACTIVE_LMTP ) == DI_STATUS_ACTIVE_LMTP )
	   printf(" DI_STATUS_ACTIVE_LMTP" );

   if( (wActiveSts & DI_STATUS_ACTIVE_LMTM ) == DI_STATUS_ACTIVE_LMTM )
	   printf(" DI_STATUS_ACTIVE_LMTM" );

   if( (wActiveSts & DI_STATUS_ACTIVE_EMG ) == DI_STATUS_ACTIVE_EMG )
	   printf(" DI_STATUS_ACTIVE_EMG" );

   if( (wActiveSts & DI_STATUS_ACTIVE_ALARM ) == DI_STATUS_ACTIVE_ALARM )
	   printf(" DI_STATUS_ACTIVE_ALARM" );

   if( (wActiveSts & DI_STATUS_ACTIVE_HOME ) == DI_STATUS_ACTIVE_HOME )
	   printf(" DI_STATUS_ACTIVE_HOME" );

   if( (wActiveSts & DI_STATUS_ACTIVE_NEARHOME ) == DI_STATUS_ACTIVE_NEARHOME )
	   printf(" DI_STATUS_ACTIVE_NEARHOME" );

   if( (wActiveSts & DI_STATUS_ACTIVE_INP ) == DI_STATUS_ACTIVE_INP )
	   printf(" DI_STATUS_ACTIVE_INP" );
		
   if( (wActiveSts & DI_STATUS_ACTIVE_INDEX ) == DI_STATUS_ACTIVE_INDEX )
	   printf(" DI_STATUS_ACTIVE_INDEX" );

   if( bIN3Sts == 0x01 )
	   printf(" DI_STATUS_IN3_HIGH" );

   printf ("\n" );
}


int main( void )
{
   short nRet;
   int i;
   
   nRet = i8094_open( bCardID ); 
   if( nRet == SUCCESS_NO_ERROR )
   {
	   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_inp( bCardID, AXIS_X, INP_ENABLE_FEATURE, INP_LOGIC_ACTIVE_HIGH);
	   printf(" i8094_set_inp returns = %d\n", nRet );

	   nRet = i8094_set_alarm( bCardID, AXIS_X, ALARM_ENABLE_FEATURE, ALARM_LOGIC_ACTIVE_LOW );
	   printf(" i8094_set_alarm returns = %d\n", nRet );

	   printf(" The DI monitor will be ternimated in 20 sec.\n" );

	   for( i=0; i<100; i++ )
	   {
	    nRet = i8094_get_mdi_status( bCardID, AXIS_X, &wActiveSts );
	    nRet = i8094_get_in3( bCardID, AXIS_X, &bIN3Sts );

	    Display_Motion_DI();
	    usleep( 500000 ); //sleep 500ms 
	   }

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

   return 0;
}

