main.c 12.4 KB
Newer Older
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
/***********************************************************************************************
* MODULE:     N957 Demo application
* PURPOSE:    
* WRITTEN BY: NDA
* COPYRIGHT:  CAEN S.p.A. all rights reserved
* USAGE:      
************************************************************************************************/
////////////////////////////////////////////
// File includes
////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/time.h>
#include <memory.h>
#include "../include/common_defs.h"
#include "../include/N957Lib.h"
#include "../include/user_settings.h"
#include "../include/keyb.h"

////////////////////////////////////////////
//File local defines
#define Sleep(t)  usleep((t)*1000)

#define N957_SAMPLE_NUM_BITS	13
#define N957_MAX_HISTO_SAMPLES	(1<<N957_SAMPLE_NUM_BITS)
Rafaela Meissner's avatar
Rafaela Meissner committed
28
29
#define SAVED_FILE "save.dat"
#define HISTO_FILENAME "histo.dat"		//!not used
30
31
32
33
34
35
36
37
38
39
40
41
42

typedef struct {
	BOOL paused;
	BOOL start;
	BOOL error_9;
	BOOL gate;
} Control; 

////////////////////////////////////////////
// File local variables declaration

////////////////////////////////////////////
// Global visible variables declaration 
Rafaela Meissner's avatar
Rafaela Meissner committed
43
44
user_setting_data user_setting;
Control system_state;
45
46
47

////////////////////////////////////////////
// File local methods declaration
Rafaela Meissner's avatar
Rafaela Meissner committed
48
49
50
51
52
53
54
void init_system(N957_UINT16 *data_buff, unsigned long *histo_buff, unsigned int *histogram);
void start(N957_UINT32 data32);
void dump(N957_UINT32 data32, short int factor, unsigned int *histogram, unsigned int nmr_ADC_pause, unsigned int paused_time, unsigned int tempo_pause_vivo, N957_UINT16 data_buff);
void pause_system(N957_UINT32 *data32, unsigned int *nmr_ADC_pause, unsigned int *paused_time, unsigned int *tempo_pause_vivo, N957_UINT16 data_buff, unsigned long histo_buff);
int determine_factor(int command);
void save_file(unsigned int *histogram);
void load_file(short int factor, unsigned int *histogram);
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

// -----------------------------------------------------------
// get time in milliseconds since first procedure call
// -----------------------------------------------------------

