-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathx123.cpp
More file actions
259 lines (224 loc) · 9.52 KB
/
x123.cpp
File metadata and controls
259 lines (224 loc) · 9.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "x123.h"
#include <iostream>
#include <stdio.h>
using namespace std ;
X123::X123(QObject *parent) :
QObject(parent)
{
chdpp = new CConsoleHelper () ;
haveSpec = false ;
bRunSpectrumTest = false ;
bRunConfigurationTest = false ;
bHaveStatusResponse = false ;
bHaveConfigFromHW = false ;
nptsSpec =2048 ;
specData = new long [nptsSpec] ;
for (int i =0 ; i<nptsSpec; i++) {
specData[i] = 0 ;
}
curSecs = 20 ;
}
X123::~X123 (){
DisconnectUSB() ;
delete [] specData ;
}
bool X123::ConnectUSB () {
cout << endl;
cout << "Running DPP LibUsb tests from console..." << endl;
cout << endl;
cout << "\tConnecting to default LibUsb device..." << endl;
if (chdpp->LibUsb_Connect_Default_DPP()) {
cout << "\t\tLibUsb DPP device connected." << endl;
cout << "\t\tLibUsb DPP devices present: " << chdpp->LibUsb_NumDevices << endl;
haveSpec = true ;
} else {
cout << "\t\tLibUsb DPP device not connected." << endl;
cout << "\t\tNo LibUsb DPP device present." << endl;
haveSpec = false ;
}
return (haveSpec) ;
}
void X123::GetDppStatus () {
if (chdpp->LibUsb_isConnected) { // send and receive status
cout << endl;
cout << "\tRequesting Status..." << endl;
if (chdpp->LibUsb_SendCommand(XMTPT_SEND_STATUS)) { // request status
cout << "\t\tStatus sent." << endl;
cout << "\t\tReceiving status..." << endl;
if (chdpp->LibUsb_ReceiveData()) {
cout << "\t\t\tStatus received..." << endl;
cout << chdpp->DppStatusString << endl;
bRunSpectrumTest = true;
bHaveStatusResponse = true;
bRunConfigurationTest = true;
} else {
cout << "\t\tError receiving status." << endl;
}
} else {
cout << "\t\tError sending status." << endl;
}
}
//this->SendPresetAcquisitionTime(20) ;
}
void X123::DisconnectUSB () {
if (chdpp->LibUsb_isConnected) { // send and receive status
cout << endl;
cout << "\tClosing connection to default LibUsb device..." << endl;
chdpp->LibUsb_Close_Connection();
cout << "\t\tDPP device connection closed." << endl;
}
return ;
}
bool X123::ClearSpectrum () {
cout << "\tRunning spectrum test..." << endl;
cout << "\t\tDisabling MCA for spectrum data/status clear." << endl;
chdpp->LibUsb_SendCommand(XMTPT_DISABLE_MCA_MCS);
Sleep(1000);
cout << "\t\tClearing spectrum data/status." << endl;
chdpp->LibUsb_SendCommand(XMTPT_SEND_CLEAR_SPECTRUM_STATUS);
Sleep(1000);
cout << "\t\tEnabling MCA for spectrum data acquisition with status ." << endl;
return (true) ;
}
bool X123::ReadConfigFile (char *cfile) {
FILE *cf = fopen (cfile,"r") ;
if (cf==NULL) {
cout << "Could not find config file : " << cfile<<endl ;
fclose (cf) ;
return false ;
}
fclose (cf) ;
std::string strCfg ;
strCfg = chdpp->SndCmd.GetDP5CfgStr (cfile) ;
cout << "\t\t\tConfiguration Length: " << (unsigned int)strCfg.length() << endl;
cout << "\t================================================================" << endl;
cout << strCfg << endl;
cout << "\t================================================================" << endl;
return true ;
}
bool X123::SendPresetAcquisitionTime(int sec)
{
curSecs = sec ;
CONFIG_OPTIONS CfgOptions;
char str[20] ;
sprintf (str,"PRET=%d;", sec) ;
string strPRET (str) ;
cout << "\tSetting Preset Acquisition Time..." << strPRET << endl;
chdpp->CreateConfigOptions(&CfgOptions, "", chdpp->DP5Stat, false);
CfgOptions.HwCfgDP5Out = strPRET ;
// send PresetAcquisitionTime string, bypass any filters, read back the mode and settings
if (chdpp->LibUsb_SendCommand_Config(XMTPT_SEND_CONFIG_PACKET_EX, CfgOptions)) {
ReadDppConfigurationFromHardware(false); // read setting back
DisplayPresets();
return true ;
// display new presets
} else {
cout << "\t\tPreset Acquisition Time NOT SET" << strPRET << endl;
return false ;
}
}
// Clears spectrum data and starts acquisition
void X123::StartAcquisition(){
int MaxMCA = curSecs / 2 + 1;
int count = 0 ;
bool bDisableMCA;
//bRunSpectrumTest = false; // disable test
if (bRunSpectrumTest) {
cout << "\tRunning spectrum test..." << endl;
cout << "\t\tDisabling MCA for spectrum data/status clear." << endl;
chdpp->LibUsb_SendCommand(XMTPT_DISABLE_MCA_MCS);
Sleep(1000);
cout << "\t\tClearing spectrum data/status." << endl;
chdpp->LibUsb_SendCommand(XMTPT_SEND_CLEAR_SPECTRUM_STATUS);
Sleep(1000);
cout << "\t\tEnabling MCA for spectrum data acquisition with status ." << endl;
chdpp->LibUsb_SendCommand(XMTPT_ENABLE_MCA_MCS);
Sleep(1000);
for(int idxSpectrum=0;idxSpectrum<MaxMCA;idxSpectrum++) {
//cout << "\t\tAcquiring spectrum data set " << (idxSpectrum+1) << " of " << MaxMCA << endl;
if (chdpp->LibUsb_SendCommand(XMTPT_SEND_SPECTRUM_STATUS)) { // request spectrum+status
if (chdpp->LibUsb_ReceiveData()) {
bDisableMCA = true; // we are aquiring data, disable mca when done
//system(CLEAR_TERM);
//chdpp->ConsoleGraph(chdpp->DP5Proto.SPECTRUM.DATA,chdpp->DP5Proto.SPECTRUM.CHANNELS,true,chdpp->DppStatusString);
for (int i=0; i<nptsSpec; i++){
specData[i] = chdpp->DP5Proto.SPECTRUM.DATA[i] ;
}
Sleep(1990);
emit (updData(2*++count)) ;
//emit(setStatus("Updating spectrum")) ;
}
}else {
cout << "\t\tProblem acquiring spectrum." << endl;
break;
}
}
// now disable MCA if we received data
if (bDisableMCA) {
////system("Pause");
//cout << "\t\tSpectrum acquisition with status done. Disabling MCA." << endl;
chdpp->LibUsb_SendCommand(XMTPT_DISABLE_MCA_MCS);
Sleep(1000);
}
}
emit(endAcquire()) ;
// Saving spectrum file
SaveSpectrumFile() ;
}
void X123::AcquireSpectrum() {
this->StartAcquisition () ;
// Saving spectrum file
SaveSpectrumFile() ;
}
void X123::DisplayPresets(){
if (bHaveConfigFromHW) {
cout << "\t\t\tPreset Mode: " << chdpp->strPresetCmd << endl;
cout << "\t\t\tPreset Settings: " << chdpp->strPresetVal << endl;
}
}
void X123::ReadDppConfigurationFromHardware(bool bDisplayCfg) {
CONFIG_OPTIONS CfgOptions;
if (bHaveStatusResponse && bRunConfigurationTest) {
//test configuration functions
// Set options for XMTPT_FULL_READ_CONFIG_PACKET
chdpp->CreateConfigOptions(&CfgOptions, "", chdpp->DP5Stat, false);
cout << endl;
cout << "\tRequesting Full Configuration..." << endl;
chdpp->ClearConfigReadFormatFlags(); // clear all flags, set flags only for specific readback properties
//chdpp->DisplayCfg = false; // DisplayCfg format overrides general readback format
chdpp->CfgReadBack = true; // requesting general readback format
if (chdpp->LibUsb_SendCommand_Config(XMTPT_FULL_READ_CONFIG_PACKET, CfgOptions)) { // request full configuration
if (chdpp->LibUsb_ReceiveData()) {
if (chdpp->HwCfgReady) { // config is ready
bHaveConfigFromHW = true;
if (bDisplayCfg) {
cout << "\t\t\tConfiguration Length: " << (unsigned int)chdpp->HwCfgDP5.length() << endl;
cout << "\t================================================================" << endl;
cout << chdpp->HwCfgDP5 << endl;
cout << "\t================================================================" << endl;
cout << "\t\t\tScroll up to see configuration settings." << endl;
cout << "\t================================================================" << endl;
} else {
cout << "\t\tFull configuration received." << endl;
}
}
}
}
}
}
void X123::SetSpectrumFile (char *ifile) {
chdpp->SetSpectrumFile (ifile) ;
}
// Saving spectrum file
void X123::SaveSpectrumFile()
{
string strSpectrum; // holds final spectrum file
chdpp->sfInfo.strSpectrumStatus = chdpp->DppStatusString; // save last status after acquisition
chdpp->sfInfo.m_iNumChan = chdpp->mcaCH; // number channels in spectrum
chdpp->sfInfo.SerialNumber = chdpp->DP5Stat.m_DP5_Status.SerialNumber; // dpp serial number
chdpp->sfInfo.strDescription = "Amptek Spectrum File"; // description
chdpp->sfInfo.strTag = "TestTag"; // tag
// create spectrum file, save file to string
strSpectrum = chdpp->CreateMCAData(chdpp->DP5Proto.SPECTRUM.DATA,chdpp->sfInfo,chdpp->DP5Stat.m_DP5_Status);
chdpp->SaveSpectrumStringToFile(strSpectrum); // save spectrum file string to file
}