//=============================================================================
// (c) Copyright 2009 Diamond Systems Corporation. Use of this source code
// is subject to the terms of Diamond Systems' Software License Agreement.
// Diamond Systems provides no warranty of proper performance if this
// source code is modified.
//
// File: DSCADSampleInt.c   version 6.01
// Description: Sample program that demonstrates how to perform an
//              interrupt-based Analog to Digital Sample.
// Contributors: KL, James Moore
//=============================================================================

#include <stdio.h>

#ifdef _WIN32
#ifndef _WIN32_WCE
#include <conio.h>
#endif

#include <windows.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
// diamond driver includes
#include "..\dscud.h"
#endif

#ifdef _WIN32_WCE

#include <string.h>
#include <Winsock2.h>


static int kbhit()
{
	int i;
	int result=0;

	
	result |= GetAsyncKeyState(VK_RETURN);
	result |= GetAsyncKeyState(VK_SPACE);

	if (result != 0)
	{
		getchar();
		return 1;
	}

	//number keys, 0-9
	for (i=48; i<=57; i++)
		result |= GetAsyncKeyState(i);

	if (result != 0)
	{
		getchar();
		return 1;
	}

	//capital character keys, A-Z
	for (i=65; i<=90; i++)
		result |= GetAsyncKeyState(i);

	if (result != 0)
	{
		getchar();
		return 1;
	}

	//lower case keys, a-z
	for (i=97; i<=122; i++)
		result |= GetAsyncKeyState(i);

	if (result != 0)
	{
		getchar();
		return 1;
	}

	return result;
}

#endif

// DOS
#ifdef __BORLANDC__
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <math.h>
#include <mem.h>
// diamond driver includes
//#include "../../../../current/dev/source/dscud.h"
#include "../../dscud.h"
#endif

// Linux and QNX
#if defined(linux) || defined(__QNXNTO__) || defined(_WRS_VXWORKS_5_X)
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
// diamond driver includes
#include "dscud.h"


#ifdef _WRS_VXWORKS_5_X
#include <selectLib.h>
#define main HeliosDSCADSampleInt
#else
#include <sys/time.h>
#endif

static int kbhit()
{
	struct timeval timeout;
	fd_set rfds;

	timeout.tv_sec = 0;
	timeout.tv_usec = 0;

	FD_ZERO(&rfds);
	FD_SET(0, &rfds);

	if ( select(0+1, &rfds, NULL, NULL, &timeout) > 0 )
		return getchar();

	return 0;
}
#endif


// macros defined
#define SLEEP_TIME 1000

// var declarations
BYTE result; // returned error code
DSCB dscb;   // handle used to refer to the board
DSCCB dsccb; // structure containing board settings
DSCADSETTINGS dscadsettings; // structure containing A/D conversion settings
DSCAIOINT dscaioint; // structure containing auto-calibration settings
DSCS dscs;           // used for interrupts.
DFLOAT voltage;
ERRPARAMS errorParams;  // structure for returning error code and error string
int intBuff;            // temp variables of size int
long longBuff;          // temp variables of size long
float floatBuff;        // temp variables of size float
int i;	     // miscellaneous counter
int k = 0;

#define HELIOS_DEFAULT_BASE_ADDRESS 0x280

char input_buffer[20]; // Buffer for parsing user character input.

//=============================================================================
// Name: main()
// Desc: Starting function that calls the driver functions used
//
//		 NOTE: By convention, you should capture the BYTE return value for each
//		 driver API call, and check the error code.
//
//	     I. Driver Initialization
//	    II. Board Initialization
//	   III. AutoCal Settings Initialization
//	    IV. Auto-Calibration
//		 V. Calibration Verification
//	    VI. Cleanup
//
//=============================================================================

