source: trigger/gui/t72/main.cpp@ 267

Last change on this file since 267 was 115, checked in by daqct3, 15 years ago
New Functions
File size: 15.1 KB
Line 
1#include <QApplication>
2#include <QFont>
3#include <QGridLayout>
4
5#include <QPushButton>
6
7#include "main.h"
8#include <QThread>
9#include <QTimer>
10#include <time.h>
11
12
13
14void sleep_now(unsigned int mseconds)
15{
16 printf("in %i\n",clock());
17 clock_t goal = mseconds + clock();
18
19 while (goal > clock());
20 printf("out %i\n",goal);
21}
22
23
24
25 MyWidget::MyWidget(QWidget *parent)
26 : QWidget(parent)
27 {
28 //PixelToTriggerPixel=new int[16];
29
30
31 PixelToTriggerPixel=new int[16];
32 /* PixelToTriggerPixel[0]= 25;
33 PixelToTriggerPixel[1]= 26;
34 PixelToTriggerPixel[2]= 27;
35 PixelToTriggerPixel[3]= 28;
36 PixelToTriggerPixel[4]= 19;
37 PixelToTriggerPixel[5]= 20;
38 PixelToTriggerPixel[6]= 21;
39 PixelToTriggerPixel[7]= 22;
40 PixelToTriggerPixel[8]= 13;
41 PixelToTriggerPixel[9]= 14;
42 PixelToTriggerPixel[10]= 15;
43 PixelToTriggerPixel[11]= 16;
44 PixelToTriggerPixel[12]= 7;
45 PixelToTriggerPixel[13]= 8;
46 PixelToTriggerPixel[14]= 9;
47 PixelToTriggerPixel[15]= 10;*/
48
49 PixelToTriggerPixel[0]= 25;
50 PixelToTriggerPixel[1]= 26;
51 PixelToTriggerPixel[2]= 27;
52 PixelToTriggerPixel[3]= 28;
53 PixelToTriggerPixel[4]= 19;
54 PixelToTriggerPixel[5]= 20;
55 PixelToTriggerPixel[6]= 21;
56 PixelToTriggerPixel[7]= 22;
57 PixelToTriggerPixel[8]= 13;
58 PixelToTriggerPixel[9]= 14;
59 PixelToTriggerPixel[10]= 15;
60 PixelToTriggerPixel[11]= 16;
61 PixelToTriggerPixel[12]= 7;
62 PixelToTriggerPixel[13]= 8;
63 PixelToTriggerPixel[14]= 9;
64 PixelToTriggerPixel[15]= 10;
65 ListToTemperaturePixel = new int[7];
66 ListToTemperaturePixel[0] = 5;
67 ListToTemperaturePixel[1] = 1;
68 ListToTemperaturePixel[2] = 0;
69 ListToTemperaturePixel[3] = 2;
70 ListToTemperaturePixel[4] = 3;
71 ListToTemperaturePixel[5] = 4;
72 ListToTemperaturePixel[6] = 6;
73 SocketWindow = new QWidget();
74 SocketWindow->setWindowTitle("trigger - Socket Interface");
75 Socket = new QTcpSocket(SocketWindow);
76 connect(Socket, SIGNAL(readyRead()), this, SLOT(ReadFromSocket()));
77 //connect(Socket, SIGNAL(disconnected()), this, SLOT(GotDisconnected()));
78 WaitForData=false;
79 ManualDisconnect=false;
80
81 IPAddress = new QLineEdit(SocketWindow);
82 IPAddress->setText("eth-vme02");
83 IPAddress->setToolTip("Address of socket server");
84 AddressLayout = new QFormLayout;
85 AddressLayout->addRow("&Remote Address", IPAddress);
86
87 Port = new QSpinBox(SocketWindow);
88 Port->setRange(0,65535);
89 Port->setValue(3492);
90 Port->setToolTip("Port number of socket server");
91 PortLayout = new QFormLayout;
92 PortLayout->addRow("&Port", Port);
93
94
95 // Button to make connection
96 Connect = new QPushButton("Connect", SocketWindow);
97 Connect->setFont(QFont("Times", 10, QFont::Bold));
98 connect(Connect, SIGNAL(clicked()), this, SLOT(MakeConnection()));
99 Connect->setToolTip("Connect to server");
100 SocketButton = new QPushButton("Socke&t\nInterface",this);
101 SocketButton->setFont(QFont("Times", 10, QFont::Bold));
102 connect(SocketButton, SIGNAL(clicked()), this, SLOT(OpenSocketWindow()));
103 SocketButton->setToolTip("Open window for socket communication");
104
105
106 connect(SocketButton, SIGNAL(clicked()), this, SLOT(OpenSocketWindow()));
107 QPushButton *quit = new QPushButton(tr("Quit"));
108 quit->setFont(QFont("Times", 18, QFont::Bold));
109 Command = new QLineEdit(SocketWindow);
110 CommandLayout = new QFormLayout;
111 CommandLayout->addRow("&Command", Command);
112 Command->setEnabled(false);
113 connect(Command, SIGNAL(returnPressed()), this, SLOT(SendToSocket()));
114 Command->setToolTip("Command to send to socket server");
115
116 // Text box for socket output
117 SocketOutput= new QPlainTextEdit(SocketWindow);
118 SocketOutput->setReadOnly(true);
119 SocketOutput->setMaximumBlockCount(MAX_OUTPUT_LINES);
120 SocketOutput->setToolTip("Output of socket server");
121
122 // Layout of all widgets
123 SocketLayout = new QGridLayout(SocketWindow);
124 SocketLayout->addLayout(AddressLayout, 0, 0, 0, 1);
125 SocketLayout->addLayout(PortLayout, 0, 2);
126 SocketLayout->addWidget(Connect, 0, 3);
127 SocketLayout->addLayout(CommandLayout, 1, 0, 1, 4);
128 SocketLayout->addWidget(SocketOutput, 2, 0, 4, 4);
129
130 connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
131
132 QGridLayout *grid = new QGridLayout;
133
134
135 pixels = new Tpixel*[36];
136
137 int i=0;
138 for( int row = 0 ; row <6 ; row++ )
139 {
140 for( int col = 0 ; col < 6 ; col++)
141 {
142
143
144 //int i=row+6*col;
145 pixels[i] = new Tpixel;
146 grid->addWidget(pixels[i], row+1, col+1);
147
148 //pixels[i]->setValue((int)((i)*255./36.));
149 pixels[i]->setValue((int)(0));
150 pixels[i]->setMaxVal(1000);
151 pixels[i]->setMinimumSize ( 50, 50 );
152 pixels[i]->setMaximumSize ( 50, 50 );
153 i++;
154 }
155
156 }
157
158 humidity = new Humidity*[1];
159 humidity[0] = new Humidity;
160
161 humidity[0]->setMaxVal(100);
162 humidity[0]->setMinVal(0);
163 humidity[0]->setValue((int)(50));
164 humidity[0]->setMinimumSize ( 70, 40 );
165 humidity[0]->setMaximumSize ( 70, 40 );
166 tempsens=new Tempsens*[7];
167 for(int j=0;j<7;j++)
168 {
169 tempsens[j]=new Tempsens;
170 tempsens[j]->setMaxVal(35);
171 tempsens[j]->setMinVal(5);
172 tempsens[j]->setValue((int)(5+j));
173 tempsens[j]->setMinimumSize ( 70, 70 );
174 tempsens[j]->setMaximumSize ( 70, 70 );
175 }
176 QHBoxLayout *hlay = new QHBoxLayout;
177 //hlay->addWidget(tempsens[0]);
178 hlay->addWidget(SocketButton);
179 hlay->addWidget(quit);
180 //hlay->addWidget(tempsens[1]);
181 grid->addWidget(tempsens[0],0,0);
182 grid->addWidget(tempsens[1],0,7);
183 grid->addWidget(tempsens[2],7,0);
184 grid->addWidget(tempsens[3],7,7);
185 tempsens[4]->setMinimumSize ( 70, 50 );
186 tempsens[5]->setMinimumSize ( 70, 50 );
187 grid->addWidget(tempsens[4],1,8);
188 grid->addWidget(tempsens[5],5,8);
189
190 QVBoxLayout *humlay = new QVBoxLayout;
191
192 tempsens[6]->setMinimumSize ( 70, 40 );
193 tempsens[6]->setMaximumSize ( 70, 40 );
194 humlay->addWidget(humidity[0]);
195 humlay->addWidget(tempsens[6]);
196 humlay->setSpacing (0);
197 //humlay->setVerticalSpacing(0) ;
198 grid->addLayout(humlay,2,8,3,1);
199
200 //grid->addWidget(tempsens[6],4,8);
201
202 QVBoxLayout *layout = new QVBoxLayout;
203 layout->addLayout(hlay);
204 //layout->addWidget(SocketButton);
205 layout->addLayout(grid);
206
207 //layout->setColumnStretch(1, 10);
208
209 setLayout(layout);
210
211 //a time to update the pixel rates every 5 seconds:
212 QTimer *timer = new QTimer(this);
213 connect(timer, SIGNAL(timeout()),this , SLOT(updateRates()));
214
215 timer->start(5000);
216
217
218
219
220 }
221void MyWidget::updateRates()
222{
223 if(Socket->state() == QAbstractSocket::ConnectedState)
224 {
225 char command[]= "V560 GetRates 1 \n\0";
226 SocketOutput->insertPlainText(command);
227 Socket->write(command);
228 //QByteArray bytes = command.toAscii();
229 //const char * ptr = bytes.data();
230 printf(command);
231
232 //usleep(3000000);
233 //SleeperThread::msleep(1000);
234
235 /*const char tmp[]="V560 GetRate 1 5 \n\0";
236 printf(tmp);
237 Socket->write(tmp);
238 SocketOutput->insertPlainText(tmp);*/
239
240
241 }
242}
243// +++ Open sub window handling the socket interface +++
244void MyWidget::OpenSocketWindow() {
245 SocketWindow->show();
246
247 // if(SocketWindow->isVisible()) SocketWindow->hide();
248 //else SocketWindow->show();
249}
250
251// +++ Acquire data through socket (acquire botton only available if socket exists) +++
252void MyWidget::GetSignalFromSocket() {
253 char Command[MAX_COM_SIZE];
254
255 GetButton->setEnabled(false);
256 WaitForData = true;
257 sprintf(Command, "test");//, BoardNo->value(), ChannelNo->value()/10, ChannelNo->value()%10);
258 Socket->write(Command);
259}
260
261// Quit application when clicking close button on window
262
263
264// +++ Connecting or disconnecting from client +++
265void MyWidget::MakeConnection() {
266 printf("Connecting...\n");
267 if(Socket->state() == QAbstractSocket::ConnectedState) {
268 ManualDisconnect = true;
269 Socket->disconnectFromHost();
270 Connect->setText("Connect");
271 }
272 else {
273
274 Socket->connectToHost(IPAddress->text(),Port->value());
275
276
277
278 Connect->setEnabled(false); // While waiting for connection, button not available
279 Socket->waitForConnected(SOCKET_TIMEOUT);
280 Connect->setEnabled(true);
281 if(Socket->state() != QAbstractSocket::ConnectedState)
282 QMessageBox::warning(this, "MyWidget Message","Could not connect to host.",QMessageBox::Ok);
283 else {
284 //Connect->setText("Disconnect");
285 //ConnectAction->setText("Disconnect");
286 Port->setEnabled(false);
287 IPAddress->setEnabled(false);
288 Command->setEnabled(true);
289 //GetButton->setEnabled(true);
290 ManualDisconnect = false;
291 Connect->setText("Disconnect");
292
293 /* OpenAction->setEnabled(false);
294 FilenameBox->setEnabled(false);
295 LoadButton->setEnabled(false);
296 FilenameBox->clear();
297 CloseDatafile();*/
298
299 /* ChannelNo->setEnabled(true);
300 BoardNo->setEnabled(true);
301 PixelID->setEnabled(true);
302 ChannelNo->setRange(0, 65535);
303 BoardNo->setRange(0, 65535);
304
305 TabWidget->setTabEnabled(1,false);
306 TabWidget->setTabEnabled(2,false);
307
308 RunHeaderDisplay->clear();
309 EventHeaderDisplay->clear();
310 Signal->hide();*/
311 }
312 }
313}
314
315// +++ Send command to socket (command button available only if socket existing) +++
316void MyWidget::SendToSocket() {
317 // QMessageBox::warning(this, "sdfsdf","sending to socket...",QMessageBox::Ok);
318 //QString
319 /*int length = strlen(Command->text().toAscii());
320 printf("send length: %i\n", length);
321 char buf[4];
322
323 buf[0] = (char) length;
324 buf[1] = (char) length >> 8;
325 buf[2] = (char) length >> 16;
326 buf[3] = (char) length >> 24;
327
328
329 Socket->write(buf);*/
330 Socket->write(Command->text().toAscii());
331 Command->clear();
332}
333
334// +++ Read data from socket and display +++
335void MyWidget::ParseData(QString data)
336{
337 data = data.remove(QChar('['));
338 data = data.remove(QChar(']'));
339data = data.remove(QChar('\ '));
340 data = data.trimmed();
341 QStringList list1 = data.split(",",QString::SkipEmptyParts);
342 int numberofelements = list1.count();
343 printf("%i\n",numberofelements);
344 numberofelements--;
345
346 if(list1[0]=="Temp")
347 {
348 printf("updating temp...\n");
349 list1.removeAt(0);
350 float temperature[numberofelements];
351 if (numberofelements>7) numberofelements = 7;
352 for(int i =0;i<numberofelements;i++)
353 {
354 temperature[i]= list1[i].toFloat();
355 QString stri;
356 stri=QString("%1").arg(ListToTemperaturePixel[i]);
357
358 QString output = "Setting temperature of sensor "+stri + " to " + list1[i]+"c\n";
359 SocketOutput->insertPlainText(output);
360 tempsens[ListToTemperaturePixel[i]]->setValue((float)(temperature[i]));
361 }
362 temperature[7]= list1[7].toFloat();
363 humidity[0]->setValue(temperature[7]);
364 //printf("hum: %i\n",temperature[7]);
365 }
366 if(list1[0]=="\'rates\'")
367 {
368 list1.removeAt(0);
369 float rates[numberofelements];
370 for(int j =0;j<numberofelements;j++)
371 {
372 // pixels[j]->setMaxVal(1);
373 }
374 for(int i =0;i<numberofelements;i++)
375 {
376
377 rates[i]= list1[i].toFloat();
378 QString stri;
379 stri=QString("%1").arg(PixelToTriggerPixel[i]);
380
381 QString output = "Setting rates of trigger pixel "+stri + " to " + list1[i]+"\n";
382 SocketOutput->insertPlainText(output);
383 pixels[PixelToTriggerPixel[i]]->setValue((int)(rates[i]));
384 /*if(rates[i]>pixels[i]->getMaxVal())
385 {
386 for(int j =0;j<numberofelements;j++)
387 {
388 pixels[j]->setMaxVal(rates[i]);
389 }
390
391 }*/
392 // pixels[i]->setMaxVal(255);
393 }
394 }
395
396
397}
398void MyWidget::ReadFromSocket() {
399 // Check if channel data expected and error message arrived
400 /*char Data[1024];
401 printf("size of int: %i\n",sizeof(int));
402 char cSize[sizeof(int)];
403 int Size;
404
405 int numBytes=Socket->read(cSize,sizeof(int));
406 Size=(int)(cSize);
407 printf ("numbytes: %i\n",numBytes);
408 printf("csize: %x\n",cSize);
409 printf("size: %i\n",Size);
410
411 numBytes+http://www.google.ch/firefox?client=firefox-a&rls=org.mozilla:en-US:official=Socket->read(Data,Size);
412 printf("Data:%s\n",Data);/**/
413
414 //QByteArray command;
415 char command[1024];
416 Socket->read(command,1023);
417 printf("cc: %s\n",command);
418 QString temp(command);
419 //SocketOutput->insertPlainText(command);
420 if(temp[0]!=QChar('['))
421 SocketOutput->insertPlainText(temp);
422 else
423 ParseData(temp);
424
425
426
427 //QString qData(Data);
428
429 //char * cData = Data.constData ();
430
431 //if (WaitForData && Data.contains("Error")) {
432 // WaitForData = false;
433 //GetButton->setEnabled(true);
434 // QMessageBox::warning(this, "MyWidget Message","Could not read waveform data from socket.",QMessageBox::Ok);
435 // return;
436 // }
437
438 // Check if channel data were transmitted, if yes and waiting for data, extract and plot them
439 /*printf("num bytes: %i",numBytes);
440 QString temp(numBytes);
441 SocketOutput->insertPlainText(temp);
442 SocketOutput->insertPlainText(Data);
443 SocketOutput->insertPlainText("TTT");
444 printf("Data\n");
445 printf("%s",Data);*/
446 //QString Text = SocketOutput->document()->toPlainText().trimmed();
447 /*usleep(100000); // Wait to limit maximum update rate
448 GetSignalFromSocket();*/
449 /*if (WaitForData && Text.endsWith(QLatin1String("==END=="))) {
450 // Extract text between ==START== and ==END==
451 QByteArray Data=Text.mid(Text.lastIndexOf("==START==")+9,Text.length()-Text.lastIndexOf("==START==")-16).toAscii();
452
453 char *NextNumber = strtok(Data.data()," "); // Number of entries that follow
454 int Count=0, NumberOfEntries = atoi(NextNumber);
455 double *x = new double [NumberOfEntries];
456 double *y = new double [NumberOfEntries];
457
458 // Convert all entries (separated by a whitespace) to numbers
459 while((NextNumber=strtok(NULL, " "))!=NULL && Count<NumberOfEntries)
460 *(y+Count++) = atof(NextNumber);
461 if (Count==NumberOfEntries && NextNumber!=0)
462 QMessageBox::warning(this, "MyWidget Message","Found too many numbers in data block, truncated.",QMessageBox::Ok);
463 // Apply sampling frequency and scaling factor
464 for(int i=2; i<Count; i++) {
465 x[i] = (i-2) / y[0];
466 y[i] = y[i] * y[1];
467 }
468 if(NumberOfEntries>2) {
469 Signal->setData(x+2, y+2, NumberOfEntries-2); // Copies data, arrays can be deleted afterwards
470 Signal->show();
471 Zoomer->setZoomBase(Signal->boundingRect());
472 }
473 delete[] x; delete[] y;
474
475 if(ContinuousBox->isChecked()) {
476 usleep(100000); // Wait to limit maximum update rate
477 GetSignalFromSocket();
478 }
479 else {
480 WaitForData = false;
481 GetButton->setEnabled(true);
482 }
483 }*/
484}
485
486// +++ Disconnect from socket +++
487void MyWidget::GotDisconnected() {
488 Connect->setText("Connect");
489 //ConnectAction->setText("Connect");
490 Port->setEnabled(true);
491 IPAddress->setEnabled(true);
492 Command->setEnabled(false);
493
494 GetButton->setEnabled(false);
495 /*FilenameBox->setEnabled(true);
496 OpenAction->setEnabled(true);
497 LoadButton->setEnabled(true);
498 ChannelNo->setEnabled(false);
499 BoardNo->setEnabled(false);
500 PixelID->setEnabled(false);
501 Signal->hide();
502 TabWidget->setTabEnabled(1, true);
503 TabWidget->setTabEnabled(2, true);*/
504
505 SocketOutput->clear();
506 if(!ManualDisconnect) QMessageBox::warning(this, "MyWidget Message","Socket disconnected, maybe host gone.",QMessageBox::Ok);
507}
508
509
510
511 int main(int argc, char *argv[])
512 {
513 QApplication app(argc, argv);
514 MyWidget widget;
515 widget.resize(400,400);
516 widget.show();
517 return app.exec();
518 }
519
Note: See TracBrowser for help on using the repository browser.