/*
 Callback example for the Crandun Technologies CTI-AR600
 software library.
 Copyright (c) 2001, Crandun Technologies Inc.

 This sample program demonstrates how to use a callback function 
 to retrieve sample data from the library, and write that data 
 to a disk file

 Note that this program must be compiled and linked with the 
 Visual C++ Multi-Threaded libraries, since the callback is called
 in a different thread from the main program.
*/

#include "CTI_AR600.h"	/* required header */
#include <stdio.h>
#include <windows.h>	/* needed for Sleep function */

/* A helper macro for checking return codes */
#define CHK_RETURN_VALUE(retCode, fnName) \
if (retCode < 0) { \
		printf("ERROR: " #fnName " returned error code: %ld\n", rc); \
		setCommClosed(sensorHandle); \
		printf("Please enter any character to exit: "); \
		scanf("%c\n", &c); \
		return (-1); \
}


#define MAXSAMPLES 200


// The data file and the Sensor handle must be global, 
// so that both the callback and main have access.
FILE * my_DataFile;
long sensorHandle;

/* This is the callback function that will be called by the library */
long myCallback(const float * pD1, 
				const long nPts1, 
				const float * pD2, 
				const long nPts2)
{
	long i,j;

	printf("In callback, reading %d samples. Writing to file\n", nPts1+nPts2);

	for (i = 0, j = 0; i < nPts1; i++, j++, pD1++) {
		printf("Range %d is %8.3f inches\n", j, *pD1);
		fprintf(my_DataFile, "%8.3f\n", *pD1);
	}

	/* read the samples from the second data pointer, if any */
	for (i = 0; i < nPts2; i++, j++, pD2++) {
		printf("Range %d is %8.3f inches\n", j, *pD2);
		fprintf(my_DataFile, "%8.3f\n", *pD2);
	}

	/* return non-zero to tell lib to remove samples from its buffer */
	return 1;
}


int main()
{
	const char * outFileName = "C:\\CallbackTest.out";
	char c;
	long rc;
	float fullScaleSpan;

	printf("Crandun Technologies CTI-AR600 Library Callback example.\n");

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

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

	printf("Opening the serial port...\n");
	rc = setCommOpen(sensorHandle, "COM1:", 9600, fullScaleSpan);
	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;
	}
	printf("Successfully opened the serial port!\n");

	printf("Opening output data file %s\n", outFileName);
	my_DataFile = fopen(outFileName, "w");
	if (my_DataFile == NULL) {
		printf("Could not open the output data file %s Aborting!\n", outFileName);
		setCommClosed(sensorHandle);
		printf("Please enter any character to exit: ");
		scanf("%c\n", &c);
		return -1;
	}


	printf("Testing callback function.\n");

	// set the function "myCallback" as the callback function
	rc = setCallbackFunction(sensorHandle, myCallback);
	CHK_RETURN_VALUE(rc, setCallbackFunction);

	// Tell library to call the callback when 5 samples are available
	// Sensor is at factory default of 5 samples/sec
	// so this should result in the callback being called 3 times
	// during the 3 second period that the main thread is sleeping
	rc = setCallbackThreshold(sensorHandle, 5);
	CHK_RETURN_VALUE(rc, setCallbackThreshold);

	// sleep for 3 seconds.
	printf("Main program is sleeping for 3 seconds...\n");
	Sleep(3000);

	printf("Main program finished sleeping - closing the serial port.\n");
	// close the sensor serial port - this also ensures that the callback is done
	rc = setCommClosed(sensorHandle);
	CHK_RETURN_VALUE(rc, setCommClosed);

	fclose(my_DataFile);	// close the data file

	setReleaseHandle(sensorHandle);

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

	return 0;
}