Rev 32 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line | 
|---|---|---|---|
| 29 | f9daq | 1 | #define USE_DAQ | 
        
| 2 | #define USE_MIKRO | 
        ||
| 3 | |||
| 4 | // Izberi ustrezni interface v meniju projektnega okna | 
        ||
| 5 | //  Options->CompilerDefines (dodaj /DSISVME ali /DWIENVME) | 
        ||
| 6 | #ifdef USE_DAQ | 
        ||
| 7 | #  define USE_CAMAC | 
        ||
| 32 | f9daq | 8 | #  include "c:\HOME\dino\sipmScan\include\camac.h" | 
        
| 29 | f9daq | 9 | #  ifdef SISVME | 
        
| 10 | #    include "sisvme_dll.h" | 
        ||
| 11 | #  endif | 
        ||
| 12 | #  ifdef WIENVME | 
        ||
| 13 | #    include "wienvme_dll.h" | 
        ||
| 14 | #  endif | 
        ||
| 15 | # include "CAENV673A.h"  | 
        ||
| 16 | # include "CAENV462.h"  | 
        ||
| 17 | #endif | 
        ||
| 18 | |||
| 19 | #ifdef USE_MIKRO | 
        ||
| 20 | #  include "MIKRO.h" | 
        ||
| 21 | #endif | 
        ||
| 22 | |||
| 23 | #include <userint.h> | 
        ||
| 24 | #include <ansi_c.h> | 
        ||
| 25 | #include <utility.h> | 
        ||
| 26 | #include <analysis.h> | 
        ||
| 27 | |||
| 32 | f9daq | 28 | #include "c:\HOME\dino\sipmScan\include\sipmScan_ui.h" | 
        
| 29 | f9daq | 29 | |
| 30 | #ifdef USE_DAQ | 
        ||
| 31 | #  define USE_CAMAC | 
        ||
| 32 | #  include "camac.h" | 
        ||
| 33 | #  define NDAC 1 | 
        ||
| 39 | f9daq | 34 | #    define ASD8 0 // channels | 
        
| 35 | #    define AMPDISSH 1 | 
        ||
| 36 | #    define SIPM 2 | 
        ||
| 29 | f9daq | 37 | //#  define CAEN_V673A   0x10110000 // FMF | 
        
| 38 | #  define CAEN_V673A   0x22220000 // IJS | 
        ||
| 39 | #  define CAEN_V462    0x100300 | 
        ||
| 40 | #endif | 
        ||
| 41 | |||
| 42 | #ifdef USE_MIKRO | 
        ||
| 43 | #  define MIKRO_COM 5 | 
        ||
| 44 | #  define MIKRO_X 1 | 
        ||
| 45 | #  define USE_MIKRO_Y | 
        ||
| 46 | #  define MIKRO_Y 2 | 
        ||
| 47 | #  define USE_MIKRO_Z | 
        ||
| 48 | #  define MIKRO_Z 3 | 
        ||
| 49 | #  define STEP_TOLERANCE 1   | 
        ||
| 50 | #endif | 
        ||
| 51 | |||
| 52 | #define MAXCH 512 | 
        ||
| 53 | #define MAX_THREADS 10 | 
        ||
| 54 | |||
| 55 | #define IWAIT 200 | 
        ||
| 56 | |||
| 39 | f9daq | 57 | #define POWERSUPPLY 66 // Power supply voltage for SiPM bias | 
        
| 29 | f9daq | 58 | #define NCH 64 | 
        
| 59 | |||
| 60 | static int p1h, pID, rID, tfID;  | 
        ||
| 61 | static int ph_tdc, ph_adc;  | 
        ||
| 62 | static int dtdc[NCH][2][MAXCH];  | 
        ||
| 63 | static int something[NCH][2][MAXCH];  | 
        ||
| 64 | |||
| 65 | static int daq_on;  | 
        ||
| 66 | static int poolHandle = 0;  | 
        ||
| 67 | static int ntics,dummy;  | 
        ||
| 68 | |||
| 69 | /************************************************************/ | 
        ||
| 70 | void wait_loop(unsigned long iloop)  | 
        ||
| 71 | |||
| 72 | { | 
        ||
| 73 | int i;  | 
        ||
| 74 | |||
| 75 | for (i=0;i<iloop;i++);  | 
        ||
| 76 | return;  | 
        ||
| 77 | } | 
        ||
| 78 | |||
| 79 | int CVICALLBACK cb_timer (int panel, int control, int event, void *callbackData,  | 
        ||
| 80 | int eventData1, int eventData2)  | 
        ||
| 81 | { | 
        ||
| 82 | QueueUserEvent (9000, p1h, P1_TIMER);  | 
        ||
| 83 | return (0);  | 
        ||
| 84 | } | 
        ||
| 85 | |||
| 86 | int update_plots (void)  | 
        ||
