/*! Time-stamp: <@(#)ATDemoTunDVBSS2.cpp   07/05/2007 - 15:46:04   P.Hoogervorst>
 *********************************************************************
 *  @file   : ATDemoTunDVBSS2.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 *strLNBTone[] =
{
	"ON",
	"OFF",
	"INVALID"
};

static const char *strLNBVolt[] =
{
	"13V",
	"18V",
	"OFF",
	"INVALID"
};

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

static const char *strMode[] = 
{
	"DVB-S  ",
	"INVALID",
	"DVB-S2 ",
	"DVB-DSS",
	"       "
};

static const char *strConst[] = 
{
	"QPSK   ",
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"INVALID",
	"8PSK   ",
	"16APSK ",
	"32APSK ",
	"       "
};

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

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

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

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

// Set the DVB-SS2 AnnexA tuner
BOOL SetTunDVBSS2(CATBoardTunDVBSS2 *pATBoardCtrl, STunDVBSS2Opt Options)
{
	char TmpStr[MAX_TMP_STR_LEN];

	Message(E_VERBOSE_2, "*****************************************************************");
	Message(E_VERBOSE_2, "Initializing Tuner....");
	// initialize the tuner
	if (!pATBoardCtrl->Init())
		return FALSE;
	Message(E_VERBOSE_2, "Initializing Tuner Done");

	Message(E_VERBOSE_2, "Set tuner....");
	// set the tuner
	if (!pATBoardCtrl->Set(Options.m_nFrequency, Options.m_nSymbolRate, Options.m_LNBTone, Options.m_LNBVolt, Options.m_LNBHighVolt))
		return FALSE;

	// give the tuner some time to lock.
	SysSleep(1000);
	Message(E_VERBOSE_2, "Set tuner Done");

	Message(E_VERBOSE_2, "Settings:");
	sprintf(TmpStr, "Frequency: %ld Hz", Options.m_nFrequency);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "nSymbolRate: %ld MSymb/s", Options.m_nSymbolRate);
	Message(E_VERBOSE_2, TmpStr);
	
	sprintf(TmpStr, "LNB Tone: %s", strLNBTone[Options.m_LNBTone]);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "LNB Voltage: %s", strLNBVolt[Options.m_LNBVolt]);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "LNB +1V: %s", strLNBHighVolt[Options.m_LNBHighVolt]);
	Message(E_VERBOSE_2, TmpStr);

	BOOL bInSync;
	BOOL bCarrierDet;
	BOOL bLock;
	s32 nSignalStrength;
	s32 nSnr;
	u32 nBer;
	src_dvbs_mode_t Mode;
	src_modulation_t Modulation;
	src_code_rate_t Fec;
	src_filter_rolloff_t FiltRollOff;
	BOOL bPilots;
	BOOL bLongFrame;
	BOOL bInversion;


	// get status
	if (!pATBoardCtrl->Get(bInSync,
						   bCarrierDet,
						   bLock,
						   nSignalStrength,
						   nSnr,
						   nBer,
						   Mode,
						   Modulation,
						   Fec,
						   FiltRollOff,
						   bPilots,
						   bLongFrame,
						   bInversion))
	{
		Message(E_VERBOSE_2, "Device not Locked");
		return FALSE;
	}

	// print status
	Message(E_VERBOSE_2, "Status:");
	sprintf(TmpStr, "In sync: %s", bInSync ? "YES" : "NO");
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Carrier detected: %s", bCarrierDet ? "YES" : "NO");
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Locked: %s", bLock ? "YES" : "NO");
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Signal Strength: %ld dBm", nSignalStrength);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Signal to noise ratio: %.1f dB", (double)nSnr/10.0);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Bit error rate: %ld bit/s", nBer);

	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Mode: %s", strMode[Mode]);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Modulation: %s", strConst[Modulation]);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Fec: %s", strFEC[Fec]);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "FiltRollOff: %s", strRollOff[FiltRollOff]);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Pilots: %s", strOnOff[bPilots]);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "LongFrame: %s", strOnOff[bLongFrame]);
	Message(E_VERBOSE_2, TmpStr);
	sprintf(TmpStr, "Inversion: %s", strOnOff[bInversion]);
	Message(E_VERBOSE_2, TmpStr);
	
	return TRUE;
}