/*************************************************************
* METHOD:     main
* PURPOSE:    Main program
* PARAMETERS: <in> int argc: number of arguments
*             <in> void *argv[]: arguments' list 
* RETURN:     ret_val
* USAGE:      allowed command line input parameters:
*************************************************************/
int main(int argc, char **argv)
{	
Rafaela Meissner's avatar
Rafaela Meissner committed
70
	//data of histogram	
71
72
73
	N957_UINT16 *data_buff= NULL;					// read data buffer
	unsigned long *histo_buff= NULL;				// Histogram data buffer
	unsigned int histogram[8192];
Rafaela Meissner's avatar
Rafaela Meissner committed
74
	short int factor = 16;
75

Rafaela Meissner's avatar
Rafaela Meissner committed
76
77
78
79
	//auxiliaries
	int w, i, command;
	unsigned int temp = 0;
	char aux_string[ 100]; 
80
	
Rafaela Meissner's avatar
Rafaela Meissner committed
81
82
83
84
	unsigned int lldt=10;				//?????
			
	unsigned int paused_time = 0;		//estrutura?
	unsigned int nmr_ADC_pause=0;//????
85
	unsigned int tempo_pause_vivo=0;
Rafaela Meissner's avatar
Rafaela Meissner committed
86
	N957_UINT32 data32;	//????
87

Rafaela Meissner's avatar
Rafaela Meissner committed
88
	N957_ConfigROM  ROM;	//used in function not called by python code
89

Rafaela Meissner's avatar
Rafaela Meissner committed
90
	int ret_val= 0;						//?????
91

Rafaela Meissner's avatar
Rafaela Meissner committed
92
	N957_UINT16 data_read;
93
94
	int32_t tot_data_read= 0;

Rafaela Meissner's avatar
Rafaela Meissner committed
95
	init_system(data_buff, histo_buff, histogram);
96

Rafaela Meissner's avatar
Rafaela Meissner committed
97
98
99
100
	if((argc+'0')>= 2)
    	user_setting.m_bd_num= atoi(argv[1]);
	else
		user_setting.m_bd_num=0; // tem problemas "falha de segmentação", mas eu n to usando.
101
	
Rafaela Meissner's avatar
Rafaela Meissner committed
102
	while(1)
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
	{
		//TRACE("flag :1 \n");
		if(system_state.start)
		{
			data_read= user_setting.m_bldim;
	
			if( user_setting.m_max_num_samples > 0)
			{
				if(tot_data_read >= user_setting.m_max_num_samples)
				{
					ret_val= 0;
					//goto exit_point;
				}
		
				if(user_setting.m_max_num_samples- tot_data_read< user_setting.m_bldim) 
				{
					data_read= ( N957_UINT16)(int32_t)(user_setting.m_max_num_samples - tot_data_read);
				}
			}
	
			if( N957_ReadData( user_setting.m_N957_handle, data_buff, &data_read)) 
Rafaela Meissner's avatar
Rafaela Meissner committed
124
			{         
125
126
				if(!system_state.error_9)
				{
Rafaela Meissner's avatar
Rafaela Meissner committed
127
128
                    system_state.error_9=TRUE;
                	TRACE("error_9\n");
129
				}
Rafaela Meissner's avatar
Rafaela Meissner committed
130
				// goto exit_point;
131
132
			}
            else 
Rafaela Meissner's avatar
Rafaela Meissner committed
133
134
            	system_state.error_9=FALSE;

135
136
137
			tot_data_read+= data_read;
		}
		if( kbhit()) {
Rafaela Meissner's avatar
Rafaela Meissner committed
138
139
140
			command = getch();
			switch(command) {
				case 'q':
141
					goto exit_point;
Rafaela Meissner's avatar
Rafaela Meissner committed
142
143
				case 's':
					start(data32);
144
					break;
Rafaela Meissner's avatar
Rafaela Meissner committed
145
				case 'r':
146
					// reset the histogram here
Rafaela Meissner's avatar
Rafaela Meissner committed
147
					//memset( histo_buff, 0, N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));										
148
					for(w=0; w<8192; w++)
Rafaela Meissner's avatar
Rafaela Meissner committed
149
						histogram[w] = 0;
150
151
152
153
154
155
156
157
158
					if( N957_SwClear(user_setting.m_N957_handle))
					{
						ret_val= -25;
						goto exit_point;
					}
					paused_time = 0;
					nmr_ADC_pause=0;
					tempo_pause_vivo=0;				
					break;
Rafaela Meissner's avatar
Rafaela Meissner committed
159
160
				case 'p':
					pause_system(&data32, &nmr_ADC_pause, &paused_time, &tempo_pause_vivo, *data_buff, *histo_buff);
161
					break;
Rafaela Meissner's avatar
Rafaela Meissner committed
162
163
				case 'i': // transmit info to python
					dump(data32, factor, histogram, nmr_ADC_pause, paused_time, tempo_pause_vivo, *data_buff);
164
					break;
Rafaela Meissner's avatar
Rafaela Meissner committed
165
				case 'j': // funcao testada
166
167
168
169
170
171
172
173
174
					lldt= ((getchar())*10 + getchar());
					N957_SetLLD( user_setting.m_N957_handle, lldt);
					break;
				case 'l':																//!NOT CALLED BY PYTHON CODE
					goto exit_point;
					//N957_SetLLD( user_setting.m_N957_handle, lldt);
					//lldt=0;
					break;
				case 'z': // salva o arquivo  em "save.dat" o qual vai ser renomeado pelo programa em python //!NOT CALLED BY PYTHON CODE
Rafaela Meissner's avatar
Rafaela Meissner committed
175
					save_file(histogram);					
176
					break;
Rafaela Meissner's avatar
Rafaela Meissner committed
177
178
				case 'y': // Abre o arquivo com o nome "save.dat"
					load_file(factor, histogram);
179
180
181
182
183
184
185
186
					break;
		        case 'k':																			//!NOT CALLED BY PYTHON CODE
					TRACE("OK\n");

					N957_GetConfigROM( user_setting.m_N957_handle, &ROM);

					TRACE1("%u \n" ,ROM.m_serial);					
					break;
Rafaela Meissner's avatar
Rafaela Meissner committed
187
                case 'm': //set auto gate     				
188
189
190
	                N957_SetAcquisitionMode(user_setting.m_N957_handle, N957ControlModeAuto);
                    system_state.gate=TRUE;
            	    break;
Rafaela Meissner's avatar
Rafaela Meissner committed
191
                case 'n': //set external gate
192
193
    		        N957_SetAcquisitionMode(user_setting.m_N957_handle, N957ControlModeExtGate);
                    system_state.gate=FALSE;
Rafaela Meissner's avatar
Rafaela Meissner committed
194
195
                    break;      
				default: factor = determine_factor(command);                            
196
197
198
199
200
201
202
203

			}
		}
			//TRACE("flag: 3 \n");

		// Calculate the histogram	
		for( i= 0; i< data_read; i++) 
		{			
Rafaela Meissner's avatar
Rafaela Meissner committed
204
			++histo_buff[ data_buff[i]& ( N957_MAX_HISTO_SAMPLES- 1)];
205
206
207
208
209
210
211
212
213
			// Log to file (if enabled)
			sprintf( aux_string, "%04d\n", data_buff[ i]);
			if (!system_state.paused)
				histogram[atoi(aux_string)+1] = histogram[atoi(aux_string)+1] +1;
			//TRACE("flag: 4 \n");

		}
	}

Rafaela Meissner's avatar
Rafaela Meissner committed
214
	if(user_setting.m_debug)
215
216
217
218
219
220
221
222
223
224
	{
		// clear software convertion
		if( N957_SetSwConvFlag( user_setting.m_N957_handle, N957_FALSE))
		{
			TRACE("erro_20\n");
			goto exit_point;
		}
	}

exit_point:
Rafaela Meissner's avatar
Rafaela Meissner committed
225
	//stop acquisition
226
227
228
229
230
231
232
233
234
235
	if(N957_StopAcquire( user_setting.m_N957_handle))
	{
		TRACE("erro_24\n");
	}

	if(N957_SwClear(user_setting.m_N957_handle))
	{
		TRACE("erro_25\n");
	}	

Rafaela Meissner's avatar
Rafaela Meissner committed
236
	//close modules
237
238
239
240
241
	user_settings_close( &user_setting);

	return ret_val;
}

