
//-- 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) download the sinusoidal waveform
//-- 4) download the ramp waveform
//-- 5) run the waveforms
	
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" );
	//awg.signature_ini(  );
	if( !awg.is_signature_file_exist )
	{
		printf( "Signature file does not exist!!\n" );
		return 1;
	}


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


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



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

	//---- Using the Built-in Waveform SIN with frequency = clock * A / B

	awg.code = (unsigned) WAVEFORM_CODE::SIN_AB;
	awg.data_length = 0x2000;
	awg.waveform_parameter_u0 = 1;
	awg.waveform_parameter_u1 = 0x10;

	//---- 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;

	//---- Marker
	awg.marker1_start = 0x10;
	awg.marker1_width = 0x100;
	awg.marker2_start = 0x200;
	awg.marker2_width = 0x100;
	awg.marker3_start = 0x100;
	awg.marker3_width = 0x100;

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


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

	//---- Using the Built-in Waveform RAMP
	awg.code = (unsigned) WAVEFORM_CODE::RAMP;
	awg.data_length = 0x4000;
	awg.waveform_parameter_u0 = 1;
	awg.waveform_parameter_u1 = 1;

	//---- Marker
	awg.marker1_start = 0x0;
	awg.marker1_width = 0x200;
	awg.marker2_start = 0x200;
	awg.marker2_width = 0x200;
	awg.marker3_start = 0x400;
	awg.marker3_width = 0x200;

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

	//---- Debug
	//awg.debugdump();
	//---- end Debug

	//---- Run the waveforms
	//---- Set the user page to 0, corresponding to the first waveform
	awg.user_page = 0;

	//---- Restart the first waveform and output it for 5 seconds
	awg.stop();
	awg.flush();
	awg.restart();

	Thread::Sleep( 5000 );

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

	//---- Restart the second waveform and output it for 5 seconds
	awg.stop();
	awg.flush();
	awg.restart();

	Thread::Sleep( 5000 );

	//---- Swap User Pages
	//---- Generate each waveform every other second
	while( 1 )
	{
		awg.user_page = 0;
		Thread::Sleep( 1000 );		// Each waveform generated repetitively for 1 second
		awg.user_page = 1;
		Thread::Sleep( 1000 );
	}		
}