/*********************************************************************
 *
 *                  iUSBDAQ Example Code Version 1.00
 *
 *********************************************************************
 * FileName:        iUSBDAQsample.c
 * Dependencies:    None
 * Compiler:        C++
 * Company:         Copyright (C) 2007 by HYTEK Automation, Inc.
 *
 * Software License Agreement
 *
 * The software supplied herewith by HYTEK Automation Incorporated
 * (the “Company”) for its iUSBDAQ. The software is owned by the 
 * Company, and is protected under applicable copyright laws. 
 * All rights are reserved.
 *
 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 * Date:       11/11/2007    
 ********************************************************************/

/*********************************************************************
 * The Example shows how to open a iUSBDAQ device session and do a 
 * single scan of all analog channels. Then display the scan result
 * in the console until the user hit 'q' and enter.
 *********************************************************************/


#include <stdio.h>
#include "iUSBDAQ.h"


int PrintError(int code)
{
	char a;
	char errDesc[256];

	iUSBDAQ_GetErrorDes(code,errDesc);
	printf("Error: %s\n", errDesc);
	printf("Press any key to exit\r\n");
	scanf("%c", &a);
	return -1;
}

void DisplayResult(float* buf, int numberOfCh)
{
	int i = 0;
	for (; i < numberOfCh; i++)
	{
		printf("Channel %d: %.3f V\n", i, buf[i]);
	}
}

int main(int argc, char* argv[])
{
	int bQuit = 0;
    char selection;
	
	unsigned long devcnt=0;
	// Device type for iUSBDAQ is 0 according to user manual
	int rt = iUSBDAQ_EnumerateDev(0, &devcnt);
	if ( rt != iUSBDAQ_NOERROR)
	{
		return PrintError(rt);				
	}
	printf("Found %d of iUSBDAQ connected, reading data from first iUSBDAQ\n", (int)devcnt);
	DevSession dev;
	rt = iUSBDAQ_OpenDevice(0, 0, &dev);
	if ( rt != iUSBDAQ_NOERROR)
	{
		return PrintError(rt);				
	}
	float ch8[8];

    printf("HYTEK Automation 2007\r\n");
    printf("===============================\r\n");
    while(!bQuit)
    {
        printf("Press <Enter> to get next set of data, press <q+Enter> to quit\r\n");
        scanf("%c",&selection);

        if (selection != 'q' && selection != 'Q')
        {
			printf("Voltage of 8 channels:\r\n");
			rt = iUSBDAQ_ReadMultiAnalogIn(dev, 0, 8, 0, ch8, 0);
			if (rt != iUSBDAQ_NOERROR)
			{
				iUSBDAQ_ReleaseDevice(dev);
				return PrintError(rt);	
			}
			else
			{
				DisplayResult(ch8, 8);	
			}
			
        }// end switch
		else
		{
			bQuit = 1;	
		}
        fflush(stdin);printf("\r\n");
    }//end while
	
	iUSBDAQ_ReleaseDevice(dev);

	return 0;
}