Rafaela Meissner's avatar
Rafaela Meissner committed
242
void init_system(N957_UINT16 *data_buff, unsigned long *histo_buff, unsigned int *histogram)
243
{
Rafaela Meissner's avatar
Rafaela Meissner committed
244
245
246
247
248
249
250
251
252
	int w;	
	for(w=0; w<8192; w++)
		histogram[w] = 0;

	system_state.paused = FALSE;
	system_state.start = FALSE;
	system_state.error_9 = FALSE;
	system_state.gate = FALSE;
                               
253
254
255
256
257
258
259
260
261
	memset( &user_setting, 0, sizeof( user_setting));
	
	// init user setting module
	if( !user_settings_open( &user_setting))
	{
		TRACE("erro_1\n");
		goto exit_point;
	}
	
Rafaela Meissner's avatar
Rafaela Meissner committed
262
	data_buff= malloc( user_setting.m_bldim* sizeof( N957_UINT16));
263
264
265
266
267
268
269
270
271
272
273
274
275
	if( data_buff== NULL)
	{
		TRACE("erro_8\n");
		goto exit_point;
	}
	histo_buff= malloc( N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));
	if( histo_buff== NULL)
	{
		TRACE("erro_8\n");
		goto exit_point;
	}
	memset( histo_buff, 0, N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));

Rafaela Meissner's avatar
Rafaela Meissner committed
276
   
277
278
}

Rafaela Meissner's avatar
Rafaela Meissner committed
279
void start(N957_UINT32 data32)
280
281
282
283
284
{
	/// input parameter check
	if( !parse_config_file( &user_setting))
	{
		TRACE("erro_2\n");
Rafaela Meissner's avatar
Rafaela Meissner committed
285
        N957_End(user_setting.m_N957_handle);
286
287
288
289
290

	}
	// now board handle is valid and we can start calling boards API
	// Get firmware revision
	char fw_rev[10];
Rafaela Meissner's avatar
Rafaela Meissner committed
291
	if( ( N957_GetFWRelease( user_setting.m_N957_handle, fw_rev, 10)))
292
293
294
295
	{
		TRACE("erro_3");
	}

Rafaela Meissner's avatar
Rafaela Meissner committed
296
	if( N957_GetScaler(user_setting.m_N957_handle, &data32))
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
	{
		TRACE("erro_5");
	}		

	if(N957_StartAcquire( user_setting.m_N957_handle, user_setting.m_mode))
	{
		TRACE("erro_6");
	}

	if( user_setting.m_debug)
	{
		if( (N957_SetSwConvFlag( user_setting.m_N957_handle, N957_TRUE)))
		{
			TRACE("erro_7");
		}
	}
    if((N957_StopAcquire( user_setting.m_N957_handle)))
	{
		TRACE("erro_24\n");
		goto exit_point;
	}
Rafaela Meissner's avatar
Rafaela Meissner committed
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
	system_state.start= TRUE;
    system_state.paused=TRUE;
	TRACE("OK\n");
}

