RSS Git Download  Clone
Raw Blame History
/*
* Title:	PupeDiagnosticsWin.cpp 
* Author:	M.Thomas BEAM Ltd
* Date:		2007-02-13
*
* Contents:	Status Display
*
* Mod Rec:
*
*/


#include <PupeDiagnosticsWin.h>
#include <qlayout.h>
#include <qvbox.h>
#include <qlabel.h>
#include <qtable.h>
#include <BError.h>
#include <TmsD.h>
#include <qvalidator.h>
#include <qmessagebox.h>
#include <BFile.h>
#include <BQComboBox.h>
#include <math.h>
#include <Globals.h>

using namespace std;
using namespace Tms;

#define	BIT(v,b)	((v >> b) & 1)


static ComboDefinition sourceDef[] = {
	{ 0,	"Source 0",	0,	1 },
	{ 1,	"Source 1",	1,	0 },
 	{ 2,	"Source 2",	2,	0 },
 	{ 3,	"Source 3",	3,	0 },
	{ 0,0,0,0 }
};

static ComboDefinition clockDef[] = {
 	{ 0,	"ADC Clock",		ClkAdcDiv_1,		1 },
 	{ 1,	"ADC Clock/2",		ClkAdcDiv_2,		0 },
	{ 2,  	"ADC Clock/5",		ClkAdcDiv_5,		0 },
	{ 3,  	"ADC Clock/10",		ClkAdcDiv_10,		0 },
	{ 4,  	"ADC Clock/20",		ClkAdcDiv_20,		0 },
	{ 5,  	"ADC Clock/50",		ClkAdcDiv_50,		0 },
	{ 6,  	"ADC Clock/100",	ClkAdcDiv_100,		0 },
	{ 7,  	"ADC Clock/200",	ClkAdcDiv_200,		0 },
	{ 8, 	"ADC Clock/500",	ClkAdcDiv_500,		0 },
	{ 9, 	"ADC Clock/1000",	ClkAdcDiv_1000,		0 },
	{ 10, 	"ADC Clock/2000",	ClkAdcDiv_2000,		0 },
	{ 11, 	"ADC Clock/5000",	ClkAdcDiv_5000,		0 },
	{ 12, 	"ADC Clock/10000",	ClkAdcDiv_10000,	0 },
	{ 13, 	"ADC Clock/20000",	ClkAdcDiv_20000,	0 },
	{ 14, 	"ADC Clock/50000",	ClkAdcDiv_50000,	0 },
	{ 15, 	"ADC Clock/100000",	ClkAdcDiv_100000,	0 },
	{ 16,	"FREF",			ClkFref,		0 },
	{ 17,	"Ms",			ClkMs,			0 },
	{ 0,0,0,0 }
};


Int32	getBitValue(UInt64 value, int startBit, int nBits, int s){
	Int64	v;
	UInt64	bm = (1ULL << nBits) - 1;
	UInt64	sm = (1ULL << (nBits - 1));
	
	v = (value >> startBit) & bm;
	
	if(s){
		v = -(v & sm) | v;
	}
	return v;
}