| 87 | { | 
        ||
| 88 | int irange, ch;  | 
        ||
| 89 | |||
| 90 | GetCtrlVal (p1h, P1_PLCH, &ch);  | 
        ||
| 91 | |||
| 92 | if (ph_tdc>0) DeleteGraphPlot (p1h, P1_TDC, ph_tdc, VAL_DELAYED_DRAW);  | 
        ||
| 93 | GetCtrlVal (p1h, P1_TDCHL, &irange);  | 
        ||
| 94 | ph_tdc = PlotY (p1h, P1_TDC, &dtdc[ch][irange], MAXCH, VAL_INTEGER,  | 
        ||
| 95 | VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);  | 
        ||
| 96 | |||
| 97 | if (ph_adc>0) DeleteGraphPlot (p1h, P1_ADC, ph_adc, VAL_DELAYED_DRAW);  | 
        ||
| 98 | GetCtrlVal (p1h, P1_ADCHL, &irange);  | 
        ||
| 99 | ph_adc = PlotY (p1h, P1_ADC, &something[ch][irange], MAXCH, VAL_INTEGER,  | 
        ||
| 100 | VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_BLUE);  | 
        ||
| 101 | return (0);  | 
        ||
| 102 | } | 
        ||
| 103 | |||
| 104 | |||
| 105 | int module_header(int recid,unsigned long *data,int len){  | 
        ||
| 106 | data[0] = recid;  | 
        ||
| 107 | data[1] = (len >0)? len : 0 ;  | 
        ||
| 108 | return data[1]+2;  | 
        ||
| 109 | } | 
        ||
| 110 | |||
| 111 | int CVICALLBACK daq_run(void *functionData)  | 
        ||
| 112 | { | 
        ||
| 113 |         // *** | 
        ||
| 114 |         // variable declarations | 
        ||
| 115 | int i,j;  | 
        ||
| 116 | int dtype,ch,rg,adc,cres,bsr;  | 
        ||
| 117 | |||
| 118 |         //unsigned short aa[NCH][4]; | 
        ||
| 119 | unsigned long tdc[NCH];  | 
        ||
| 120 | unsigned long step_minutes, end_time_s, cur_time_s;  | 
        ||
| 121 | int start_hours, start_minutes, start_seconds;  | 
        ||
| 122 | |||
| 123 | int status,fmax,fcount,fev;  | 
        ||
| 124 | int popupret;  | 
        ||
| 125 | |||
| 126 | char dfile[MAX_PATHNAME_LEN],dfile0[MAX_PATHNAME_LEN];  | 
        ||
| 127 | int supr0,tdcmin,fseed;  | 
        ||
| 128 | float frac;  | 
        ||
| 129 | double fracg;  | 
        ||
| 130 | unsigned short dum16;  | 
        ||
| 131 | int count;  | 
        ||
| 132 | int print = 0;  | 
        ||
| 133 | int timer_out = 0;  | 
        ||
| 134 |   //time_t t,told, tstart; | 
        ||
| 135 | int ntrig;  | 
        ||
| 136 | int chip, len;  | 
        ||
| 137 | unsigned long *pdata;  | 
        ||
| 138 | |||
| 139 | FILE *fp;  | 
        ||
| 140 |   #define RUNREC_ID 1 | 
        ||
| 141 |   #define ENDREC_ID 2 | 
        ||
| 142 |   #define POSREC_ID 3 | 
        ||
| 143 |   #define EVTREC_ID 4 | 
        ||
| 144 | |||
| 145 | typedef struct {  | 
        ||
| 146 | unsigned long id,len;  | 
        ||
| 147 | unsigned long fver,time;  | 
        ||
| 148 | unsigned long nev,nch,ped,xy;  | 
        ||
| 149 | long nx,x0,dx,ny,y0,dy;  | 
        ||
| 150 | } RUNREC;  | 
        ||
| 151 |         RUNREC runrec; | 
        ||
| 152 | |||
| 153 | typedef struct {  | 
        ||
| 154 | unsigned long id,len;  | 
        ||
| 155 | unsigned long time;  | 
        ||
| 156 | } ENDREC;  | 
        ||
| 157 |         ENDREC endrec; | 
        ||
| 158 | |||
| 159 | typedef struct {  | 
        ||
| 160 | unsigned long id,len;  | 
        ||
| 161 | unsigned long time;  | 
        ||
| 162 | long ix,x,xset,iy,y,yset;  | 
        ||
| 163 | } POSREC;  | 
        ||
| 164 |         POSREC posrec; | 
        ||
| 165 | long mikroX;  | 
        ||
| 166 | long mikroY;  | 
        ||
| 167 | |||
| 168 | typedef struct {  | 
        ||
| 169 | unsigned long id,len;  | 
        ||
| 170 | unsigned long nev;  | 
        ||
| 171 |     //unsigned short data[NCH]; | 
        ||
| 172 | } EVTREC;  | 
        ||
| 173 |         EVTREC evtrec; | 
        ||
| 174 | #define BSIZE 20000 | 
        ||
| 175 | |||
| 176 | int maxn = BSIZE-4; // 2 words x 2 headers  | 
        ||
| 177 | int tdcdata, edge_type, overflow, tdc_num, channel, ev_dat, last_dat, nval_dat;  | 
        ||
| 178 |         // end of declarations | 
        ||
| 179 |         // *** | 
        ||
| 180 | |||
| 181 | runrec.id = RUNREC_ID;  | 
        ||
| 182 | runrec.len = sizeof(runrec);  | 
        ||
| 183 |         //runrec.fver = 0x10000; | 
        ||
| 184 | runrec.nch = NCH;  | 
        ||
| 185 | GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy);//runrec.xy = 1;  | 
        ||
