/*! Time-stamp: <@(#)ATDemoModDVBS2.cpp   03/04/2008 - 15:46:04   P.Hoogervorst>
 *********************************************************************
 *  @file   : ATDemoModDVBS2.cpp
 *
 *  Project : ATDemoApp. Testappliction for Alitronika devices.
 *			  Supports Linux and windows operating System.
 *
 *  Package : 
 *
 *  Company : Engineering Spirit
 *
 *  Author  : P.Hoogervorst                              Date: 03/04/2008
 *
 *  Purpose : Implementation of methods for class 
 *
 *********************************************************************
 * Version History:
 *
 * V 0.10  03/04/2008  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[] =
{
	"QPSK   ",
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"8PSK   ",
	"16APSK ",
	"32APSK ",
	"INVALID"
};

static const char *strModFecCodeRate[] =
{
	"INVALID",
	"1/2    ",
	"2/3    ",
	"3/4    ",
	"4/5    ",
	"5/6    ",
	"INVALID",
	"7/8    ",
	"8/9    ",
	"INVALID",
	"1/4    ",
	"1/3    ",
	"2/5    ",
	"3/5    ",
	"9/10   ",
	"INVALID"
};

static const char *strFltRollOff[] =
{
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"20%    ",
	"25%    ",
	"35%    ",
	"INVALID"
};

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

static const char *strDvbSMode[] =
{
	"DVB-S   ",
	"DVB-DSNG",
	"DVB-S2  "
};

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

// Set the DVB-S2 modulator
BOOL SetModDVBS2(CATBoardModDVBS2 *pATBoardCtrl, SModDVBS2Opt 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);
	// initialize the RF and IF
	if (!pATBoardCtrl->InitRfIf())
		return FALSE;
	SysSleep(100);
	Message(E_VERBOSE_2, "Initializing Modulator Done");

	Message(E_VERBOSE_2, "Set Modulator....");
	// set the modulation parameters
	if (!pATBoardCtrl->Set(		Options.m_Modulation,
								Options.m_FECCodeRate,
								Options.m_FltRollOff,
								Options.m_Inversion,
								Options.m_nSymbolRate,
								0x20000,
								Options.m_DvbModeSel,
								TRUE,
								FALSE,
								FALSE,
								FALSE,
								FALSE,
								FALSE))
		return FALSE;
	
	SysSleep(100);
	// 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);
	Message(E_VERBOSE_2, "Set modulator Done");

	// print settings
	Message(E_VERBOSE_2, "Settings:");
	sprintf(TmpStr, "DVB mode: %s", strDvbSMode[Options.m_DvbModeSel]);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Modulation: %s", strModulation[Options.m_Modulation]);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "FEC code rate: %s", strModFecCodeRate[Options.m_FECCodeRate]);
	Message(E_VERBOSE_2, TmpStr);
	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, "Inversion: %s", strModInversion[Options.m_Inversion]);
	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 bTsInSync;
	BOOL bTs204PacketSize;
	BOOL bTsOFlow;
	BOOL bRfIfLocked;

	// get the modulation status
	if (!pATBoardCtrl->Get(bTsInSync, bTs204PacketSize, bTsOFlow))
		return FALSE;
	// get the RF and IF status
	if (!pATBoardCtrl->GetRfIf(bRfIfLocked))
		return FALSE;

	// print status
	Message(E_VERBOSE_2, "Status:");
	sprintf(TmpStr, "Transport stream input in sync: %s", bTsInSync ? "YES" : "NO");
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Transport stream input packet size: %s", bTs204PacketSize ? "204" : "188");
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Transport stream input overflow status: %s", bTsOFlow ? "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);

	return TRUE;
}
