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

#include <PupeSimulateWin.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qvgroupbox.h>
#include <qmessagebox.h>
#include <qlabel.h>
#include <qfiledialog.h>
#include <qcheckbox.h>
#include <BFile.h>
#include <qgroupbox.h>
#include <qvbox.h>
#include <Globals.h>
#include <qgrid.h>
#include <TmsLib.h>

using namespace Tms;

PupeSimulateWin::PupeSimulateWin(QWidget* parent,Control& c) : ocontrol(c)  {
	QGridLayout*	grid;
	QLabel*		contentsLabel;
	QButton*	fileSelectButton;
	QGroupBox*	internalTimings;
	QGroupBox*	cycleType;
	QGroupBox*	controls;
	QWidget*	w;
	QGridLayout* 	v;
	BString		s;
	int		row = 0;
	
	grid = new QGridLayout(this,5,3);
        grid->setMargin(20);
        grid->setSpacing(20);

	s = "<p><b>Pupe Simulation<b></p>";
	s += "<p>Enter specifications for the test data of a Pupe channel</p>";

	contentsLabel 		= new QLabel(s.retStr(),this);
		
	QVGroupBox * 	testRam	= new QVGroupBox("Pupe Pickup Test Data",this,"testRam");
			w 	= new QWidget(testRam,"testRamW");
			v 	= new QGridLayout(w);
	v->setSpacing(10);
	row = 0;
	v->addWidget(new QLabel("Channel Number",w),row,0);
	v->addWidget(ochannelTestRam	= new QSpinBox(1,60,1,w,""),row,1);
	row++;
	v->addWidget(new QLabel("Test Data file",w),row,0);
	v->addMultiCellWidget(osimulationRamFilename 	= new QLineEdit(w,"ramfilename"),row,row,1,2);
	row++;
	v->addWidget(fileSelectButton 	= new QPushButton("Select",w,""),1,3);	
	v->addWidget(oloadSimulationRam = new QPushButton("Load Test Data",w,""),2,1);
	v->addWidget(oclearSimulationRam = new QPushButton("Clear Test Data",w,""),2,2);
	oloadSimulationRam->setMaximumWidth(200);
	oclearSimulationRam->setMaximumWidth(200);


	internalTimings 	= new QVGroupBox("Internal Timing Control",this,"internalTimings");
			w 	= new QWidget(internalTimings,"internalTimingsW");
		 	v 	= new QGridLayout(w);
	v->setSpacing(10);
	row = 0;
	v->addWidget(new QLabel("Channel Number",w),row,0);
	v->addWidget(ochannelTiming	= new QSpinBox(1,60,1,w,""),row,1);
	row++;

	v->addMultiCellWidget(ouseInternalAdcClockRef = new QCheckBox("Use Internal Adc Clock Ref",w,""),row,row,0,1); 
	v->addMultiCellWidget(odisableBlr = new QCheckBox("Disable Blr",w,""),row,row,2,3); 
	row++;
	row++;

	v->addWidget(oFref       = new QCheckBox("Fref",w,""),row,0);
	v->addWidget(oHChange    = new QCheckBox("H Change",w,""),row,1);
	v->addWidget(oInjection  = new QCheckBox("Injection",w,""),row,2);
	v->addWidget(oCalStop    = new QCheckBox("Cal Stop",w,""),row,3);
	row++;
	v->addWidget(oCalStart   = new QCheckBox("Cal Start",w,""),row,0);
	v->addWidget(oCycleStop  = new QCheckBox("Cycle Stop",w,""),row,1);
	v->addWidget(oCycleStart = new QCheckBox("Cycle Start",w,""),row,2);
	v->addWidget(oClock      = new QCheckBox("Clock",w,""),row,3);
	row++;
	oiApply = new QPushButton("Apply",internalTimings,"");
	oiApply->setMaximumWidth(200);


	cycleType 	= new QVGroupBox("Cycle Type",this,"cycleType");
			w 	= new QWidget(cycleType,"cycleTypeW");
		 	v 	= new QGridLayout(w);
	v->setSpacing(10);
	row = 0;
	v->addWidget(ocycleType = new BQComboBox(w),row,0);
	v->addWidget(new QWidget(w,"dummy"),row,1);
	row++;
	v->addWidget(oapplyCycleType = new QPushButton("Apply",w),row,0);
	oapplyCycleType->setMaximumWidth(200);
	
	
	controls 	= new QVGroupBox("Server Control",this,"serverControl");
			w 	= new QWidget(controls,"controlsW");
		 	v 	= new QGridLayout(w);
	
	v->addWidget(oInitialise = new QPushButton("Initialise TMS",w),row,0);
	v->addWidget(new QWidget(w,"dummy"),row,1);
	
	oInitialise->setMaximumWidth(200);
	
	ochannelTestRam->setWrapping(true);	
	ochannelTiming->setWrapping(true);	
	
	row = 0;
	grid->addMultiCellWidget(contentsLabel,row,row,0,2);		row += 1;
	grid->addMultiCellWidget(testRam,row,row+1,0,2);		row += 2;
	grid->addMultiCellWidget(internalTimings,row,row + 1,0,2); 	row += 2;
	grid->addMultiCellWidget(cycleType,row,row +1,0,2);	 	row += 2;
	grid->addMultiCellWidget(controls,row,row,0,2);	 	row++;

	connect(fileSelectButton,SIGNAL(clicked()),this,SLOT(selectFile()));
	connect(oloadSimulationRam,SIGNAL(clicked()),this,SLOT(loadRam()));
	connect(oclearSimulationRam,SIGNAL(clicked()),this,SLOT(clearRam()));
	connect(oInitialise,SIGNAL(clicked()),this,SLOT(initialiseTMS()));
	connect(oiApply,SIGNAL(clicked()),this,SLOT(applyInternalTimings()));
	connect(oapplyCycleType,SIGNAL(clicked()),this,SLOT(applyCycleType()));
	connect(ochannelTiming,SIGNAL(valueChanged(int)),this,SLOT(updateTimingMask()));
	connect(&ocontrol,SIGNAL(newConnection()),this,SLOT(update()));
}
	