PupeDiagnosticsWin::PupeDiagnosticsWin(QWidget* w,Control& c) : ocontrol(c)  {
	QVBox* 		mv = new QVBox(this);
			
			oplots.append(oplotA = new BGraph(mv));
			oplots.append(oplotB = new BGraph(mv));
			oplots.append(oplotC = new BGraph(mv));
			oplots.append(oplotD = new BGraph(mv));
			oplots.append(oplotE = new BGraph(mv));
			oplots.append(oplotF = new BGraph(mv));
			oplots.append(oplotG = new BGraph(mv));
			oplots.append(oplotH = new BGraph(mv));
			oplots.append(oplotI = new BGraph(mv));
			oplots.append(oplotJ = new BGraph(mv));
			oplots.append(oplotK = new BGraph(mv));
			oplots.append(oplotL = new BGraph(mv));
			oplots.append(oplotM = new BGraph(mv));
			oplots.append(oplotN = new BGraph(mv));
			oplots.append(oplotO = new BGraph(mv));
			ographControls = new BGraphControlBasic(mv);

	QValidator* 		validator = new QIntValidator(0, (int)pow(2,31), this );
	QRegExpValidator* 	validatorHex = new QRegExpValidator(this);
	QRegExp		rx;
	
	QWidget*	t = new QWidget(mv);
	QGridLayout* 	v = new QGridLayout(t);


	int		row = 0;
	int		col = 0;
	
	BList<BString>	colours;
	
	
	rx.setPattern("0x[0-9,a-f][0-9,a-f]");
	validatorHex->setRegExp(rx);
	

	QHBox*		h = new QHBox(mv);
	QPushButton*	refreshButton = new QPushButton("Refresh Data",h,"refreshButton");
	QPushButton*	saveButton = new QPushButton("Save To File",h,"saveButton");
	okst = new QCheckBox(t,"");

	ochannel 	= new QSpinBox(1,60,1,t,"");

	osource 	= new BQComboBox(sourceDef,t,"");
	oclock 		= new BQComboBox(clockDef,t,"");

	ostartTime 	= new QLineEdit(t,"");ostartTime->setValidator(validator);
	ostartTime->setText("0");

	opostTriggerDelay 	= new QLineEdit(t,"");opostTriggerDelay->setValidator(validator);
	opostTriggerDelay->setText("0");
	
	otriggerMask = new QLineEdit(t,"");otriggerMask->setValidator(validatorHex);
	otriggerMask->setText("0x02");

	otriggerSourceData 	= new QCheckBox(t,"");
	otriggerAnd 	= new QCheckBox(t,"");
	otriggerStore 	= new QCheckBox(t,"");

	ochannel->setWrapping(true);
	h->setMargin(4);
	refreshButton->setMaximumWidth(200);
	saveButton->setMaximumWidth(200);


	int	width = 200;
	ochannel->setMaximumWidth(width);
	osource->setMaximumWidth(width);
	oclock->setMaximumWidth(width);
	ostartTime->setMaximumWidth(width);
	opostTriggerDelay->setMaximumWidth(width);

	otriggerMask->setMaximumWidth(width);
	otriggerAnd->setMaximumWidth(width);
	otriggerStore->setMaximumWidth(width);
	otriggerSourceData->setMaximumWidth(width);

	
	col = 0;row = 0;
	v->addWidget(new QLabel("Channel",t,""),row,col);		v->addWidget(ochannel,row,col+1);	row++;
	v->addWidget(new QLabel("Source",t,""),row,col);		v->addWidget(osource,row,col+1);	row++;
	v->addWidget(new QLabel("Clock",t,""),row,col); 	    	v->addWidget(oclock,row,col+1);	row++;
	v->addWidget(new QLabel("StartTime",t,""),row,col);     	v->addWidget(ostartTime,row,col+1);	row++;
	v->addWidget(new QLabel("Post Trigger Delay",t,""),row,col);   	v->addWidget(opostTriggerDelay,row,col+1);	row++;
	
	col = 2;row = 0;
	v->addWidget(new QLabel("Trigger Src Data",t,""),row,col);   	v->addWidget(otriggerSourceData,row,col+1);	row++;
	v->addWidget(new QLabel("Trigger And",t,""),row,col);     	v->addWidget(otriggerAnd,row,col+1);	row++;
	v->addWidget(new QLabel("Trigger Store",t,""),row,col);     	v->addWidget(otriggerStore,row,col+1);	row++;
	v->addWidget(new QLabel("Trigger Mask Data Hex (eg 0x3c)",t,""),row,col);  	v->addWidget(otriggerMask      ,row,col+1);	row++;

	v->addWidget(new QLabel("Save Format Kst",t,""),row,col);  	v->addWidget(okst      ,row,col+1);	row++;

	v->setSpacing(5);
	
	connect(refreshButton,SIGNAL(clicked()),this,SLOT(refresh()));
	connect(saveButton,SIGNAL(clicked()),this,SLOT(saveFile()));
	
	BIter	i;
	for (oplots.start(i);! oplots.isEnd(i);oplots.next(i)) {
		ographControls->addGraph(oplots[i]);
		oplots[i]->setGrid(50,0);
		oplots[i]->installEventFilter(this);
	}
}
	
PupeDiagnosticsWin::~PupeDiagnosticsWin() {}

	
void PupeDiagnosticsWin::show() {
	QWidget::show();
}	


