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

   This example shows how to access two sensors simultaneously

*/

#include "CTI_AR600.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\n", &c); \
		return (-1); \
}

int main()
{
	char c;
	long rc;
	float rangeData[MAXSAMPLES];
	float fullScaleSpan1=4.0;	/* Change this to reflect actual sensor model used */
	float fullScaleSpan2=8.0;	/* Change this to reflect actual sensor model used */

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

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

	sensorHandle_1 = getNewCTIAR600();
	if (sensorHandle_1 < 0) {
		printf("ERROR: getNewCTIAR600 returned error code %ld\n", sensorHandle_1);
		printf("Please enter any character to exit: ");
		scanf("%c", &c);
		return -1;
	}

	sensorHandle_2 = getNewCTIAR600();
	if (sensorHandle_2 < 0) {
		printf("ERROR: getNewCTIAR600 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, Full Scale Span=%5.3f\n", fullScaleSpan1 );
	rc = setCommOpen(sensorHandle_1, "COM1:", 9600, fullScaleSpan1);
	CHK_RETURN_VALUE(rc, setCommOpen, sensorHandle_1)
	printf("Successfully opened sensor #1 serial port!\n");

	printf("Opening sensor #2 serial port COM5, Full Scale Span=%5.3f\n", fullScaleSpan2 );
	rc = setCommOpen(sensorHandle_2, "COM5:", 9600, fullScaleSpan2);
	CHK_RETURN_VALUE(rc, setCommOpen, sensorHandle_2)
	printf("Successfully opened sensor #2 serial port!\n");

	/* Set Sensor #1 to 10 samples/second */
	/* Leave Sensor #2 at factory default of 5 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 buffers for both sensors, then sleep for one sec. */
	printf("Sleeping for one second\n");
	setClearBuffer(sensorHandle_1);	/* Always returns success */
	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 5 samples available */
	printf("Reading samples from sensor #1...\n");
	numSamples1 = getSamples(sensorHandle_1, rangeData, MAXSAMPLES, 5, 1000);
	CHK_RETURN_VALUE(numSamples1, getSamples, sensorHandle_1)

	printf("Reading samples from sensor #2...\n");
	numSamples2 = getSamples(sensorHandle_2, rangeData, MAXSAMPLES, 5, 1000);
	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\n", &c);
	return 0;
}
