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

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

#define maxSamples 20

int main()
{
	char c;
	char errMsg[300]; 
	long i, rc;
	float fullScaleSpan, rangeData[maxSamples];
	long numSkipped;
	long sensorHandle = -1;

	printf("Crandun Technologies CTI-AR200 Library 'Hello World' example.\n");

	sensorHandle = getNewCTIAR200();
	if (sensorHandle < 0) {
		printf("ERROR: getNewCTIAR200 returned error code %ld\n", sensorHandle);
		return -1;
	}

	printf("Enter the Full Scale Span of the AR200 sensor you are using (e.g. 25): ");
	scanf("%f", &fullScaleSpan);

	rc = setCommOpen(sensorHandle, "COM1", 9600, fullScaleSpan);
	if (rc != CTI_SUCCESS) {
		printf("ERROR: setCommOpen returned error: %ld\n", rc);
		if (getIsError(sensorHandle)) { 
			getErrorMessage(sensorHandle, errMsg, sizeof(errMsg)); 
			printf("Library err msg is: %s\n", errMsg); 
		}
		printf("Please enter any character to exit: "); 
		scanf("%c\n", &c); 
		return -1;
	}

	printf("Reading samples from sensor ...\n");
	rc = getSamples(sensorHandle, rangeData, maxSamples, 10, 3000);
	if (rc < 0) {
		printf("ERROR: getSamples returned error: %ld", rc);
		if (getIsError(sensorHandle)) { 
			getErrorMessage(sensorHandle, errMsg, sizeof(errMsg)); 
			printf("Library err msg is: %s\n", errMsg); 
		}
		printf("Please enter any character to exit: "); 
		scanf("%c\n", &c); 
		return -1;
	}

	printf("Read %ld range samples from the sensor.\n", rc);
	for (i = 0; i < rc; i++)
		printf("Range %d is %8.3f mm\n", i, rangeData[i]);

	numSkipped = getNumBytesSkipped(sensorHandle);
	printf("%ld bytes of data were skipped.\n", numSkipped);

	setCommClosed(sensorHandle);
	setReleaseHandle(sensorHandle);

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