PupeSimulateWin::~PupeSimulateWin() {}



void PupeSimulateWin::selectFile() {
    QString s = QFileDialog::getOpenFileName(
                    "/usr/tms/data",
                    "Tms Channel Test Data File (*.psd)",
                    this,
                    "open file dialog",
                    "Choose a file" );
	osimulationRamFilename->setText(s);
}
	
void PupeSimulateWin::show() {
	QWidget::show();
	update();
}	


void PupeSimulateWin::update() {
	if (! isVisible())
		return;
	if (updateMaxChannels())
		return;
	if (updateTimingMask())
		return;
	if (updateControlList())
		return;
}


BError	PupeSimulateWin::updateMaxChannels() {
	BError	err;
	Tms::ConfigInfo	info;

	if (err = ocontrol.getConfiguration(info)) {
		return err;
	}
	ochannelTestRam->setMaxValue(info.puReferences.size());
	ochannelTiming->setMaxValue(info.puReferences.size());
	return err;
}

BError PupeSimulateWin::updateTimingMask() {
	BError	err;
	Tms::PuChannel		puChannel;
	Tms::PupeConfig		pupeConfig;

	if(err = ocontrol.getPuChannel(ochannelTiming->value(), puChannel)){
		warningDialog("Pupe Update Timing Mask - ",err);
		return err;
	}
	if(err = ocontrol.getPupeConfig(puChannel, pupeConfig)){
		warningDialog("Pupe Update Timing Mask - ",err);
		return err;
	}
	UInt32	mask = pupeConfig.internalTimingMask;
	oClock->setChecked(mask & TimingSigClock);
	oCycleStart->setChecked(mask & TimingSigCycleStart);
	oCycleStop->setChecked(mask & TimingSigCycleStop);
	oCalStart->setChecked(mask &  TimingSigCalStart);
	oCalStop->setChecked(mask & TimingSigCalStop);
	oInjection->setChecked(mask & TimingSigInjection);
	oHChange->setChecked(mask & TimingSigHChange);
	oFref->setChecked(mask & TimingSigFRef);
	return err;
}