| 186 | GetCtrlVal(p1h, P1_DEBUG, &print);  | 
        ||
| 187 | endrec.id = ENDREC_ID;  | 
        ||
| 188 | endrec.len = sizeof(endrec);  | 
        ||
| 189 | posrec.id = POSREC_ID;  | 
        ||
| 190 | posrec.len = sizeof(posrec);  | 
        ||
| 191 | evtrec.id = EVTREC_ID;  | 
        ||
| 192 | evtrec.len = sizeof(evtrec);  | 
        ||
| 193 | |||
| 194 | |||
| 195 | cres = 0;  | 
        ||
| 196 | |||
| 197 | GetCtrlVal (p1h, P1_NEVE, &runrec.nev);  | 
        ||
| 198 | GetCtrlVal (p1h, P1_PEDESTAL, &runrec.ped);  | 
        ||
| 199 | |||
| 200 | GetCtrlVal (p1h, P1_NX, &runrec.nx);  | 
        ||
| 201 | GetCtrlVal (p1h, P1_XSTEP, &runrec.dx);  | 
        ||
| 202 | GetCtrlVal (p1h, P1_XMIN, &runrec.x0);  | 
        ||
| 203 | GetCtrlVal (p1h, P1_NY, &runrec.ny);  | 
        ||
| 204 | GetCtrlVal (p1h, P1_YSTEP, &runrec.dy);  | 
        ||
| 205 | GetCtrlVal (p1h, P1_YMIN, &runrec.y0);  | 
        ||
| 206 | |||
| 207 | GetCtrlVal (p1h, P1_NMIN, &step_minutes);  | 
        ||
| 208 | GetSystemTime(&start_hours, &start_minutes, &start_seconds);  | 
        ||
| 209 |         //cur_time_s = start_hours*3600 + start_minutes*60 + start_seconds; | 
        ||
| 210 | time(&cur_time_s);  | 
        ||
| 211 | end_time_s = cur_time_s + step_minutes*60;  | 
        ||
| 212 | printf("START:%2d-%2d-%2d (cur_time = %u s, end_time = %u s)\n", start_hours, start_minutes, start_seconds, cur_time_s, end_time_s);  | 
        ||
| 213 | |||
| 214 |         //GetCtrlVal (p1h, P1_DSAVE, &dsave); | 
        ||
| 215 |   //if (dsave) { | 
        ||
| 216 | GetCtrlVal (p1h, P1_DFILE, dfile0);  | 
        ||
| 217 | |||
| 218 | fev=0;  | 
        ||
| 219 | fcount=1;  | 
        ||
| 220 | GetCtrlVal (p1h, P1_NEWF, &fmax);  | 
        ||
| 221 | fmax*=1000000;//fmax in Mega Bytes  | 
        ||
| 222 |   //} | 
        ||
| 223 | GetCtrlVal (p1h, P1_SUPR, &supr0);  | 
        ||
| 224 | if (supr0) {  | 
        ||
| 225 | GetCtrlVal (p1h, P1_TDCMIN, &tdcmin);  | 
        ||
| 226 | GetCtrlVal (p1h, P1_FRAC, &frac);  | 
        ||
| 227 |         } | 
        ||
| 228 | |||
| 229 | #ifdef USE_DAQ | 
        ||
| 230 | V673A_map(0,CAEN_V673A,0);  | 
        ||
| 231 | V673A_init(0);  | 
        ||
| 232 | |||
| 233 | V462_map(0,CAEN_V462,0);  | 
        ||
| 234 | V462_set0(0,1);  | 
        ||
| 235 | #endif | 
        ||
| 236 | |||
| 237 |   //if (dsave) { | 
        ||
| 238 | sprintf(dfile,"%s_file%02d.dat",dfile0,fcount);  | 
        ||
| 239 | |||
| 240 | fp = fopen (dfile, "wb");  | 
        ||
| 241 | time (&runrec.time);  | 
        ||
| 242 | status = fwrite (&runrec, 1, runrec.len, fp);  | 
        ||
| 243 |         //} | 
        ||
| 244 | if (supr0) {  | 
        ||
| 245 | fseed = runrec.time & 0x7fffffff;  | 
        ||
| 246 | Uniform (1, fseed, &fracg);  | 
        ||
| 247 |         } | 
        ||
