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

#pragma comment(lib,"UniDAQ.lib")
#define dwDataNum 200
WORD wTotalBoards;
WORD wBoardNo;
WORD wRtn;
IXUD_DEVICE_INFO sDevInfo[MAX_BOARD_NUMBER];
IXUD_CARD_INFO sCardInfo[MAX_BOARD_NUMBER];

DWORD dwBuffer[dwDataNum];
float fSamplingRateKHz;
WORD wPortNo;

void main(int argc, char* argv[])
{
	
	WORD wBoardIndex;
	DWORD dwDIDataIndex;
	DWORD dwShift;

	char szModelName[20]="Unknow Device";
	//Driver Initial
	wRtn = Ixud_DriverInit(&wTotalBoards);
	if(wRtn)
	{
		printf("Driver Init Error(%d)",wRtn);
		exit(0);
	}

	for (wBoardIndex=0;wBoardIndex<wTotalBoards;wBoardIndex++)
	{
		//Get Card Information
		wRtn=Ixud_GetCardInfo(wBoardIndex,&sDevInfo[wBoardIndex],&sCardInfo[wBoardIndex],szModelName);
		if (wRtn!=0)
			printf("Get Card %d Info has Error!!!\n",wBoardIndex);
		else
			printf("[%2d]%0.15s \n",wBoardIndex,szModelName);		
	}
	wBoardNo = 0;
	if (wTotalBoards>1)
	{
		printf("Please selection board (0 ~ %d)):\n",wTotalBoards-1);
		scanf("%d",&wBoardNo);
	}

	printf("Please selection Port (0 ~ %d)):\n",sCardInfo[wBoardNo].wDIOPorts+sCardInfo[wBoardNo].wDIPorts-1);
	scanf("%d",&wPortNo);

	printf("Please input Sampling Rate(kHz)):\n");
	scanf("%f",&fSamplingRateKHz);
	//Start DI Pacer
	wRtn = Ixud_StartDI(wBoardNo,wPortNo,0,fSamplingRateKHz*1000.0,dwDataNum);
	if(wRtn)
	{
		printf("Start DI Error!(%d)",wRtn);
		exit(0);
	}
	//Get DI Buffer Data
	wRtn = Ixud_GetDIBufferH(wBoardNo,wPortNo,dwDataNum,dwBuffer);
	if(wRtn)
	{
		printf("Get DI Buffer Error!(%d)",wRtn);
		exit(0);
	}
    //Stop DI Pacer
	wRtn = Ixud_StopDI(wBoardNo,wPortNo);
	if(wRtn)
	{
		printf("Stop DI Error!(%d)",wRtn);
		exit(0);
	}
	//Printf the DI Buffer Data
	for(dwDIDataIndex=0; dwDIDataIndex<dwDataNum/5; dwDIDataIndex++)
	{
		for(dwShift=0; dwShift<5; dwShift++)
			printf("[%3d]0x%lx ",dwDIDataIndex*5+dwShift, dwBuffer[dwDIDataIndex*5+dwShift]);
	}
	
	//Driver Close
	wRtn = Ixud_DriverClose();
	
	system("pause");
	
}