void dump(N957_UINT32 data32, short int factor, unsigned int *histogram, unsigned int nmr_ADC_pause, unsigned int paused_time, unsigned int tempo_pause_vivo, N957_UINT16 data_buff)
{
	int i,w;
	unsigned int temp = 0;
		
	TRACE("OK\n");
	TRACE("[");
	for(w=0; w<(8192-factor+1); w=w+factor) 
		{
	 	for(i=w; i<=(w+factor-1); i++)
			temp = temp + histogram[i];
		TRACE1("%i",temp);
		if (w!=(8192-factor)) 
			TRACE(",");
		temp = 0;
		//JULIO w=w+factor;
		}
	TRACE("]\n");
	N957_GetScaler(user_setting.m_N957_handle, &data32);
	TRACE1("%d\n",data32 + nmr_ADC_pause);
	N957_GetTimer(user_setting.m_N957_handle, &data32);
	TRACE1("%d\n",data32 +paused_time);
	N957_GetLiveTime(user_setting.m_N957_handle, &data32);
	TRACE1("%d\n",data32 +tempo_pause_vivo);

	/*N957_UINT16 tamanho;
	N957_GetBufferOccupancy( user_setting.m_N957_handle, &tamanho);

	TRACE1("%d\n",tamanho);*/  // mostra se quntas contagens tem no buffer	
}

void pause_system(N957_UINT32 *data32, unsigned int *nmr_ADC_pause, unsigned int *paused_time, unsigned int *tempo_pause_vivo, N957_UINT16 data_buff, unsigned long histo_buff)
{
	if(!system_state.paused)
	{		
		if((N957_StopAcquire( user_setting.m_N957_handle)))
		{
			TRACE("erro_24\n");
			goto exit_point;
		}
	}
	else //if(system_state.paused) 
	{
		N957_GetScaler(user_setting.m_N957_handle, &data32);
		*nmr_ADC_pause= *nmr_ADC_pause + *data32;					
		N957_GetTimer(user_setting.m_N957_handle, &data32);
		*paused_time = *paused_time + *data32;
		N957_GetLiveTime(user_setting.m_N957_handle, &data32);
		*tempo_pause_vivo = *tempo_pause_vivo + *data32;
          		
		if((N957_StartAcquire(user_setting.m_N957_handle,user_setting.m_mode)))
		{
			TRACE("erro_6\n");
			goto exit_point;
		}
        if (system_state.gate==FALSE)
              N957_SetAcquisitionMode( user_setting.m_N957_handle, N957ControlModeExtGate);							
		if( user_setting.m_debug) {
			if((N957_SetSwConvFlag( user_setting.m_N957_handle, N957_TRUE)))
			{
				TRACE("erro_7\n");
				goto exit_point;
			}
		}	
		data_buff= (N957_UINT16*) malloc( user_setting.m_bldim* sizeof( N957_UINT16));
		if( data_buff== NULL)
		{
			TRACE("erro_8\n");
			goto exit_point;
		}
		histo_buff= (unsigned int*) malloc( N957_MAX_HISTO_SAMPLES* sizeof( histo_buff));
		if( histo_buff== NULL)
		{
			TRACE("erro_8\n");
			goto exit_point;
		}
		memset( &histo_buff, 0, N957_MAX_HISTO_SAMPLES* sizeof( histo_buff));
	
	}					
	system_state.paused= !system_state.paused;
}

int determine_factor(int command)
{
	short int factor;
	switch(command) 
	{	
		case 'a': // 8192 channels
			factor = 1;
			break;
		case 'b':// 4096 channels
			factor = 2;
			break;
		case 'c': // 2048 channels
			factor = 4;
			break;
		case 'd': // 1024 channels
			factor = 8;
			break;
		case 'e': // 512 channels
			factor = 16;
			break;
		case 'f': // 256 channels
			factor = 32;
			break;
		case 'g':// 128 channels
			factor = 64;
			break;
		case 'h': // 64 channels
			factor = 128;
			break;
		default: 
			factor = 16;
	}
	return factor;
}

void save_file(unsigned int *histogram)
{
	int w;	
	FILE* log = fopen(SAVED_FILE, "w+");
	for(w=0; w<8192; w++)
		fprintf(log, "%i\n", histogram[w]);
	fclose(log);
}

void load_file(short int factor, unsigned int *histogram)
{
	int i, w; unsigned int temp = 0; 	
	FILE* log = fopen(SAVED_FILE, "r");
	for(w=0; w<(8192-factor+1); w=w+factor)
	{
		fscanf(log, "%i", &temp);
	 	for(i=w; i<=(w+factor-1); i++)
			histogram[i] = temp/factor;
	}
	fclose(log);
460
}