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

#include "CTI_HSIF.h"	/* required header */
#include <stdio.h>
#define maxSamples 20

int main()
{
	char c;
	long i, rc;
	HSIF_DATA_PT rangeData[maxSamples];
	long sensorHandle=-1;

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

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

	/* Set board I/O address and interrupt number (0=don't use interrupts) */
	rc = setBoardParams(sensorHandle, "100", 0);
	if (rc != CTI_SUCCESS) {
		printf("ERROR: setBoardParams returned error %d\n", rc);
		printf("Please enter any character to exit: ");
		scanf("%c\n", &c);
		return -1;
	}

	/* Set calibration data file to use. MUST match the actual sensor used */
	rc = setCalibrationFile(sensorHandle, "C:\\lookuphs");
	if (rc != CTI_SUCCESS) {
		printf("ERROR: setCalibrationFile returned error %d\n", rc);
		printf("Please enter any character to exit: ");
		scanf("%c\n", &c);
		return -1;
	}

	/* Open communications to the serial port and high-speed interface */
	rc = setCommOpen(sensorHandle, "COM1:", 9600);
	if (rc != CTI_SUCCESS) {
		printf("ERROR: setCommOpen returned error %d\n", rc);
		printf("Please enter any character to exit: ");
		scanf("%c\n", &c);
		return -1;
	}

	/* Get samples from the sensor */
	printf("Reading samples from sensor ...\n");
	rc = getSamples(sensorHandle, rangeData, maxSamples, 10, 1000);
	if (rc < 0) {
		printf("ERROR: getSamples returned error %d\n", rc);
		printf("Please enter any character to exit: ");
		scanf("%c\n", &c);
		setCommClosed(sensorHandle);
		return -1;
	}

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

	setCommClosed(sensorHandle);
	setReleaseHandle(sensorHandle);

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

