Commit c57dfb8e authored by Rafaela Meissner's avatar Rafaela Meissner
Browse files

mais mudanças em Main

parent c0900f21
import Tkinter as tk
import pexpect
import time
import numpy as np
from array import *
##############################################################################
##############################################################################
#Classe que faz o Pipe com o programa em C
class Modulo:
def __init__(self, Nmr_modulo):
self.child = pexpect.spawn('./N957Run '+ Nmr_modulo)
#self.Controle['Nmr_serial']=Serial
self.Data=array('L',[0]*8192)
self.Controle=dict(pause=1,Nmr_serial=0,Nmr_canais=512, ON=0)
self.Livetime = dict(ADC_Conversion=0 , Time=0. , DeadTime=0.)
def start(self):
self.child.send('s')
msg=(self.child.readline().strip("\r\n"))
return msg
def set_fator(self, canais): # Define o fator para conversao do numero de canais
self.Controle['Nmr_canais']=canais
if int(canais) == 8192: # 8192 pontos
self.child.send('a')
elif int(canais) == 4096: # 4096 pontos
self.child.send('b')
elif int(canais) == 2048: # 2048 pontos
self.child.send('c')
elif int(canais) == 1024: # 1024 pontos
self.child.send('d')
elif int(canais) == 512: # 512 pontos
self.child.send('e')
elif int(canais) == 256: # 256 pontos
self.child.send('f')
elif int(canais) == 128: # 128 pontos
self.child.send('g')
elif int(canais) == 64: # 64 pontos
self.child.send('h')
def load(self):
self.child.send('y')
def LLDT(self, lldt):
self.child.send('j')
self.child.send(chr(int(lldt)/10))
self.child.send(chr(int(lldt)%10))
def Read_Data(self):
msg=(self.child.readline().strip("\r\n"))
if msg == 'OK':
self.Data = eval(self.child.readline().strip("\r\n"))
self.Livetime['ADC_Conversion'] = int(self.child.readline().strip("\r\n"))
self.Livetime['Time'] = float(self.child.readline().strip("\r\n"))
self.Livetime['DeadTime'] = (self.Livetime['Time'] - float(self.child.readline().strip("\r\n")))*100/self.Livetime['Time']
return msg
def dump(self): # Sinal para programa coletor enviar os dados
self.child.send('i')
def reset(self):
self.child.send('r')
for k in range(int(self.Controle['Nmr_canais'])):
self.Data[k] = 0
def pause(self):
self.child.send('p')
def exit(self):
self.child.send('q')
time.sleep(0.1)
self.child.close(force=True)
def set_AutoGate(self):
self.child.send('m')
def set_ExternalGate(self):
self.child.send('n')
/***********************************************************************************************
* 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
......@@ -10,7 +10,7 @@ int getch(void);
/* KBHIT */
/*****************************************************************************/
int kbhit();
/*
#else
#include <conio.h>
#include <ctype.h>
......@@ -18,7 +18,7 @@ int kbhit();
// 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
#define kbhit _kbhit*/
#endif
#ifdef LINUX
#include "../include/keyb.h"
/*****************************************************************************/
/* GETCH */
/*****************************************************************************/
......@@ -12,8 +12,8 @@ int getch(void);
int kbhit();
#else
#include <conio.h>
#include <ctype.h>
#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.
......
......@@ -25,15 +25,8 @@
#define N957_SAMPLE_NUM_BITS 13
#define N957_MAX_HISTO_SAMPLES (1<<N957_SAMPLE_NUM_BITS)
#define exit q
#define start s
#define reset r
#define pause p
#define LLDT j
#define load_file y
#define set_autoGate m
#define set_externalGate n
#define SAVED_FILE "save.dat"
#define HISTO_FILENAME "histo.dat" //!not used
typedef struct {
BOOL paused;
......@@ -44,13 +37,21 @@ typedef struct {
////////////////////////////////////////////
// File local variables declaration
const char* HISTO_FILENAME= "histo.dat";
////////////////////////////////////////////
// Global visible variables declaration
user_setting_data user_setting;
Control system_state;
////////////////////////////////////////////
// File local methods declaration
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);
// -----------------------------------------------------------
// get time in milliseconds since first procedure call
......@@ -66,48 +67,41 @@ const char* HISTO_FILENAME= "histo.dat";
*************************************************************/
int main(int argc, char **argv)
{
user_setting_data user_setting; // user settings
//data of histogram
N957_UINT16 *data_buff= NULL; // read data buffer
unsigned long *histo_buff= NULL; // Histogram data buffer
unsigned int histogram[8192];
short int fator = 16;
short int factor = 16;
int w;
//auxiliaries
int w, i, command;
unsigned int temp = 0;
char aux_string[ 100];
unsigned int temp = 0, lldt=10;
unsigned int paused_time = 0;
unsigned int nmr_ADC_pause=0;
unsigned int lldt=10; //?????
unsigned int paused_time = 0; //estrutura?
unsigned int nmr_ADC_pause=0;//????
unsigned int tempo_pause_vivo=0;
N957_UINT32 data32; //????
FILE* log;
N957_ConfigROM ROM; //used in function not called by python code
N957_ConfigROM ROM;
N957_UINT32 data32;
int ret_val= 0; //?????
int ret_val= 0;
// procedure exit value
N957_UINT16 data_read;
int32_t tot_data_read= 0;
int ii, i;
char aux_string[ 100];
Control system_state;
system_state.paused = FALSE;
system_state.start = FALSE;
system_state.error_9 = FALSE;
system_state.gate = FALSE;
for(w=0; w<8192; w++)
histogram[w] = 0;
init_system(data_buff, histo_buff, histogram);
initSystem(&user_setting, &data_buff, &histo_buff);
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.
while(true)
while(1)
{
//TRACE("flag :1 \n");
N957_UINT16 data_read;
if(system_state.start)
{
data_read= user_setting.m_bldim;
......@@ -127,35 +121,32 @@ int main(int argc, char **argv)
}
if( N957_ReadData( user_setting.m_N957_handle, data_buff, &data_read))
{
{
if(!system_state.error_9)
{
system_state.error_9=TRUE;
TRACE("error_9\n");
system_state.error_9=TRUE;
TRACE("error_9\n");
}
//goto exit_point;
// goto exit_point;
}
else
system_state.error_9=FALSE;
system_state.error_9=FALSE;
tot_data_read+= data_read;
}
if( kbhit()) {
switch( getch()) {
case exit:
command = getch();
switch(command) {
case 'q':
goto exit_point;
case start:
start(&user_setting, data32);
system_state.start= TRUE;
system_state.paused=TRUE;
TRACE("OK\n");
case 's':
start(data32);
break;
case reset:
case 'r':
// 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++)
histogram[w] = 0;
histogram[w] = 0;
if( N957_SwClear(user_setting.m_N957_handle))
{
ret_val= -25;
......@@ -165,114 +156,15 @@ int main(int argc, char **argv)
nmr_ADC_pause=0;
tempo_pause_vivo=0;
break;
case pause:
if(!system_state.paused)
{
if((N957_StopAcquire( user_setting.m_N957_handle)))
{
TRACE("erro_24\n");
goto exit_point;
}
}
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= malloc( user_setting.m_bldim* sizeof( N957_UINT16));
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));
}
system_state.paused= !system_state.paused;
break;
case 'a': // 8192 canais
fator = 1;
break;
case 'b':// 4096 canais
fator = 2;
break;
case 'c': // 2048 canais
fator = 4;
break;
case 'd': // 1024 canais
fator = 8;
break;
case 'e': // 512 canais
fator = 16;
break;
case 'f': // 256 canais
fator = 32;
break;
case 'g':// 128 canais
fator = 64;
case 'p':
pause_system(&data32, &nmr_ADC_pause, &paused_time, &tempo_pause_vivo, *data_buff, *histo_buff);
break;
case 'h': // 64 canais
fator = 128;
case 'i': // transmit info to python
dump(data32, factor, histogram, nmr_ADC_pause, paused_time, tempo_pause_vivo, *data_buff);
break;
case 'i': // função que transmite a informação para o python
TRACE("OK\n");
TRACE("[");
for(w=0; w<(8192-fator+1); w=w+fator)
{
for(ii=w; ii<=(w+fator-1); ii++)
temp = temp + histogram[ii];
TRACE1("%i",temp);
if (w!=(8192-fator))
TRACE(",");
temp = 0;
//JULIO w=w+fator;
}
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
break;
case LLDT: // funcao testada
case 'j': // funcao testada
lldt= ((getchar())*10 + getchar());
N957_SetLLD( user_setting.m_N957_handle, lldt);
break;
case 'l': //!NOT CALLED BY PYTHON CODE
goto exit_point;
......@@ -280,20 +172,10 @@ int main(int argc, char **argv)
//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
log = fopen("save.dat", "w+");
for(w=0; w<8192; w++)
fprintf(log, "%i\n", histogram[w]);
fclose(log);
save_file(histogram);
break;
case load_file: // Abre o arquivo com o nome "save.dat"
log = fopen("save.dat", "r");
for(w=0; w<(8192-fator+1); w=w+fator)
{
fscanf(log, "%i", &temp);
for(i=w; i<=(w+fator-1); i++)
histogram[i] = temp/fator;
}
fclose(log);
case 'y': // Abre o arquivo com o nome "save.dat"
load_file(factor, histogram);
break;
case 'k': //!NOT CALLED BY PYTHON CODE
TRACE("OK\n");
......@@ -302,14 +184,15 @@ int main(int argc, char **argv)
TRACE1("%u \n" ,ROM.m_serial);
break;
case set_autoGate:
case 'm': //set auto gate
N957_SetAcquisitionMode(user_setting.m_N957_handle, N957ControlModeAuto);
system_state.gate=TRUE;
break;
case set_externalGate:
case 'n': //set external gate
N957_SetAcquisitionMode(user_setting.m_N957_handle, N957ControlModeExtGate);
system_state.gate=FALSE;
break;
break;
default: factor = determine_factor(command);
}
}
......@@ -318,7 +201,7 @@ int main(int argc, char **argv)
// Calculate the histogram
for( i= 0; i< data_read; i++)
{
++histo_buff[ data_buff[ i]& ( N957_MAX_HISTO_SAMPLES- 1)];
++histo_buff[ data_buff[i]& ( N957_MAX_HISTO_SAMPLES- 1)];
// Log to file (if enabled)
sprintf( aux_string, "%04d\n", data_buff[ i]);
if (!system_state.paused)
......@@ -328,9 +211,8 @@ int main(int argc, char **argv)
}
}
if( user_setting.m_debug)
if(user_setting.m_debug)
{
//
// clear software convertion
if( N957_SetSwConvFlag( user_setting.m_N957_handle, N957_FALSE))
{
......@@ -340,28 +222,34 @@ int main(int argc, char **argv)
}
exit_point:
// Stop acquisition
//stop acquisition
if(N957_StopAcquire( user_setting.m_N957_handle))
{
TRACE("erro_24\n");
//goto exit_point;
}
if(N957_SwClear(user_setting.m_N957_handle))
{
TRACE("erro_25\n");
//goto exit_point;
}
// close modules
//close modules
user_settings_close( &user_setting);
return ret_val;
}
void initSystem(user_setting_data *user_setting, N957_UINT16 *data_buff, unsigned long *histo_buff)
void init_system(N957_UINT16 *data_buff, unsigned long *histo_buff, unsigned int *histogram)
{
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;
memset( &user_setting, 0, sizeof( user_setting));
// init user setting module
......@@ -371,7 +259,7 @@ void initSystem(user_setting_data *user_setting, N957_UINT16 *data_buff, unsigne
goto exit_point;
}
data_buff= malloc( user_setting->m_bldim* sizeof( N957_UINT16));
data_buff= malloc( user_setting.m_bldim* sizeof( N957_UINT16));
if( data_buff== NULL)
{
TRACE("erro_8\n");
......@@ -385,41 +273,34 @@ void initSystem(user_setting_data *user_setting, N957_UINT16 *data_buff, unsigne
}
memset( histo_buff, 0, N957_MAX_HISTO_SAMPLES* sizeof( *histo_buff));
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.
}
void start(user_setting_data *user_setting)
void start(N957_UINT32 data32)
{
/// input parameter check
if( !parse_config_file( &user_setting))
{
TRACE("erro_2\n");
N957_End(user_setting->m_N957_handle);
break;
N957_End(user_setting.m_N957_handle);
}
// now board handle is valid and we can start calling boards API
// Get firmware revision
char fw_rev[10];
if( ( N957_GetFWRelease( user_setting->m_N957_handle, fw_rev, 10)))
if( ( N957_GetFWRelease( user_setting.m_N957_handle, fw_rev, 10)))
{
TRACE("erro_3");
break;
}
if( N957_GetScaler(user_setting->m_N957_handle, &data32))
if( N957_GetScaler(user_setting.m_N957_handle, &data32))
{
TRACE("erro_5");
break;
}
if(N957_StartAcquire( user_setting.m_N957_handle, user_setting.m_mode))
{
TRACE("erro_6");
break;
}