/******************************************************************************
 *                                                                         
 *               Copyright 2004-2006 Agilent Technologies
 *               All rights reserved.
 *
 *****************************************************************************/
 
#include "stdafx.h"
#include "CoAgilent34980A.h"



/// <summary>
/// Configure the specified channels for period measurements but does not initiate
/// the scan.
/// </summary>
/// <remarks>
/// 
/// </remarks>
/// <returns>
///	
/// </returns>
/// <param name="ChannelList">
///	One or more channels in the form sccc.
/// </param>
/// <param name="Range">
///	Desired range in seconds.
/// </param>
/// <param name="Resolution">
///	Select from Least (4.5 digits), Default (5.5 digits) and Best (6.5 digits).
/// </param>
HRESULT Agilent34980A::IAgilent34980APeriod_Configure(BSTR ChannelList, double Range, Agilent34980AResolutionEnum Resolution)
{
	HRESULT hr = S_OK;

	CString sList = CorrectChannelList( ChannelList );

	if( Range<0 )
	{ // get rid of neg sign
		CString sCmd = PopInstrCommand();
		sCmd.Delete( sCmd.Find( '-' ) );
		PushInstrCommand( sCmd );
		hr = InstrPrintCommand();
		if( ::SysStringLen( ChannelList ) )
		{
			RETURN_IF_FAIL( InstrPrintf( "PER:VOLT:RANG:AUTO 1, (@%s)", sList ) );
		}
		else
		{
			RETURN_IF_FAIL( InstrPrintf( "PER:VOLT:RANG:AUTO 1" ) );
		}
	}
	else
	{
		hr = InstrPrintCommand();
	}

	return hr;
}

/// <summary>
///	Sets or gets the gate time for a period or frequency measurement.
/// </summary>
/// <remarks>
///	Allowable values are 10E-3 seconds, 100E-3 seconds, and 1 second.  These values
/// are also used and set by the Configure and Measurement routine’s resolution
/// settings of LEAST, DEFAULT, and BEST respectively.\r\n
/// If the ChannelList parameter is empty, this command applies to the internal
/// DMM, independent of any channels or a scan list.
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980APeriod_get_Aperture(BSTR ChannelList, double* Val)
{
	HRESULT hr = S_OK;

	CString sList(ChannelList);
	if( -1 != sList.Find( ',' ) || -1!=sList.Find( ':' ) || -1 != sList.Find( '-' ) )
	{
		return ReportError( E_AGILENT34980A_MULTIPLE_CHANNELS_IN_GET, _T("IAgilent34980APeriod_get_Aperture"), _T("ChannelList"));
	}
	CorrectChannelList( ChannelList, false );
	hr = InstrQueryCommand(Val);

	return hr;
}

/// <summary>
///	Sets or gets the gate time for a period or frequency measurement.
/// </summary>
/// <remarks>
///	Allowable values are 10E-3 seconds, 100E-3 seconds, and 1 second.  These values
/// are also used and set by the Configure and Measurement routine’s resolution
/// settings of LEAST, DEFAULT, and BEST respectively.\r\n
/// If the ChannelList parameter is empty, this command applies to the internal
/// DMM, independent of any channels or a scan list.
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980APeriod_put_Aperture(BSTR ChannelList, double Val)
{
	HRESULT hr = S_OK;

	CorrectChannelList( ChannelList );

	hr = InstrPrintCommand();

	return hr;
}

/// <summary>
///	Enables/Disables autoranging on the specified channel.
/// </summary>
/// <remarks>
///	
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980APeriod_get_AutoRange(BSTR ChannelList, VARIANT_BOOL* Val)
{
	HRESULT hr = S_OK;

	CString sList(ChannelList);
	if( -1 != sList.Find( ',' ) || -1!=sList.Find( ':' ) || -1 != sList.Find( '-' ) )
	{
		return ReportError( E_AGILENT34980A_MULTIPLE_CHANNELS_IN_GET, _T("IAgilent34980APeriod_get_AutoRange"), _T("ChannelList"));
	}
	CorrectChannelList( ChannelList, false );
	hr = InstrQueryCommandBool(Val);

	return hr;
}

/// <summary>
///	Enables/Disables autoranging on the specified channel.
/// </summary>
/// <remarks>
///	
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980APeriod_put_AutoRange(BSTR ChannelList, VARIANT_BOOL Val)
{
	HRESULT hr = S_OK;

	CorrectChannelList( ChannelList );

	hr = InstrPrintCommand();

	return hr;
}


/// <summary>
///	Returns the period range, this is always 0.333333 seconds.
/// </summary>
/// <remarks>
///	If the ChannelList parameter is empty, this command applies to the internal
/// DMM, independent of any channels or a scan list.
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980APeriod_get_Range(BSTR ChannelList, double* Val)
{
	HRESULT hr = S_OK;
/*
	CString sList(ChannelList);
	if( -1 != sList.Find( ',' ) || -1!=sList.Find( ':' ) || -1 != sList.Find( '-' ) )
	{
		return ReportError( E_AGILENT34980A_MULTIPLE_CHANNELS_IN_GET, _T("IAgilent34980APeriod_get_Range"), _T("ChannelList"));
	}
	sList = CorrectChannelList( ChannelList, false );
	RETURN_IF_FAIL( InstrQueryCommand(Val) );
	long lAuto;
	if( ::SysStringLen( ChannelList ) )
		hr = InstrQueryf( "PER:VOLT:RANG:AUTO? (@%s)", &lAuto, sList );
	else
		hr = InstrQuery( "PER:VOLT:RANG:AUTO?", &lAuto );

	if( lAuto )
		*Val = -(*Val);
*/
	*Val = 0.333333;
	return hr;
}


/// <summary>
/// Resets all measurement parameters on the specified channels to their default
/// values. Then, configures the channels for period measurements and immediately
/// sweeps through the specified channels one time.
/// </summary>
/// <remarks>
/// The results are sent directly to the instrument's output buffer. If you supply
/// empty ChannelList, this method applies to the internal DMM, independent of any
/// channels or a scan list.
/// </remarks>
/// <returns>
///	
/// </returns>
/// <param name="ChannelList">
///	One or more channels in the form sccc.
/// </param>
/// <param name="Range">
///	Desired range in seconds.
/// </param>
/// <param name="Resolution">
///	Select from Least (4.5 digits), Default (5.5 digits) and Best (6.5 digits).
/// </param>
HRESULT Agilent34980A::IAgilent34980APeriod_Measure(BSTR ChannelList, double Range, Agilent34980AResolutionEnum Resolution, SAFEARRAY** val)
{
	HRESULT hr = S_OK;

	CString sList = CorrectChannelList( ChannelList );

	if( Range<0 )
	{ // get rid of neg sign
		CString sCmd = PopInstrCommand();
		sCmd.Delete( sCmd.Find( '-' ) );
		PushInstrCommand( sCmd );
		// prepare new command for AUTO ranging
		PopInstrCommand();
		sCmd.Format("MEAS:PER? AUTO, %s, (@%s)", EnumToString<Agilent34980AResolutionEnum>(Resolution), sList );
		PushInstrCommand( sCmd ); // pop is in ForwardingShims.h
	}
	RETURN_IF_FAIL( InstrPrintCommandNoPoll() );
	hr = FormattedReadList( NtlAsciiType_R8, ",", val );



	return hr;
}
