
//-- 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 AWG272 or AWG472 (Multiple Channel models) in continuous-mode ( infinite loop )
//-- 2) select the clock frequency 2 GHz
//-- 3) download a sinusoidal waveform to Channel A
//-- 4) download a ramp waveform to Channel B
//-- 5) repeat steps 3-4 with different parameters for each channel on the next user page
//-- 6) 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 );

	printf( "Module model id: %X\n", awg.module_id_number );
	printf( "Module model name: %s\n", awg.module_name );
	
	//---- Check if the module_id_number is AWG272 or AWG472
	//---- Dual channel only
	if( ( awg.module_id_number != (unsigned) MODULE_ID::AWG272 ) && ( awg.module_id_number != (unsigned) MODULE_ID::AWG472 ) )
	{
		printf( "Wrong Module!! Must use a dual channel module\n" );
		return 1;
	}

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

	//---- Note that Marker parameters and the Data Length are determined by what you download to Channel B
	//---- 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;

		//---- Program CHA	
		awg.channel_select = 0;	// Channel A

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

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

		//---- Program CHB	
		awg.channel_select = 1;	// Channel B

		//---- Using the Built-in Waveform RAMP
		awg.code = (unsigned) WAVEFORM_CODE::RAMP;
		awg.data_length = 0x2000;						// Must be the same data length as CHA
		awg.waveform_parameter_u0 = 2;
		awg.waveform_parameter_u1 = 1;

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

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

	//---- Note that Marker parameters and the Data Length are determined by what you download to Channel B
	//---- 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;

		//---- Program CHA	
		awg.channel_select = 0;	// Channel A

		//---- Using the Built-in Waveform SIN with frequency = clock * A / B
		awg.code = (unsigned) WAVEFORM_CODE::SIN_AB;
		awg.data_length = 0x4000;
		awg.waveform_parameter_u0 = 1;
		awg.waveform_parameter_u1 = 0x40;

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

		//---- Program CHB	
		awg.channel_select = 1;	// Channel B

		//---- Using the Built-in Waveform RAMP
		awg.code = (unsigned) WAVEFORM_CODE::RAMP;
		awg.data_length = 0x4000;						// Must be the same data length as CHA
		awg.waveform_parameter_u0 = 1;
		awg.waveform_parameter_u1 = 1;

		//---- 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 pair of waveforms
	awg.user_page = 0;

	//---- Restart the first pair of waveforms and output them for 5 seconds
	awg.stop();
	awg.flush();
	awg.restart();

	Thread::Sleep( 5000 );

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

	//---- Restart the second pair of waveforms and output them for 5 seconds
	awg.stop();
	awg.flush();
	awg.restart();

	Thread::Sleep( 5000 );

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