//
// "Hello World" example for the Crandun Technologies CTI-HSIF software library.
// Copyright (c) 2001, Crandun Technologies Inc.
//
#include <iostream>
#include "CTI_HSIF.h"	// required header file

using namespace std;
using namespace Crandun;	// all CTI library symbols are in this namespace

int main()
{
	char c;
	long rc;
	const int maxSamples = 20;
	HSIF_DATA_PT rangeData[maxSamples];
	
	CTI_HSIF my_Sensor;

	cout << "Crandun Technologies CTI-HSIF Library 'Hello World' example." << endl;
 
	// Set board I/O address and interrupt number (0=don't use interrupts) 
	rc = my_Sensor.setBoardParams("100", 0);
	if (rc != CTI_SUCCESS) {
		cerr << "ERROR: setBoardParams returned error: " << rc << endl;
		cerr << "Enter any character to exit: ";
		cin >> c;
		return -1;
	}

	// Set calibration data file to use. MUST match the particular HSIF card 
	rc = my_Sensor.setCalibrationFile("C:\\lookuphs");
	if (rc != CTI_SUCCESS) {
		cerr << "ERROR: setCalibrationFile returned error: " << rc << endl;
		cerr << "Enter any character to exit: ";
		cin >> c;
		return -1;
	}

	// Open communications to the serial port and high-speed interface 
	rc = my_Sensor.setCommOpen("COM1:", 9600);
	if (rc != CTI_SUCCESS) {
		cerr << "ERROR: setCommOpen returned error: " << rc << endl;
		cerr << "Enter any character to exit: ";
		cin >> c;
		return -1;
	}

	// Get samples from the sensor
	cout << "Reading samples from sensor ..." << endl;
	rc = my_Sensor.getSamples(rangeData, maxSamples, 10, 1000);
	if (rc < 0) {
		cerr << "ERROR: getSamples returned error: " << rc << endl;
		my_Sensor.setCommClosed();
		cerr << "Enter any character to exit: ";
		cin >> c;
		return -1;
	}

	cout << "Read " << rc << " range samples from the sensor." << endl;
	for (int i = 0; i < rc; i++)
		cout << "Range " << i << " is " << rangeData[i].R_X << endl;

	my_Sensor.setCommClosed();
	cout << "Enter any character to exit: ";
	cin >> c;
	return 0;
}