| 248 | |||
| 249 | for (posrec.ix=0; posrec.ix<runrec.nx; posrec.ix++) {  | 
        ||
| 250 | posrec.xset = runrec.x0 + posrec.ix*runrec.dx;  | 
        ||
| 251 | #ifdef USE_MIKRO | 
        ||
| 252 | do {  | 
        ||
| 32 | f9daq | 253 | if (print) printf("MIKRO_MoveTo (1, x);%d\n",posrec.xset);  | 
        
| 29 | f9daq | 254 | MIKRO_MoveTo (MIKRO_X, posrec.xset);  | 
        
| 255 | MIKRO_GetPosition(MIKRO_X, &mikroX);  | 
        ||
| 32 | f9daq | 256 | if (print) printf("%d\n", abs(posrec.xset - mikroX));  | 
        
| 29 | f9daq | 257 | } while (abs(posrec.xset - mikroX) > STEP_TOLERANCE);  | 
        
| 258 | #endif | 
        ||
| 259 | posrec.xset = mikroX; // true value  | 
        ||
| 260 | SetCtrlVal (p1h, P1_X, posrec.xset);  | 
        ||
| 261 | SetCtrlVal (p1h, P1_IX, posrec.ix);  | 
        ||
| 262 | for (posrec.iy=0; posrec.iy<runrec.ny; posrec.iy++) {  | 
        ||
| 263 | posrec.yset = runrec.y0 + posrec.iy*runrec.dy;  | 
        ||
| 264 | #ifdef USE_MIKRO_Y | 
        ||
| 32 | f9daq | 265 | if (print) printf("MIKRO_MoveTo (2, y);%d\n",posrec.yset);  | 
        
| 29 | f9daq | 266 | do {  | 
        
| 267 | MIKRO_MoveTo (MIKRO_Y, posrec.yset);  | 
        ||
| 268 | MIKRO_GetPosition(MIKRO_Y, &mikroY);  | 
        ||
| 32 | f9daq | 269 | if (print) printf("%d\n", abs(posrec.yset - mikroY));  | 
        
| 29 | f9daq | 270 | } while (abs(posrec.yset - mikroY) > STEP_TOLERANCE);  | 
        
| 32 | f9daq | 271 | if (print) printf("->MIKRO_MoveTo (2, y);%d\n",posrec.yset);  | 
        
| 29 | f9daq | 272 | #endif | 
        
| 273 | posrec.yset = mikroY;  | 
        ||
| 274 | SetCtrlVal (p1h, P1_Y, posrec.yset);  | 
        ||
| 275 | SetCtrlVal (p1h, P1_IY, posrec.iy);  | 
        ||
| 276 |       //if (dsave) { | 
        ||
| 277 | if (fmax && (ftell(fp) > fmax)) {  | 
        ||
| 278 | fcount+=1;  | 
        ||
| 279 | sprintf(dfile,"%s_file%02d.dat",dfile0,fcount);  | 
        ||
| 280 | fclose(fp);  | 
        ||
| 281 | fp = fopen (dfile, "wb");  | 
        ||
| 282 |       } | 
        ||
| 283 | |||
| 284 | time (&posrec.time);  | 
        ||
| 285 | status = fwrite (&posrec, 1, posrec.len, fp);  | 
        ||
| 286 | if (print) printf("POSREC status %d len %d\n", status, posrec.len);  | 
        ||
| 287 |                   //} | 
        ||
| 288 | |||
| 289 | // clear the plots | 
        ||
| 290 | for (j=0;j<NCH;j++) {  | 
        ||
| 291 | for (i=0;i<MAXCH;i++){  | 
        ||
| 292 | dtdc[j][0][i]=0;  | 
        ||
| 293 | dtdc[j][1][i]=0;  | 
        ||
| 294 |         } | 
        ||
| 295 |       } | 
        ||
| 296 | |||
| 297 | evtrec.nev=1;  | 
        ||
| 298 |       //time(&t); | 
        ||
| 299 |       //tstart=t; | 
        ||
| 300 | if (print) printf("RUN START.\n");  | 
        ||
| 301 | |||
| 302 | do {  | 
        ||
| 303 | unsigned long word[BSIZE];  | 
        ||
| 304 | count=0;  | 
        ||
| 305 | ntics=0;  | 
        ||
| 306 | if (print) printf("Event counter: %d\n", evtrec.nev);  | 
        ||
| 307 | |||
| 308 |                 //if (t!=told ) printf("%d in %2.2f min daq::event() %s\n",i, (double)(t-tstart)/60., ctime(&t));  | 
        ||
| 309 | |||
| 310 | #ifdef USE_DAQ | 
        ||
| 311 | #ifdef CAEN_V673A | 
        ||
| 312 | |||
| 313 | |||
| 314 | V462_start0(0);  | 
        ||
| 315 |                 //tmlnk (tout);  | 
        ||
| 316 | do {  | 
        ||
| 317 | ntrig = V673A_ntrig(0);  | 
        ||
| 318 | } while (ntrig==0 &&(ntics<2));  | 
        ||
| 319 |         //tmulk(); | 
        ||
| 320 |         //if (timer_out) { | 
        ||
| 321 |         //   fprintf(stderr,"-----------------> V673a timeout !!!\n"); | 
        ||
| 322 |         //   V673A_clallb(0);  | 
        ||
| 323 |         //   continue; | 
        ||
| 324 |         //} | 
        ||
| 325 | |||
| 326 | if (ntics>=2) {  | 
        ||
| 327 | fprintf(stderr,"-----------------> V673a timeout !!!\n");  | 
        ||
| 328 | printf("Timer tics %d\n", ntics);  | 
        ||
| 329 | ntics = 0;  | 
        ||
| 330 | evtrec.nev--;  | 
        ||
| 331 | V673A_clallb(0);  | 
        ||
| 332 | continue;  | 
        ||
| 333 |           } | 
        ||
| 334 | |||
| 335 | if (ntrig>0){  | 
        ||
| 336 |           // chip readout | 
        ||
| 337 | for (chip=0;chip<2;chip++){  | 
        ||
| 338 | if (chip) len = V673A_read1 (0, &word[count+2],maxn-count-2);  | 
        ||
| 339 | else len = V673A_read0 (0, &word[count+2],maxn-count-2);  | 
        ||
| 340 | |||
| 341 | if (len>16*32*4){  | 
        ||
| 342 | if (print) printf("Length > 2048 per chip!\n");  | 
        ||
| 343 | chip=2;  | 
        ||
| 344 | V673A_clallb(0);  | 
        ||
| 345 | evtrec.nev--;  | 
        ||
| 346 | continue;  | 
        ||
| 347 |             } | 
        ||
| 348 | pdata=&word[count+2];  | 
        ||
| 349 | for(i=0;i< len;i++){  | 
        ||
| 350 | tdcdata = pdata[i] & 0xFFFF ;  | 
        ||
| 351 | edge_type = (pdata[i]>>16)&0x1 ;  | 
        ||
| 352 | overflow = (pdata[i]>>17)&0x1 ;  | 
        ||
| 353 | tdc_num = (pdata[i]>>25)&0x1 ;  | 
        ||
| 354 | channel = ((pdata[i]>>18)&0x1F) |tdc_num<<5 ;  | 
        ||
| 355 | ev_dat = (pdata[i]>>23)&0x1 ;  | 
        ||
| 356 | last_dat = (pdata[i]>>30)&0x1 ;  | 
        ||
| 357 | nval_dat = (pdata[i]>>31)&0x1 ;  | 
        ||
| 358 | if (ev_dat) {  | 
        ||
| 359 | if(print) printf("Event %d\n",tdcdata);  | 
        ||
| 360 | } else {  | 
        ||
| 361 | dtdc[channel][edge_type][tdcdata]++;  | 
        ||
| 362 | |||
| 363 | if (print) printf  | 
        ||
| 364 | ("ch=%d edge=%d ev=%d data=%d last=%d nval=%d\n",channel, edge_type,ev_dat,tdcdata,last_dat,nval_dat);  | 
        ||
| 365 |               } | 
        ||
| 366 |             } | 
        ||
| 367 | if (count+2 < maxn) count+=module_header(0x140+chip,&word[count],len);  | 
        ||
| 368 |                   } | 
        ||
| 369 | if (print) printf("Received triggers: %d \n", ntrig);  | 
        ||
| 370 | } else {  | 
        ||
| 371 | if (ntrig==0) {  | 
        ||
| 372 | fprintf(stderr,"-----------------> no trigger");  | 
        ||
| 373 | break;  | 
        ||
| 374 | } else {  | 
        ||
| 375 | if (print) fprintf(stderr,"-----------------> wrong number of triggers in chips V673a !!!\n");  | 
        ||
| 376 |           } | 
        ||
| 377 | V673A_clallb(0);  | 
        ||
| 378 | evtrec.nev--;  | 
        ||
| 379 | continue;  | 
        ||
| 380 |         } | 
        ||
| 381 | |||
| 382 | V673A_clallb(0);  | 
        ||
| 383 | #endif | 
        ||
| 384 | #endif | 
        ||
| 385 | |||
| 386 | |||
| 387 | GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy);  | 
        ||
| 388 | evtrec.len = count*sizeof(unsigned long) + sizeof(evtrec);  | 
        ||
| 389 | status = fwrite (&evtrec, 1, sizeof(evtrec), fp);  | 
        ||
| 390 | status = fwrite (&word, 1, count*sizeof(unsigned long), fp);  | 
        ||
| 391 | |||
| 392 | |||
| 393 | if (!(evtrec.nev%1000)) SetCtrlVal (p1h, P1_CEVE, evtrec.nev);  | 
        ||
| 394 | |||
| 395 | if (!((evtrec.nev+1)%1000)) {  | 
        ||
| 396 | if (fmax && (ftell(fp) > fmax)) {  | 
        ||
| 397 | time (&endrec.time);  | 
        ||
| 398 | status = fwrite (&endrec, 1, endrec.len, fp);  | 
        ||
| 399 | fcount+=1;  | 
        ||
| 400 | sprintf(dfile,"%s_file%02d.dat",dfile0,fcount);  | 
        ||
| 401 | fclose(fp);  | 
        ||
| 402 | fp = fopen (dfile, "wb");  | 
        ||
| 403 |             } | 
        ||
| 404 |         } | 
        ||
| 405 | |||
| 406 | if(step_minutes > 0) {  | 
        ||
| 407 | GetSystemTime(&start_hours, &start_minutes, &start_seconds);  | 
        ||
| 408 |                                   //cur_time_s = start_hours*3600 + start_minutes*60 + start_seconds; | 
        ||
| 409 | time(&cur_time_s);  | 
        ||
| 410 | if(cur_time_s >= end_time_s) {  | 
        ||
| 411 | end_time_s = cur_time_s + step_minutes*60;  | 
        ||
| 412 | printf("STEP (nev):%2d-%2d-%2d @ %d\n", start_hours, start_minutes, start_seconds, posrec.xset);  | 
        ||
| 413 | break;  | 
        ||
| 414 |                 } | 
        ||
| 415 |         } | 
        ||
| 416 |         //told=t; | 
        ||
| 417 | |||
| 418 | GetCtrlVal(p1h, P1_DAQ, &daq_on);  | 
        ||
| 419 | |||
| 420 | } while (evtrec.nev++<runrec.nev && daq_on);  | 
        ||
| 421 | if (!daq_on) break;  | 
        ||
| 422 | } // x loop  | 
        ||
