
#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

float fSamplingRateKHz,fWaveSamplingRate;
WORD wPortNo;
IXUD_DEVICE_INFO sDevInfo[MAX_BOARD_NUMBER];
IXUD_CARD_INFO sCardInfo[MAX_BOARD_NUMBER];

DWORD dwAOBuf[dwDataNum];

void main(int argc, char* argv[])
{
	WORD wTotalBoards;
	WORD wRtn;
	WORD wBoardNo;
	WORD wIndex;
	WORD wBoardIndex;
	WORD wGainCode;
	WORD wAOChannel;

	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 Analog Output Channel (0 ~ %d)):\n",sCardInfo[wBoardNo].wAOChannels-1);
	scanf("%d",&wAOChannel);
	
	printf("Please input Sampling Rate(kHz)):\n");
	scanf("%f",&fSamplingRateKHz);

	printf("0:Unipolar  5V\n");
	printf("1:Bipolar กำ 5V\n");
	printf("2:Unipolar 10V\n");
	printf("3:Bipolar กำ10V\n");

	printf("Please selection Gain Code:\n");
    scanf("%d",&wGainCode);
	
	wRtn = Ixud_ConfigAO(wBoardNo,wAOChannel,wGainCode);		
	if (wRtn) 
	{
		printf("\nConfig AO Error!!");
		exit(0);
	}
	//Sin Wave pattern
    for(wIndex = 0; wIndex<dwDataNum ; wIndex ++)
	{
		dwAOBuf[wIndex] = (int)(sin(((float)wIndex/(float)(dwDataNum))*2.0*3.141592)*32767.0)+0x7FFF ;
	}

	fWaveSamplingRate = (fSamplingRateKHz*1000.0)/(float)dwDataNum;
	wRtn = Ixud_StartAOVoltageH(wBoardNo, wAOChannel, wGainCode, (float)(fWaveSamplingRate*dwDataNum),dwDataNum,0,dwAOBuf);
	if(wRtn)
	{
		printf("Start Analog Output Generator Error(%d)",wRtn);
		exit(0);
	}

	printf("Generate the %3.3f Hz Sin Wave(%ld points).",fWaveSamplingRate,dwDataNum);
	printf("Press any key to stop.");
    for(;;)
	{
		if (kbhit()) break;
	}

	wRtn = Ixud_StopAO(wBoardNo,wAOChannel);
	if(wRtn)
	{
		printf("Stop Analog Output Pattern Generate Error(%d)",wRtn);
		exit(0);
	}

	wRtn=Ixud_DriverClose();

	system("PAUSE");
}