
//-- There is NO need for a header
//-- Simply use the reference in the Project::Properties::Common Properties::Reference
//-- to add Euvis_Module_V1p1.DLL (the API file) as reference
using	namespace	MOL;
using	namespace	MOL::AWG;
using	namespace	System::Threading;

//-- If you use the traditional unmanaged codes
//-- You can also include the header
//-- Unmanaged Header
#include <stdio.h>

//==============================================================================
//==============================================================================
//-- The following code is a simple example to:
//-- 1) setup the AWG452 continuous-mode ( infinite loop )
//-- 2) select the clock frequency at 4 GHz
//-- 3) generate the bulk memory waveform consisting of 8 pulses of varying slope
//-- 4) download the waveform
//-- 5) generate a 1010 pattern waveform for the next user page. download the waveform
//-- 6) run the waveform

int	main( )
{
	AWG_Group_API	awg_group;
	AWG_API			awg;

	//---- Check if there is at least one AWG module connected
	if( awg_group.number == 0 )
	{
		printf( "No Module Exists!!\n" );
		return 1;
	}

	//---- Get the module series number
	int	SeriesNumber = awg_group.get_sn(0);
	printf( "Found Device with SN: %d\n", SeriesNumber );

	//---- Initialize the AWG
	//---- If you use multiple modules of the same type
	//---- you need to qualify the module_series number for the specified module
	awg.ini( SeriesNumber );

	//---- Check if the module_id_number is AWG452
	//---- module_id_number is used to identify the selected module 
	//---- in case multiple modules of different types, like AWG252 or DSM302, coexist
	/*
	if( awg.module_id_number != (unsigned) MODULE_ID::AWG452 )
	{
		printf( "Wrong Module!!\n" );
		printf( "Expect %d but got %X\n", (unsigned) MODULE_ID::AWG452, awg.module_id_number );
		return 1;
	}
	*/

	printf( "Module model id: %X\n", awg.module_id_number );
	printf( "Module model name: %s\n", awg.module_name );

	//---- Specify the signature file location and initialize it
	awg.signature_ini_dir( "C:\\Program Files\\Euvis\\AWG" );
	if( !awg.is_signature_file_exist )
	{
		printf( "Signature file does not exist!!\n" );
		return 1;
	}

	//---- Select the clock frequency 
	awg.Clock_Frequency = 4e9;


	//---- Configure the memory and waveform control
	awg.memory_depth = 0x100000;
	awg.loop_count = 0;				// Infinite Loop

	//---- Enable Markers 2 and 3 with positive polarity. 
	//---- Marker 1 is always enabled automatically, but you should set enable and polarity for the others
	awg.marker2_enabled		= true;
	awg.marker2_polarity	= true;
	awg.marker3_enabled		= true;
	awg.marker3_polarity	= true;

	//---- Select user page 0
	awg.user_page = 0;

	//---- Using bulk memory
	int		i;
	int		numOfPoints = 0x1000;
	int		period		= 0x200;
	int		width		= 0xc0;		// width between middle of pulse rise and fall
	int		TSTART		= 0x80;		// middle of pulse rise
	int		markerwidth = 0x20;
	unsigned *u = new unsigned[numOfPoints];
	unsigned *c = new unsigned[numOfPoints];
	const unsigned AMP	=  0xFFF;	// pulse height maximum amplitude

	for( i=0; i<numOfPoints; i++ )
	{
		c[i]=u[i]=0;		// initialize arrays with zeros
	}

	for( i=0; i<numOfPoints; i++ )
	{
		int j = i/period;	// index into which of 8 pulses
		int n = i%period;	// index within each of the 8 sub-waveforms
		int step = j*0x10;	// each pulse rises 16 times slower than last pulse

		if ( TSTART < n && n <= (TSTART+markerwidth) )
		{
			c[i] = 7;		// all 3 markers high
		}

		// rise and fall in trapezoidal pulse
		// width at mid-height is the same for each
		// so start rising at half the number of steps before marker
		// and start falling at half the number of steps before the intended mid-height end of the pulse
		if ( ( TSTART - (step/2) ) < n && n <= ( TSTART + (step/2) ) )
		{
			u[i] = AMP/step + u[i-1];	// rising
		}
		else if ( ( TSTART + (step/2) ) < n && n <= ( TSTART + width - (step/2) ) )
		{
			u[i] = AMP;					// maintaining full amplitude
		}
		else if ( ( TSTART + width - (step/2) ) < n && n <= ( TSTART + width + (step/2) ) )
		{
			u[i] = u[i-1] - AMP/step;	// falling
		}
		else
		{
			u[i] = 0;					// staying low
		}
	}


	//---- optional Debug
	//---- writes data to a file if you want to see what data the above code generated
	FILE	*fout = fopen( "eightpulses.log", "w+" );
	if( !fout )
	{
		printf( "File opening Error!!\n" );
		return 1;
	}

	for( i=0; i<numOfPoints; i++ )
	{
		fprintf( fout, "%6X %3X %1x\n", i, u[i], c[i] );
	}

	fclose( fout );
	//---- end of optional Debug section


	//---- write the data
	awg.user_define_bulk( u, c, numOfPoints );
	awg.data_length = numOfPoints;
	awg.code = (unsigned) WAVEFORM_CODE::USER_DEFINED;

	delete u;
	delete c;

	//---- Download the waveform
	awg.memory_dump = true;
	awg.stop();
	printf( "Downloading ...\n" );
	awg.download();
	printf( "Download Done!!\n" );




	//---- Set up the 1010 clock pattern
	//---- Select user page 1
	awg.user_page = 1;

	numOfPoints = 512;
	u = new unsigned[numOfPoints];
	c = new unsigned[numOfPoints];
	for( i=0; i<numOfPoints; i++ )
	{
		u[i] = ( i % 2 ) * AMP;
		if( i < numOfPoints / 2 ) c[i] = 7;
	}

	//---- write the data
	awg.user_define_bulk( u, c, numOfPoints );
	awg.data_length = numOfPoints;
	awg.code = (unsigned) WAVEFORM_CODE::USER_DEFINED;

	delete u;
	delete c;

	awg.stop();
	printf( "Downloading ...\n" );
	awg.download();
	printf( "Download Done!!\n" );

	//---- Run the waveform
	awg.user_page = 0;

	//---- Restart the waveform
	awg.stop();
	awg.flush();
	awg.restart();




	//---- In the Command window, type the number corresponding to the desired user page
	//---- The user page numbers begin with 0.
	while( 1 )
	{
		scanf( "%d", &i );
		if( i )
		{
			awg.user_page = 1;
		}
		else
		{
			awg.user_page = 0;
		}
	}
}