
#include "UniDAQ.h"
#include "windows.h"
#include "stdio.h"
#include "conio.h"
#include "process.h"
#include "dos.h"
#include "time.h"

volatile unsigned long wApcCount,wApcCount1;
WORD wReturnCode,i;
float fAIBuf[4096];
DWORD dwDataCount;
WORD wBoardNo,wGainCode;
WORD wProcessing;
DWORD dwBufCount;
WORD wStatus;

void WINAPI MyApcFunction(DWORD param)
{
    //--------- Do you action in this function ------------
	if(wProcessing == 0)
	{
		wProcessing = 1;
		wApcCount++;
		wProcessing = 0;
    }
	wApcCount++;

}

void main(int argc, char* argv[])
{
	WORD wTotalBoards;

	DWORD wTemp;
	HANDLE hEvent=NULL;


    IXUD_DEVICE_INFO A;
    IXUD_CARD_INFO B;
	char szModelName[20]="Unknow Device";

	//Driver Initial
	wReturnCode=Ixud_DriverInit(&wTotalBoards);
	
	printf("The Total Boards is %d\n",wTotalBoards);

	printf("ModelName ModelNo. VendorID  DeviceID  SubVendor  SubDevice  SubAux\n");
	//printf("Bar0 Bar1 Bar2 Bar3 Bar4 Bar5");
	for (i=0;i<wTotalBoards;i++)
	{
		//Get Card Information
		wReturnCode=Ixud_GetCardInfo(i,&A,&B,szModelName);
		if (wReturnCode!=0)
			printf("Get Card %d Info has Error!!!\n",i);
		else
			printf("%0.15s 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",szModelName,B.dwModelNo ,A.wVendorID,A.wDeviceID,A.wSubVendorID ,A.wSubDeviceID ,A.Aux  );
		
	}

	printf("Please selection board (Board 0 ~ %d)):\n",wTotalBoards-1);
	scanf("%d",&wBoardNo);
    system("cls");

	wApcCount = 0;
	wTemp = 0;
    //Set the Port 0 to Output Mode, Other Port to Input Mode
	wReturnCode = Ixud_SetDIOModes32(wBoardNo,1);
	//Set Callback Event, Use Source = 1(INT_0) Event Type = Active High|Hardware Interrupt, Callback function :MyApcFunction
    wReturnCode = Ixud_SetEventCallback(wBoardNo, IXUD_HARDWARE_INT|IXUD_ACTIVE_HIGH,1,&hEvent,MyApcFunction,0);
	if(wReturnCode)
		printf("\nSetEvent Error!!!");
	else
		printf("\nSetEvent Successfull!!!");
    //Install the IRQ for INT_0
	wReturnCode = Ixud_InstallIrq(wBoardNo, 0x1);
	if (wReturnCode) 
		printf("\nInstall IRQ Error!!!");
	else
		printf("\nInstall IRQ Success!!!");

	wProcessing = 0;

    while (1)
	{		
		wReturnCode = Ixud_WriteDO(wBoardNo,0,0);
		wReturnCode = Ixud_WriteDO(wBoardNo,0,0xFF);
		if(wApcCount > wTemp)
		{
			printf("\nAPC Service Count = %d", wApcCount); 
			wTemp = wApcCount;
		}
		if(kbhit())
		{

		//	bProcessing = !bProcessing;
			break;		
		}
	}

	// Revmove the Interrupt
	wReturnCode = Ixud_RemoveIrq(wBoardNo);
    // Remove the Callback Event
	wReturnCode = Ixud_RemoveEventCallback(wBoardNo,0);
	// Driver Close
	wReturnCode=Ixud_DriverClose();

	system("PAUSE");
}