int main(int argc, char ** argv )
{
	//=========================================================================
	// I. DRIVER INITIALIZATION
	//
	//    Initializes the DSCUD library.
	//
	//=========================================================================

	if( dscInit( DSC_VERSION ) != DE_NONE )
	{
		dscGetLastError(&errorParams);
		fprintf( stderr, "dscInit error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );
		return 0;
	}

	//=========================================================================
	// II. BOARD INITIALIZATION
	//
	//	   Initialize the HELIOS board. This function passes the various
	//	   hardware parameters to the driver and resets the hardware.
	//
	//=========================================================================

	printf( "\nHELIOS BOARD INITIALIZATION:\n" );

	if(argc == 1)
	{
	    printf ( "Enter the base address (default 0x280, hit ENTER) : " );
	    fgets ( input_buffer, 20, stdin );
	    if ( input_buffer[0] == '\n' )
	    {
		dsccb.base_address = HELIOS_DEFAULT_BASE_ADDRESS;
	    }
	    else
	    {
		sscanf ( input_buffer, "%hx", &dsccb.base_address );
	    }
	}else
	{
		sscanf(argv[1], "%hx", &dsccb.base_address );
	}
	dsccb.int_level = 5;

	if(dscInitBoard(DSC_HELIOS, &dsccb, &dscb)!= DE_NONE)
	{
		dscGetLastError(&errorParams);
		fprintf( stderr, "dscInitBoard error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );
		return 0;
	}

	//=========================================================================
	// III. AD SETTINGS INITIALIZATION
	//
	//	    Initialize the structure containing the AD conversion settings and
	//		then pass it to the driver.
	//
	//=========================================================================

	/* PRE-FILLED EXAMPLE
	dscadsettings.range = RANGE_10;
	dscadsettings.polarity = BIPOLAR;
	dscadsettings.gain = GAIN_1;
	dscadsettings.load_cal = (BYTE)TRUE;
	dscadsettings.current_channel = 0;
	*/

	printf( "\nAD SETTINGS INITIALIZATION\n" );

	memset(&dscadsettings, 0, sizeof(DSCADSETTINGS));

	dscadsettings.range = RANGE_10;
	if(argc <= 2)
	{
		printf( "Enter the polarity (0 for BIPOLAR, 1 for UNIPOLAR): " );
		scanf("%d", &intBuff);
	}else
	{
		sscanf(argv[2], "%d", &intBuff);
	}
	dscadsettings.polarity = (BYTE) intBuff;

	// The Helios SBC only has a 10V physical range. This 
	//   struct member is for backward compatibility.

	dscadsettings.range = RANGE_10;

	if(argc <= 4)
	{
		printf( "Enter the gain (0 for GAIN 1, 1 for GAIN 2, 2 for GAIN 4, 3 for GAIN 8): " );
		scanf("%d", &intBuff);
	}else
	{
		sscanf(argv[4], "%d", &intBuff);
	}
	dscadsettings.gain = (BYTE) intBuff;

	dscadsettings.load_cal = 0;

	dscadsettings.current_channel = 0;

	if( ( result = dscADSetSettings( dscb, &dscadsettings ) ) != DE_NONE )
	{
		dscGetLastError(&errorParams);
		fprintf( stderr, "dscADSetSettings error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );
		return 0;	}

	//=========================================================================
	// IV. I/O INTERRUPT SETTINGS INITIALIZATION
	//
	//	   Initialize the structure containing the analog I/O interrupt
	//	   settings.
	//
	//	   NOTE: You must allocate space for the buffer holding the returned
	//		     sample values. Also, be generous in allocating storage.
	//			 Allocating insufficient memory to hold sample data will result
	//			 in improper behavior of the driver, such as hanging interrupt
	//			 operations or assertion errors.
	//
	//=========================================================================

	/* PRE-FILLED EXAMPLE
	dscaioint.num_conversions = 1024;
	dscaioint.conversion_rate = 1000;
	dscaioint.cycle = (BYTE)FALSE;
	dscaioint.internal_clock = (BYTE)TRUE;
	dscaioint.low_channel = 0;
	dscaioint.high_channel = 3;
	dscaioint.external_gate_enable = (BYTE)FALSE;
	dscaioint.internal_clock_gate = (BYTE)FALSE;
	dscaioint.fifo_enab = (BYTE)TRUE;
	dscaioint.fifo_depth = 32;
	dscaioint.dump_threshold = 256;
	*/

	printf( "\nI/O INTERRUPT SETTINGS INITIALIZATION\n" );

	memset(&dscaioint, 0, sizeof(DSCAIOINT));
	if(argc <= 5)
	{
		printf( "Enter the number of conversions (must be a multiple of FIFO depth) : " );
		scanf("%ld", &longBuff);
	}else
	{
		sscanf(argv[5], "%ld", &longBuff);
	}
	dscaioint.num_conversions = (DWORD)longBuff;

	if(argc <= 6)
	{
		printf( "Enter the conversion rate in Hz (must be less than 100000) : " );
		scanf("%f", &floatBuff);
	}else
	{
		sscanf(argv[6], "%f", &floatBuff);
	}
	dscaioint.conversion_rate = (FLOAT) floatBuff;
	if(argc <= 7)
	{
		printf( "Enter the cycle flag (0 for FALSE, 1 for TRUE) : " );
		scanf("%d", &intBuff);
	}else
	{
		sscanf(argv[7], "%d", &intBuff);
	}
	dscaioint.cycle = (BOOL) intBuff;
	
	if(argc <= 8)
	{
		printf( "Enter the internal clock flag (0 for FALSE, 1 for TRUE) : " );
		scanf("%d", &intBuff);
	}else
	{
		sscanf(argv[8], "%d", &intBuff);
	}
	dscaioint.internal_clock  = (BOOL) intBuff;
	
	if(argc <= 9)
	{
		printf( "Enter the lowest channel in the scan range (0-15) : " );
		scanf("%d", &intBuff);
	}else
	{
		sscanf(argv[9], "%d", &intBuff);
	}
	dscaioint.low_channel = (BYTE) intBuff;
	
	if(argc <= 10)
	{
		printf( "Enter the highest channel in the scan range (0-15) : " );
		scanf("%d", &intBuff);
	}else
	{
		sscanf(argv[10], "%d", &intBuff);
	}
	dscaioint.high_channel = (BYTE) intBuff;

	dscaioint.external_gate_enable = 0; // can enable it if need be

	dscaioint.internal_clock_gate = 0;   // can enable it if need be

	dscaioint.fifo_enab = 1;
	if(argc <= 11)
	{
		printf( "Enter the FIFO depth (must be 46 or less) : " );
		scanf("%d", &intBuff);
	}else
	{
		sscanf(argv[11], "%d", &intBuff);
	}
	dscaioint.fifo_depth = (WORD) intBuff;

	if(argc <= 12)
	{
		printf("Enter the dump threshold ( must be less or equal to num conversions ) : ");
		scanf("%ld", &longBuff);
		getchar();
	}else
	{
		sscanf(argv[12], "%ld", &longBuff);
	}
	dscaioint.dump_threshold = (DWORD)longBuff;

	// allocate space for buffer
	dscaioint.sample_values = (DSCSAMPLE*)malloc( sizeof(DSCSAMPLE) * dscaioint.num_conversions );
	

	//=========================================================================
	// V. SAMPLING AND OUTPUT
	//
	//    Perform the actual sampling and then output the results. To calculate
	//	  the actual input voltages, we must convert the sample code (which
	//	  must be cast to a short to get the correct code) and then plug it
	//	  into one of the formulas located in the manual for your board (under
	//	  "A/D Conversion Formulas"). 
	//=========================================================================

	printf( "\nSAMPLING AND OUTPUT\n" );

	do 	{
		if( ( result = dscADSampleInt( dscb, &dscaioint ) ) != DE_NONE )
		{
			dscGetLastError(&errorParams);
			fprintf( stderr, "dscADSampleInt error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );
			free( dscaioint.sample_values ); // remember to deallocate malloc() memory
			return 0;
		}
	
		/////////////////////////
		// Check Interrupt status
		/////////////////////////
		dscs.transfers = 0;
		dscs.overflows = 0;
		dscs.op_type = OP_TYPE_INT;
		
		do{
			dscSleep(SLEEP_TIME);
			dscGetStatus(dscb, &dscs);	
			printf("A/D Interrupt Transfers in progress: %lu, overflows %lu\n", dscs.transfers, dscs.overflows);

// This feature, if un-commented, will print out the returned values 
//   from the board each time the program comes through this loop.
// (Hit ENTER to stop).

//		k = 0;
//              while ( k < dscaioint.num_conversions )
//              {
//                  printf ( "sample=%ld indices=%ld\n", dscaioint.sample_values[k], k ); 
//                  k++;
//              }

		}while (dscs.op_type != OP_TYPE_NONE && !kbhit());
	
		// cancel interrupts manually for recycled mode or if interrupts are still running
		if( dscs.op_type != OP_TYPE_NONE)
		{
			if( (result = dscCancelOp(dscb)) != DE_NONE)
			{
				dscGetLastError(&errorParams);
				fprintf( stderr, "dscCancelOp error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );
				free( dscaioint.sample_values ); // remember to deallocate malloc() memory
				return 0;
			}
		}

		printf( "\nSample readouts:" );
	
		for( i = dscaioint.low_channel; i < dscaioint.high_channel + 1; i++)
			printf( " %hd", dscaioint.sample_values[i] );
	
		printf( "\nActual voltages:" );

		for( i = dscaioint.low_channel; i < dscaioint.high_channel + 1; i++)
		{
			if( dscADCodeToVoltage(dscb, dscadsettings, dscaioint.sample_values[i], &voltage) != DE_NONE )
			{
				dscGetLastError(&errorParams);
				fprintf( stderr, "dscADCodeToVoltage error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );
				free(dscaioint.sample_values);
				return 0;
			}

			printf( " %5.3lfV", voltage);
		}

		printf( "\n\nPress 'q' to exit or ENTER to continue " );
	
	}while ( getchar() != 'q' );

	//=========================================================================
	// VI. CLEANUP
	//
	//	   Cleanup any remnants left by the program and free the resources used
	//	   by the driver.
	//
	//     STEPS TO FOLLOW:
	//
	//	   1. free the memory allocated for sample values
	//	   2. free the driver resources
	//=========================================================================

	free( dscaioint.sample_values );

	dscFree();

	printf( "\nDSCADSampleInt completed.\n" );

	return 0;
} // end main()


#ifdef _WIN32_WCE
int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPTSTR    lpCmdLine,
					int       nCmdShow)
{
	main();

	return 0;
}

#endif