void PupeDiagnosticsWin::refresh() {
	BError	err;
	
	Tms::TestCaptureInfo 	captureInfo;
	Tms::PuChannel 		puPhysChannel;
	
	// Build request

	if (err = ocontrol.getPuChannel (ochannel->value(),puPhysChannel)) {
		warningDialog("Reading Test Data",err);
		return;
	}
	
	captureInfo.source = (int)osource->getValue();
	captureInfo.clock = (int)oclock->getValue();
	captureInfo.startTime = ostartTime->text().toUInt();
	captureInfo.postTriggerDelay = opostTriggerDelay->text().toUInt();
	captureInfo.triggerSourceData = otriggerSourceData->isChecked();
	captureInfo.triggerAnd = otriggerAnd->isChecked();
	captureInfo.triggerStore = otriggerStore->isChecked();
	
	sscanf(otriggerMask->text().ascii(),"%i",&captureInfo.triggerMask);

	if (err = ocontrol.getTestData(puPhysChannel,captureInfo,odata)) {
		warningDialog("Reading Test Data",err);
		return;
	}
	setPlotData();
}	

void	PupeDiagnosticsWin::setPlotData() {
	BIter	i;	
	// Bit Pattern Source 0
	int 	source = (int)osource->getValue();
	int	invert = 1;
	int	height = 620;

	for (oplots.start(i);! oplots.isEnd(i);oplots.next(i)) {
		oplots[i]->setXLabel("");
	}		

	if (source == 0) {
		if (! otriggerStore->isChecked()) {
			setGraphHeights(height,15);
			addBitFieldDataToGraph(oplotA,"fref",63, 1, 0);	 		// FREF
			addBitFieldDataToGraph(oplotB,"PllMsb",62, 1, 0,invert);	 	// PllMsb (FRef Reference)
			addBitFieldDataToGraph(oplotC,"PhaseTableMsb",61, 1, 0,invert);	// PhaseTableMsb (Local FREF)
			addBitFieldDataToGraph(oplotD,"lo1",60, 1, 0);	  		// LO1
			addBitFieldDataToGraph(oplotE,"sigma",32, 12, 1);	// Sigma
			addBitFieldDataToGraph(oplotF,"lo2",56, 1, 0);	   		// LO2
			addBitFieldDataToGraph(oplotG,"gate",49, 1, 0); 	// Gate
			addBitFieldDataToGraph(oplotH,"blr",48, 1, 0);  	// Blr
			addBitFieldDataToGraph(oplotI,"mean1",50, 1, 0);	// Mean1
			addBitFieldDataToGraph(oplotJ,"mean2",51, 1, 0);		// Mean2
			addBitFieldDataToGraph(oplotK,"rfSelect1",52, 1, 0);	 	// RfSelect1
			addBitFieldDataToGraph(oplotL,"rfSelect2",53, 1, 0);	 	// RfSelect2
			addBitFieldDataToGraph(oplotM,"selFilter",54, 1, 0);	 	// SelFilter
			addBitFieldDataToGraph(oplotN,"SwitchState",44,4, 0);	//SwitchState			// Sigma
			addBitFieldDataToGraph(oplotO,"dds_freq",0, 32, 1);	// DDS_FREQ
			oplotO->setXLabel("Sample");

		}
		else {
			setGraphHeights(height,8);
			addBitFieldDataToGraph(oplotA,"FREF",		7, 1, 0);
			addBitFieldDataToGraph(oplotB,"HCHANGE",	6, 1, 0);
			addBitFieldDataToGraph(oplotC,"INJECTION",	5, 1, 0);
			addBitFieldDataToGraph(oplotD,"CAL_STOP",	4, 1, 0);
			addBitFieldDataToGraph(oplotE,"CAL_START",	3, 1, 0);
			addBitFieldDataToGraph(oplotF,"CYCLE_STOP",	2, 1, 0);
			addBitFieldDataToGraph(oplotG,"CYCLE_START",	1, 1, 0);
			addBitFieldDataToGraph(oplotH,"10MHz",	0, 1, 0);
			oplotH->setXLabel("Sample");
		}		
	}
	else if (source == 1) {
		setGraphHeights(height,9);
		addBitFieldDataToGraph(oplotA,"FREF", 63, 1, 0);	     // FREF
		addBitFieldDataToGraph(oplotB,"PllMsb", 62, 1, 0,invert);	     // PllMsb
		addBitFieldDataToGraph(oplotC,"PhaseTableMsb", 61, 1, 0,invert);	 // PhaseTableMSB
		addBitFieldDataToGraph(oplotD,"LO_pulse1", 60, 1, 0);       // LO_pulse1
		addBitFieldDataToGraph(oplotE,"sigma", 57, 3, 1),	   // Sigma
		addBitFieldDataToGraph(oplotF,"LO_pulse2", 56, 1, 0);       // LO_pulse2
		addBitFieldDataToGraph(oplotG,"mult_out1", 38,14, 1);       // mult_out1
		addBitFieldDataToGraph(oplotH,"mult_out2", 24, 14, 1);      // mult_out2
		addBitFieldDataToGraph(oplotI,"f_error", 0, 24, 1);	    // f_error
		oplotI->setXLabel("Sample");
	}

	else if (source == 2) {
		setGraphHeights(height,9);
		addBitFieldDataToGraph(oplotA,"FREF",63, 1, 0); 	
		addBitFieldDataToGraph(oplotB,"PllMsb",62, 1, 0,invert);
		addBitFieldDataToGraph(oplotC,"PhaseTableMsb",61, 1, 0,invert);
		addBitFieldDataToGraph(oplotD,"LO1",60, 1, 0);
		addBitFieldDataToGraph(oplotE,"sigma",57, 3, 1);
		addBitFieldDataToGraph(oplotF,"LO2",56, 1, 0);
		addBitFieldDataToGraph(oplotG,"Gate",55, 1, 0);
		addBitFieldDataToGraph(oplotH,"B0",32,23, 1);
		addBitFieldDataToGraph(oplotI,"Result0",0,32, 1);
		oplotI->setXLabel("Sample");
	}
	
	else if (source == 3) {
		setGraphHeights(height,6);
		addBitFieldDataToGraph(oplotA,"Sigma", 48,14, 1);	  
		addBitFieldDataToGraph(oplotB,"Gate", 62, 1, 0);	  
		addBitFieldDataToGraph(oplotC,"BLR", 63, 1, 0);	
		addBitFieldDataToGraph(oplotD,"X0", 0,16, 0);    
		addBitFieldDataToGraph(oplotE,"E0", 16,16, 0),  
		addBitFieldDataToGraph(oplotF,"Y0", 32,16, 1); 
		oplotF->setXLabel("Sample");
	}
	else  {
		BError	err;
		err.set(1,"Unsupported source type");
		warningDialog("Viewing Pupe Diagnostics",err);
	}
	ographControls->setMax(odata.size());
}


