//vcDP5 SDK #include "stdafx.h" #include ".\tuneslowthreshold.h" #include "DppSlowThreshold.h" #include "SCommon.h" #include "UserMsgs.h" CTuneSlowThreshold::CTuneSlowThreshold(void) { } CTuneSlowThreshold::~CTuneSlowThreshold(void) { } void CTuneSlowThreshold::GetSpectrumData(double * dblMCAData, long * lngMCAData, int mcaCH) { for (int chan=0; chan < mcaCH; chan++) dblMCAData[chan] = lngMCAData[chan]; } void CTuneSlowThreshold::SetSlowThreshToChPos(int ChPosIn, int mcaCH) { CDppSlowThreshold SlowThreshNew; CString strChPos; int ChPos; double FSReal; ChPos = ChPosIn; if (ChPos == 0) ChPos = 1; SlowThreshNew.SetGain(mcaCH); SlowThreshNew.GetSlowThresholdSettingFromChannels((double)ChPos); s.DppConfig.SlowChThreshold = SlowThreshNew.GetSlowChThreshold(); FSReal = (double)s.DppConfig.SlowChThreshold / 1024.0; s.DppConfig.SlowThresholdPct = FSReal; if (s.isDppConnected) { s.HwCfgDP5Out.Format("THSL=%.2f;",(FSReal * 100.0)); s.HwCfgReady = TRUE; ::PostMessage(s.hWnd_Main,WM_USER_SEND_CONFIG_TO_HW,0L,0L); } } int CTuneSlowThreshold::GetSlowThreshCalStart(double * SpectrumBuffer, double UpperChMax, int mcaCH) { int MinValidChan = 40; int MaxValidChan = 245; int LastSlwCh = (int)(mcaCH * 0.24); int StartCh = LastSlwCh; CString strStatus; //DisplaySpecBuf25(SpectrumBuffer); for (int i = LastSlwCh; i >= 0; i--) { if (SpectrumBuffer[i] <= UpperChMax) { if (i < StartCh) StartCh = i; strStatus.Format(" Finding Slow Threshold Test Channel %d ...", i); } else { break; } } if ((StartCh > 1) && (StartCh < LastSlwCh - 1)) StartCh++; // remove the last (bad) ch // try to tweak 3x if really bad if ((SpectrumBuffer[StartCh] > UpperChMax) && (StartCh < LastSlwCh - 1)) { StartCh++; } if ((SpectrumBuffer[StartCh] > UpperChMax) && (StartCh < LastSlwCh - 1)) { StartCh++; } if ((SpectrumBuffer[StartCh] > UpperChMax) && (StartCh < LastSlwCh - 1)) { StartCh++; } MinValidChan = GetSlowThreshChStrFromVal(6, mcaCH); if (StartCh < MinValidChan) { // set to >= 0.5% FS if no slow thresh needed StartCh = MinValidChan; } MaxValidChan = GetSlowThreshChStrFromVal(245, mcaCH); if (StartCh > MaxValidChan) { // set to >= 24.02% FS if no slow thresh needed StartCh = MaxValidChan; AfxMessageBox("Slow threshold value too high. \n Please check your settings.", MB_TOPMOST | MB_OK | MB_ICONSTOP); } return StartCh; } double CTuneSlowThreshold::GetUpperChCounts(double * SpectrumBuffer, int mcaCH) { int LastSlwCh = (int)(mcaCH * 0.24); double ChCounts = 0; double CurChCount = 0; for (int i = LastSlwCh; i < mcaCH; i++) { CurChCount = SpectrumBuffer[i]; if (CurChCount > 0) { ChCounts += CurChCount; } } return ChCounts; } double CTuneSlowThreshold::GetUpperChMax(double * SpectrumBuffer, int mcaCH) { int LastSlwCh = (int)(mcaCH * 0.24); double ChCount = 0; double CurChCount = 0; for (int i = LastSlwCh; i < mcaCH; i++) { CurChCount = SpectrumBuffer[i]; if (CurChCount > ChCount) ChCount = CurChCount; } return ChCount; } void CTuneSlowThreshold::DelayTask(DWORD d) { MSG msg; d += GetCurrentTime(); while (GetCurrentTime() < d) { if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } } } int CTuneSlowThreshold::UpdatePlotCursorPosFromIntChPos(int CurCh, double dblPixPerChan) { double dblPixPos; double dblChanPerPix; if (dblPixPerChan > 0.0) { dblChanPerPix = 1.0 / dblPixPerChan; dblPixPos = (double)CurCh * dblChanPerPix; int dpos = (int)dblPixPos; return dpos; } else { return CurCh; } } void CTuneSlowThreshold::DisplaySpecBuf25(double * SpectrumBuffer) { CString strBuffer(""); CString strChan(""); for (int i=0;i<25;i++) { strChan.Format("%f\n",SpectrumBuffer[i]); strBuffer += strChan; } AfxMessageBox(strBuffer); } ////@mfunc Get Slow Threshold display text string from Slow Threshold value. ////@parm Slow Threshold value ////@rdesc Slow Threshold display text string int CTuneSlowThreshold::GetSlowThreshChStrFromVal(unsigned char byteSlowThres, int mcaCH) { long intSlowThresh; double GainChannels; GainChannels = (double)mcaCH; intSlowThresh = (int)(((double)byteSlowThres * GainChannels / 1024.0) + 0.5); return intSlowThresh; }