-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata.cpp
executable file
·1470 lines (1244 loc) · 43.6 KB
/
data.cpp
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* data.cpp
* FOXSI_GSE
*
* Created by Steven Christe on 10/31/11.
* Copyright 2011 NASA GSFC. All rights reserved.
*
* pthread tutorial
* https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal
*
*/
#include "Application.h"
#include "data.h"
#include "threads.h"
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include "gui.h"
#include "usbd2xx.h"
#include "okFrontPanelDLL.h"
#include "telemetry.h"
#include <FL/Fl_File_Chooser.H>
#include <FL/Fl_Preferences.H>
#define MAXPATH 128
#define NUM_THREADS 8
#define XSTRIPS 128
#define YSTRIPS 128
#define MAX_CHANNEL 1024
#define NUM_DETECTORS 7
// Note that an unsigned short int is 2 bytes
// for formatter
//#define FRAME_SIZE_IN_SHORTINTS 295
//#define FRAME_SIZE_IN_BYTES 590
#define FRAME_SIZE_IN_SHORTINTS 1024
#define FRAME_SIZE_IN_BYTES 2048
// for ASIC
//#define FRAME_SIZE_IN_SHORTINTS 784
//#define FRAME_SIZE_IN_BYTES 1568
extern Gui *gui;
char dataFilename[MAXPATH];
FILE *dataFile;
FILE *formatterFile;
okCFrontPanel *dev;
int newdisplay;
int stop_message;
time_t start_time;
time_t current_time;
int nreads;
extern int data_source;
unsigned short int buffer[FRAME_SIZE_IN_SHORTINTS];
unsigned short int buffer0[FRAME_SIZE_IN_SHORTINTS];
unsigned short int framecount;
int *taskids[NUM_THREADS];
int fout;
pthread_mutex_t mymutex;
pthread_mutex_t timebinmutex;
extern char *data_file_save_dir;
extern char *formatter_configuration_file;
extern int file_type;
extern char *formatter_playback_file;
extern char read_filename[200];
void data_initialize(void)
{
// Initialize a connection to a data stream
// Read the data source from the preferences
gui->prefs->get("data_source", data_source, 0);
gui->frame_missOutput->value(0);
if (data_source == 0)
{
gui->app->print_to_console("Initializing Simulated data.\n");
gui->startReadingDataButton->activate();
gui->app->flush_image();
gui->app->flush_histogram();
gui->app->flush_timeseries();
gui->app->print_to_console("Done initializing.\n");
}
if (data_source == 1)
{
gui->app->print_to_console("Initializing USB/ASIC connection.\n");
if (gui->usb->open() < 0)
{
gui->app->print_to_console("Could not open device.\n");
gui->app->print_to_console("Initialization Failed!\n");
}
else
{
gui->startReadingDataButton->activate();
gui->closeBut->activate();
gui->closeBut->value(0);
gui->sendParamsWindow_sendBut->activate();
gui->setHoldTimeWindow_setBut->activate();
//gui->setHoldTimeWindow_autorunBut->activate();
gui->app->flush_image();
gui->app->flush_histogram();
gui->app->flush_timeseries();
gui->app->print_to_console("Done initializing.\n");
}
}
if (data_source == 2)
{
char dll_date[32], dll_time[32];
int init_state = 1;
gui->app->print_to_console("Initializing USB/Formatter connection.\n");
if (FALSE == okFrontPanelDLL_LoadLib(NULL))
{
gui->app->print_to_console("FrontPanel DLL could not be loaded.\n");
init_state = 0;
}
okFrontPanelDLL_GetVersion(dll_date, dll_time);
printf("FrontPanel DLL loaded.Built: %s %s\n", dll_date, dll_time);
okCFrontPanel *devi = data_initialize_formatter_FPGA();
if (NULL == devi)
{
gui->app->print_to_console("FPGA could not be initialized.\n");
gui->app->print_to_console("Initialization Failed!\n");
init_state = 0;
}
if (init_state == 1){
gui->startReadingDataButton->activate();
//gui->closeBut->activate();
//gui->detector_choice->activate();
gui->app->flush_image();
gui->app->flush_histogram();
gui->app->flush_timeseries();
gui->shutterstateOutput->value(0);
gui->app->print_to_console("Done initializing.\n");
}
}
if (data_source == 3)
{
int init_state = 1;
//char formatter_playback_filename[] = "/Users/foxsi/Desktop/translation_data/data_121025_0022_det0_translateX.dat";
gui->app->print_to_console("Initializing data file connection.\n");
formatterFile = fopen("/Users/foxsi/Desktop/2014alignmentA/data_141109_211141.dat", "r");
//formatterFile = fopen("/Users/foxsi/Desktop/data_launch_121102/data_launch_121102_114631.dat", "r");
//strcpy(read_filename,"/Users/foxsi/Desktop/data_launch_121102/data_launch_121102_114631.dat");
//formatterFile = fopen(gui->app->get_datafilename(), "r");
printf("%s\n", gui->app->get_datafilename());
//strcpy(read_filename,"");
if (formatterFile == NULL){
gui->app->printf_to_console("Cannot open file %s.\n", gui->app->get_datafilename(), NULL);
gui->app->print_to_console("File stream could not be initialized.\n");
gui->app->print_to_console("Initialization Failed!\n");
init_state = 0;
}
else {
gui->app->printf_to_console("Opened file %s.\n", gui->app->get_datafilename(), NULL);
init_state = 1;
}
if (init_state == 1){
gui->startReadingDataButton->activate();
//gui->closeBut->activate();
//gui->detector_choice->activate();
gui->app->flush_image();
gui->app->flush_histogram();
gui->app->flush_timeseries();
gui->shutterstateOutput->value(0);
gui->app->print_to_console("Done initializing.\n");
}
}
}
void data_close(void){
if (data_source == 3) {
fclose(formatterFile);
}
}
void* data_watch_buffer(void* p)
{
/* Watches the buffer variable for changes. When changes occur, parse
* the buffer variable into a data packet and update the display.
*/
// This function never stops unless told to do so by setting stop_message to 1
while(1)
{
if (newdisplay == 1){
// update the display
pthread_mutex_lock( &mymutex); /* wait on readgse */
Fl::lock();
if (gui->writeFileBut->value() == 1){
data_frame_print_to_file(buffer0);
}
data_update_display(buffer0);
newdisplay = 0;
fflush(stdout);
pthread_mutex_unlock(&mymutex);
gui->mainHistogramWindow->redraw();
gui->mainImageWindow->redraw();
gui->mainLightcurveWindow->redraw();
gui->detectorsImageWindow->redraw();
gui->detectorsHistogramWindow->redraw();
Fl::awake(); // Without this it may not redraw until next event (like a mouse movement)!
Fl::unlock();
}
if (stop_message == 1){
pthread_exit(NULL);
}
}
}
void* data_timer(void *p)
{
/* Keep track of the timer */
while(1)
{
// a sleep statement so that it does not pole the time too often
usleep(0.5/1000000.0);
current_time = time(NULL);
gui->app->elapsed_time_sec = difftime(time(NULL), start_time);
if (stop_message == 1){
pthread_exit(NULL);}
}
}
void* data_countrate(void *p)
{
/* Keep track of the timer */
unsigned int i = 1;
while(1)
{
float binsize = 0;
int microseconds;
binsize = gui->timebinsize_counter->value();
microseconds = binsize*1000000.0;
// System activity may lengthen the sleep by an indeterminate amount.
// therefore this is not the best way to measure count rate
// quick and dirty
usleep(microseconds);
gui->mainLightcurveWindow->reset(binsize);
if (stop_message == 1){
pthread_exit(NULL);}
}
}
void* data_read_data(void *p)
{
/* Read the data in continuously. A data frame is read into the buffer
* variable and written to disk (if a file has been opened),
* and then copied into the buffer0 variable. This variable is being watched
* by data_watch_buffer which uses it to update the displays.
*/
ssize_t wlen;
long len;
int badSync = 0;
int badRead = 0;
int status = 0;
char textbuffer[50];
int maxreads;
int read_status = 0;
maxreads = gui->nEvents->value();
// Read the read delay from the preferences
int read_delay;
gui->prefs->get("read_delay", read_delay, 0);
// Read the data source from the preferences
gui->prefs->get("data_source", data_source, 0);
// Check to see if a file is open and write header to it before starting
// to write data to it
if((dataFile != NULL) && (data_source == 1))
{
gui->usb->writeHeader(dataFile);
}
// This function never stops unless told to do so by setting stop_message to 1
while (1) {
read_status = 0;
// For the desired reading speed
if (read_delay != 0) {usleep(read_delay);}
// read the data
if (data_source == 0){
// read from the simulation
data_simulate_data();
read_status = 1;
gui->app->frame_read_count++;
}
if (data_source == 1) {
// read from the USB/ASIC
status = gui->usb->findSync();
if(status < 1){
badSync++;
}
printf("reading frame\n");
status = gui->usb->readFrame();
if(status < 1){
badRead++;
printf("Bad read\n");
} else {
read_status = 1;
gui->app->frame_read_count++;
}
}
if (data_source == 2){
// read from the USB/Formatter, reads 4 frames at a time.
len = dev->ReadFromBlockPipeOut(0xA0,1024,2048,(unsigned char *) buffer);
// set the read status based on how len returned
if (len == 2048){
read_status = 1;
gui->app->frame_read_count++;
} else {
printf("bad read!\n");
}
}
if (data_source == 3) {
// read data from file, read 4 frames at a time
len = fread((unsigned char *) buffer, 2048, 1, formatterFile);
// skip 10 frames
fseek(formatterFile, 204800/4, SEEK_CUR);
//printf("%d\n", len);
// set the read status based on how len returned
if (len == 1){
read_status = 1;
gui->app->frame_read_count++;
} else {
printf("bad read!\n");
}
}
if (read_status == 1) {
if(fout > 0)
{
if( (wlen = write(fout,(const void *) buffer,FRAME_SIZE_IN_BYTES) ) != FRAME_SIZE_IN_BYTES)
{
// then good write
} else {
printf("bad data write!\n");
}
}
if (pthread_mutex_trylock(&mymutex) == 0) /* if fail missed as main hasn't finished */
{
if (newdisplay == 0)
{
memcpy((void *) buffer0,(void *) buffer, FRAME_SIZE_IN_BYTES);
newdisplay = 1;
gui->app->frame_display_count++;
}
pthread_mutex_unlock(&mymutex);
} else {
// printf("failed to pass off data\n");
//gui->app->frame_miss_count++;
}
// Check to see if if only a fixed number of frames should be read
// if so set the stop message
if ((maxreads != 0) && (nreads >= maxreads)){
stop_message = 1;
}
}
if (stop_message == 1){
Fl::lock();
gui->app->print_to_console("Read finished or stopped.\n");
//gui->stopReadingDataButton->deactivate();
//gui->startReadingDataButton->deactivate();
//gui->writeFileBut->deactivate();
//gui->closeBut->deactivate();
//gui->initializeBut->activate();
if(nreads > maxreads-1){
//gui->nEventsDone->value(0);
nreads = 0;
}
Fl::unlock();
if (fout > 0){
gui->app->printf_to_console("Closing file: %s.\n", dataFilename, NULL);
close(fout);
}
if (dataFile != NULL){
gui->app->printf_to_console("Closing file: %s.\n", dataFilename, NULL);
fclose(dataFile);
}
if (data_source == 1)
{
sprintf(textbuffer, "%d bad syncs, %d bad reads.\n", badSync, badRead);
gui->consoleBuf->insert(textbuffer);
//clean up usb interface
gui->usb->close();
}
if (data_source == 2) {
// clean up formatter interface
// nothing to do
// quitting the program will clean up the interface
}
pthread_mutex_destroy(&mymutex);
pthread_mutex_destroy(&timebinmutex);
pthread_exit(NULL);
}
}
}
void data_simulate_data(void)
{
// Add some delay for the actual read
usleep(100);
framecount++;
unsigned short int tempx;
unsigned short int tempy;
unsigned short int tempenergy;
struct voltage_data {
unsigned value: 12;
unsigned status: 4;
};
strip_data strip;
voltage_data volt;
struct strip_data // 2 bytes
{
unsigned data : 10;
unsigned number : 6;
};
buffer[0] = 60304; // Sync 1 - Hex EB90 - missing in formatter data
buffer[0] = 63014; // Sync 2 - Hex F626
buffer[1] = (unsigned short int) (arc4random() % 100); // Time 1 (MSB)
buffer[2] = (unsigned short int) (arc4random() % 100); // Time 2 (MSB)
buffer[3] = (unsigned short int) (arc4random() % 100); // Time 3 (LSB)
buffer[4] = framecount; // Frame Counter 1
buffer[5] = framecount; // Frame Counter 2
// 7 Housekeeping 0
// 8 Cmd Count
// 9 Command 1
// 10 Command 2
// 11 Housekeeping 1
// 12 Formatter Status
// 13 0
volt.value = telemetry_voltage_convert_hvvalue(250);
volt.status = 4;
memcpy(&buffer[13],&volt,2);
// 14 HV value/status
// 15 Housekeeping 2
// 16 Status 0
// 17 Status 1
// 18 Status 2
// 19 Housekeeping 3
// 20 Status 3
// 21 Status 4
// 22 Status 5
// 23 Status 6
// 24 Detector 0 Time
buffer[25] = (unsigned short int) (arc4random()); // 25 0ASIC0 mask0
buffer[26] = (unsigned short int) (arc4random()); // 26 0ASIC0 mask1
buffer[27] = (unsigned short int) (arc4random()); // 27 0ASIC0 mask2
buffer[28] = (unsigned short int) (arc4random()); // 28 0ASIC0 mask3
// 29 0Strip0A Energy
// Choose some random pixels to light up
tempx = (arc4random() % 128 + 1);
tempy = (arc4random() % 128 + 1);
tempenergy = (arc4random() % MAX_CHANNEL + 1);
// if (tempx < 64){ buffer[25] = ~(~0 << 1) << (tempx -
strip.data = tempx;
strip.number = tempenergy;
memcpy(&buffer[29],&strip,2);
// 30 0Strip0B Energy
}
void data_start_file(void)
{
/* Open a file to write the data to. The file pointer is set to fout.
*
*/
// the following variables holds the fully qualified filename (dir + filename)
// if directory NOT set then will create file inside of current working directory
// which is likely <foxsi gse dir>/build/Debug/
if (gui->writeFileBut->value() == 1) {
data_set_datafilename();
gui->app->printf_to_console("Trying to open file: %s\n", dataFilename, NULL);
if (gui->fileTypeChoice->value() == 0) {
dataFile = fopen(dataFilename, "w");
if (dataFile == NULL)
gui->app->printf_to_console("Cannot open file %s.\n", dataFilename, NULL);
else
gui->app->printf_to_console("Opened file %s.\n", dataFilename, NULL);
//gui->app->write_header(dataFile);
}
if (gui->fileTypeChoice->value() == 1) {
if((fout = open(dataFilename,O_RDWR|O_CREAT,0600)) < 0){
gui->app->printf_to_console("Cannot open file %s.\n", dataFilename, NULL);}
else {
gui->app->printf_to_console("Opened file %s.\n", dataFilename, NULL);
}
}
} else {
gui->app->printf_to_console( "Closing file %s.\n", dataFilename, NULL);
if (gui->fileTypeChoice->value() == 1){close(fout);}
if (gui->fileTypeChoice->value() == 0){fclose(dataFile);}
fout = 0;
}
}
void data_start_reading(void)
{
pthread_t thread;
struct sched_param param;
pthread_attr_t tattr;
int *variable;
int ret;
int newprio;
stop_message = 0;
start_time = time(NULL);
// define a high (custom) priority for the read_data thread
newprio = -10;
param.sched_priority = newprio;
ret = pthread_attr_init(&tattr);
ret = pthread_attr_setschedparam (&tattr, ¶m);
// start the read_data thread
ret = pthread_create(&thread, &tattr, data_read_data, (void *) taskids[0]);
pthread_mutex_init(&mymutex,NULL);
pthread_mutex_init(&timebinmutex,NULL);
newprio = -5;
param.sched_priority = newprio;
ret = pthread_attr_init(&tattr);
ret = pthread_attr_setschedparam (&tattr, ¶m);
// start the watch_buffer thread
ret = pthread_create(&thread, NULL, data_watch_buffer, (void *) taskids[1]);
//newprio = -5;
//param.sched_priority = newprio;
//ret = pthread_attr_init(&tattr);
//ret = pthread_attr_setschedparam (&tattr, ¶m);
// start the data reading clock
ret = pthread_create(&thread, NULL, data_timer, (void *) taskids[2]);
// start the count rate thread
ret = pthread_create(&thread, NULL, data_countrate, (void *) taskids[3]);
gui->stopReadingDataButton->activate();
gui->app->print_to_console("Reading...\n");
}
void data_stop_reading(void)
{
stop_message = 1;
}
void data_frame_print_to_file(unsigned short int *frame)
{
if (data_source == 1)
{
gui->usb->writeFrame(dataFile);
}
}
void data_update_display(unsigned short int *frame)
{
// Parse the buffer variable and update the display
// reading from the ASIC one frame at a time
// reading from the Formater four frames at a time
unsigned short int strip_data;
unsigned short int strip_number;
//int ymask[128] = {0};
//int xmask[128] = {0};
// xstrips are defined as p strips
//signed int xstrips[128] = {0};
// ystrips are defined as n strips
//signed int ystrips[128] = {0};
unsigned int asic0n_data[64] = {0};
unsigned int asic1n_data[64] = {0};
unsigned int asic2p_data[64] = {0};
unsigned int asic3p_data[64] = {0};
//gui->nEventsDone->value(nreads);
gui->inttimeOutput->value(gui->app->elapsed_time_sec);
if (data_source == 0) {
// Parse simulated data
int num_hits;
unsigned high_voltage_status;
unsigned high_voltage;
high_voltage_status = buffer[13] & 0xF;
high_voltage = (buffer[13] >> 12) & 0xFFF;
gui->framenumOutput->value(frame[5]);
gui->HVOutput->value(high_voltage);
if (high_voltage_status == 1){gui->HVOutput->textcolor(FL_RED);}
if (high_voltage_status == 2){gui->HVOutput->textcolor(FL_BLUE);}
if (high_voltage_status == 4){gui->HVOutput->textcolor(FL_BLACK);}
for(int detector_num = 0; detector_num < NUM_DETECTORS; detector_num++)
{
int z, x, y;
// simulate up to 5 hits, only the first one goes to image, all others go to mask
for(int i = 0; i < num_hits; i++)
{
z = arc4random() % MAX_CHANNEL + 1;
x = arc4random() % XSTRIPS + 1;
y = arc4random() % YSTRIPS + 1;
if (i == 0) {
gui->mainHistogramWindow->add_count(z, detector_num);
gui->detectorsImageWindow->add_count_to_image(x, y, detector_num);
gui->mainLightcurveWindow->add_count(detector_num);
} else {
gui->detectorsImageWindow->add_count_to_mask(x, y, detector_num);
}
}
}
gui->frame_missOutput->value((float) 100 * gui->app->frame_miss_count/gui->app->frame_read_count);
//pthread_mutex_lock( &timebinmutex);
//current_timebin += num_hits;
//for(int detector_num = 0; detector_num < (NUM_DETECTORS+1); detector_num++)
//{
// gui->mainLightcurveWindow->current_timebin_detectors[detector_num] += num_hits;
//}
//pthread_mutex_unlock(&timebinmutex);
}
if (data_source == 1){
// Parse the data from the USB/ASIC connection
//
// Notes
// -----
//
// * the order for asics n n then p p
// * the asic mask does not work
//
unsigned short int xstrips[128];
unsigned short int ystrips[128];
int index = 0;
unsigned int common_mode = 0;
unsigned int common_mode_nside[2] = {0};
int read_status = 1;
int frame_number = 0;
// Loop through the 4 ASICS
for( int j = 0; j < 4; j++)
{
read_status = 1;
if (j == 0){ frame_number = frame[0]; }
if ((j == 1) && (frame[index] != 0xEB91)) { read_status = 0; }
if ((j == 2) && (frame[index] != 0xEB92)) { read_status = 0; }
if ((j == 3) && (frame[index] != 0xEB93)) { read_status = 0; }
if (gui->printasicframe_button->value() == 1)
{
if (j == 0){ printf("FRAME START\n-----------\neb90\n"); }
else {
printf( "%x\n", frame[index]); index++;
}
printf( "frame number: %u\n", frame[index]);//frame counter
index++;
printf( "%x\t", frame[index]); index++; //start
printf( "%x\t", frame[index]); index++; //chip
printf( "%x\t", frame[index]); index++; //trigger
printf( "%x\t", frame[index]); index++; //seu
// channel mask displayed in hex
// First two channel mask bits are stored as one word each.
printf( "%x", frame[index]); index++;
printf( "%x", frame[index]); index++;
printf( "%x", frame[index]); index++;
printf( "%x", frame[index]); index++;
printf( "%x", frame[index]); index++;
printf( "%x\t", frame[index]); index++;
printf( "%u\t", frame[index]); index++; //pedestal
} else {
if (j == 0){ index += 12;} else { index += 13; }
}
// store the common mode
// note the common mode is not calculated for n sides
// need to calculate it in software later
common_mode = frame[index+64];
// strip data
for(int i = 0; i < 64; i++){
if (gui->printasicframe_button->value() == 1){
printf( "%u\t", frame[index]);}
if (j == 0){ asic0n_data[i] = frame[index]; }
if (j == 1){ asic1n_data[i] = frame[index]; }
if (j == 2){
asic2p_data[i] = frame[index];
xstrips[i + (j-2)*64] = frame[index] - common_mode;
}
if (j == 3){
asic3p_data[i] = frame[index];
xstrips[i + (j-2)*64] = frame[index] - common_mode;
}
index++;
}
if (gui->printasicframe_button->value() == 1){
printf( "common mode: %u\n", frame[index]);
index++; //common mode
// 10 extra words of readout information
printf( "%u\t", frame[index]); index++;
printf( "%u\t", frame[index]); index++;
printf( "%u\t", frame[index]); index++;
printf( "%u\t", frame[index]); index++;
printf( "%u\t", frame[index]); index++;
printf( "%u\t", frame[index]); index++;
printf( "%u\t", frame[index]); index++;
printf( "%u\t", frame[index]); index++;
printf( "%u\t", frame[index]); index++;
printf( "%u\t", frame[index]); index++;
printf("\n\n");
} else {
index += 11;
}
}
if ((gui->printasicframe_button->value() == 1) && (read_status == 0))
{ printf("Bad frame skipping\n"); }
if (read_status == 1) {
common_mode_nside[0] = median(asic0n_data, 64);
common_mode_nside[1] = median(asic1n_data, 64);
gui->framenumOutput->value(frame_number);
if (gui->printasicframe_button->value() == 1){
printf("common mode: %u %u\n\n", common_mode_nside[0], common_mode_nside[1]);}
for(int i = 0; i < XSTRIPS; i++)
{
if (xstrips[i] > 0){ gui->mainHistogramWindow->add_count(xstrips[i], 0); }
if (xstrips[i] >= gui->mainHistogramWindow->get_lowthreshold()){
//num_hits++;
for(int j = 0; j < YSTRIPS; j++)
{
if (j < YSTRIPS){
if (((asic0n_data[j] - common_mode_nside[0]) > 50) && (asic0n_data[j] < 1023)){ystrips[j] = 1;}
}
if (j >= YSTRIPS){
if (((asic1n_data[j] - common_mode_nside[1]) > 50) && (asic1n_data[j] < 1023)){ystrips[j] = 1;}
}
if (xstrips[i] * ystrips[j] != 0) {
gui->detectorsImageWindow->add_count_to_image(i, j, 0);}
}
}
}
for(int i = 0; i < XSTRIPS; i++){printf("%u\t", ystrips[i]);}
printf("\n");
//pthread_mutex_lock(&timebinmutex);
//current_timebin += num_hits;
//pthread_mutex_unlock(&timebinmutex);
}
}
if ((data_source == 2) || (data_source == 3)) {
// parse and display the data from the USB/Formatter connection
//
// Notes
// -----
//
// * the order for asics n n then p p
// * the asic mask does not work
// * the data read in, contains four frames
// * the sync words are f628 at the beginning of the frame and eb90 at
// the end of the frame.
// * the asic data contains the three max strips as well as the common
// mode in the fourth data word.
// * the word right before the ending sync word is a checksum
unsigned short int tmp;
unsigned short int high_voltage_status;
unsigned short int high_voltage;
unsigned short int strip_data;
unsigned short int strip_number;
unsigned short int formatter_status;
bool attenuator_actuating = 0;
int index = 0;
unsigned short int read_status;
uint32_t frameNumber;
// parse the buffer variable and update the display
unsigned short int temperature_monitors[12] = {0};
unsigned short int voltage_monitors[4] = {0}; // order is 5V, -5V, 1.5V, -3.3V
unsigned short int frame_value; // to know which frame are provided what telemetry data
for( int frame_num = 0, index = 0; frame_num < 4; frame_num++)
{
//printf("f628: %x\n", buffer[index++]);
if (buffer[index] == 0xf628) {
read_status = 1;
//printf("-----Frame Start------\n");
//printf("frame number = %u\n", frame_num);
//printf("eb90: %x\n", buffer[index+255]);
//for(int blah = 0; blah < 16; blah++){printf("%i-%04hX: %i\n", blah, buffer[blah], buffer[blah]);}
// check the checksum
for( int word_number = 0; word_number < 256; word_number++ ){read_status ^= buffer[word_number];}
if( read_status == 1 ){
long long time;
unsigned short int command_count;
uint32_t command_value;
nreads++;
index++;
time = (((unsigned long long) buffer[index] << 32) & 0xFFFF00000000);
index++;
time |= ((unsigned long long) (buffer[index] << 16) & 0xFFFF0000);
index++;
time |= (unsigned long long) buffer[index];
time /= 10e6;
if (gui->app->formatter_start_time == 0){ gui->app->formatter_start_time = time; }
index++;
//index++;
frameNumber = ((uint32_t)(buffer[index] << 16) & 0xFFFF0000) | buffer[index+1];
if (gui->app->frame_number != 0) {gui->app->frame_miss_count += frameNumber - gui->app->frame_number - 1;}
gui->app->frame_number = frameNumber;
gui->app->frame_display_count++;
frame_value = buffer[index+1] & 0x3;
index++;
index++;
// Housekeeping 0
switch (frame_value) {
case 0:
// temperature reference
temperature_monitors[0] = buffer[index++];
break;
case 1:
//1.5 V monitor
voltage_monitors[2] = buffer[index++];
break;
case 2:
temperature_monitors[4] = buffer[index++];
break;
case 3:
temperature_monitors[8] = buffer[index++];
break;
default:
//printf("housekeeping 0: %u\n", buffer[index++]);
index++;
break;
}
command_count = buffer[index];
//printf("cmd count: %x\n", buffer[index++]);
index++;
command_value = ((uint32_t)(buffer[index] << 16) & 0xFFFF0000) | buffer[index+1];
index++;
index++;
//printf("command 1: %x\n", buffer[index++]);
//printf("command 2: %x\n", buffer[index++]);
// Housekeeping 1
//printf("%i, housekeeping 1: %x\n", index, buffer[index]);
switch (frame_value) {
case 0:
// 5 V monitor
voltage_monitors[0] = buffer[index++];
break;
case 1:
temperature_monitors[1] = buffer[index++];
break;
case 2:
temperature_monitors[5] = buffer[index++];
break;
case 3:
temperature_monitors[9] = buffer[index++];
break;
default:
//printf("Housekeeping 1: %u\n", buffer[index++]);
index++;
break;
}
formatter_status = buffer[index++]; // printf("FormatterStatus: %u\n", buffer[index++]);
if (getbits(formatter_status, 2, 1)) {
attenuator_actuating = 1;
}
index++;
//printf("HV index %i\n", index);
high_voltage_status = buffer[index] & 0xF;
high_voltage = ((buffer[index] >> 4) & 0xFFF)/8.0;
index++;
// Housekeeping 2
//printf("%i, housekeeping 2: %x\n", index, buffer[index]);
switch (frame_value) {
case 0:
// -5 V monitor
voltage_monitors[1] = buffer[index++];
break;
case 1:
temperature_monitors[2] = buffer[index++];
break;
case 2:
temperature_monitors[6] = buffer[index++];
break;
case 3:
temperature_monitors[10] = buffer[index++];
break;
default:
//printf("Housekeeping 2: %u\n", buffer[index++]);
index++;
break;
}
//printf("Status 0: %x\n", buffer[index++]);
index++;