//-- There is NO need of any header
//-- Simply use the reference in the Project::Properties::Common Properties::Reference
//-- to add the Euvis_Module_V1p0.DLL as reference
using	namespace	MOL;
using	namespace	MOL::DSM;
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 DSM303 continuous-mode ( infinite loop )
//-- 2) select the clock frequency at 2 GHz
//-- 3) download the FMCW waveform
//-- 5) run the waveform
	
int	main( )
{
	DSM_Group_API	dsm_group;
	DSMX			dsm;

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

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

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

	//---- Check if the module_id_number is dsm303
	//---- module_id_number is used to identify the selected module 
	//---- in case multiple modules of different types, like AWG's or DSM302, coexist
	if( dsm.module_id_number != (unsigned)MODULE_ID::DSM303 )
	{
		printf( "Got ID number: %d\n", dsm.module_id_number );
		printf( "No DSM Module Exists!!\n" );
		return 1;
	}
	
	//---- Select the clock frequency and update
	//---- Note: You have to copy the signature file into the output folder of the executible
	//dsm.signature_ini();
	dsm.signature_ini_dir( "C:\\bin" );
	if( !dsm.is_signtaure_file_exist )
	{
		printf( "There is no signature file exist in the program folder\n" );
		return 1;
	}
	dsm.Clock_Frequency = 2e9;

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

	//---- Avtivate the memory reset
	dsm.dds_reset_by_memory = true;
		
	//--------------------------------------------------------------------------
	//---- First Waveform
	//--------------------------------------------------------------------------
	//---- Select the user page 0
	dsm.user_page = 0;

	//---- Select the waveform code
	dsm.code		= (int) WAVEFORM_CODE::LINEAR_CHIRP;
	dsm.delay		= 0;
	dsm.data_length = 0x48;

	//---- Setup the waveform parameters
	dsm.chirp1	= 0x1000000;
	dsm.chirp2	= 0x10000000;
	dsm.chirp3	= 0x1000000;

	dsm.RESET_T1	= 0;
	dsm.RESET_T2	= 5;
	dsm.RESET_T3	= 3;

	//---- Marker
	dsm.marker_start	= 0x0;
	dsm.marker_width	= 0x10;
	dsm.marker_enabled	= true;
	dsm.marker_polarity	= true;

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

	//--------------------------------------------------------------------------
	//---- Second Waveform
	//--------------------------------------------------------------------------
	
	//---- Select the user page 1
	dsm.user_page = 1;

	//---- Select the waveform code
	dsm.code		= (int) WAVEFORM_CODE::LINEAR_CHIRP_DOWN;
	dsm.delay		= 0;
	dsm.data_length	= 0x208;

	//---- Setup the second waveform parameters
	dsm.chirp1	= 0x10000000;
	dsm.chirp2	= 0x10000;
	dsm.chirp3	= 0x80000;

	dsm.RESET_T1	= 0;
	dsm.RESET_T2	= 5;
	dsm.RESET_T3	= 3;

	//---- Marker
	dsm.marker_start	= 0x0;
	dsm.marker_width	= 0x10;
	dsm.marker_enabled	= true;
	dsm.marker_polarity	= true;

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

	//---- Alternate the user pages
	int	i;
	for( i=0; ;i++ )
	{
		if( !i )
		{	// Restart only needed in the beginning
			dsm.stop();
			dsm.flush();
			dsm.restart();
		}
		dsm.user_page = i%2;	// Dynamic Paging; i.e. changing the user page on the fly
		Thread::Sleep( 2000 );
	}
}