Commit 1e488563 authored by Matheus Müller's avatar Matheus Müller
Browse files

Verso 0.1 operaciontal com plot periodico

parent 77987ca2
...@@ -36,14 +36,6 @@ ...@@ -36,14 +36,6 @@
#define N957_SAMPLE_NUM_BITS 13 #define N957_SAMPLE_NUM_BITS 13
#define N957_MAX_HISTO_SAMPLES (1<<N957_SAMPLE_NUM_BITS) #define N957_MAX_HISTO_SAMPLES (1<<N957_SAMPLE_NUM_BITS)
// Executable gnuplot. NOTE: use pgnuplot instead of wgnuplot in Windows, otherwise
// the pipe will not work.
#ifdef WIN32
#define GNUPLOT_COMMAND "pgnuplot"
#else
#define GNUPLOT_COMMAND "gnuplot"
#endif
//////////////////////////////////////////// ////////////////////////////////////////////
// File local variables declaration // File local variables declaration
//////////////////////////////////////////// ////////////////////////////////////////////
...@@ -101,9 +93,11 @@ int main(int argc, char **argv) ...@@ -101,9 +93,11 @@ int main(int argc, char **argv)
{ {
int w; int w;
unsigned int histograma[8192];
unsigned long int histograma[8192]; unsigned int temp = 0;
for(w=0; w<8191; w++) FILE* log;
short int fator = 1;
for(w=0; w<8192; w++)
{ {
histograma[w] = 0; histograma[w] = 0;
...@@ -118,8 +112,6 @@ int main(int argc, char **argv) ...@@ -118,8 +112,6 @@ int main(int argc, char **argv)
unsigned long *histo_buff= NULL; // Histogram data buffer unsigned long *histo_buff= NULL; // Histogram data buffer
int32_t tot_data_read= 0; int32_t tot_data_read= 0;
int i; int i;
FILE* gnu_plot_pipe= NULL; // gnu_plot pipe
long next_refresh= 0;
char aux_string[ 100]; char aux_string[ 100];
BOOL paused= FALSE; BOOL paused= FALSE;
BOOL do_exit= FALSE; BOOL do_exit= FALSE;
...@@ -133,17 +125,7 @@ int main(int argc, char **argv) ...@@ -133,17 +125,7 @@ int main(int argc, char **argv)
memset( &user_setting, 0, sizeof( user_setting)); memset( &user_setting, 0, sizeof( user_setting));
//
// print header
printf( "\n");
printf( "-------------------------------------------------------\n");
printf( "- N 9 5 7 R U N -\n");
printf( "- -\n");
printf( "- www.caen.it rev.02.03 -\n");
printf( "-------------------------------------------------------\n");
printf( "\n");
//
// init user setting module // init user setting module
if( !user_settings_open( &user_setting)) if( !user_settings_open( &user_setting))
{ {
...@@ -169,20 +151,20 @@ int main(int argc, char **argv) ...@@ -169,20 +151,20 @@ int main(int argc, char **argv)
{ {
// Get firmware revision // Get firmware revision
char fw_rev[10]; char fw_rev[10];
TRACE( "\nN957 FW Rev. "); //TRACE( "\nN957 FW Rev. ");
if( ( N957_error_code= N957_GetFWRelease( user_setting.m_N957_handle, fw_rev, 10))!= N957Success) if( ( N957_error_code= N957_GetFWRelease( user_setting.m_N957_handle, fw_rev, 10))!= N957Success)
{ {
TRACE( N957_DecodeError( N957_error_code)); // TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n"); // TRACE( "\n");
ret_val= -3; ret_val= -3;
goto exit_point; goto exit_point;
} }
TRACE( fw_rev); // TRACE( fw_rev);
} }
TRACE( "\n"); // TRACE( "\n");
TRACE( "\nHit 's' to start acquisition mode , any other to quit ...\n"); // TRACE( "\nHit 's' to start acquisition mode , any other to quit ...\n");
switch( getch()) { switch( getch()) {
case 's': case 's':
case 'S': case 'S':
...@@ -193,63 +175,63 @@ int main(int argc, char **argv) ...@@ -193,63 +175,63 @@ int main(int argc, char **argv)
} }
// Scaler Reading // Scaler Reading
TRACE("\nADC Conversions Number : "); // TRACE("\nADC Conversions Number : ");
if( ( N957_error_code=N957_GetScaler(user_setting.m_N957_handle, &data32)!= N957Success)) if( ( N957_error_code=N957_GetScaler(user_setting.m_N957_handle, &data32)!= N957Success))
{ {
TRACE( N957_DecodeError( N957_error_code)); // TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n"); // TRACE( "\n");
ret_val= -5; ret_val= -5;
goto exit_point; goto exit_point;
} }
TRACE1("%d", data32); // TRACE1("%d", data32);
// Timer Reading // Timer Reading
TRACE("\nTime (ms) : "); // TRACE("\nTime (ms) : ");
if( ( N957_error_code=N957_GetTimer(user_setting.m_N957_handle, &data32)!= N957Success)) if( ( N957_error_code=N957_GetTimer(user_setting.m_N957_handle, &data32)!= N957Success))
{ {
TRACE( N957_DecodeError( N957_error_code)); // TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n"); // TRACE( "\n");
ret_val= -6; ret_val= -6;
goto exit_point; goto exit_point;
} }
TRACE1("%d", data32); // TRACE1("%d", data32);
// Live Time Reading // Live Time Reading
TRACE("\nLive Time (ms) : "); // TRACE("\nLive Time (ms) : ");
if( ( N957_error_code=N957_GetLiveTime(user_setting.m_N957_handle, &data32)!= N957Success)) if( ( N957_error_code=N957_GetLiveTime(user_setting.m_N957_handle, &data32)!= N957Success))
{ {
TRACE( N957_DecodeError( N957_error_code)); // TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n"); // TRACE( "\n");
ret_val= -7; ret_val= -7;
goto exit_point; goto exit_point;
} }
TRACE1("%d",data32); // TRACE1("%d",data32);
// Start acquisition with specific mode // Start acquisition with specific mode
TRACE1( "\nN957 Start Acquisition mode %i : ", user_setting.m_mode); // TRACE1( "\nN957 Start Acquisition mode %i : ", user_setting.m_mode);
if( ( N957_error_code= N957_StartAcquire( user_setting.m_N957_handle, user_setting.m_mode))!= N957Success) if( ( N957_error_code= N957_StartAcquire( user_setting.m_N957_handle, user_setting.m_mode))!= N957Success)
{ {
TRACE( N957_DecodeError( N957_error_code)); // TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n"); // TRACE( "\n");
ret_val= -8; ret_val= -8;
goto exit_point; goto exit_point;
} }
TRACE( "Ok"); // TRACE( "Ok");
if( user_setting.m_debug) { if( user_setting.m_debug) {
// //
// If debugging features enable, convertion well be simulated by software // If debugging features enable, convertion well be simulated by software
// //
// Set software convertion // Set software convertion
TRACE( "\nN957 Debug Software convertion enable : "); // TRACE( "\nN957 Debug Software convertion enable : ");
if( ( N957_error_code= N957_SetSwConvFlag( user_setting.m_N957_handle, N957_TRUE))!= N957Success) if( ( N957_error_code= N957_SetSwConvFlag( user_setting.m_N957_handle, N957_TRUE))!= N957Success)
{ {
TRACE( N957_DecodeError( N957_error_code)); // TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n"); // TRACE( "\n");
ret_val= -9; ret_val= -9;
goto exit_point; goto exit_point;
} }
TRACE( "Ok"); // TRACE( "Ok");
} }
// Allocate buffer storage // Allocate buffer storage
...@@ -257,7 +239,7 @@ int main(int argc, char **argv) ...@@ -257,7 +239,7 @@ int main(int argc, char **argv)
if( data_buff== NULL) if( data_buff== NULL)
{ {
// Insufficient memory // Insufficient memory
TRACE1( "N957 Insufficient memory to allocate data buffer (%li bytes)", (long)( user_setting.m_bldim* sizeof( N957_UINT16))); // TRACE1( "N957 Insufficient memory to allocate data buffer (%li bytes)", (long)( user_setting.m_bldim* sizeof( N957_UINT16)));
ret_val= -10; ret_val= -10;
goto exit_point; goto exit_point;
} }
...@@ -266,7 +248,7 @@ int main(int argc, char **argv) ...@@ -266,7 +248,7 @@ int main(int argc, char **argv)
if( histo_buff== NULL) if( histo_buff== NULL)
{ {
// Insufficient memory // Insufficient memory
TRACE1( "N957 Insufficient memory to allocate histogram data buffer (%li bytes)", (long)N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff)); // TRACE1( "N957 Insufficient memory to allocate histogram data buffer (%li bytes)", (long)N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));
ret_val= -11; ret_val= -11;
goto exit_point; goto exit_point;
} }
...@@ -276,43 +258,13 @@ int main(int argc, char **argv) ...@@ -276,43 +258,13 @@ int main(int argc, char **argv)
// Create output file // Create output file
if( ( out_file= fopen( user_setting.m_log_filename, "wt"))== NULL) if( ( out_file= fopen( user_setting.m_log_filename, "wt"))== NULL)
{ {
TRACE1( "N957 output file creation failure '%s'", user_setting.m_log_filename); // TRACE1( "N957 output file creation failure '%s'", user_setting.m_log_filename);
ret_val= -12; ret_val= -12;
goto exit_point; goto exit_point;
} }
} }
/*// open gnuplot for the first time
{
char *gnu_plot_filename= (char*)malloc( MAX_FILENAME_LENGHT);
sprintf( gnu_plot_filename, "%s%s", user_setting.m_gnu_plot_path, GNUPLOT_COMMAND);
// open the pipe
if(( gnu_plot_pipe = popen( gnu_plot_filename, "w"))== NULL){
TRACE( "N957 GNU Plot pipe creation failure");
ret_val= -13;
goto exit_point;
}
//// set some variables in gnuplot
//fprintf( gnu_plot_pipe, "ch = %d; mod = %d; unit = %d; sens = %f\n", ChSel, ModSel, Unit, Sensitivity);
free( gnu_plot_filename);
}
// gnu plot commands
strcpy( aux_string, "load 'plot_histo.cfg'\n");
if( fwrite( aux_string, 1, strlen( aux_string), gnu_plot_pipe)!= strlen( aux_string)) {
// out file write error
TRACE( "\n Pipe write failure\n");
ret_val= -14;
goto exit_point;
}*/
TRACE( "\nAcquisition running ...\n");
TRACE( "\nHit 'r' to reset histogram, any other to quit ...");
//TRACE( "\nHit 'p' to pause/resume, any other to quit ...");
TRACE( "\nAny other to quit ...");
//
// Main loop
do_exit= FALSE; do_exit= FALSE;
while( !do_exit) { while( !do_exit) {
...@@ -356,8 +308,8 @@ int main(int argc, char **argv) ...@@ -356,8 +308,8 @@ int main(int argc, char **argv)
// //
// ReadData // ReadData
if( ( N957_error_code= N957_ReadData( user_setting.m_N957_handle, data_buff, &data_read))!= N957Success) { if( ( N957_error_code= N957_ReadData( user_setting.m_N957_handle, data_buff, &data_read))!= N957Success) {
TRACE( N957_DecodeError( N957_error_code)); // TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n"); // TRACE( "\n");
ret_val= -15; ret_val= -15;
goto exit_point; goto exit_point;
} }
...@@ -370,29 +322,61 @@ int main(int argc, char **argv) ...@@ -370,29 +322,61 @@ int main(int argc, char **argv)
case 'r': case 'r':
case 'R': case 'R':
// reset the histogram here // reset the histogram here
memset( histo_buff, 0, N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff)); //memset( histo_buff, 0, N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));
for(w=0; w<8192; w++)
histograma[w] = 0;
break; break;
case 'd':
while(getch() != 'e')
Sleep( 1);
case 'p': case 'p':
case 'P': case 'P':
paused= !paused; paused= !paused;
break; break;
case 'a':
fator = 1;
break;
case 'b':
fator = 2;
break;
case 'c':
fator = 4;
break;
case 'd':
fator = 8;
break;
case 'e':
fator = 16;
break;
case 'f':
fator = 32;
break;
case 'g':
fator = 64;
break;
case 'h':
fator = 128;
break;
case 'i': case 'i':
fflush(stdout); for(w=0; w<(8192-fator+1); w)
for(w=0; w<8191; w++)
{ {
TRACE1("%li\n",histograma[w]); for(i=w; i<=(w+fator-1); i++)
temp = temp + histograma[i];
TRACE1("%i\n",temp);
temp = 0;
w=w+fator;
} }
for(w=0; w<8191; w++) break;
{ case 'z':
histograma[w] = 0; log = fopen("save.dat", "w+");
for(w=0; w<8192; w++)
fprintf(log, "%i\n", histograma[w]);
fclose(log);
break;
case 'y':
log = fopen("save.dat", "r");
for(w=0; w<8192; w++){
fscanf(log, "%i", &histograma[w]);
} }
fclose(log);
break;
default: default:
do_exit= TRUE; do_exit= TRUE;
break; break;
...@@ -405,132 +389,58 @@ int main(int argc, char **argv) ...@@ -405,132 +389,58 @@ int main(int argc, char **argv)
if( out_file) { if( out_file) {
sprintf( aux_string, "%04d\n", data_buff[ i]); sprintf( aux_string, "%04d\n", data_buff[ i]);
histograma[atoi(aux_string)+1] = histograma[atoi(aux_string)+1] +1; histograma[atoi(aux_string)+1] = histograma[atoi(aux_string)+1] +1;
TRACE1("%d \n", atoi(aux_string)); //TRACE1("%d \n", atoi(aux_string));
/*if(fwrite( aux_string, 1, strlen( aux_string), out_file)!= strlen( aux_string)) {
// out file write error
TRACE( "\n output file write failure\n");
ret_val= -16;
goto exit_point;
}*/
}
}
/*if( get_time()>= next_refresh+1000) {
FILE* histo_file= NULL;
next_refresh= get_time()+ user_setting.m_gnu_plot_refresh;
for(w=0; w<8191; w++)
{
TRACE1("%li ",histograma[w]);
} }
TRACE("\n \n \n \n");
} }
if(( histo_file= fopen( HISTO_FILENAME, "wt"))== NULL){
// out file write error
TRACE1( "N957 output file creation failure '%s'", HISTO_FILENAME);
ret_val= -17;
goto exit_point;
}
// Save data to histogram file
for( i= 0; i< N957_MAX_HISTO_SAMPLES; i++) {
// Check for user commands
if( kbhit()) {
switch( getch()) {
case 'r':
case 'R':
// reset the histogram here
memset( histo_buff, 0, N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));
break;
//case 'p':
//case 'P':
// paused= !paused;
// break;
default:
do_exit= TRUE;
break;
}
}
sprintf( aux_string, "%ld\n", histo_buff[ i]);
//sprintf( aux_string, "%f\t%ld\n", (float)i* user_setting.m_gnu_plot_x_scale, histo_buff[ i]);
if( fwrite( aux_string, 1, strlen( aux_string), histo_file)!= strlen( aux_string)){
// out file write error
TRACE( "\n Histogram file write failure\n");
ret_val= -18;
goto exit_point;
}
}
fclose( histo_file);
// write to gnuplot
strcpy( aux_string, "plot 'histo.dat' with histogram 2\n");
// strcpy( aux_string, "set style histogram rows\n plot 'histo.dat' using 2, '' using 4, '' using 6:xticlabels(1)\n");
if( fwrite( aux_string, 1, strlen( aux_string), gnu_plot_pipe)!= strlen( aux_string))
{
// out file write error
TRACE( "\n Pipe write failure\n");
ret_val= -19;
goto exit_point;
}
fflush( gnu_plot_pipe);
Sleep( 1);
}*/
} }
if( user_setting.m_debug) if( user_setting.m_debug)
{ {
// //
// clear software convertion // clear software convertion
TRACE( "\nN957 Debug Software convertion disable : "); // TRACE( "\nN957 Debug Software convertion disable : ");
if( ( N957_error_code= N957_SetSwConvFlag( user_setting.m_N957_handle, N957_FALSE))!= N957Success) if( ( N957_error_code= N957_SetSwConvFlag( user_setting.m_N957_handle, N957_FALSE))!= N957Success)
{ {
TRACE( N957_DecodeError( N957_error_code)); // TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n"); //TRACE( "\n");
ret_val= -20; ret_val= -20;
goto exit_point; goto exit_point;
} }
TRACE( "Ok"); // TRACE( "Ok");
} }
//Scaler Reading //Scaler Reading
TRACE("\nADC Conversions Number : "); // TRACE("\nADC Conversions Number : ");
if( ( N957_error_code=N957_GetScaler(user_setting.m_N957_handle, &data32)!= N957Success)) if( ( N957_error_code=N957_GetScaler(user_setting.m_N957_handle, &data32)!= N957Success))
{ {
TRACE( N957_DecodeError( N957_error_code)); // TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n"); // TRACE( "\n");
ret_val= -21; ret_val= -21;
goto exit_point; goto exit_point;
} }
TRACE1("%d", data32); TRACE1("%d\n", data32);
//Timer Reading //Timer Reading
TRACE("\nTime (ms) : "); // TRACE("\nTime (ms) : ");
if( ( N957_error_code=N957_GetTimer(user_setting.m_N957_handle, &data32)!= N957Success)) if( ( N957_error_code=N957_GetTimer(user_setting.m_N957_handle, &data32)!= N957Success))
{ {
TRACE( N957_DecodeError( N957_error_code)); // TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n"); // TRACE( "\n");
ret_val= -22; ret_val= -22;
goto exit_point; goto exit_point;
} }
TRACE1("%d", data32); TRACE1("%d\n", data32);
//Live Time Reading //Live Time Reading
TRACE("\nLive Time (ms) : "); // TRACE("\nLive Time (ms) : ");
if( ( N957_error_code=N957_GetLiveTime(user_setting.m_N957_handle, &data32)!= N957Success)) if( ( N957_error_code=N957_GetLiveTime(user_setting.m_N957_handle, &data32)!= N957Success))
{ {
TRACE( N957_DecodeError( N957_error_code)); // TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n"); // TRACE( "\n");
ret_val= -23; ret_val= -23;
goto exit_point; goto exit_point;
} }
TRACE1("%d",data32); TRACE1("%d\n",data32);
exit_point: exit_point:
///////////////////////////////////////// /////////////////////////////////////////
...@@ -541,45 +451,32 @@ exit_point: ...@@ -541,45 +451,32 @@ exit_point:
// Demo application specific // Demo application specific
///////////////////////////////////////// /////////////////////////////////////////
TRACE( "\n\nHit any key to exit..."); //TRACE( "\n\nHit any key to exit...");
getch(); getch();
// Stop acquisition // Stop acquisition
TRACE( "\nN957 Stop Acquisition : "); // TRACE( "\nN957 Stop Acquisition : ");
if( ( N957_error_code= N957_StopAcquire( user_setting.m_N957_handle))!= N957Success) if( ( N957_error_code= N957_StopAcquire( user_setting.m_N957_handle))!= N957Success)
{ {
TRACE( N957_DecodeError( N957_error_code)); // TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n"); // TRACE( "\n");
ret_val= -24; ret_val= -24;
goto exit_point; goto exit_point;
} }
TRACE( "Ok"); // TRACE( "Ok");
/*if( out_file!= NULL)
{
fclose( out_file);
}
if( gnu_plot_pipe!= NULL)
{
fprintf( gnu_plot_pipe, "exit\n");
pclose( gnu_plot_pipe);
}
if( data_buff!= NULL)
{
free( data_buff);
}*/
// SW Clear // SW Clear
TRACE( "\nSW Clear : "); //TRACE( "\nSW Clear : ");
if( ( N957_error_code= N957_SwClear(user_setting.m_N957_handle)!= N957Success)) if( ( N957_error_code= N957_SwClear(user_setting.m_N957_handle)!= N957Success))
{ {
TRACE( N957_DecodeError( N957_error_code)); // TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n"); // TRACE( "\n");
ret_val= -25; ret_val= -25;
goto exit_point; goto exit_point;
} }
TRACE( "Ok");