| 423 | if (!daq_on) break;  | 
        ||
| 424 | } // y loop  | 
        ||
| 425 | |||
| 426 |   //if (dsave) { | 
        ||
| 427 | time (&endrec.time);  | 
        ||
| 428 | status = fwrite (&endrec, 1, endrec.len, fp);  | 
        ||
| 429 | if (print) printf("ENDREC status %d len %d\n", status, endrec.len);  | 
        ||
| 430 | fclose(fp);  | 
        ||
| 431 |         //} | 
        ||
| 432 | GetSystemTime(&start_hours, &start_minutes, &start_seconds);  | 
        ||
| 433 | printf("STOP:%2d-%2d-%2d (start_time = %u s, end_time = %u s)\n", start_hours, start_minutes, start_seconds, runrec.time, endrec.time);  | 
        ||
| 434 | printf("Elapsed time: %u s.\n", endrec.time-runrec.time);  | 
        ||
| 435 | |||
| 436 | daq_on=0;  | 
        ||
| 437 | SetCtrlVal (p1h, P1_DAQ, daq_on);  | 
        ||
| 438 | SetCtrlVal (p1h, P1_CEVE, evtrec.nev);  | 
        ||
| 439 | if (print) printf("RUN END. \n\n");  | 
        ||
| 440 | |||
| 441 | return 0;  | 
        ||
