//=============================================================================
// (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: DSCDAAutoCal.c  v5.9
// Desc: Sample program that demonstrates how to DA calibrate the 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>
#include <string.h>
// diamond driver includes
#include "dscud.h"
#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"
#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 DMMATDSCDAAutoCal
#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
DSCADSETTINGS dscadsettings; // structure containing A/D conversion settings
DSCDACALPARAMS dscparams;    // structure containing auto-calibration settings
ERRPARAMS errorParams; // structure for returning error code and error string
int intBuff;           // temp variable of size int
float floatBuff;       // temp variable of size float
int i;	               // miscellaneous counter

//=============================================================================
// 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( void )
{
	//=========================================================================
	// I. DRIVER INITIALIZATION
	//
	//    Initializes the DSCUD library.
	//
	//=========================================================================

	if( ( result = 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 DMM-AT board. This function passes the various
	//	   hardware parameters to the driver and resets the hardware.
	//
	//=========================================================================

	/* PRE-FILLED EXAMPLE
	dsccb.boardtype = DSC_DMMAT;
	dsccb.base_address = 0x300;
	dsccb.int_level = 5;
	*/

	printf( "BOARD INITIALIZATION:\n" );

	dsccb.boardtype = DSC_DMMAT;

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

	dsccb.int_level = 5;

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

	//=========================================================================	
	//
	//	III. AUTOCAL SETTINGS INITIALIZATION
	//
	//		Initialize the structure containing the DA autocal settings. 
	//
	//=========================================================================

	/* PRE-FILLED EXAMPLE
	dscparams.darange = 5.5;
	dscparams.ch0pol = 0;
	dscparams.ch0prog = 1 ;
	dscparams.ch0ext = 0;

	dscparams.ch1pol = 0;
	dscparams.ch1prog = 0;
	dscparams.ch1ext = 0;
	*/

	printf( "\nAUTOCAL SETTINGS INITIALIZATION:\n" );

	memset(&dscparams, 0, sizeof(DSCDACALPARAMS));

	printf( "Enter the DA range code 0 being fixed or \na float between 0-10.0 being the voltage : " );
	scanf("%f", &floatBuff);
	dscparams.darange = (FLOAT) floatBuff;

	printf("Enter polarity for DA channel 0 (0 BIPOLAR, 1 UNIPOLAR) : ");
	scanf("%d", &intBuff);
	dscparams.ch0pol = (BYTE) intBuff;

	printf("Enter fixed/programmable for DA channel 0 (0 fixed, 1 programmable) : ");
	scanf("%d", &intBuff);
	dscparams.ch0prog = (BYTE) intBuff;

	printf("Enter internal/external for DA channel 0 (0 internal, 1 external) : ");
	scanf("%d", &intBuff);
	dscparams.ch0ext = (BYTE) intBuff;

	printf("Enter polarity for DA channel 1 (0 BIPOLAR, 1 UNIPOLAR) : ");
	scanf("%d", &intBuff);
	dscparams.ch1pol = (BYTE) intBuff;

	printf("Enter fixed/programmable for DA channel 1 (0 fixed, 1 programmable) : ");
	scanf("%d", &intBuff);
	dscparams.ch1prog = (BYTE) intBuff;

	printf("Enter internal/external for DA channel 1 (0 internal, 1 external) : ");
	scanf("%d", &intBuff);
	dscparams.ch1ext = (BYTE) intBuff;


	//=========================================================================
	// IV. AUTO-CALIBRATION
	//
	//	   Perform the actual DA auto-calibration of the board.
	//
	//=========================================================================

	printf( "\nAUTO-CALIBRATION:\n" );
	printf( "Starting DA auto-calibration...\n" );

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

	//=========================================================================
	// V. CALIBRATION VERIFICATION
	//
	//	  To verify that the auto-calibration was successfully completed, you
	//	  should run dscDACalVerify() to ensure that the gain and offset errors
	//	  meet a specified tolerance (2 in this example).
	//
	//=========================================================================

	printf( "\nCALIBRATION VERIFICATION:\n" );

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

	printf( "Offset Error: %9.3f, Gain Error: %9.3f\n", dscparams.offset, dscparams.gain ); 

	if ( fabs( dscparams.offset ) > 2.0 || 
		 fabs( dscparams.gain ) > 2.0 )
		printf( "Value for offset or gain exceeded specified tolerance\n" );
	else printf( "Values for offset and gain met specified tolerance\n" );

	//=========================================================================
	// VI. 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( "\nDSCDAAutoCal completed.\n" );

	return 0;
} // end main()


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

	return 0;
}

#endif
