/*
 Crandun Technologies CTI-AR4000 Software Library Callback Example
 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_AR4000.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", &c); \
		return (-1); \
} 


/* File handle is global, so that both the callback and main have access. */
FILE * my_DataFile;

/* Declare the callback function that will be called by the library */
long myCallback(const float * pD1, 
				const long nPts1, 
				const float * pD2, 
				const long nPts2);

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

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

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

	printf("Opening the serial port...\n");
	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", &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", &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 callback is done  */
	rc = setCommClosed(sensorHandle);
	CHK_RETURN_VALUE(rc, setCommClosed);

	fclose(my_DataFile);	/* close the data file */

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

	return 0;
}



/* 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;
}


