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

#include "CTI_AR200.h"	// required header
#include <iostream>		// needed for cout

using namespace std;		// we use cout and cerr
using namespace Crandun;	// all library symbols are in this namespace

int main()
{
	char c;
	char errMsg[300];
	long rc;
	const int maxSamples = 20;
	float fullScaleSpan, rangeData[maxSamples];

	CTI_AR200 my_AR200;

	cout << "Crandun Technologies CTI-AR200 Library 'Hello World' example." << endl;

	cout << "Enter the Full Scale Span of the sensor you are using (e.g. 25): ";
	cin >> fullScaleSpan;

	rc = my_AR200.setCommOpen("COM1", 9600, fullScaleSpan);
	if (rc != CTI_SUCCESS) {
		cerr << "ERROR: setCommOpen returned error: " << rc << endl;
		if (my_AR200.getIsError()) { 
			my_AR200.getErrorMessage(errMsg, sizeof(errMsg)); 
			cerr << "Library error msg is: " << errMsg << endl; 
		}
		cerr << "Please enter any character to exit: "; 
		cin >> c; 
		return -1;
	}

	cout << "Reading samples from sensor ..." << endl;
	rc = my_AR200.getSamples(rangeData, maxSamples, 10, 3000);
	if (rc < 0) {
		cerr << "ERROR: getSamples returned error: " << rc << endl;
		if (my_AR200.getIsError()) { 
			my_AR200.getErrorMessage(errMsg, sizeof(errMsg)); 
			cerr << "Library error msg is: " << errMsg << endl; 
		}
		my_AR200.setCommClosed();
		cerr << "Please 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] << endl;

	cout << my_AR200.getNumBytesSkipped() << " bytes of data were skipped." << endl;

	my_AR200.setCommClosed();

	cout << "Please enter any character to exit: ";
	cin >> c;

	return 0;
}