void	PupeDiagnosticsWin::setGraphHeights(int max,int num) {
	BIter	i;
	int	ledgend = 30;
	int	height = (max - ledgend)/num;
	int	n = 1;
	
	for (oplots.start(i);! oplots.isEnd(i);oplots.next(i)) {
		oplots[i]->show();
		if (n < num) {
			oplots[i]->setMaximumHeight(height);
			oplots[i]->setMinimumHeight(height);
		}
		else if ( n == num) {
			oplots[i]->setMinimumHeight(height + ledgend);
		}
		else {
			oplots[i]->hide();
			oplots[i]->setMinimumHeight(height);
		}
		n++;
	}
}


void	PupeDiagnosticsWin::saveFile() {
	BError		err;
	unsigned int	n;
	BString		fname;
	BFile		file;
	
	
	fname.printf("pupeSource%d.txt",(int)osource->getValue());
	if (file.open(fname,"w+")) {
		warningDialog("PupeDiagnostics",BString("Cannot open save file (") + fname + ")");
		return;
	}
	
	if (osource->getValue() == 0) {
		if (okst->isChecked())
			file.printf("FrefIn PllMsb PhaseTableMsb Lo1 Sigma Lo2 Gate Blr Mean1 Mean2 RfSelect1 RfSelect2 SelFilter SwitchState DdsFreq\n");
		for (n = 0;n < odata.size();n++) {
			file.printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
				getBitValue(odata[n], 63, 1, 0),		// FREF
				!getBitValue(odata[n], 62, 1, 0),	// PllMsb (FRef Reference)
				!getBitValue(odata[n], 61, 1, 0),		// PhaseTableMsb (Local FREF)
				getBitValue(odata[n], 60, 1, 0),		// LO1
				getBitValue(odata[n], 32, 12, 1),	// Sigma (12 bits)
				getBitValue(odata[n], 56, 1, 0),		// LO2
				getBitValue(odata[n], 49, 1, 0),		// Gate
				getBitValue(odata[n], 48, 1, 0),		// Blr
				getBitValue(odata[n], 50, 1, 0),		// Mean1
				getBitValue(odata[n], 51, 1, 0),		// Mean2
				getBitValue(odata[n], 52, 1, 0),		// RfSelect1
				getBitValue(odata[n], 53, 1, 0),		// RfSelect2
				getBitValue(odata[n], 54, 1, 0),		// SelFilter
				getBitValue(odata[n], 44, 4, 0),		// SwitchState
				getBitValue(odata[n], 0, 32, 1)		// DDS_FREQ
				);
		}	
	}
	else if (osource->getValue() == 1) {
		if (okst->isChecked())
			file.printf("FrefIn PllMsb PhaseTableMsb Lo1 Sigma Lo2 MultOut1 MultOut2 F_Error\n");
		for(n = 0; n < odata.size(); n++){

			file.printf("%d %d %d %d %d %d %d %d %d\n",
				getBitValue(odata[n], 63, 1, 0),		// FREF
				!getBitValue(odata[n], 62, 1, 0),	// PllMsb (FRef Reference)
				!getBitValue(odata[n], 61, 1, 0),	// PhaseTableMsb (Local FREF)
				getBitValue(odata[n], 60, 1, 0),		// LO1
				getBitValue(odata[n], 57, 3, 1),		// Sigma (3 bits)
				getBitValue(odata[n], 56, 1, 0),		// LO2

				getBitValue(odata[n], 38, 14, 1),	// mult_out1
				getBitValue(odata[n], 24, 14, 1),	// mult_out2
				getBitValue(odata[n], 0, 24, 1)		// f_error
				);
		}		
	}
	else if (osource->getValue() == 2) {
		if (okst->isChecked())
			file.printf("FrefIn PllMsb PhaseTableMsb Lo1 Sigma Lo2 Gate B0 Result0\n");
		for(n = 0; n < odata.size(); n++){

			file.printf("%d %d %d %d %d %d %d %d %d\n",
				getBitValue(odata[n], 63, 1, 0),		// FREF
				!getBitValue(odata[n], 62, 1, 0),	// PllMsb (FRef Reference)
				!getBitValue(odata[n], 61, 1, 0),	// PhaseTableMsb (Local FREF)
				getBitValue(odata[n], 60, 1, 0),		// LO1
				getBitValue(odata[n], 57, 3, 1),		// Sigma (3 bits)
				getBitValue(odata[n], 56, 1, 0),		// LO2

				getBitValue(odata[n], 55, 1, 0),		// Gate
				getBitValue(odata[n], 32, 23, 1),	// B0
				getBitValue(odata[n], 0, 32, 1)		// Result0
				);
		}
	}
	else if (osource->getValue() == 3) {
		if (okst->isChecked())
			file.printf("Sigma Gate Blr X0 E0 Y0\n");
		for(n = 0; n < odata.size(); n++){

			file.printf("%d %d %d %d %d %d %d\n",
				getBitValue(odata[n], 48, 14, 1),	// Sigma
				getBitValue(odata[n], 62, 1, 0),		// Gate
				getBitValue(odata[n], 63, 1, 0),		// Blr
				getBitValue(odata[n], 0, 16, 1),		// X0
				getBitValue(odata[n], 16, 16, 1),	// E0
				getBitValue(odata[n], 32, 16, 1)		// Y0
				);
		}
	}
	else {
		err.set(1,"Unknown pupe source type");
		warningDialog("Attempting file Save",err);
	}
	file.close();
	BString msg;
	msg =  BString("File Saved (") + fname + ")";
	gstatusbar->message(msg.retStr(),2000);
}	
	

void	PupeDiagnosticsWin::warningDialog(BString title, BError err){
	BString	m;
	
	m = BString("<h5>") + title + "</h5><p>" + err.getString() + "</p>";
	QMessageBox::warning(this, "Warning", m.retStr());
}

// Utility routines


void	PupeDiagnosticsWin::addBitFieldDataToGraph(BGraph* g,BString title,int startBit, int nBits, int s,int invert) {
	BArray<float>	data;
	unsigned int	n;
	float		f;
	
	data.resize(odata.size());

	for(n = 0; n < odata.size(); n++){
		f = getBitValue(odata[n], startBit, nBits, s);
		if (invert)
			data[n] = ! f;
		else
			data[n] = f;
				
	}
	g->setData(data);
	g->setYLabel(title);
}

bool PupeDiagnosticsWin::eventFilter( QObject *o, QEvent *e )
    {
        if ( e->type() == QEvent::Wheel ) {
        	QWheelEvent*	we = (QWheelEvent*)e;
	    
	    	int	delta = (we->delta() > 0) ? -1 : 1;
		ographControls->scroll(delta);
            return TRUE; 
        } else {
            // standard event processing
            return FALSE;
        }
    }