| 442 | } | 
        ||
| 443 | |||
| 444 | |||
| 445 | int SetDac(int ch, double val){  | 
        ||
| 446 | |||
| 447 | const unsigned int maxdac=0xFFF;  | 
        ||
| 448 | const double RANGE = +9.9976; //V  | 
        ||
| 449 | unsigned int dac;  | 
        ||
| 450 | |||
| 451 | if ((val > RANGE) || (val < 0)) {  | 
        ||
| 452 | printf("DAC value OUT OF RANGE!!!\n");  | 
        ||
| 453 | return(-1);  | 
        ||
| 454 |         } | 
        ||
| 455 | |||
| 456 | dac = (val/RANGE)*maxdac;  | 
        ||
| 457 | CSSA_W(NDAC,ch,16,&dac);  | 
        ||
| 458 | printf("DAC ch %d set to %f V dac=%d\n", ch, val, dac);  | 
        ||
| 459 | return 0;  | 
        ||
| 460 | } | 
        ||
| 461 | |||
| 462 | int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,  | 
        ||
| 463 | LPSTR lpszCmdLine, int nCmdShow)  | 
        ||
| 464 | { | 
        ||
| 465 | int i,j,status,refon;  | 
        ||
| 466 | long int xpos, ypos, zpos;  | 
        ||
| 467 | char dfile[300];  | 
        ||
| 468 | FILE *fp;  | 
        ||
| 469 | |||
| 470 | typedef struct {  | 
        ||
| 471 | unsigned long id,len;  | 
        ||
| 472 | unsigned long fver,time;  | 
        ||
| 473 | unsigned long nev,nch,ped,xy;  | 
        ||
| 474 | long nx,x0,dx,ny,y0,dy;  | 
        ||
| 475 | } RUNREC;  | 
        ||
| 476 |   RUNREC runrec; | 
        ||
| 477 | |||
| 478 | if (InitCVIRTE (hInstance, 0, 0) == 0)  | 
        ||
| 479 | return -1; /* out of memory */  | 
        ||
| 480 | |||
| 481 | SetSleepPolicy(VAL_SLEEP_MORE);  | 
        ||
| 482 | CmtNewThreadPool (MAX_THREADS, &poolHandle);  | 
        ||
| 483 | |||
| 484 | SetStdioWindowOptions (1000, 0, 0);  | 
        ||
| 485 | SetStdioWindowSize (200, 560);  | 
        ||
| 486 | SetStdioWindowPosition (825, 250);  | 
        ||
| 487 | |||
| 488 | #ifdef USE_DAQ | 
        ||
| 489 | VME_START(NULL);  | 
        ||
| 490 | #endif | 
        ||
| 491 | |||
| 492 | #ifdef USE_MIKRO | 
        ||
| 493 | MIKRO_Open (MIKRO_COM);  | 
        ||
| 494 | MIKRO_Init (MIKRO_X,0);  | 
        ||
| 495 | #ifdef USE_MIKRO_Y | 
        ||
| 496 | MIKRO_Init (MIKRO_Y,0);  | 
        ||
| 497 | #endif | 
        ||
| 498 | #ifdef USE_MIKRO_Z | 
        ||
| 499 | MIKRO_Init (MIKRO_Z,0);  | 
        ||
| 500 | #endif | 
        ||
| 501 | #endif | 
        ||
| 502 | |||
| 503 | if ((p1h = LoadPanel (0, "include/sipmScan_ui.uir", P1)) < 0) return -1;  | 
        ||
| 504 | DisplayPanel (p1h);  | 
        ||
| 505 | SetCtrlAttribute (p1h, P1_PLCH, ATTR_MAX_VALUE, NCH-1);  | 
        ||
| 506 | |||
| 507 | GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy);  | 
        ||
