/*! Time-stamp: <@(#)ATDemoModDVBC.cpp   07/05/2007 - 15:46:04   P.Hoogervorst>
 *********************************************************************
 *  @file   : ATDemoModDVBC.cpp
 *
 *  Project : ATDemoApp. Testappliction for Alitronika devices.
 *			  Supports Linux and windows operating System.
 *
 *  Package : 
 *
 *  Company : Engineering Spirit
 *
 *  Author  : P.Hoogervorst                              Date: 07/05/2007
 *
 *  Purpose : Implementation of methods for class 
 *
 *********************************************************************
 * Version History:
 *
 * V 0.10  07/05/2007  BN : First Revision
 *
 *********************************************************************
 */

/**********************************************************************************************************/
// operating system specific operations
#ifndef LINUX
#pragma warning (disable : 4996)
#pragma warning (disable : 4005)
#endif

/**********************************************************************************************************/
// includes
#include <stdio.h>
#include "ATDemo.h"

/**********************************************************************************************************/
// static strings
static const char *strModulation[] =
{
	"INVALID",
	"QAM_16",
	"QAM_32",
	"QAM_64",
	"QAM_128",
	"QAM_256",
	"INVALID",
	"INVALID",
	"INVALID"
};

static const char *strFltRollOff[] =
{
	"12%",
	"13%",
	"15%",
	"18%"
};

static const char *strInterleaver[] =
{
	"I128/J1",
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"I64/J2",	
	"I32/J4",	
	"I16/J8",	
	"I8/J16",	
	"I12/J17"	
};

static const char *strModInversion[] =
{
	"OFF",
	"ON"
};

/**********************************************************************************************************/
// functions

// Set the DVB-C Modulator
BOOL SetModDVBC(CATBoardModDVBC *pATBoardCtrl, SModDVBCOpt Options)
{
	char TmpStr[MAX_TMP_STR_LEN];

	Message(E_VERBOSE_2, "*****************************************************************");
	Message(E_VERBOSE_2, "Initializing Modulator....");
	// initialize the modulation
	if (!pATBoardCtrl->Init())
		return FALSE;
	SysSleep(100);
	Message(E_VERBOSE_2, "Initializing RF/IF....");
	// initialize the RF and IF
	if (!pATBoardCtrl->InitRfIf())
		return FALSE;
	SysSleep(100);

	Message(E_VERBOSE_2, "Set Modulator parameters....");
	// set the modulation parameters
	if (!pATBoardCtrl->Set(		Options.m_Modulation,
								Options.m_FltRollOff,
								Options.m_Interleaver,
								Options.m_Inversion,
								Options.m_nSymbolRate,
								Options.m_bAnnexBEn))
		return FALSE;
	
	SysSleep(100);
	Message(E_VERBOSE_2, "Set RF/IF parameters....");
	// set the RF and IF parameters
	if (!pATBoardCtrl->SetRfIf(	Options.m_bRfIfSel,
								Options.m_iIfFrequency,
								Options.m_nRfFrequency,
								Options.m_iRfoutputLevel))
		return FALSE;

	// give the modulator some time to lock.
	SysSleep(1000);

	// print settings
	Message(E_VERBOSE_2, "Settings:");
	sprintf(TmpStr, "Modulation: %s", strModulation[Options.m_Modulation]);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Interleaver: %s", strInterleaver[Options.m_Interleaver]);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Inversion: %s", strModInversion[Options.m_Inversion]);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "AnnexB enabled: %s", Options.m_bAnnexBEn ? "YES" : "NO");
	Message(E_VERBOSE_2, TmpStr);
	if (Options.m_bAnnexBEn)
	{
		sprintf(TmpStr, "Interleaver: %s", strInterleaver[Options.m_Interleaver]);
		Message(E_VERBOSE_2, TmpStr);
	}
	else
	{
		sprintf(TmpStr, "Filter roll off: %s", strFltRollOff[Options.m_FltRollOff]);
		Message(E_VERBOSE_2, TmpStr);
		sprintf(TmpStr, "SymbolRate: %ld", Options.m_nSymbolRate);
		Message(E_VERBOSE_2, TmpStr);
	}
	sprintf(TmpStr, "RF output enabled: %s", Options.m_bRfIfSel ? "YES" : "IF ONLY");
	Message(E_VERBOSE_2, TmpStr);
	if (Options.m_bRfIfSel)
	{
		sprintf(TmpStr, "RF output frequency: %ld Hz", Options.m_nRfFrequency);
		Message(E_VERBOSE_2, TmpStr);
		sprintf(TmpStr, "RF output level: %d dBm", Options.m_iRfoutputLevel/10);
		Message(E_VERBOSE_2, TmpStr);
	}
	else
	{
		sprintf(TmpStr, "IF output frequency: %ld Hz", Options.m_iIfFrequency);
		Message(E_VERBOSE_2, TmpStr);
	}
	
	BOOL bModInSync;
	BOOL bMod204PacketSize;
	BOOL bModOFlow;
	BOOL bRfIfLocked;

	// get the modulation status
	if (!pATBoardCtrl->Get(bModInSync, bMod204PacketSize, bModOFlow))
		return FALSE;
	// get the RF and IF status
	if (!pATBoardCtrl->GetRfIf(bRfIfLocked))
		return FALSE;

	// print status
	Message(E_VERBOSE_2, "Status:");
	sprintf(TmpStr, "Modulator is in sync: %s", bModInSync ? "YES" : "NO");
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Modulator packet size: %s", bMod204PacketSize ? "204" : "188");
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Modulator overflow status : %s", bModOFlow ? "YES ERROR" : "OK");
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Modulator RF is locked: %s", Options.m_bRfIfSel ? (bRfIfLocked ? "YES" : "NO") : "ONLY IF");
	Message(E_VERBOSE_2, TmpStr);

	if(bModInSync == FALSE)
		return FALSE;

	return TRUE;
}
