/*
   C-Language example for the 
   Crandun Technologies CTI-AR4000 software library.
   Copyright (c) 2001, Crandun Technologies Inc.

   This example shows how to access two sensors simultaneously

*/

#include "CTI_AR4000.h"	/* required header */
#include <stdio.h>
#include <windows.h>

#define MAXSAMPLES 50

/* A helper macro for checking return codes */
#define CHK_RETURN_VALUE(retCode, fnName, sensorHandle) \
if (retCode < 0) { \
		printf("ERROR: " #fnName " returned error code: %ld\n", rc); \
		setCommClosed(sensorHandle); \
		printf("Please enter any character to exit: "); \
		scanf("%c", &c); \
		return (-1); \
} 

int main()
{
	char c;
	long rc;
	float rangeData[MAXSAMPLES];

	long numSkipped;
	long numSamples1, numSamples2;
	long sensorHandle_1, sensorHandle_2;

	printf("Crandun Technologies CTI-AR4000 Multiple sensors sample program.\n");
	sensorHandle_1 = sensorHandle_2 = -1;

	sensorHandle_1 = getNewCTIAR4000();
	if (sensorHandle_1 < 0) {
		printf("ERROR: getNewCTIAR4000 returned error code %ld\n", sensorHandle_1);
		printf("Please enter any character to exit: "); \
		scanf("%c", &c); \
		return -1;
	}
	
	sensorHandle_2 = getNewCTIAR4000();
	if (sensorHandle_2 < 0) {
		printf("ERROR: getNewCTIAR4000 returned error code %ld\n", sensorHandle_2);
		printf("Please enter any character to exit: "); \
		scanf("%c", &c); \
		return -1;
	}
	
	printf("Opening sensor #1 serial port COM1\n");
	rc = setCommOpen(sensorHandle_1, "COM1:", 9600);
	CHK_RETURN_VALUE(rc, setCommOpen, sensorHandle_1)
	printf("Successfully opened sensor #1 serial port!\n");

	printf("Opening sensor #2 serial port COM5\n");
	rc = setCommOpen(sensorHandle_2, "COM5:", 9600);
	CHK_RETURN_VALUE(rc, setCommOpen, sensorHandle_2)
	printf("Successfully opened sensor #2 serial port!\n");

	/* Set Sensor #1 to 10 samples/second */
	printf("Changing sensor #1 to 10 samples/second\n");
	rc = setSamplesPerSec(sensorHandle_1, 10);
	CHK_RETURN_VALUE(rc, setSamplesPerSec, sensorHandle_1)


	/* Clear both the buffer for both sensors, then sleep for one sec. */
	printf("Sleeping for one second\n");
	rc = setClearBuffer(sensorHandle_1);
	CHK_RETURN_VALUE(rc, setClearBuffer, sensorHandle_1)
	rc = setClearBuffer(sensorHandle_2);
	CHK_RETURN_VALUE(rc, setClearBuffer, sensorHandle_2)

	Sleep(1000);	/* Sleep for one second */

	/* Read the samples.  Sensor #1 should have approx. 10 samples
	   and sensor #2 should have approx 20 samples available */
	printf("Reading samples from sensor #1...\n");
	numSamples1 = getSamples(sensorHandle_1, rangeData, MAXSAMPLES, 5);
	CHK_RETURN_VALUE(numSamples1, getSamples, sensorHandle_1)

	printf("Reading samples from sensor #2...\n");
	numSamples2 = getSamples(sensorHandle_2, rangeData, MAXSAMPLES, 5);
	CHK_RETURN_VALUE(numSamples2, getSamples, sensorHandle_2)

	printf("Read %ld range samples from sensor #1.\n", numSamples1);
	numSkipped = getNumBytesSkipped(sensorHandle_1);
	printf("%ld bytes of data were skipped.\n", numSkipped);
	
	printf("Read %ld range samples from sensor #2.\n", numSamples2);
	numSkipped = getNumBytesSkipped(sensorHandle_2);
	printf("%ld bytes of data were skipped.\n", numSkipped);
	
	setCommClosed(sensorHandle_1);
	setCommClosed(sensorHandle_2);

	printf("Please enter any character to exit: ");
	scanf("%c", &c);
	return 0;
}
