//
//   C-Language example for the 
//   Crandun Technologies CTI-AR200 software library.
//   Copyright (c) 2001, Crandun Technologies Inc.
//
//   This example shows how to retrieve the sensor's
//   firmware version, and how to change the sensor's baud rate.
//


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

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


#define MAXSAMPLES 200

// A helper macro for checking return codes
#define CHK_RETURN_VALUE(retCode, fnName) \
if (retCode < 0) { \
		cerr << "ERROR: " #fnName " returned error code: " << rc << endl; \
		{	char errMsg[300]; if (my_Sensor.getIsError()) { \
				my_Sensor.getErrorMessage(errMsg, sizeof(errMsg)); \
				cerr << "Library error msg is: " << errMsg << endl; \
			} \
		}\
		my_Sensor.setCommClosed(); \
		cerr << "Please enter any character to exit: "; \
		cin >> c; \
		return (-1); \
} 


int main()
{
	char c;
	long i;
	long rc;
	float fullScaleSpan, rangeData[MAXSAMPLES];
	char sFirmwareVersion[100];
	char sLibName[100];

	CTI_AR200 my_Sensor;

	cout << "Crandun Technologies CTI-AR200 Baud Rate change sample program." << endl;

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

	cout << "Opening the serial port..." << endl;
	rc = my_Sensor.setCommOpen("COM1", 9600, fullScaleSpan);
	CHK_RETURN_VALUE(rc, setCommOpen)
	cout << "Successfully opened the serial port!" << endl;

	// Get the sensor's firmware version 
	memset(sFirmwareVersion, 0, sizeof(sFirmwareVersion));
	cout << "Querying the sensor firmware version..." << endl;
	rc = my_Sensor.getFirmwareVersion(sFirmwareVersion, sizeof(sFirmwareVersion));
	CHK_RETURN_VALUE(rc, getFirmwareVersion)
	cout << "The sensor firmware version is '" << sFirmwareVersion << "'." << endl;

	// Get the library's name
	memset(sLibName, 0, sizeof(sLibName));
	cout << "Querying the library's name..." << endl;
	rc = my_Sensor.getLibraryName(sLibName, sizeof(sLibName));
	CHK_RETURN_VALUE(rc, getLibraryName)
	cout << "The library name is '" << sLibName << "'." << endl;


	// Now change the baud rate 
	cout << "Changing baud rate to 19200." << endl;
	rc = my_Sensor.setBaudRate(19200);
	CHK_RETURN_VALUE(rc, setBaudRate)
	cout << "Successfully changed baud rate to 19200." << endl;

	// Change the sensor sample rate 
	cout << "Changing sample rate to 10 samples/sec." << endl;
	rc = my_Sensor.setSamplesPerSec(10);
	CHK_RETURN_VALUE(rc, setSamplesPerSec)
	cout << "Successfully changed sample rate." << endl;


	cout << "Reading samples from sensor..." << endl;
	rc = my_Sensor.getSamples(rangeData, MAXSAMPLES, 5, 2000);
	CHK_RETURN_VALUE(rc, getSamples)

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

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

	// Now change the baud rate to 38400 
	cout << "Changing baud rate to 38400." << endl;
	rc = my_Sensor.setBaudRate(38400);
	CHK_RETURN_VALUE(rc, setBaudRate)
	cout << "Successfully changed baud rate to 38400." << endl;

	cout << "Reading samples from sensor..." << endl;
	rc = my_Sensor.getSamples(rangeData, MAXSAMPLES, 5, 2000);
	CHK_RETURN_VALUE(rc, getSamples)

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

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

	// closing the comm port changes the baud rate back to factory default of 9600 
	my_Sensor.setCommClosed();

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