#include "H1D.h"
#include "H2D.h"
#include <formatio.h>
#include <utility.h>
#include <ansi_c.h>
#include <cvirte.h>
#include <userint.h>
#include "drs4.h"
#include "drsread.h"
#define MIKRO
#ifdef MIKRO
#include "MIKRO.h"
#endif
static int daq_on;
static int ph, p2,plothandle[4]= {0,0,0,0};
static int scanplothandle;
static int tfID;
static int controlID;
#define MAX_THREADS 10
static CmtThreadPoolHandle poolHandle = 0;
static float gSum[4]={0,0,0,0};
int main (int argc, char *argv[]) {
short port;
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((ph = LoadPanel (0, "drs4.uir", PANEL)) < 0)
return -1;
if ((p2 = LoadPanel (0, "drs4.uir", SCAN)) < 0)
return -1;
SetStdioPort (CVI_STDIO_WINDOW);
SetSleepPolicy(VAL_SLEEP_MORE);
CmtNewThreadPool (MAX_THREADS, &poolHandle);
DisplayPanel (ph);
DisplayPanel (p2);
#ifdef MIKRO
GetCtrlVal(p2, SCAN_PORT, &port);
if (MIKRO_Open (port)) MessagePopup ("Error", "Mikro Port Not found !\n Change in the GUI") ;
MIKRO_Init(1,0);
MIKRO_Init(2,0);
#endif
RunUserInterface ();
DiscardPanel (ph);
DiscardPanel (p2);
CmtDiscardThreadPool (poolHandle);
MIKRO_Close ();
return 0;
}
char strbuf[0xFF];
int gLog=0;
int printf(const char *format
, ...
) {
va_list aptr;
int ret;
FILE *flog;
SetCtrlVal(ph,PANEL_STDIO,strbuf);
if (gLog) {
flog
= fopen ("stdio.log", "a");
}
return(ret);
}
static void start_timer (double tout) {
SetCtrlAttribute (ph, PANEL_TIMER, ATTR_INTERVAL, tout);
SetCtrlAttribute (ph, PANEL_TIMER, ATTR_ENABLED, 1);
}
static void stop_timer ( void ) {
SetCtrlAttribute (ph, PANEL_TIMER, ATTR_ENABLED, 0);
DRSSetTimeout();
}
void CVICALLBACK EndOfThread ( CmtThreadPoolHandle poolhandle,
CmtThreadFunctionID functionID, unsigned int event,
int value, void *callbackData ) {
daq_on=0;
//SetDimming(0);
return ;
}
static float xs[4][1024];
int CVICALLBACK daq(void *functionData) {
int neve;
char filename[0xff];
int imask[4];
unsigned long mask;
int frequency;
double trgdelay;
double trglevel;
int trgtype;
int trgchannel;
int trgpolarity;
int verbose;
double range;
int pfreq;
int enabledoutput;
int neveold = 0;
double rate;
int *args = (int *) functionData;
double twin[2];
GetCtrlVal(ph, PANEL_CH0, &imask[0] );
GetCtrlVal(ph, PANEL_CH1, &imask[1] );
GetCtrlVal(ph, PANEL_CH2, &imask[2] );
GetCtrlVal(ph, PANEL_CH3, &imask[3] );
mask = 0;
for (int i=0; i<4; i++) {
if (imask[i]) mask |= (1<<i);
}
GetCtrlVal(ph,PANEL_NEVE, &neve);
GetCtrlVal(ph,PANEL_DEBUG, &verbose);
GetCtrlVal(ph,PANEL_PFREQ, &pfreq);
GetCtrlVal(ph,PANEL_ENABLEDOUTPUT, &enabledoutput);
GetCtrlVal(ph,PANEL_FREQUENCY, &frequency);
GetCtrlVal(ph,PANEL_TRGDELAY, &trgdelay);
GetCtrlVal(ph,PANEL_TRGCHANNEL, &trgchannel);
GetCtrlVal(ph,PANEL_TRGTYPE, &trgtype);
GetCtrlVal(ph,PANEL_TRGLEVEL, &trglevel);
GetCtrlVal(ph,PANEL_TRGPOLARITY, &trgpolarity);
GetCtrlVal(ph,PANEL_RANGE, &range);
GetCtrlVal(ph,PANEL_TWIN0, &twin[0]);
GetCtrlVal(ph,PANEL_TWIN1, &twin[1]);
//printf("mask=0x%x\n",mask);
DRSSetMask( (unsigned char)( mask & 0xF ) );
DRSSetFrequency( frequency );
DRSSetTriggerDelay(trgdelay );
DRSSetTriggerChannel(trgchannel );
DRSSetTriggerType( trgtype );
DRSSetTriggerLevel(trglevel);
DRSSetTriggerPolarity(trgpolarity);
DRSSetRange ( range );
FILE *fp= NULL;
if (enabledoutput) {
if (args[0]) {
GetCtrlVal(ph, PANEL_FILENAME, filename );
fp
= fopen(filename
,"wb");
} else {
GetCtrlVal(p2, SCAN_FILENAME, filename );
fp
= fopen(filename
,"ab");
}
}
static unsigned char *buffer;
int buffer_size = 0;
const int nBoards=1;
const int waveDepth=1024;
if (buffer_size == 0) {
buffer_size = 8; // file header + time header
buffer_size += nBoards * (4 + 4*(4+waveDepth*4)); // bin widths
buffer_size += 24 + nBoards * (8 + 4*(8+waveDepth*2));
buffer
= (unsigned char *)malloc(buffer_size
);
}
time_t t=0,told=0, tstart=0;
if (!DRSInit()) {
told=tstart;
int nev=0;
for (int k = 0;k<4;k++){
gSum[k]=0;
}
for (int i=0; i<neve; i++) {
start_timer(1);// 1 s timeout
int retval = DRSRead(0);
stop_timer();
int nb = ( retval == 0 && fp ) ? DRSToBuffer( buffer , i ) : 0;
SetCtrlVal(ph,PANEL_CEVE,i);
if (retval) i--;
nev++;
if (!daq_on) break;
if (t!=told ) {
rate = (i-neveold);
printf("%d events in %2.2f min (%d s) Rate %f Hz %s ",i
+1, (double)(t
-tstart
)/60.
,(t
-tstart
), rate
, ctime(&t
));
GetCtrlVal(ph,PANEL_PFREQ, &pfreq);
neveold = i;
}
told=t;
// Save data
if (nb
>0 && fp
) fwrite(buffer
, 1,nb
,fp
);
// Plot Data
for (int k=0; k<4; k++) {
if ( (mask & ( 0x1<<k )) ){
float *t=DRSGetTime(k);
float *x=DRSGetWave(k);
;
for (int j=0 ; j<1024 ; j++) {
xs[k][j]= x[j]*1e-3;
if (t
[j
]> twin
[0] && t
[j
] < twin
[1]) gSum
[k
]+= fabs(xs
[k
][j
]);
if (verbose
) printf("[%d] %d. x= %3.2f y=%3.2f\n", k
, i
, t
[j
], x
[j
] );
//h[k]->Fill( t[i], x[i]*1e-3);
}
if (i % pfreq == 0) {
const int col[4]= {VAL_WHITE,VAL_RED,VAL_GREEN,VAL_BLUE};
if (plothandle[k]) DeleteGraphPlot (ph, PANEL_GRAPH, plothandle[k], VAL_IMMEDIATE_DRAW);
plothandle[k] = PlotXY (ph, PANEL_GRAPH, t, xs[k], 1024, VAL_FLOAT, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[k]);
}
}
}
}
printf("%d events in %2.2f min (%d s) %s",nev
, (double)(t
-tstart
)/60.
,t
-tstart
, ctime(&t
));
DRSEnd();
}
return 0;
}
int CVICALLBACK scan(void *functionData) {
int dx[3]={0,0,0};
int nx[3]={0,0,0};
int x0[3]={0,0,0};
int ix[3]={0,0,0};
int idx[3]={0,0,0};
int size;
char posrec[4]="POSR";
char runbuf[4]="PRUN";
int n[3];
char filename[0xFF];
int enabledoutput;
FILE *fp;
GetCtrlVal(p2, SCAN_FILENAME, filename );
GetCtrlVal(ph,PANEL_ENABLEDOUTPUT, &enabledoutput);
if ( GetFileInfo(filename,&size) ) {
MessagePopup ("Warning","File exist. Remove it first or choose another file");
return 0;
}
GetCtrlVal(p2, SCAN_STEPX, &dx[0]);
GetCtrlVal(p2, SCAN_STEPY, &dx[1]);
GetCtrlVal(p2, SCAN_NSTEPSX, &nx[0]);
GetCtrlVal(p2, SCAN_NSTEPSY, &nx[1]);
GetCtrlVal(p2, SCAN_STARTX, &x0[0]);
GetCtrlVal(p2, SCAN_STARTY, &x0[1]);
for (int k=0;k<4;k++) {
H2D_Init(k, "charge","Induced charge",
nx[0], x0[0] - dx[0]*0.5 ,x0[0] + dx[0] * ( nx[0] - 0.5) ,
nx[1], x0[1] - dx[1]*0.5 ,x0[1] + dx[1] * ( nx[1] - 0.5));
H1D_Init(k, "charge","Induced charge projection x",
nx[0], x0[0] - dx[0]*0.5 ,x0[0] + dx[0] * ( nx[0] - 0.5) );
H1D_Init(100 + k, "charge","Induced charge projection y",
nx[1], x0[1] - dx[1]*0.5 ,x0[1] + dx[1] * ( nx[1] - 0.5) );
}
if (enabledoutput) {
fp
= fopen(filename
,"ab");
if (fp) {
size=36;
}
}
for (int i=0; i<nx[0]; i++) {
ix[0]= x0[0]+i*dx[0];
#ifdef MIKRO
MIKRO_MoveTo(1,ix[0]);
#endif
SetCtrlVal (p2, SCAN_IX, i);
for (int j=0; j<nx[1]; j++) {
SetCtrlVal (p2, SCAN_IY, j);
ix[1]= x0[1]+j*dx[1];
#ifdef MIKRO
MIKRO_MoveTo(2,ix[1]);
MIKRO_GetPosition(1,&n[0]);
SetCtrlVal (p2, SCAN_XP, n[0]);
MIKRO_GetPosition(2,&n[1]);
SetCtrlVal (p2, SCAN_YP, n[1]);
if (enabledoutput) {
fp
= fopen(filename
,"ab");
if (fp) {
idx[0]=i;
idx[1]=j;
size=24;
}
}
#endif
daq(functionData);
for (int k=0;k<4;k++) {
H2D_Fill(k, ix[0], ix[1] , gSum[k] );
H1D_Fill(k, ix[0] , gSum[k] );
H1D_Fill(100+ k, ix[1] , gSum[k] );
}
PlotScanHistogramCB(0,0,EVENT_COMMIT,NULL, 0,0);
if (!daq_on) break;
}
if (!daq_on) break;
}
return 0;
}
int CVICALLBACK StartCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
ThreadFunctionPtr mythread = NULL;
switch (event) {
case EVENT_COMMIT:
controlID=0;
if (panel == ph && control == PANEL_START) {
mythread = daq;
controlID= control;
}
if (panel == p2 && control == SCAN_SCAN) mythread = scan;
if (mythread!=NULL) {
printf("New Thread panel=%d button=%d\n", panel
, control
);
// SetDimming(1);
daq_on=1;
CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &controlID,
DEFAULT_THREAD_PRIORITY,
EndOfThread,
EVENT_TP_THREAD_FUNCTION_END,
NULL, RUN_IN_SCHEDULED_THREAD,
&tfID);
}
break;
}
return 0;
}
int CVICALLBACK StopCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
switch (event) {
case EVENT_COMMIT:
daq_on=0;
break;
}
return 0;
}
int CVICALLBACK ExitCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
switch (event) {
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}
int CVICALLBACK MoveStageCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
int axis=0, step=1000, direction=1, n;
switch (event) {
case EVENT_COMMIT:
if (panel == p2) {
switch (control) {
case SCAN_BR :
axis = 1;
direction = 1;
GetCtrlVal(p2, SCAN_STEPX, &step);
break;
case SCAN_BL :
axis = 1;
direction = -1;
GetCtrlVal(p2, SCAN_STEPX, &step);
break;
case SCAN_BU :
axis = 2;
direction = 1;
GetCtrlVal(p2, SCAN_STEPY, &step);
break;
case SCAN_BD :
axis = 2;
direction = -1;
GetCtrlVal(p2, SCAN_STEPY, &step);
break;
}
#ifdef MIKRO
MIKRO_MoveFor(axis, direction*step );
MIKRO_GetPosition(axis,&n);
if (axis == 1) SetCtrlVal (p2, SCAN_XP, n);
if (axis == 2) SetCtrlVal (p2, SCAN_YP, n);
#endif // MIKRO
}
break;
}
return 0;
}
int CVICALLBACK GoXCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
int n2;
switch (event) {
case EVENT_COMMIT:
GetCtrlVal (p2, SCAN_XG, &n2);
#ifdef MIKRO
MIKRO_MoveTo(1,n2);
MIKRO_GetPosition(1,&n2);
#endif
SetCtrlVal (p2, SCAN_XP, n2);
break;
}
return 0;
}
int CVICALLBACK GoYCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
int n2;
switch (event) {
case EVENT_COMMIT:
GetCtrlVal (p2, SCAN_YG, &n2);
#ifdef MIKRO
MIKRO_MoveTo(2,n2);
MIKRO_GetPosition(2,&n2);
#endif
SetCtrlVal (p2, SCAN_YP, n2);
break;
}
return 0;
}
int CVICALLBACK GetCurrentPositionCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
int n[2];
switch (event) {
case EVENT_COMMIT:
#ifdef MIKRO
MIKRO_GetPosition(1,&n[0]);
SetCtrlVal (p2, SCAN_XP, n[0]);
MIKRO_GetPosition(2,&n[1]);
SetCtrlVal (p2, SCAN_YP, n[1]);
#endif
break;
}
return 0;
}
int CVICALLBACK HomeCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
switch (event) {
case EVENT_COMMIT:
#ifdef MIKRO
MIKRO_ReferenceMove(1);
MIKRO_ReferenceMove(2);
GetCurrentPositionCB(panel, control, event, NULL, 0, 0);
#endif
break;
}
return 0;
}
int CVICALLBACK PlotScanHistogramCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
int hid=0;
int nx;
int ny;
switch (event) {
case EVENT_COMMIT:
GetCtrlVal (p2, SCAN_CHANNEL, &hid);
GetCtrlVal(p2, SCAN_NSTEPSX, &nx);
GetCtrlVal(p2, SCAN_NSTEPSY, &ny);
if (nx>1 && ny>1) {
H2D_Draw(hid,p2,SCAN_GRAPH,&scanplothandle);
} else {
if (nx>1) {
H1D_Draw(hid,p2,SCAN_GRAPH,&scanplothandle);
}
if (ny>1) {
H1D_Draw(100+hid,p2,SCAN_GRAPH,&scanplothandle);
}
}
break;
}
return 0;
}
int CVICALLBACK OpenGuiCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
switch (event) {
case EVENT_COMMIT:
DisplayPanel (ph);
DisplayPanel (p2);
break;
}
return 0;
}