#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 tfID;
static int controlID;
#define MAX_THREADS 10
static CmtThreadPoolHandle poolHandle = 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;
}
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];
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;
GetCtrlVal(ph, PANEL_FILENAME, filename );
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);
//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]) fp
= fopen(filename
,"wb");
else 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 = 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=0,told=0, tstart=0;
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 ) {
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
if (i % pfreq == 0) 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_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, 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 scan(void *functionData) {
int dx[2];
int nx[2];
int x0[2];
int ix[2];
int size;
char buffer[12]="POSR";
int *n = (int *) &buffer[4];
char filename[0xFF];
int enabledoutput;
FILE *fp;
GetCtrlVal(ph, PANEL_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 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) {
}
}
#endif
daq(functionData);
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;
}