| 508 | SetCtrlVal(p1h, P1_ADCHL, runrec.xy-2);  | 
        ||
| 509 | |||
| 510 | QueueUserEvent (1000, p1h, P1_RESET);  | 
        ||
| 511 | QueueUserEvent (1000, p1h, P1_ASD8THR);  | 
        ||
| 512 | QueueUserEvent (1000, p1h, P1_AMPDISSHTHR);  | 
        ||
| 39 | f9daq | 513 | QueueUserEvent (1000, p1h, P1_BIAS);  | 
        
| 514 | |||
| 515 | printf("BIAS offset set to: %d\n", POWERSUPPLY);  | 
        ||
| 516 | |||
| 29 | f9daq | 517 | do {  | 
        
| 518 | GetUserEvent (1, &pID, &rID);  | 
        ||
| 519 | switch (rID) {  | 
        ||
| 520 | case P1_TIMER:  | 
        ||
| 521 | ntics+=1;  | 
        ||
| 522 | GetCtrlVal (p1h, P1_REFON, &refon);  | 
        ||
| 523 | if (refon) update_plots();  | 
        ||
| 524 | break;  | 
        ||
| 525 | case P1_REFRESH:  | 
        ||
| 526 | update_plots();  | 
        ||
| 527 | break;  | 
        ||
| 528 | case P1_DAQ:  | 
        ||
| 529 | GetCtrlVal (p1h, P1_DAQ, &daq_on);  | 
        ||
| 530 | if (daq_on) {  | 
        ||
| 531 | CmtScheduleThreadPoolFunction (poolHandle, daq_run, (void *)&dummy, &tfID);  | 
        ||
| 532 | } else {  | 
        ||
| 533 | CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID,  | 
        ||
| 534 | OPT_TP_PROCESS_EVENTS_WHILE_WAITING);  | 
        ||
| 535 | CmtReleaseThreadPoolFunctionID (poolHandle, tfID);  | 
        ||
| 536 |             } | 
        ||
| 537 | break;  | 
        ||
| 538 | |||
| 539 | case P1_ZSET:  | 
        ||
| 540 | if (!daq_on) {  | 
        ||
| 541 | GetCtrlVal (p1h, P1_ZSET, &zpos);  | 
        ||
| 542 | #ifdef USE_MIKRO_Z | 
        ||
| 543 | MIKRO_MoveTo (MIKRO_Z, zpos);  | 
        ||
| 544 | #endif | 
        ||
| 545 |         } | 
        ||
| 546 | break;  | 
        ||
| 547 | case P1_REREAD:  | 
        ||
| 548 | if (!daq_on) {  | 
        ||
| 549 | status = FileSelectPopup ("", "*.dat", ".dat",  | 
        ||
| 550 | "Izberi datoteko s podatki",  | 
        ||
| 551 | VAL_LOAD_BUTTON, 0, 0, 1, 0, dfile);  | 
        ||
| 552 | if (status==1) {  | 
        ||
| 553 | fp = fopen (dfile, "rb");  | 
        ||
| 554 | status = fread (&runrec, 1, sizeof(runrec), fp);  | 
        ||
| 555 | fclose(fp);  | 
        ||
| 556 | if (runrec.id==1) {  | 
        ||
| 557 | SetCtrlVal (p1h, P1_NX, runrec.nx);  | 
        ||
| 558 | SetCtrlVal (p1h, P1_XSTEP, runrec.dx);  | 
        ||
| 559 | SetCtrlVal (p1h, P1_XMIN, runrec.x0);  | 
        ||
| 560 | SetCtrlVal (p1h, P1_NY, runrec.ny);  | 
        ||
| 561 | SetCtrlVal (p1h, P1_YSTEP, runrec.dy);  | 
        ||
| 562 | SetCtrlVal (p1h, P1_YMIN, runrec.y0);  | 
        ||
| 563 | SetCtrlVal (p1h, P1_NEVE, runrec.nev);  | 
        ||
| 564 |             } | 
        ||
| 565 |               } | 
        ||
| 566 |         } | 
        ||
| 567 | break;  | 
        ||
| 568 | case P1_MGET:  | 
        ||
| 569 |           #ifdef USE_MIKRO | 
        ||
| 570 | MIKRO_GetPosition(MIKRO_X,&xpos);  | 
        ||
| 571 | Delay(0.01);  | 
        ||
| 572 | SetCtrlVal (p1h, P1_X, xpos);  | 
        ||
| 573 |           #ifdef USE_MIKRO_Y     | 
        ||
| 574 | MIKRO_GetPosition(MIKRO_Y,&ypos);  | 
        ||
| 575 | Delay(0.01);  | 
        ||
| 576 | SetCtrlVal (p1h, P1_Y, ypos);  | 
        ||
| 577 |           #endif | 
        ||
| 578 |           #ifdef USE_MIKRO_Z     | 
        ||
| 579 | MIKRO_GetPosition(MIKRO_Z,&zpos);  | 
        ||
| 580 | Delay(0.01);  | 
        ||
| 581 | SetCtrlVal (p1h, P1_Z, zpos);  | 
        ||
| 582 |           #endif  | 
        ||
| 583 |           #endif | 
        ||
| 584 | break;  | 
        ||
| 585 | case P1_HO:  | 
        ||
| 586 | if (!daq_on) {  | 
        ||
| 587 | SetWaitCursor (1);  | 
        ||
| 588 | #ifdef USE_MIKRO | 
        ||
| 589 | MIKRO_ReferenceMove (MIKRO_X);  | 
        ||
| 590 |                 #ifdef USE_MIKRO_Y       | 
        ||
| 591 | MIKRO_ReferenceMove (MIKRO_Y);  | 
        ||
| 592 |                 #endif | 
        ||
| 593 |                 #ifdef USE_MIKRO_Z | 
        ||
| 594 | MIKRO_ReferenceMove (MIKRO_Z);  | 
        ||
| 595 |                 #endif | 
        ||
| 596 | #endif | 
        ||
| 597 | SetWaitCursor (0);  | 
        ||
| 598 |             } | 
        ||
| 599 | break;  | 
        ||
| 600 | case P1_RESET:  | 
        ||
| 601 | for (j=0;j<NCH;j++) {  | 
        ||
| 602 | for (i=0;i<MAXCH;i++){  | 
        ||
| 603 | dtdc[j][0][i]=0;  | 
        ||
| 604 | dtdc[j][1][i]=0;  | 
        ||
| 605 | |||
| 606 |           } | 
        ||
| 607 |         } | 
        ||
| 608 | update_plots();  | 
        ||
| 609 | break;  | 
        ||
| 610 | case P1_TDCLOG:  | 
        ||
| 611 | GetCtrlVal (p1h, P1_TDCLOG, &status);  | 
        ||
| 612 | SetCtrlAttribute (p1h, P1_TDC, ATTR_YMAP_MODE, status);  | 
        ||
| 613 | update_plots();  | 
        ||
| 614 | break;  | 
        ||
| 615 | case P1_ADCLOG:  | 
        ||
| 616 | GetCtrlVal (p1h, P1_ADCLOG, &status);  | 
        ||
| 617 | SetCtrlAttribute (p1h, P1_ADC, ATTR_YMAP_MODE, status);  | 
        ||
| 618 | update_plots();  | 
        ||
| 619 | break;  | 
        ||
| 620 | case P1_ASD8THR:  | 
        ||
| 621 |             { | 
        ||
| 622 | double value;  | 
        ||
| 623 | GetCtrlVal (p1h, P1_SASD8THR, &value);  | 
        ||
| 39 | f9daq | 624 | SetDac(ASD8, value);  | 
        
| 29 | f9daq | 625 | |
| 626 |             } | 
        ||
| 627 | break;  | 
        ||
| 628 | case P1_AMPDISSHTHR:  | 
        ||
| 629 |             { | 
        ||
| 630 | double value;  | 
        ||
| 631 | GetCtrlVal (p1h, P1_SAMPDISSHTHR, &value);  | 
        ||
| 39 | f9daq | 632 | SetDac(AMPDISSH, value);  | 
        
| 29 | f9daq | 633 | |
| 634 |             }   | 
        ||
| 635 | break;  | 
        ||
| 39 | f9daq | 636 | case P1_BIAS:  | 
        
| 637 |                 { | 
        ||
| 638 | double value;  | 
        ||
| 639 | GetCtrlVal (p1h, P1_SBIAS, &value);  | 
        ||
| 640 | value -= POWERSUPPLY;  | 
        ||
| 641 | SetDac(SIPM, value);  | 
        ||
| 642 |                 } | 
        ||
| 643 | break;  | 
        ||
| 29 | f9daq | 644 | |
| 645 |         } | 
        ||
| 646 | |||
| 647 | } while ((rID != P1_EXIT)||daq_on);  | 
        ||
| 648 | |||
| 649 | CmtDiscardThreadPool (poolHandle);  | 
        ||
| 650 | DiscardPanel (p1h);  | 
        ||
| 651 | |||
| 652 | #ifdef USE_MIKRO | 
        ||
| 653 | MIKRO_Close ();  | 
        ||
| 654 | #endif | 
        ||
| 655 | |||
| 656 | #ifdef USE_DAQ | 
        ||
| 657 | VME_STOP();  | 
        ||
| 658 | #endif   | 
        ||
| 659 | return 0;  | 
        ||
| 660 | |||
| 661 | } |