Commit 9167b271 authored by Matheus Müller's avatar Matheus Müller Committed by Matheus Müller
Browse files

Commit inicial do software da Estação de Espectrometria

A lista de tarefas está incluida em TODO
parents
############################ LICENÇA #################################
O software XXX é composto de mais de um programa, escritos em C e Python.
######################################################################
#### YYY ####
O programa YYY é baseado no N957Demo disponibilizado pela CAEN no site ....
Os termos desta licença estão contidos no arquivo LICENCE.CAEN
#### ZZZZ ####
O programa ZZZ é licenciado pela Licença Pública do GNU versão 3 (GNU GPL v3)
Os termos da GNU GPL v.3 estão contidos no arquivo GPLV3.txt
########################################################################
#
# --- CAEN SpA - Computing Division ---
#
# CAENVMElib Software Project
#
# Created : Nov 2005 (Rel. 1.0)
#
# Auth: NDA
#
########################################################################
EXE = N957Demo
CC = gcc
COPTS = -fPIC -DLINUX -Wall
#COPTS = -g -fPIC -DLINUX -Wall
FLAGS = -Wall -s
#FLAGS = -Wall
DEPLIBS = -lCAENN957 -lm
LIBS =
INCLUDEDIR = -I. -I./include
OBJS = main.o user_settings.o keyb.o
INCLUDES = ./include/N957Lib.h ./include/N957types.h ./include/N957oslib.h keyb.h user_settings.h
#########################################################################
all : $(EXE)
clean :
/bin/rm -f $(OBJS) $(EXE)
$(EXE) : $(OBJS)
/bin/rm -f $(EXE)
$(CC) $(FLAGS) -o $(EXE) $(OBJS) $(DEPLIBS)
$(OBJS) : $(INCLUDES) Makefile
%.o : %.c
$(CC) $(COPTS) $(INCLUDEDIR) -c -o $@ $<
# ****************************************************************
# N957Demo Configuration File
# ****************************************************************
# Lines starting with # (first column) are comments
#
# The board number
BOARD_NUM 0
#
# path to the executable file of gnuplot
GNUPLOT_PATH ""
#
# Save readout data into the Output File (0=don't save)
LOG_TO_FILE 1
#
# Readout data Output Filename ( meaningful only for LOG_TO_FILE!= 0)
LOG_FILENAME "data.log"
#
# Maximum number of samples to acquire (-1 means no limit)
MAX_NUM_SAMPLES -1
#
# Data Block dimension [1..65536]
DATA_BLOCK_DIM 32768
#
# Debug mode (0= debug disabled)
DEBUG 0
#
# Acquisition mode: (0= Ext Gate 1= Auto)
ACQ_MODE 1
#
# LLD Threshold value [00..99]
LLD_THR_VAL 30
#
# Gnu plot refresh rate (msec)
GNU_PLOT_REFRESH 2000
#
# Gnu plot X scale factor
GNU_PLOT_X_SCALE 0.3
Tarefas Estação de Espectrometria
- Renomear programas
- Editar arquivo de licença: Licença CAEN para software em C e GPL para parte em python
/***********************************************************************************************
* MODULE: common_defs.h
* PURPOSE: common definitions
* WRITTEN BY: NDA
* COPYRIGHT: CAEN S.p.A. all rights reserved
* USAGE: compiled with gcc ?!?!
************************************************************************************************/
#ifndef __COMMON_DEFS_DEF_H
#define __COMMON_DEFS_DEF_H
////////////////////////////////////////////
// File includes
////////////////////////////////////////////
#include <stdio.h>
#include "keyb.h"
////////////////////////////////////////////
// Global defines
////////////////////////////////////////////
//
// Data tyes
typedef int BOOL;
#define TRUE -1
#define FALSE 0
typedef unsigned char UINT8;
typedef unsigned short UINT16;
typedef unsigned int UINT32;
typedef unsigned long long UINT64;
typedef char INT8;
typedef short INT16;
typedef int INT32;
typedef long long INT64;
typedef const char* PCSTR;
//
// Debugging macros
#define TRACE( format) printf( format)
#define TRACE1( format, p1) printf( format, p1)
#define TRACE2( format, p1, p2) printf( format, p1, p2)
#define TRACE3( format, p1, p2, p3) printf( format, p1, p2, p3)
#define TRACE4( format, p1, p2, p3, p4) printf( format, p1, p2, p3, p4)
#define TRACE5( format, p1, p2, p3, p4, p5) printf( format, p1, p2, p3, p4, p5)
//
// Helper functions
#ifdef LINUX
#define stricmp( str1, str2) strcasecmp( str1, str2)
#define strnicmp( str1, str2, n) strncasecmp( str1, str2, n)
#endif
////////////////////////////////////////////
// Global variables declaration
////////////////////////////////////////////
////////////////////////////////////////////
// Global methods declaration
////////////////////////////////////////////
#endif
#ifdef LINUX
#include <sys/time.h> /* struct timeval, select() */
#include <termios.h> /* tcgetattr(), tcsetattr() */
#include <stdlib.h> /* atexit(), exit() */
#include <unistd.h> /* read() */
#include <stdio.h> /* printf() */
#include <string.h> /* memcpy() */
static struct termios g_old_kbd_mode;
/*****************************************************************************/
static void cooked(void)
{
tcsetattr(0, TCSANOW, &g_old_kbd_mode);
}
static void raw(void)
{
static char init;
/**/
struct termios new_kbd_mode;
if(init)
return;
/* put keyboard (stdin, actually) in raw, unbuffered mode */
tcgetattr(0, &g_old_kbd_mode);
memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios));
new_kbd_mode.c_lflag &= ~(ICANON | ECHO);
new_kbd_mode.c_cc[VTIME] = 0;
new_kbd_mode.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &new_kbd_mode);
/* when we exit, go back to normal, "cooked" mode */
atexit(cooked);
init = 1;
}
/*****************************************************************************/
/* GETCH */
/*****************************************************************************/
int getch(void)
{
unsigned char temp;
raw();
/* stdin = fd 0 */
if(read(0, &temp, 1) != 1)
return 0;
return temp;
}
/*****************************************************************************/
/* KBHIT */
/*****************************************************************************/
int kbhit()
{
struct timeval timeout;
fd_set read_handles;
int status;
raw();
/* check stdin (fd 0) for activity */
FD_ZERO(&read_handles);
FD_SET(0, &read_handles);
timeout.tv_sec = timeout.tv_usec = 0;
status = select(0 + 1, &read_handles, NULL, NULL, &timeout);
if(status < 0)
{
printf("select() failed in kbhit()\n");
exit(1);
}
return (status);
}
#else // Windows
#include <conio.h>
#endif
#ifdef LINUX
/*****************************************************************************/
/* GETCH */
/*****************************************************************************/
int getch(void);
/*****************************************************************************/
/* KBHIT */
/*****************************************************************************/
int kbhit();
#else
#include <conio.h>
#include <ctype.h>
// getch and kbhit was declared deprecated in Microsoft Visual C++. The ISO C++
// conformant name with '_' preceding the name is suggested.
#define getch _getch
#define kbhit _kbhit
#endif
/***********************************************************************************************
* 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>
#ifdef WIN32
#include <time.h>
#include <sys/timeb.h>
#include <conio.h>
#include <process.h>
#define popen _popen // redefine 'deprecated' popen as _popen
#define pclose _pclose // redefine 'deprecated' pclose as _pclose
#else
#include <unistd.h>
#include <sys/time.h>
#define Sleep(t) usleep((t)*1000)
#include <memory.h>
#endif
#include "common_defs.h"
#include "./include/N957Lib.h"
#include "user_settings.h"
#include "keyb.h"
////////////////////////////////////////////
// File local defines
////////////////////////////////////////////
#define N957_SAMPLE_NUM_BITS 13
#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
////////////////////////////////////////////
const char* HISTO_FILENAME= "histo.dat";
////////////////////////////////////////////
// Global visible variables declaration
////////////////////////////////////////////
////////////////////////////////////////////
// File local methods declaration
////////////////////////////////////////////
// ---------------------------------------------------------------------------
// get time in milliseconds since first procedure call
// ---------------------------------------------------------------------------
long get_time()
{
static long startup= 0;
long time_ms;
#ifdef WIN32
struct _timeb timebuffer;
_ftime( &timebuffer );
if( !startup) {
startup= (long)timebuffer.time;
}
time_ms = (long)( timebuffer.time- startup) * 1000 + (long)timebuffer.millitm;
#else
struct timeval t1;
struct timezone tz;
gettimeofday(&t1, &tz);
if( !startup) {
startup= (long)t1.tv_sec;
}
time_ms = (t1.tv_sec- startup) * 1000 + t1.tv_usec / 1000;
#endif
return time_ms;
}
/**************************************************
**************************************************/
/***********************************************************************************************
* METHOD: main
* PURPOSE: Main program
* PARAMETERS: <in> int argc: number of arguments
* <in> void *argv[]: arguments' list
* RETURN:
* USAGE: allowed command line input parameters:
************************************************************************************************/
int main(int argc, char **argv)
{
N957_UINT32 data32;
int ret_val= 0; // procedure exit value
N957ErrorCodes N957_error_code; // returned error code
user_setting_data user_setting; // user settings
FILE* out_file= NULL; // output file
N957_UINT16 *data_buff= NULL; // read data buffer
unsigned long *histo_buff= NULL; // Histogram data buffer
int32_t tot_data_read= 0;
int i;
FILE* gnu_plot_pipe= NULL; // gnu_plot pipe
long next_refresh= 0;
char aux_string[ 100];
BOOL paused= FALSE;
BOOL do_exit= FALSE;
/////////////////////////////////////////
// Demo application specific
/////////////////////////////////////////
memset( &user_setting, 0, sizeof( user_setting));
//
// print header
printf( "\n");
printf( "-------------------------------------------------------\n");
printf( "- N 9 5 7 D E M O -\n");
printf( "- -\n");
printf( "- www.caen.it rev.02.03 -\n");
printf( "-------------------------------------------------------\n");
printf( "\n");
//
// init user setting module
if( !user_settings_open( &user_setting))
{
ret_val= -1;
goto exit_point;
}
//
// input parameter check
if( !user_settings_parse_input_param( &user_setting, argc, (char**)argv))
{
ret_val= -2;
goto exit_point;
}
/////////////////////////////////////////
// Library specific
/////////////////////////////////////////
//
// now board handle is valid and we can start calling boards API
{
// Get firmware revision
char fw_rev[10];
TRACE( "\nN957 FW Rev. ");
if( ( N957_error_code= N957_GetFWRelease( user_setting.m_N957_handle, fw_rev, 10))!= N957Success)
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
ret_val= -3;
goto exit_point;
}
TRACE( fw_rev);
}
TRACE( "\n");
TRACE( "\nHit 's' to start acquisition mode , any other to quit ...\n");
switch( getch()) {
case 's':
case 'S':
break;
default:
ret_val= -4;
goto exit_point;
}
// Scaler Reading
TRACE("\nADC Conversions Number : ");
if( ( N957_error_code=N957_GetScaler(user_setting.m_N957_handle, &data32)!= N957Success))
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
ret_val= -5;
goto exit_point;
}
TRACE1("%d", data32);
// Timer Reading
TRACE("\nTime (ms) : ");
if( ( N957_error_code=N957_GetTimer(user_setting.m_N957_handle, &data32)!= N957Success))
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
ret_val= -6;
goto exit_point;
}
TRACE1("%d", data32);
// Live Time Reading
TRACE("\nLive Time (ms) : ");
if( ( N957_error_code=N957_GetLiveTime(user_setting.m_N957_handle, &data32)!= N957Success))
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
ret_val= -7;
goto exit_point;
}
TRACE1("%d",data32);
// Start acquisition with specific 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)
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
ret_val= -8;
goto exit_point;
}
TRACE( "Ok");
if( user_setting.m_debug) {
//
// If debugging features enable, convertion well be simulated by software
//
// Set software convertion
TRACE( "\nN957 Debug Software convertion enable : ");
if( ( N957_error_code= N957_SetSwConvFlag( user_setting.m_N957_handle, N957_TRUE))!= N957Success)
{
TRACE( N957_DecodeError( N957_error_code));
TRACE( "\n");
ret_val= -9;
goto exit_point;
}
TRACE( "Ok");
}
// Allocate buffer storage
data_buff= malloc( user_setting.m_bldim* sizeof( N957_UINT16));
if( data_buff== NULL)
{
// Insufficient memory
TRACE1( "N957 Insufficient memory to allocate data buffer (%li bytes)", (long)( user_setting.m_bldim* sizeof( N957_UINT16)));
ret_val= -10;
goto exit_point;
}
// Allocate histogram buffer storage
histo_buff= malloc( N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));
if( histo_buff== NULL)
{
// Insufficient memory
TRACE1( "N957 Insufficient memory to allocate histogram data buffer (%li bytes)", (long)N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));
ret_val= -11;
goto exit_point;
}
memset( histo_buff, 0, N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));
if( user_setting.m_log_to_file) {
// Create output file
if( ( out_file= fopen( user_setting.m_log_filename, "wt"))== NULL)
{
TRACE1( "N957 output file creation failure '%s'", user_setting.m_log_filename);
ret_val= -12;
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;
while( !do_exit) {
N957_UINT16 data_read;
//// Check for user commands
while( paused){
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 'd':
while(getch() != 'e')
Sleep( 1);
case 'p':
case 'P':
paused= !paused;
break;
default:
ret_val= 0;
goto exit_point;
}
}
Sleep( 1);
}
data_read= user_setting.m_bldim;
if( user_setting.m_max_num_samples> 0){
// Check for maximun numsber of samples
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);
}
}
//
// ReadData
if( ( N957_error_code= N957_ReadData( user_setting.m_N957_handle, data_buff, &data_read))!= N957Success) {
TRACE( N957_DecodeError( N957_error_code));