#include <utility.h>
#include <ansi_c.h>
#include <cvirte.h>
#include <userint.h>
#include "drs4.h"
#include "drsread.h"
static int daq_on;
static int ph, plothandle[4];
static int tfID;
static int controlID;
#define MAX_THREADS 10
static CmtThreadPoolHandle poolHandle = 0;
int main (int argc, char *argv[]) {
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((ph = LoadPanel (0, "drs4.uir", PANEL)) < 0)
return -1;
SetStdioPort (CVI_STDIO_WINDOW);
SetSleepPolicy(VAL_SLEEP_MORE);
CmtNewThreadPool (MAX_THREADS, &poolHandle);
DisplayPanel (ph);
RunUserInterface ();
DiscardPanel (ph);
CmtDiscardThreadPool (poolHandle);
return 0;
}
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 ;
}
int CVICALLBACK daq(void *functionData) {
int neve;
char filename[0xff];
char smask[0xff];
unsigned long mask;
int frequency;
double trgdelay;
double trglevel;
int trgtype;
int trgchannel;
int trgpolarity;
int verbose;
double range;
GetCtrlVal(ph, PANEL_FILENAME, filename );
GetCtrlVal(ph, PANEL_MASK, smask );
GetCtrlVal(ph,PANEL_NEVE, &neve);
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_DEBUG, &verbose);
DRSSetMask( (unsigned char)( mask & 0xF ) );
DRSSetFrequency( frequency );
DRSSetTriggerPolarity(trgpolarity);
DRSSetTriggerLevel(trglevel);
DRSSetRange ( range );
DRSSetTriggerType( trgtype );
DRSSetTriggerChannel(trgchannel );
FILE
*fp
=fopen(filename
,"wb");
static unsigned char *buffer;
int buffer_size = 0;
const int nBoards=1;
const int waveDepth=1024;
if (buffer_size == 0) {
buffer_size = 4 + nBoards * (4 + 4*(4+waveDepth*4));
buffer_size += 24 + nBoards * (8 + 4*(4+waveDepth*2));
buffer
= (unsigned char *)malloc(buffer_size
);
}
time_t t,told, tstart;
if (!DRSInit()){
told=tstart;
int i=0;
for (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--;
if (!daq_on) break;
if (t!=told ) {
printf("%d events in %2.2f min (%d s) %s",i
+1, (double)(t
-tstart
)/60.
,(t
-tstart
), ctime(&t
));
}
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 )) ) continue;
float *t=DRSGetTime(k);
float *x=DRSGetWave(k);
const int col[4]={VAL_BLACK,VAL_RED,VAL_GREEN,VAL_BLUE};
if (plothandle[k]) DeleteGraphPlot (ph, PANEL_GRAPH, plothandle[k], VAL_DELAYED_DRAW);
plothandle[k] = PlotXY (ph, PANEL_GRAPH, t, x, 1024, VAL_FLOAT, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[k]);
for (int i=0 ; i<1024 ; i++) {
if (verbose
) printf("[%d] %d. x= %3.2f y=%3.2f\n", k
, i
, t
[i
], x
[i
] );
//h[k]->Fill( t[i], x[i]*1e-3);
}
}
printf("%d events in %2.2f min (%d s) %s",i
+1, (double)(t
-tstart
)/60.
,t
-tstart
, ctime(&t
));
}
DRSEnd();
}
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:
mythread = daq;
if (mythread!=NULL) {
printf("New Thread panel=%d button=%d\n", panel
, control
);
// SetDimming(1);
controlID= control;
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;
}