/*
 Crandun Technologies CTI-HSIF 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_HSIF.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); \
}


/* 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 HSIF_DATA_PT * pD1, const long N1,
				const HSIF_DATA_PT * pD2, const long N2);

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

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

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

	/* Set board I/O address and interrupt number (0=don't use interrupts) */
	rc = setBoardParams(sensorHandle, "100", 3);
	if (rc != CTI_SUCCESS) {
		printf("ERROR: setBoardParams returned error %d\n", rc);
		printf("Please enter any character to exit: ");
		scanf("%c\n", &c);
		return -1;
	}

	/* Set calibration data file to use. MUST match the actual sensor used */
	rc = setCalibrationFile(sensorHandle, "C:\\lookuphs");
	if (rc != CTI_SUCCESS) {
		printf("ERROR: setCalibrationFile returned error %d\n", rc);
		printf("Please enter any character to exit: ");
		scanf("%c\n", &c);
		return -1;
	}

	/* Open communications to the serial port and high-speed interface */
	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\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 sample rate of 5000 samples/second */
	rc = setSamplesPerSec(sensorHandle, 5000);
	CHK_RETURN_VALUE(rc, setSamplesPerSec);

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

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

	setResetHSIFBoard(sensorHandle);// reset HSIF board (always returns success)
	setClearBuffer(sensorHandle);	// purge anything currently in buffer (always returns success)

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

	rc = setReleaseHandle(sensorHandle);
	CHK_RETURN_VALUE(rc, setReleaseHandle);

	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 HSIF_DATA_PT * pD1, const long N1,
				const HSIF_DATA_PT * pD2, const long N2)
{
	long i,j;

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

	for (i = 0, j = 0; i < N1; i++, j++, pD1++) {
		fprintf(my_DataFile, "%8.3f\n", pD1->R_X );
	}

	/* read the samples from the second data pointer, if any */
	for (i = 0; i < N2; i++, j++, pD2++) {
		fprintf(my_DataFile, "%8.3f\n", pD2->R_X );
	}

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


