Rev 265 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#include <tcpsupp.h>
#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 "scaler.h"
#include "drsread.h"
#include "K6517.h"
K6517STATUS K6517Stat;
static int K6517_Interface,K6517_Port,K6517_Gdev;
//#define MIKRO
#ifdef MIKRO
#include "MIKRO.h"
#endif
/*---------------------------------------------------------------------------*/
/* Module-globals */
/*---------------------------------------------------------------------------*/
static unsigned int g_hconversation;
static int g_hmainPanel;
static int g_connected = 0;
static int g_TCPError = 0;
int daq_on;
int ph, p2,p3,p4, plothandle[4]= {0,0,0,0};
static int qdcplothandle[4]= {0,0,0,0};
static int scanplothandle;
static int tfID;
static int rID;
static int controlID;
#define MAX_THREADS 10
static CmtThreadPoolHandle poolHandle = 0;
static float gSum[4]={0,0,0,0};
int gActive[4]= {0,0,0,0};
double gTwinMin[4]= {0,0,0,0};
double gTwinMax[4]= {0,0,0,0};
double gFx[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;
if ((p3 = LoadPanel (0, "drs4.uir", QDC)) < 0)
return -1;
if ((p4 = LoadPanel (0, "scaler.uir", SCALER)) < 0)
return -1;
DisableBreakOnLibraryErrors();
SetStdioPort (CVI_STDIO_WINDOW);
SetSleepPolicy(VAL_SLEEP_MORE);
CmtNewThreadPool (MAX_THREADS, &poolHandle);
DisplayPanel (ph);
DisplayPanel (p2);
DisplayPanel (p3);
DisplayPanel (p4);
#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
K6517_open (1,4,3,0,13);
RunUserInterface ();
DiscardPanel (ph);
DiscardPanel (p2);
DiscardPanel (p3);
DiscardPanel (p4);
CmtDiscardThreadPool (poolHandle);
#ifdef MIKRO
MIKRO_Close ();
#endif
return 0;
}
char strbuf[0xFF];
int gLog=0;
int printf(const char *format, ...) {
va_list aptr;
int ret;
FILE *flog;
va_start(aptr, format);
ret = vsprintf(strbuf, format, aptr);
va_end(aptr);
SetCtrlVal(ph,PANEL_STDIO,strbuf);
if (gLog) {
flog = fopen ("stdio.log", "a");
fprintf (flog, "%s", strbuf);
fclose (flog);
}
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 EndOfThresholdScanThread ( CmtThreadPoolHandle poolhandle,
CmtThreadFunctionID functionID, unsigned int event,
int value, void *callbackData ) {
printf("End of Threshold Scan Thread \n");
return ;
}
void CVICALLBACK EndOfThread ( CmtThreadPoolHandle poolhandle,
CmtThreadFunctionID functionID, unsigned int event,
int value, void *callbackData ) {
daq_on=0;
//SetDimming(0);
printf("End of Thread \n");
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;
int wfm=0;
const int init[4] = { QDC_INIT_1 ,QDC_INIT_2 ,QDC_INIT_3 ,QDC_INIT_4 };
const int fx[4] = { PANEL_SCALE_1 ,PANEL_SCALE_2 ,PANEL_SCALE_3 ,PANEL_SCALE_4 };
const int active[4] = { QDC_ACTIVE_1 ,QDC_ACTIVE_2 ,QDC_ACTIVE_3 ,QDC_ACTIVE_4 };
GetCtrlVal(ph, PANEL_CH0, &imask[0] );
GetCtrlVal(ph, PANEL_CH1, &imask[1] );
GetCtrlVal(ph, PANEL_CH2, &imask[2] );
GetCtrlVal(ph, PANEL_CH3, &imask[3] );
GetCtrlVal(p3, QDC_WFM, &wfm );
mask = 0;
for (int i=0; i<4; i++) {
if (imask[i]) {
mask |= (1<<i);
ScaleCB(ph,fx[i],EVENT_COMMIT,NULL, 0,0);
SetActiveCB(p3,active[i],EVENT_COMMIT,NULL, 0,0);
InitQdcHistogramCB(p3, init[i],EVENT_COMMIT,NULL, 0,0);
}
}
LogScaleCB(p3, QDC_LOGY,EVENT_COMMIT,NULL, 0,0);
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 (1==1) {
GetCtrlVal(ph, PANEL_FILENAME, filename );
printf("%s\n", filename);
fp = fopen(filename,"wb");
} else {
GetCtrlVal(p2, SCAN_FILENAME, filename );
fp = fopen(filename,"ab");
printf("%s\n", filename);
}
}
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()) {
time(&tstart);
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;
time(&t);
if (t!=told ) {
PlotQdcHistogramCB(0,0,EVENT_COMMIT,NULL, 0,0);
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 )) ){
double fSum= 0;
float *t=DRSGetTime(k);
float *x=DRSGetWave(k);
;
for (int j=0 ; j<1024 ; j++) {
xs[k][j]= x[j]*1e-3*gFx[k];
if (gActive[k]){
if (t[j]> gTwinMin[k] && t[j] < gTwinMax[k]) {
fSum+= fabs(xs[k][j]);
}
if (wfm){
H2D_Fill(200+k,t[j], xs[k][j],1);
}
//if (verbose) printf("[%d] %d. x= %3.2f y=%3.2f\n", k, i, t[j], x[j] );
}
}
if (verbose) printf("qdc[%d] %3.2f active=%d\n", k, fSum ,gActive[k]);
if (gActive[k]) {
if (!wfm) {
H1D_Fill(200+k,fSum,1.);
}
}
gSum[k]+=fSum;
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]);
}
}
}
}
time(&t);
printf("%d events in %2.2f min (%d s) %s",nev, (double)(t-tstart)/60.,t-tstart, ctime(&t));
DRSEnd();
}
if (fp) fclose(fp);
free(buffer);
return 0;
}
void CVICALLBACK EndOfThrScan ( CmtThreadPoolHandle poolhandle,
CmtThreadFunctionID functionID, unsigned int event,
int value, void *callbackData ) {
//SetDimming(0);
printf("End of thrscan \n");
return ;
}
int scaler(int min, int max, int dx, int time_set, char *filename);
int CVICALLBACK thrscan(void *functionData) {
char str[0xFF];
char path[0xFF];
int step,min,max, mtime;
GetCtrlVal(p4, SCALER_STEP,&step);
GetCtrlVal(p4, SCALER_TIME,&mtime);
GetCtrlVal(p4, SCALER_MAX,&max);
GetCtrlVal(p4, SCALER_MIN,&min);
GetCtrlVal(ph, PANEL_FILENAME,path);
sprintf(str, "%s.thr", path);
scaler(min,max,step,mtime,str);
return 0;
}
int CVICALLBACK acquire_data (void *arg) {
char *sarg = (char *) arg;
printf("sarg %s\n", sarg);
char str[0xFF];
char path[0xFF];
GetCtrlVal(ph,PANEL_PATH,path);
sprintf(str, "%s%s.dat", path, sarg);
SetCtrlVal(ph, PANEL_FILENAME,str);
int flag=0;
ThreadFunctionPtr mythread = thrscan;
int data;
int threadhandle;
CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &data,
DEFAULT_THREAD_PRIORITY,
EndOfThresholdScanThread,
EVENT_TP_THREAD_FUNCTION_END,
NULL, RUN_IN_SCHEDULED_THREAD,
&threadhandle);
daq(&flag);
CmtWaitForThreadPoolFunctionCompletionEx (poolHandle, threadhandle, OPT_TP_PROCESS_EVENTS_WHILE_WAITING, CMT_WAIT_FOREVER);
return 0;
}
int CVICALLBACK SwitchOnOffCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
unsigned char state;
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal(panel, control, &state);
K6517_vsource_operate (state);
break;
}
return 0;
}
int CVICALLBACK SetVoltageCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
double vbr;
switch (event) {
case EVENT_COMMIT:
GetCtrlVal (ph, PANEL_VBR, &vbr );
K6517_vsource_set (vbr+2);
break;
}
return 0;
}
int TScan () {
double temp;
double volt,vbr;
int n=0;
int led;
GetNumTableRows (ph, PANEL_TABLE, &n );
daq_on = 1;
for(int i=0; i<n; i++) {
if (!daq_on) break;
GetTableCellVal (ph, PANEL_TABLE, MakePoint (1,i+1), &temp);
GetTableCellVal (ph, PANEL_TABLE, MakePoint (2,i+1), &vbr);
if (temp>100) break;
char transmitBuf[512]= {0};
sprintf(transmitBuf, "1 %f\n", temp);
if (g_connected)
if ( ClientTCPWrite (g_hconversation, transmitBuf, strlen (transmitBuf), 1000) < 0) printf("Transmit Error\n"); else printf("%s\n", transmitBuf);
SetCtrlVal(ph,PANEL_LED, 0);
do {
time_t t0 = time(NULL);
SetCtrlVal(ph,PANEL_TIME, ctime(&t0));
Delay(1);
GetCtrlVal(ph,PANEL_LED, &led);
if (led) {
for (int k=0;k<10;k++){
Delay(1);
if (!daq_on) break;
}
GetCtrlVal(ph,PANEL_LED, &led);
}
if (!daq_on) break;
} while (!led);
if (!daq_on) break;
int itemp = (int) temp;
int n1=0;
GetNumTableRows (ph, PANEL_VTABLE, &n1 );
for(int k=0; k<n1; k++) {
if (!daq_on) break;
GetTableCellVal (ph, PANEL_VTABLE, MakePoint (1,k+1), &volt);
if (volt<-100) break;
double voltage = vbr + volt;
int ivolt=(int)(volt*100);
K6517_vsource_set (voltage);
K6517_vsource_operate (1);
char str[0xFF];
sprintf(str,"%d_%d", itemp, ivolt);
printf("%d %d %s\n",i,k, str);
acquire_data(str);
K6517_vsource_operate (0);
}
}
return 0;
}
int CVICALLBACK TScanCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
switch (event) {
case EVENT_COMMIT:{
ThreadFunctionPtr mythread = TScan;
CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &rID,
DEFAULT_THREAD_PRIORITY,
EndOfThread,
EVENT_TP_THREAD_FUNCTION_END,
NULL, RUN_IN_SCHEDULED_THREAD,
&tfID);
}
break;
}
return 0;
}
int CVICALLBACK LogScaleCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
const unsigned int gr[4] = { QDC_GRAPH_1 ,QDC_GRAPH_2 ,QDC_GRAPH_3 ,QDC_GRAPH_4 };
int wfm=0;
int attr, logy, selected;
switch (event) {
case EVENT_COMMIT:
GetCtrlVal(p3,QDC_WFM,&wfm);
GetCtrlVal(panel, control, &selected);
attr= ATTR_YMAP_MODE;
if(selected) logy=VAL_LOG; else logy=VAL_LINEAR;
if (wfm) logy=VAL_LINEAR;
for (int i=0;i<4;i++) SetCtrlAttribute (panel, gr[i],attr, logy);
break;
}
return 0;
}
int CVICALLBACK ExecRootCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
switch (event) {
case EVENT_COMMIT:{
char fileName[100];
char fname[100];
char path[100];
int trgchannel;
GetCtrlVal(panel, PANEL_FILENAME, fileName);
GetCtrlVal(panel,PANEL_TRGCHANNEL, &trgchannel);
GetCtrlVal(panel, PANEL_PATH, path);
sprintf(fname, "%s/%s", path, fileName);
//sprintf(fname, "%s", fileName);
int status;
char name[MAX_PATHNAME_LEN];
// char dfile[MAX_PATHNAME_LEN];
// char efile[MAX_PATHNAME_LEN];
// status = FileSelectPopup ("dat", "*.dat", ".dat",
// "Izberi datoteko s podatki",
// VAL_LOAD_BUTTON, 0, 0, 1, 0, efile);
// EscapeString(efile,dfile);
sprintf(name ,"C:/home/dominik/runroot.bat \"C:/home/dominik/AnalyseWaveForms.cxx(-1,1000,\\\"%s\\\",%d)\" ", fname, trgchannel);
printf("%s\n",name);
LaunchExecutable(name);
break;
}
}
return 0;
}
int CVICALLBACK TempTableCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
switch (event) {
case EVENT_COMMIT: {
int status;
char name[MAX_PATHNAME_LEN];
char dfile[MAX_PATHNAME_LEN];
char efile[MAX_PATHNAME_LEN];
status = FileSelectPopup ("dat", "*.dat", ".dat",
"Izberi datoteko s podatki",
VAL_LOAD_BUTTON, 0, 0, 1, 0, efile);
printf("%s", efile);
FILE *fp=fopen(efile,"r");
float I;
float V;
int temp;
int ndim=400;
char line[400];
if (fp) {
int k=0;
while (fgets(line,ndim,fp)!=NULL) {
k++;
sscanf(line,"%*s%d%f%f",&temp,&I,&V);
printf("%d %f %f\n", temp, I, V);
SetTableCellVal (ph, PANEL_TABLE, MakePoint (1,k), (double)temp);
SetTableCellVal (ph, PANEL_TABLE, MakePoint (2,k), V);
}
SetTableCellVal (ph, PANEL_TABLE, MakePoint (1,k+1), 1000.0);
fclose(fp);
}
break;
}
}
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;
fwrite(runbuf, 1,4 ,fp);
fwrite(&size , 1,4 ,fp);
fwrite(x0 , 1,4*3 ,fp);
fwrite(dx , 1,4*3 ,fp);
fwrite(nx , 1,4*3 ,fp);
fclose(fp);
}
}
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;
fwrite(posrec, 1,4 ,fp);
fwrite(&size , 1,4 ,fp);
fwrite(n , 1,4*3 ,fp);
fwrite(idx , 1,4*3 ,fp);
fclose(fp);
}
}
#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 (panel == p4 && control == SCALER_START) mythread = thrscan;
if (panel == ph && control == PANEL_ACQUIRE) {
mythread = acquire_data;
controlID=50;
}
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 InitQdcHistogramCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
const int min[4] = { QDC_MIN_1 ,QDC_MIN_2 ,QDC_MIN_3 ,QDC_MIN_4 };
const int max[4] = { QDC_MAX_1 ,QDC_MAX_2 ,QDC_MAX_3 ,QDC_MAX_4 };
const int nbin[4] = { QDC_NBIN_1 ,QDC_NBIN_2 ,QDC_NBIN_3 ,QDC_NBIN_4 };
const int init[4] = { QDC_INIT_1 ,QDC_INIT_2 ,QDC_INIT_3 ,QDC_INIT_4 };
char title[0xFF];
char name[0xFF];
switch (event) {
case EVENT_COMMIT:{
int ctrl=-1;
for (int k=0;k<4;k++) if ( init[k]==control & panel==p3 ) ctrl=k;
if (ctrl<0) break;
double minx;
double maxx;
int nbins;
int wfm;
int frq;
GetCtrlVal(p3,min[ctrl],&minx);
GetCtrlVal(p3,max[ctrl],&maxx);
GetCtrlVal(p3,nbin[ctrl],&nbins);
GetCtrlVal(p3,QDC_WFM,&wfm);
GetCtrlVal(ph,PANEL_FREQUENCY,&frq);
sprintf(name,"qdc%d", ctrl);
sprintf(title,"QDC ch %d", ctrl);
H1D_Init(200+ctrl, name, title, nbins, minx,maxx);
sprintf(name,"avgwfm%d", ctrl);
sprintf(title,"Average waveform ch %d", ctrl);
H2D_Init(200+ctrl, name, title, 1024, 0,1024/frq, 100, -0.5,0.5);
break;
}
}
return 0;
}
int CVICALLBACK PlotQdcHistogramCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
int hid=0;
int nx;
int ny;
const unsigned int gr[4] = { QDC_GRAPH_1 ,QDC_GRAPH_2 ,QDC_GRAPH_3 ,QDC_GRAPH_4 };
int wfm=0;
switch (event) {
case EVENT_COMMIT:
GetCtrlVal(p3,QDC_WFM,&wfm);
for (int i=0;i<4;i++) if (wfm) H2D_Draw(200+i,p3,gr[i],&qdcplothandle[i]); else H1D_Draw(200+i,p3,gr[i],&qdcplothandle[i]);
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);
printf("redraw 2d\n");
} else {
if (nx>1) {
printf("redraw 1d x\n");
H1D_Draw(hid,p2,SCAN_GRAPH,&scanplothandle);
}
if (ny>1) {
printf("redraw 1d x\n");
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;
}
int CVICALLBACK OpenQdcCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
switch (event) {
case EVENT_COMMIT:
DisplayPanel (ph);
DisplayPanel (p3);
break;
}
return 0;
}
int CVICALLBACK CalibrateCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
int status = 0;
switch (event) {
case EVENT_COMMIT:
status = DRSCalibrateTiming();
printf("DRSCalibrateTiming() status = %d\n", status);
break;
}
return 0;
}
int CVICALLBACK SetActiveCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
const int c[4] = { QDC_ACTIVE_1 ,QDC_ACTIVE_2 ,QDC_ACTIVE_3 ,QDC_ACTIVE_4 };
const double twin0[4] = { QDC_TWIN0_1 ,QDC_TWIN0_2 ,QDC_TWIN0_3 ,QDC_TWIN0_4 };
const double twin1[4] = { QDC_TWIN1_1 ,QDC_TWIN1_2 ,QDC_TWIN1_3 ,QDC_TWIN1_4 };
int val=0;
switch (event) {
case EVENT_COMMIT:
GetCtrlVal(panel, control, &val);
int ctrl=-1;
for (int k=0;k<4;k++) if ( c[k]==control & panel==p3 ) ctrl=k;
if (ctrl<0) break;
gActive[ctrl] = val;
GetCtrlVal(panel, twin0[ctrl], &gTwinMin[ctrl]);
GetCtrlVal(panel, twin1[ctrl], &gTwinMax[ctrl]);
printf("Channel gActive[%d]=%d tmin %f tmax %f\n", ctrl,gActive[ctrl], gTwinMin[ctrl], gTwinMax[ctrl]);
break;
}
return 0;
}
int CVICALLBACK ScaleCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
const int c[4] = { PANEL_SCALE_1 ,PANEL_SCALE_2 ,PANEL_SCALE_3 ,PANEL_SCALE_4 };
switch (event) {
case EVENT_COMMIT: {
double val;
GetCtrlVal(panel, control, &val);
int ctrl=-1;
for (int k=0;k<4;k++) if ( c[k]==control & panel==ph ) ctrl=k;
if (ctrl<0) break;
gFx[ctrl] = val;
printf("Scaling factor gFx[%d]=%f\n", ctrl,gFx[ctrl]);
break;
}
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* Macros */
/*---------------------------------------------------------------------------*/
#define tcpChk(f) if ((g_TCPError=(f)) < 0) {ReportTCPError();}
/*---------------------------------------------------------------------------*/
/* Internal function prototypes */
/*---------------------------------------------------------------------------*/
int CVICALLBACK ClientTCPCB (unsigned handle, int event, int error,
void *callbackData);
static void ReportTCPError (void);
/*---------------------------------------------------------------------------*/
/* Report TCP Errors if any */
/*---------------------------------------------------------------------------*/
static void ReportTCPError(void)
{
if (g_TCPError < 0)
{
char messageBuffer[1024];
sprintf(messageBuffer,
"TCP library error message: %s\nSystem error message: %s",
GetTCPErrorString (g_TCPError), GetTCPSystemErrorString());
MessagePopup ("Error", messageBuffer);
g_TCPError = 0;
}
}
/*---------------------------------------------------------------------------*/
int CVICALLBACK DisconnectCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
switch (event) {
case EVENT_COMMIT:
if (g_connected)
DisconnectFromTCPServer (g_hconversation);
g_hconversation = 0;
g_connected = 0;
break;
}
return 0;
}
int CVICALLBACK ConnectCB (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) {
int portNum=10000;
char tempBuf[512];
sprintf(tempBuf,"localhost");
switch (event) {
case EVENT_COMMIT:
if (g_connected) return 0;
if (ConnectToTCPServer (&g_hconversation, portNum, tempBuf, ClientTCPCB, NULL, 5000) < 0)
MessagePopup("TCP Client", "Connection to server failed !");
else
{
SetWaitCursor (0);
g_connected = 1;
/* We are successfully connected -- gather info */
if (GetTCPHostAddr (tempBuf, 256) >= 0) printf("%s\n" ,tempBuf);
if (GetTCPHostName (tempBuf, 256) >= 0) printf("%s\n" ,tempBuf);
tcpChk (GetTCPPeerAddr (g_hconversation, tempBuf, 256));
printf("%s\n" ,tempBuf);
tcpChk (GetTCPPeerName (g_hconversation, tempBuf, 256));
printf("%s\n" ,tempBuf);
}
break;
}
return 0;
}
int CVICALLBACK ClientTCPCB (unsigned handle, int event, int error, void *callbackData)
{
char receiveBuf[256] = {0};
ssize_t dataSize = sizeof (receiveBuf) - 1;
switch (event)
{
case TCP_DATAREADY:
if ((dataSize = ClientTCPRead (g_hconversation, receiveBuf,
dataSize, 1000))
< 0)
{
printf( "Receive Error\n");
}
else
{
receiveBuf[dataSize] = '\0';
//printf("%s", receiveBuf);
float temp = 0;
float humidity = 0;
float dt=0;
float tdiff=0;
int t0 = 0;
sscanf(receiveBuf, "%d%f%f%f%f", &t0,&humidity,&temp, &tdiff, &dt);
SetCtrlVal(ph,PANEL_TMON, temp);
SetCtrlVal(ph,PANEL_HUMIDITY, humidity);
if (fabs(tdiff)<0.2 && fabs(dt) < 0.05) SetCtrlVal(ph,PANEL_LED, 1);
else SetCtrlVal(ph,PANEL_LED, 0);
}
break;
case TCP_DISCONNECT:
MessagePopup ("TCP Client", "Server has closed connection!");
g_connected = 0;
break;
}
return 0;
}