//=============================================================================
// (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: DSCDAConvert.c   version 6.01
// Description: Sample program that demonstrates how to perform a D/A conversion
// 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>
// 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 HELIOSDSCDAConvert
#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 ERROR_PREFIX "HELIOS Driver ERROR:"

// var declarations
BYTE result;  // returned error code
DSCB dscb;    // handle used to refer to the board
DSCCB dsccb;  // structure containing board settings
int channel; // selected output channel
DSCDACODE output_code;  // DA code for desired output voltage
ERRPARAMS errorParams;  // structure for returning error code and error string
int intBuff;            // temp variables of size int
int i;        // miscellaneous counter

char input_buffer[20];
int polarity = 0;
int gain = 0;
int load_cal = 0;

int dasim = 0;

DSCDASETTINGS dasettings;

#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.
//
//     STEPS TO FOLLOW:
//
//	     I. Driver Initialization
//	    II. Board Initialization
//	   III. DA Conversion and Output
//	    IV. Cleanup
//
//=============================================================================

int main( int argc, char* argv[] )
{
        memset ( &dasettings, 0, sizeof(DSCDASETTINGS) );

	//=========================================================================
	// I. DRIVER INITIALIZATION
	//
	//    Initializes the DSCUD library.
	//
	//    STEPS TO FOLLOW:
	//
	//	  1. initialize the driver, using the driver version for validation
	//=========================================================================

	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.
	//
	//     STEPS TO FOLLOW:
	//
	//	   1. set the board type to DSC_HELIOS for HELIOS board
	//	   2. set the base address to 0x280
	//	   3. set the interrupt level 5
	//	   4. intialize and register the board with the driver, after which
	//		  the struct, dscb, now holds the handle for the board
	//=========================================================================

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

	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 );
	}

	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. DA CONVERSION AND OUTPUT
	//
	//	    Perform the actual DA conversion, then take an AD sample to verify
	//	    and output the results. Note that in order to verify the conversion
	//	    process on a particular output channel, we must set the input
	//	    channel on the AD conversion to (Output Channel + 4). To calculate
	//	    the output code to obtain a particular input voltage, we must use
	//	    one of the formulas located in the manual for your board (under the
	//	    the section, "Generating an Analog Output"). For example, the
	//	    formula for bipolar mode is:
	//
	//	    Output code = Output Voltage / Full-Scale Voltage * 2048 + 2048
	//
	//	    To calculate the actual input voltages from sample codes, 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"). For
	//	    example, if you are using a bipolar input range and 32-bit signed
	//	    integers:
	//
	//	    Input voltage = (AD Code / 32768) x Full-Scale Voltage
	//
	//	    STEPS TO FOLLOW:
	//
	//	    1. set the channel (must be between 0-3)
	//	    2. set the output code
	//	    3. write the output value to the selected output channel
	//	    4. repeat step 3 until a key is pressed
	//=========================================================================

	/* PRE-FILLED EXAMPLE
	channel = 0;
	output_code = 2048;
	*/

	do 
	{

                char input_buffer[20];
                memset ( input_buffer, 0, sizeof(input_buffer) );

		printf( "\nDA CONVERSION AND OUTPUT\n" );

		printf( "Enter the channel (0-3): " );
                fgets ( input_buffer, 20, stdin );
                sscanf ( input_buffer, "%d", &channel );

		printf( "Enter the output code: " );
                fgets ( input_buffer, 20, stdin );
                sscanf ( input_buffer, "%d", &output_code );

              printf( "Enter polarity ( 0: unipolar, 1: bipolar ): " );
              fgets ( input_buffer, 20, stdin );
              sscanf ( input_buffer, "%d", &polarity );
		dasettings.daPolEn = 1;

                printf( "Enter gain ( 0: 1x; 1: 2x; 2: 4x; 3: 8x ): " );
                fgets ( input_buffer, 20, stdin );
                sscanf ( input_buffer, "%d", &gain );

                printf( "Enter DASIM bit ( 0, 1 ): " );
                fgets ( input_buffer, 20, stdin );
                sscanf ( input_buffer, "%d", &dasim );

                printf( "Enter load_cal ( 0: no; 1: yes): " );
                fgets ( input_buffer, 20, stdin );
                sscanf ( input_buffer, "%d", &load_cal );

		// Using an integer as an intermediary avoids incompatibilities between compilers using different sizes for BYTE (unsigned char)
		dasettings.polarity = polarity;
		dasettings.gain = gain;
		dasettings.load_cal = load_cal;
		dasettings.dasim = dasim;

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

		printf( "\nSending output code, %d, to channel %d...\n", (int) output_code, (int)channel );

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


		printf("Press 'q' to quit or ENTER to continue ");
	}while(getchar() != 'q');

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

	return 0;
} // end main()


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

	return 0;
}

#endif
