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

#pragma comment(lib,"UniDAQ.lib")
#define dwDataNum 512

DWORD dwDOBuf[dwDataNum];
float fSamplingRateKHz;
WORD wPortNo;
IXUD_DEVICE_INFO sDevInfo[MAX_BOARD_NUMBER];
IXUD_CARD_INFO sCardInfo[MAX_BOARD_NUMBER];

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

	WORD wRtn;
	WORD wBoardNo;
	WORD wIndex;
	WORD wBoardIndex;

	char *szModelName="Unknow Device";

	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].wDOPorts-1);
	scanf("%d",&wPortNo);
	
	printf("Please input Sampling Rate(kHz)):\n");
	scanf("%f",&fSamplingRateKHz);

	//Set Digital Output Pattern
    for(wIndex = 0; wIndex<dwDataNum ; wIndex ++)
	{
     dwDOBuf[wIndex] = wIndex &0xFF;
	 printf("[%4x]",dwDOBuf[wIndex]);
	}
	//Start DO Pattern on Continuous Mode
	wRtn = Ixud_StartDO(wBoardNo, wPortNo, 0, fSamplingRateKHz*1000.0,dwDataNum,0,dwDOBuf);
	printf("\nStart to output digital pattern[0x00~0xFF] %fkHz",fSamplingRateKHz*1000.0);
	printf("\nPress any key to stop digital output.");
	for(;;)
	{
		if(kbhit())
		{
			break;
		}
	}

	//Stop DO Pattern Output
	wRtn = Ixud_StopDO(wBoardNo, 0);
	
	//Driver Close
	wRtn=Ixud_DriverClose();
	
	system("PAUSE");
}