CLSSIS3820Scaler::CLSSIS3820Scaler(const QString &baseName, QObject *parent) :
	QObject(parent)
{
	connectedOnce_ = false;
	switchingReadModes_ = false;
	triggerSourceTriggered_ = false;

	triggerChannelMapper_ = new QSignalMapper(this);

	triggerSource_ = new AMDetectorTriggerSource(QString("%1TriggerSource").arg(baseName), this);
	connect(triggerSource_, SIGNAL(triggered(AMDetectorDefinitions::ReadMode)), this, SLOT(onTriggerSourceTriggered(AMDetectorDefinitions::ReadMode)));

	dwellTimeSource_ = new AMDetectorDwellTimeSource(QString("%1DwellTimeSource").arg(baseName), this);
	connect(dwellTimeSource_, SIGNAL(setDwellTime(double)), this, SLOT(onDwellTimeSourceSetDwellTime(double)));

	synchronizedDwellKey_ = QString("%1:startScan NPP NMS").arg(baseName);

	CLSSIS3820ScalerChannel *tmpChannel;
	for (int x = 0; x < 32; x++) {
		tmpChannel = new CLSSIS3820ScalerChannel(baseName, x, this);
		scalerChannels_.append(tmpChannel);
		connect(tmpChannel, SIGNAL(connected(bool)), this, SLOT(onConnectedChanged()));
		connect(tmpChannel, SIGNAL(sensitivityChanged()), this, SIGNAL(sensitivityChanged()));
		connect(tmpChannel, SIGNAL(readingChanged(int)), triggerChannelMapper_, SLOT(map()));
	}

	startToggle_ = new AMPVControl("Start/Scanning", baseName+":startScan", baseName+":startScan", QString(), this, 0.1);
	dwellTime_ = new AMPVControl("DwellTime", baseName+":delay", baseName+":delay", QString(), this, 1);
	scanPerBuffer_ = new AMPVControl("ScanPerBuffer", baseName+":nscan", baseName+":nscan", QString(), this, 0.5);
	totalScans_ = new AMPVControl("TotalScans", baseName+":scanCount", baseName+":scanCount", QString(), this, 0.5);

	continuousToggle_ = new CLSSIS3820ScalerModeControl("Mode", QString(), this, "SIS3820 Scaler Mode Control");
	continuousToggle_->setScanCountControl(totalScans_);
	continuousToggle_->setNumberOfScansPerBufferControl(scanPerBuffer_);
	continuousToggle_->setStartScanControl(startToggle_);

	reading_ = new AMReadOnlyPVControl("Reading", baseName+":scan", this);

	dwellTime_->setAttemptMoveWhenWithinTolerance(false);

	allControls_ = new AMControlSet(this);
	allControls_->addControl(startToggle_);
	allControls_->addControl(continuousToggle_);
	allControls_->addControl(dwellTime_);
	allControls_->addControl(scanPerBuffer_);
	allControls_->addControl(totalScans_);
	allControls_->addControl(reading_);

	connect(startToggle_, SIGNAL(valueChanged(double)), this, SLOT(onScanningToggleChanged()));
	connect(continuousToggle_, SIGNAL(valueChanged(double)), this, SLOT(onContinuousToggleChanged()));
	connect(dwellTime_, SIGNAL(valueChanged(double)), this, SLOT(onDwellTimeChanged(double)));
	connect(scanPerBuffer_, SIGNAL(valueChanged(double)), this, SLOT(onScanPerBufferChanged(double)));
	connect(totalScans_, SIGNAL(valueChanged(double)), this, SLOT(onTotalScansChanged(double)));
	/*
	connect(reading_, SIGNAL(valueChanged(double)), this, SIGNAL(readingChanged()));
	*/
	connect(reading_, SIGNAL(valueChanged(double)), this, SLOT(onReadingChanged(double)));
	connect(allControls_, SIGNAL(connected(bool)), this, SLOT(onConnectedChanged()));
}
REIXSXESMCPDetector::REIXSXESMCPDetector(QObject *parent) :
	AMDetector("XESMCPDetector", "REIXS XES MCP Detector", parent)
{
	dwellTime_ = -1;
	totalCountTarget_ = -1;
	finishedConditions_ = REIXSXESMCPDetector::FinishedTotalTime;
	clearOnStart_ = true;
	dwellTimeTimer_ = 0; //NULL

	basePVName_ = "CPD1610-01";

	totalCountsControl_ = new AMReadOnlyPVControl("MCPTotalCounts", QString("%1:totalCounts").arg(basePVName_), this);
	countsPerSecondControl_ = new AMReadOnlyPVControl("MCPCountsPerSecond", QString("%1:countsPerSecond").arg(basePVName_), this);
	imageControl_ = new AMReadOnlyPVControl("MCPImage", QString("%1:image").arg(basePVName_), this);
	instantaneousImageControl_ = new AMReadOnlyPVControl("MCPInstantaneousImage", QString("%1:instantaneousImage").arg(basePVName_), this);
	resolutionXControl_ = new AMReadOnlyPVControl("MCPXResolution", QString("%1:resolutionX").arg(basePVName_), this);
	resolutionYControl_ = new AMReadOnlyPVControl("MCPYResolution", QString("%1:resolutionY").arg(basePVName_), this);

	clearControl_ = new AMSinglePVControl("MCPClear", QString("%1:clear").arg(basePVName_), this, 0.5);
	averagingPeriodSecsControl_ = new AMSinglePVControl("mcpAveragingPeriod", QString("%1:averagingPeriodSecs").arg(basePVName_), this, 0.1);
	persistTimeSecsControl_ = new AMSinglePVControl("mcpPersistDuration", QString("%1:persistTimeSecs").arg(basePVName_), this, 0.1);

	image_ = new REIXSXESMCPDataSource("xesImage", imageControl_, resolutionXControl_, resolutionYControl_, this);
	image_->setDescription("Accumulated Detector Image");
	instantaneousImage_ = new REIXSXESMCPDataSource("xesRealtimeImage", instantaneousImageControl_, resolutionXControl_, resolutionYControl_, this);
	instantaneousImage_->setDescription("Instantaneous Detector Image");

	vetoControl_ = new AMSinglePVControl("Veto", "PDTR1610-4-I21-01:OprVeto", this, 0.5);
	vetoStateControl_ = new AMReadOnlyPVControl("VetoState", "PDTR1610-4-I21-01:Veto:state", this);

	allControls_ = new AMControlSet(this);
	allControls_->addControl(totalCountsControl_);
	allControls_->addControl(countsPerSecondControl_);
	allControls_->addControl(imageControl_);
	allControls_->addControl(instantaneousImageControl_);
	allControls_->addControl(resolutionXControl_);
	allControls_->addControl(resolutionYControl_);

	allControls_->addControl(clearControl_);
	allControls_->addControl(averagingPeriodSecsControl_);
	allControls_->addControl(persistTimeSecsControl_);
	allControls_->addControl(vetoControl_);
	allControls_->addControl(vetoStateControl_);

	triggerSource_ = new AMDetectorTriggerSource(QString("REIXSXESMCPTriggerSource"), this);
	dwellTimeSource_ = new AMDetectorDwellTimeSource(QString("REIXSXESMCPDwellTimeSource"), this);

	connect(triggerSource_, SIGNAL(triggered(AMDetectorDefinitions::ReadMode)), this, SLOT(onTriggerSourceTriggered(AMDetectorDefinitions::ReadMode)));
	connect(dwellTimeSource_, SIGNAL(setDwellTime(double)), this, SLOT(onDwellTimeSourceSetDwellTime(double)));

	connect(allControls_, SIGNAL(connected(bool)), this, SLOT(onControlsConnected(bool)));
	connect(allControls_, SIGNAL(controlConnectedChanged(bool,AMControl*)), this, SLOT(onControlsTimedOut()));

	connect(imageControl_->readPV(), SIGNAL(valueChanged()), this, SIGNAL(imageDataChanged()));
	connect(instantaneousImageControl_->readPV(), SIGNAL(valueChanged()), this, SIGNAL(instantaneousImageDataChanged()));
	connect(countsPerSecondControl_, SIGNAL(valueChanged(double)), this, SIGNAL(countsPerSecondChanged(double)));
	connect(totalCountsControl_, SIGNAL(valueChanged(double)), this, SIGNAL(totalCountsChanged(double)));
}