//=============================================================================
// (c) Copyright 2005 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: MeasureReferences.c  v5.9
// Desc: Sample program that demonstrates how to measure and set the reference
//       voltages in the EEPROM of your board
//	Created by	KL
//=============================================================================

#include <stdio.h>

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

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

// DOS
#ifdef __BORLANDC__
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <math.h>
// diamond driver includes
#include "../../../../current/dev/source/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>
// diamond driver includes
#include "dscud.h"

#ifdef _WRS_VXWORKS_5_X
#include <selectLib.h>
#define main HERCMeasureReferences
#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


// var declarations
BYTE result; // returned error code
DSCB dscb;   // handle used to refer to the board
DSCCB dsccb; // structure containing board settings
DSCAUTOCAL dscautocal; // structure containing auto-calibration settings
ERRPARAMS errorParams; // errorParams
int menu;

int i;	     // miscellaneous counter

DFLOAT refs[5];

//=============================================================================
// 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. Measure References
//	    IV. Cleanup
//
//=============================================================================

int main( void )
{
	//=========================================================================
	// 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 HERCULES board. This function passes the various
	//	   hardware parameters to the driver and resets the hardware.
	//
	//=========================================================================

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

	dsccb.boardtype = DSC_HERCEBX;

	printf("Enter the base address in hex (default 0x240): ");
	scanf("%hx", &dsccb.base_address );	

	dsccb.int_level = 5;

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

	printf("\n");
		
	getchar();

	//=========================================================================
	// III. MEASURE REFERENCES
	//
	//		Measure the reference voltages for your board and then mention how
	//		to store these values in the EEPROM. 
	//
	//=========================================================================

	do {
		printf("\nSelect from the following menu \n");
		printf("0) Exit\n");
		printf("1) EEPROM dump \n");
		printf("2) Measure A/D reference voltages\n");
		printf("3) Measure D/A offset voltages\n");

		scanf("%d", &menu);

		if( menu == 0) 
			break;
		else if( menu == 1)
		{
			if( dscGetReferenceVoltages( dscb, refs) != DE_NONE )
			{
				dscGetLastError(&errorParams);
				fprintf( stderr, "dscGetReferenceVoltages error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );	
				return 0;
			}
	
			printf("\nThe stored A/D Reference Voltages are:\n");
			for( i = 0; i < 4 ; i++)
			{
				 printf("%d: %9.5lfV\n", i, refs[i]);
			}

			printf("\n");

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

			printf("The stored D/A average offset voltage is: %9.5lfV\n", refs[0]);

		}
		else if( menu == 2)
		{
		
			printf( "\nPlace the ground probe of your voltmeter\non pin 2 of J33 and the positive probe \non pin 1 of J33\n\n" );
	
			if ( dscSetCalMux(dscb, 1) != DE_NONE )
			{
				dscGetLastError(&errorParams);
				fprintf( stderr, "dscSetCalMux error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );
				return 0;
			}

			for( i = 0; i < 4; i++ )
			{
				if( dscADSetChannel(dscb, (unsigned char) i, (unsigned char)i) != DE_NONE )
				{
					dscGetLastError(&errorParams);
					fprintf( stderr, "dscADSetChannel error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );
					return 0;
				}
		
				printf( "Enter the voltage on the voltmeter (cal mux channel %d): ", i );
				scanf( "%lf", &refs[i] );
			}

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

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

			for( i =0; i < 4; i++)
				refs[i] =0;
	
			if( dscGetReferenceVoltages( dscb, refs) != DE_NONE )
			{
				dscGetLastError(&errorParams);
				fprintf( stderr, "dscGetReferenceVoltages error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );	
				return 0;
			}

			printf("The stored A/D Reference Voltages are:\n");
			for( i = 0; i < 4 ; i++)
			{
				 printf("%d: %9.5lfV\n", i, refs[i]);
			}

			printf("\n");

			printf( "Complete!\n" );
		}
		else if (menu == 3)
		{
			DFLOAT average;

			printf( "\nPlace the ground probe of your voltmeter on pin 5 of J9.\n"); 
			dscSetCalMux(dscb, 0);
			
			dscRegisterWrite(dscb, 1, 0x04);
			for ( i = 0; i < 4; i++ ) {
				if( dscDAConvert(dscb, (BYTE)i, 0) != DE_NONE )
				{
					dscGetLastError(&errorParams);
					fprintf( stderr, "dscDAConvert error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );	
					return 0;
				}
			}

			average = 0;
			
			for( i = 0; i < 4; i++ )
			{
				printf( "Place the positive probe on pin %d of J9. Enter the voltmeter reading: ", i+1 );
				scanf( "%lf", &refs[i] );

				average += (refs[i] - refs[0]);
			}

			average /= 4.0;

			printf("\nNow storing the average D/A offset [%1.5lfV] into the EEPROM..\n", average);
			
			refs[0] = average;
			
			if( dscDASetOffsets(dscb, refs, 1) != DE_NONE )
			{
				dscGetLastError(&errorParams);
				fprintf( stderr, "dscDASetOffsets error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );	
				return 0;
			}

			for( i =0; i < 4; i++)
				refs[i] =0;
	
			if( dscDAGetOffsets(dscb, refs, 1) != DE_NONE )
			{
				dscGetLastError(&errorParams);
				fprintf( stderr, "dscDAGetOffsets error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );	
				return 0;
			}
	

			printf("\nThe stored D/A average offset voltage is: %9.5lfV\n", refs[0]);

			printf("\n");

			printf( "Complete!\n" );

		}
	} while (1);
	//=========================================================================
	// IV. CLEANUP
	//
	//	   Cleanup any remnants left by the program and free the resources used
	//	   by the driver.
	//
	//     STEPS TO FOLLOW:
	//
	//	   1. free the driver resources
	//=========================================================================

	dscFree();

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

	return 0;
} // end main()



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

	return 0;
}

#endif