BError PupeSimulateWin::updateControlList() {
	BError		err;
	BIter		i;
	int 		n = 0;
	UInt32 		cycleNumber;
	BString		cycleType;
	BList<CycleParamItem> l;
	
	if (err = ocontrol.getControlList(l)) {
		warningDialog("Pupe Get Control List - ",err);
		return err;
	}
	if (err = ocontrol.getCycleInfo(cycleNumber,cycleType)) {
		warningDialog("Pupe Get Control Info -",err);
		return err;
	}

	ocycleType->clear();
	for (l.start(i);! l.isEnd(i);l.next(i)) {
		ocycleType->insertItem(l[i].cycleType.retStr());
		if (l[i].cycleType == cycleType) {
			ocycleType->setCurrentItem(n);
		}	
		n++;	
	}
	return err;
}



void PupeSimulateWin::loadRam() {
	BError			err;
	BFile			f;
	BString			fname;
	Tms::PuChannel		puChannel;
	BArray< UInt32 >   	 data;
	int			nb;
	
	fname = osimulationRamFilename->text().ascii();

	if(err = ocontrol.getPuChannel(ochannelTestRam->value(), puChannel)){
		warningDialog("Pupe Test Data - Loading",err);
		return;
	}
	if (err = f.open(fname,"r")) {
		warningDialog("Pupe Test Data - Loading",err);
		return;
	}
	data.resize(1024);
	if((nb = f.read(&data[0], 1024 * sizeof(UInt32))) <= 0){
		err.set(1, BString("Error: Reading 1024 32bit data items from file: ") + fname);
		warningDialog("Pupe Test Data",err);
		return;		
	}
	data.resize(nb / sizeof(UInt32));
	if (err = ocontrol.setTestData(puChannel,true,data)) {
		warningDialog("Pupe Test Data -  Loading",err);
		return;
	}
	gstatusbar->message("Ram Loaded",2000);
}

void PupeSimulateWin::initialiseTMS() {
	BError	err;

	if (err = ocontrol.initialiseServer()) {
		warningDialog("Initialising Server",err);
		return;
	}
	gstatusbar->message("TMS initialised",2000);
}

void PupeSimulateWin::applyInternalTimings() {
	BError	err;
	Tms::PuChannel		puChannel;
	Tms::PupeConfig		pupeConfig;

	pupeConfig.internalTimingMask = 
		oClock->isChecked() << 0 |       
		oCycleStart->isChecked() << 1 |       
		oCycleStop->isChecked() << 2 |       
		oCalStart->isChecked() << 3 |       
		oCalStop->isChecked() << 4 |       
		oInjection->isChecked() << 5 |       
		oHChange->isChecked() << 6 |       
		oFref->isChecked() << 7;


	pupeConfig.internalAdcClockRef = ouseInternalAdcClockRef->isChecked();
	pupeConfig.disableBlr = odisableBlr->isChecked();

	if(err = ocontrol.getPuChannel(ochannelTiming->value(), puChannel)){
		warningDialog("Pupe Apply Timings - ",err);
		return;
	}

	if(err = ocontrol.setPupeConfig(puChannel, pupeConfig)){
		warningDialog("Pupe Apply Timings - ",err);
		return;
	}
	gstatusbar->message("Internal Timings Applied",2000);
}


void PupeSimulateWin::applyCycleType() {
	BError 	err;
	BString	ct;
	BString msg;
	UInt32	cycleNumber;
	BString	cycleType;
	
	// Get current cycle info
	if(err = ocontrol.getCycleInfo(cycleNumber,cycleType)){
		warningDialog("Setting Cycle Type - GetCycleInfo",err);
	return;
		}
		
	cycleType = ocycleType->currentText().ascii();	
	cycleNumber++;

	// Set NextCycle type
	if(err = ocontrol.setNextCycle(cycleNumber, cycleType)){
		warningDialog("Setting Cycle Type - SetNextCycle",err);
		return;
	}
	msg.printf("Next CycleType (%s)\n",cycleType.retStr()); 
	gstatusbar->message(msg.retStr(),2000);
}



void	PupeSimulateWin::clearRam() {
	BError			err;
	Tms::PuChannel		puChannel;
	BArray< UInt32 >   	data;

	if(err = ocontrol.getPuChannel(ochannelTestRam->value(), puChannel)){
		warningDialog("Pupe Test Data - Clear",err);
		return;
	}
	if(err = ocontrol.setTestData(puChannel, 0, data)){
		warningDialog("Pupe Test Data- Clear",err);
		return;
	}
	gstatusbar->message("Ram Cleared",2000);
}

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