Subversion Repositories f9daq

Rev

Rev 241 | Rev 243 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #include "redpitaya_gui.h"
  2. #include <ansi_c.h>
  3. #include <tcpsupp.h>
  4. #include <utility.h>
  5. #include <cvirte.h>
  6. #include <userint.h>
  7. #include "redpitaya_gui.h"
  8.  
  9. #include "H1D.h"
  10.  
  11.  
  12. #define NBEFORE 150
  13. #define MAXSAMPLES 16384
  14. #define MINTRGDELAY -8192
  15.  
  16.  
  17. static int panelHandle;
  18.  
  19. static unsigned int chandle = 0;
  20. static int tfID;
  21. int pfreq;
  22. static int plothandle[2]= {0,0};
  23. static int tdcplothandle[2]= {0,0};
  24. static int adcplothandle[2]= {0,0};
  25. int debug ;
  26. int initialized = 0;
  27. #define MAX_THREADS 10
  28. int nsamples=0;
  29. int adctype;
  30. float athreshold, twin0,twin1;
  31. float timebins[0XFFFF];
  32.  
  33. FILE *foutput;
  34.  
  35. static CmtThreadPoolHandle poolHandle = 0;
  36. int ctrl_c=0;
  37.  
  38.  
  39. int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData);
  40.  
  41.  
  42.  
  43. static int export_data (int hid) {
  44.  
  45.   char filename[0xFF],rootcmd[0xFF];
  46.   int type=0;
  47.  
  48.   GetCtrlVal(panelHandle,PANEL_EXPORTNAME,filename);
  49.   GetCtrlVal(panelHandle,PANEL_FILETYPE,&type);
  50.  
  51.   FILE *fp;
  52.  
  53.   switch (type) {
  54.     case 0:
  55.     case 1:
  56.       fp  =fopen(filename,"wb");
  57.       if (fp) {
  58.         H1D_Write2File(hid,fp);
  59.         fclose(fp);
  60.         if (type) {
  61.           sprintf(rootcmd ,"thisroot.bat && root.exe H1Dload.cxx(\\\"%s\\\")", filename);
  62.           LaunchExecutable(rootcmd);
  63.  
  64.         }
  65.       }
  66.       break;
  67.     case 2:
  68.       fp=fopen(filename,"w");
  69.       if (fp) {
  70.         for (int i=0; i<H1D_GetNbinsX(hid); i++) fprintf(fp,"%f\n", H1D_GetBinContent(hid,i) );
  71.         fclose(fp);
  72.       }
  73.       break;
  74.  
  75.   }
  76.  
  77.  
  78.   return (0);
  79. }
  80.  
  81.  
  82. int main (int argc, char *argv[]) {
  83.   if (InitCVIRTE (0, argv, 0) == 0)
  84.     return -1;  /* out of memory */
  85.   if ((panelHandle = LoadPanel (0, "redpitaya_gui.uir", PANEL)) < 0)
  86.     return -1;
  87.  
  88.  
  89.   SetStdioPort (CVI_STDIO_WINDOW);
  90.   SetSleepPolicy(VAL_SLEEP_MORE);
  91.   CmtNewThreadPool (MAX_THREADS,  &poolHandle);
  92.  
  93.   //for (int i=0;i<1000;i++) H1D_Fill(1,i,i);
  94.   //H1D_Draw(1,panelHandle,PANEL_ADC1,&adcplothandle[0]);
  95.   printf("size of double = %d\n",sizeof(double));
  96.   DisplayPanel (panelHandle);
  97.   RunUserInterface ();
  98.   DiscardPanel (panelHandle);
  99.   CmtDiscardThreadPool (poolHandle);
  100.   if (chandle!=0) DisconnectFromTCPServer (chandle);
  101.   return 0;
  102. }
  103.  
  104. char strbuf[0xFF];
  105.  
  106. int gLog=0;
  107.  
  108. int printf(const char *format, ...) {
  109.   va_list aptr;
  110.   int ret;
  111.   FILE *flog;
  112.  
  113.   va_start(aptr, format);
  114.   ret = vsprintf(strbuf, format, aptr);
  115.   va_end(aptr);
  116.   SetCtrlVal(panelHandle,PANEL_STDIO,strbuf);
  117.  
  118.   if (gLog) {
  119.     flog = fopen ("stdio.log", "a");
  120.     fprintf (flog, "%s", strbuf);
  121.     fclose (flog);
  122.   }
  123.   return(ret);
  124. }
  125.  
  126. int histoinit() {
  127.   int nch;
  128.   float min,max;
  129.  
  130.   GetCtrlVal(panelHandle,PANEL_NCH_1, &nch);
  131.   GetCtrlVal(panelHandle,PANEL_MINX_1, &min);
  132.   GetCtrlVal(panelHandle,PANEL_MAXX_1, &max);
  133.  
  134.   H1D_Init(1, "ADC ch 1","Pulse height", nch, min, max );
  135.   H1D_SetTitleX(1,"ADC (V)");
  136.   H1D_SetTitleY(1,"N");
  137.  
  138.   GetCtrlVal(panelHandle,PANEL_NCH_2, &nch);
  139.   GetCtrlVal(panelHandle,PANEL_MINX_2, &min);
  140.   GetCtrlVal(panelHandle,PANEL_MAXX_2, &max);
  141.  
  142.   H1D_Init(2, "ADC ch 2","Pulse height", nch, min, max );
  143.   H1D_SetTitleX(2,"ADC (V)");
  144.   H1D_SetTitleY(2,"N");
  145.  
  146.   GetCtrlVal(panelHandle,PANEL_NCH_3, &nch);
  147.   GetCtrlVal(panelHandle,PANEL_MINX_3, &min);
  148.   GetCtrlVal(panelHandle,PANEL_MAXX_3, &max);
  149.  
  150.   H1D_Init(3, "TDC ch 1","TDC", nch, min, max  );
  151.   H1D_SetTitleX(3,"TDC (us)");
  152.   H1D_SetTitleY(3,"N");
  153.  
  154.   GetCtrlVal(panelHandle,PANEL_NCH_4, &nch);
  155.   GetCtrlVal(panelHandle,PANEL_MINX_4, &min);
  156.   GetCtrlVal(panelHandle,PANEL_MAXX_4, &max);
  157.  
  158.   H1D_Init(4, "TDC ch 2","TDC", nch, min, max   );
  159.   H1D_SetTitleX(4,"TDC (us)");
  160.   H1D_SetTitleY(4,"N");
  161.  
  162.   SetCtrlAttribute  (panelHandle, PANEL_ADC1, ATTR_XNAME, H1D_GetTitleX(1) );
  163.   SetCtrlAttribute  (panelHandle, PANEL_ADC1, ATTR_YNAME, H1D_GetTitleY(1) );
  164.   SetCtrlAttribute  (panelHandle, PANEL_ADC2, ATTR_XNAME, H1D_GetTitleX(2) );
  165.   SetCtrlAttribute  (panelHandle, PANEL_ADC2, ATTR_YNAME, H1D_GetTitleY(2) );
  166.  
  167.   SetCtrlAttribute  (panelHandle, PANEL_TDC1, ATTR_XNAME, H1D_GetTitleX(3) );
  168.   SetCtrlAttribute  (panelHandle, PANEL_TDC1, ATTR_YNAME, H1D_GetTitleY(3) );
  169.   SetCtrlAttribute  (panelHandle, PANEL_TDC2, ATTR_XNAME, H1D_GetTitleX(4) );
  170.   SetCtrlAttribute  (panelHandle, PANEL_TDC2, ATTR_YNAME, H1D_GetTitleY(4) );
  171.  
  172.   GetCtrlVal(panelHandle,PANEL_MINX_5, &min);
  173.   GetCtrlVal(panelHandle,PANEL_MAXX_5, &max);
  174.   SetAxisScalingMode (panelHandle, PANEL_GRAPH, VAL_LEFT_YAXIS, VAL_MANUAL, min, max);
  175.   SetCtrlAttribute  (panelHandle, PANEL_GRAPH, ATTR_LABEL_TEXT , "sampling adc data");
  176.   SetCtrlAttribute  (panelHandle, PANEL_GRAPH, ATTR_XNAME, "t(us)" );
  177.   SetCtrlAttribute  (panelHandle, PANEL_GRAPH, ATTR_YNAME, "U(V)" );
  178.  
  179.   GetCtrlVal(panelHandle,PANEL_TWIN0, &twin0);
  180.   GetCtrlVal(panelHandle,PANEL_TWIN1, &twin1);
  181.   GetCtrlVal(panelHandle,PANEL_ADCTYPE, &adctype);
  182.   GetCtrlVal(panelHandle,PANEL_ITRGLEVEL  , &athreshold);
  183.   return 0;
  184. }
  185.  
  186. int analyse(int nb, unsigned char *cdata) {
  187.  
  188.   int   *ibuf = (int *)cdata;
  189.   float *fbuf = (float *)cdata;
  190.   int neve=0;
  191.   int *data = (ibuf+3);
  192.   int nr=0;
  193.   static float adc = 10000;
  194.   static float qdc = 0;
  195.   printf("Run HDR LEN=%d NEVE=%d dt=%f adc=%f qdc=%f\n", ibuf[0],ibuf[1],fbuf[2],adc,qdc);
  196.   while (nr<nb) {
  197.  
  198.     int recid   = *data++;
  199.     int chmask  = *data++;
  200.     nr +=8;
  201.     if (recid!=0x2) continue;
  202.     for (int id=0; id<2; id++) {
  203.       if ( !(chmask & (1 << id)) ) {
  204.         if (neve % pfreq == 0)
  205.           if (plothandle[id]) {
  206.             DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW);
  207.             plothandle[id] = 0;
  208.           }
  209.         continue;
  210.       }
  211.       if ( id != *(data++) ) printf("Error\n");
  212.  
  213.       int nsamples = *(data++);
  214.       if (nsamples<=0 || nsamples>16*1024) {
  215.         printf("Error nsamples %d\n", nsamples);
  216.         return -1;
  217.       }
  218.       float *fdata = (float *) data;
  219.       if ( nsamples>0  && neve % pfreq == 0) {
  220.         const int col[4]= {VAL_RED,VAL_GREEN,VAL_BLUE,VAL_WHITE};
  221.         if (plothandle[id]) DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW);
  222.  
  223.         plothandle[id] = PlotXY (panelHandle, PANEL_GRAPH, timebins, fdata, nsamples, VAL_FLOAT, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[id]);
  224.  
  225.         //plothandle[id] = PlotXY (panelHandle, PANEL_GRAPH, timebins, data, nsamples, VAL_FLOAT, VAL_INTEGER, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[id]);
  226.         H1D_Draw(1,panelHandle,PANEL_ADC1,&adcplothandle[0]);
  227.         H1D_Draw(2,panelHandle,PANEL_ADC2,&adcplothandle[1]);
  228.         H1D_Draw(3,panelHandle,PANEL_TDC1,&tdcplothandle[0]);
  229.         H1D_Draw(4,panelHandle,PANEL_TDC2,&tdcplothandle[1]);
  230.  
  231.         if (debug) for (int k=0; k<10; k++) printf("%d %d (%f , %d)\n", id,k, timebins[k],data[k]);
  232.       }
  233.  
  234.       nr+=8;
  235.       adc=10000;
  236.       qdc=0;
  237.       int nqdc;
  238.       for (int k=1; k<nsamples; k++) {
  239.         float t =timebins[k];
  240.         float tp=timebins[k-1];
  241.         if (fdata[k] < adc) adc = fdata[k];
  242.         if (t>twin0 && t<twin1 ) {
  243.           nqdc++;
  244.           qdc+=fdata[k];
  245.         }  
  246.         if (fdata[k]< athreshold && fdata[k-1]> athreshold) {
  247.           double t0= tp+(athreshold-fdata[k-1])/(fdata[k]-fdata[k-1])* (t-tp);
  248.           H1D_Fill(3+id, t0,1);
  249.         }
  250.       }
  251.       if (nqdc) qdc/=nqdc;
  252.       //printf("adc %f qdc %f\n", adc,qdc);
  253.       if (adctype)
  254.         H1D_Fill(1+id, -adc,1);
  255.       else
  256.         H1D_Fill(1+id, -qdc,1);
  257.      
  258.       nr+=4*nsamples;
  259.       data+=nsamples;
  260.     }
  261.     recid      = *data++;
  262.     int event  = *data++;
  263.     nr+=8;
  264.     neve++;
  265.     if (debug) printf("recid %d event %d\n",recid, event );
  266.   }
  267.  
  268.   return neve;
  269. }
  270.  
  271. const int maxlen = 100000000;
  272. unsigned char data[maxlen];
  273. int *idata = (int *) &data[0];
  274.  
  275.  
  276.  
  277. int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData) {
  278.  
  279.   int nb = 0 ;
  280.   static int event = 0;
  281.   static int ncalls = 0;
  282.  
  283.   switch (xType) {
  284.     case TCP_DISCONNECT:
  285.       printf("TCP_DISCONNECT ErrorString %s\n",GetTCPErrorString(errCode));
  286.       printf("TCP_DISCONNECT SystemErrorString %s\n",GetTCPSystemErrorString());
  287.  
  288.       chandle = 0;
  289.       break;
  290.     case TCP_DATAREADY: {
  291.       int  hdr[2]= {0,0};
  292.       nb = ClientTCPRead(handle,&hdr[0],8,1000);
  293.  
  294.       int size = hdr[1] - 8;
  295.       if (size>maxlen) size=maxlen;
  296.       nb = 0;
  297.       while (nb < size) {
  298.         int retval = ClientTCPRead(handle,&data[nb],size-nb,1000);
  299.         if (retval<1) break;
  300.         nb += retval;
  301.       }
  302.       if (debug) printf("Received RECID %d HDRLEN %d == read %d\n", hdr[0], size, nb);
  303.  
  304.  
  305.       switch (hdr[0]) {
  306.         case 0:
  307.           data[nb]=0;
  308.           printf("%s\n",data);
  309.           break;
  310.         case 1:// read
  311.  
  312.           GetCtrlVal(panelHandle,PANEL_CEVE     , &event);
  313.           GetCtrlVal(panelHandle,PANEL_NTOTAL   , &ncalls);
  314.           event += analyse(nb,data);
  315.           if (foutput) fwrite(data, 1, nb, foutput);
  316.  
  317.           SetCtrlVal(panelHandle,PANEL_CEVE     , event);
  318.           if (event>=ncalls) StartCB (panelHandle, PANEL_START, EVENT_COMMIT, NULL,1,0);
  319.  
  320.           break;
  321.         default:
  322.           printf("Unknown command = %d\n", hdr[0]);
  323.           break;
  324.       }
  325.       break;
  326.  
  327.     }
  328.   }
  329.   return 0;
  330. }
  331.  
  332.  
  333.  
  334. int CVICALLBACK StartCB (int panel, int control, int event,
  335.                          void *callbackData, int eventData1, int eventData2) {
  336.   char ip[0xFF];
  337.   int hdr[0xFF];
  338.   char filename[0xFF];
  339.   unsigned short *sbuff = (unsigned short *)&hdr[5] ;
  340.   unsigned char  *cbuff = (unsigned char *)&sbuff[3] ;
  341.   int imask[2];
  342.   unsigned char mask;
  343.   unsigned char trigger;
  344.   unsigned short nsamples;
  345.   int delay;
  346.   int nbefore;
  347.   unsigned short decimation;
  348.   unsigned short neve;
  349.   int output;
  350.   switch (event) {
  351.     case EVENT_COMMIT: {
  352.       GetCtrlVal(panel,PANEL_IP, ip);
  353.       GetCtrlVal(panel,PANEL_TRIGGER, &trigger);
  354.       GetCtrlVal(panel,PANEL_SAMPLES, &nsamples);
  355.       GetCtrlVal(panel,PANEL_DECIMATION,&decimation);
  356.       GetCtrlVal(panel,PANEL_NEVE   , &neve);
  357.       GetCtrlVal(panel,PANEL_CH0    , &imask[0] );
  358.       GetCtrlVal(panel,PANEL_CH1    , &imask[1]  );
  359.       GetCtrlVal(panel,PANEL_PFREQ  , &pfreq);
  360.       GetCtrlVal(panel,PANEL_DEBUG  , &debug);
  361.       GetCtrlVal(panel,PANEL_NBEFORE  , &nbefore);
  362.       GetCtrlVal(panel,PANEL_ENABLEDOUTPUT, &output);
  363.       GetCtrlVal(panel,PANEL_FILENAME, filename);
  364.  
  365.       delay= MINTRGDELAY + nsamples - nbefore + 1;
  366.  
  367.       mask = 0;
  368.       for (int i=0; i<2; i++) {
  369.         if (imask[i]) mask |= (1<<i);
  370.       }
  371.  
  372.       double level =0;
  373.       GetCtrlVal(panel,PANEL_TRGLEVEL , &level);
  374.      
  375.  
  376.       switch (control) {
  377.         case PANEL_CONNECT: {
  378.           int state;
  379.           GetCtrlVal(panel,PANEL_CONNECT, &state);
  380.           if (state) {
  381.             ConnectToTCPServerEx (&chandle, 9930, ip, SocketCB, NULL, 0, TCP_ANY_LOCAL_PORT);
  382.           } else {
  383.             if (chandle!=0) DisconnectFromTCPServer (chandle);
  384.             chandle = 0;
  385.           }
  386.         }
  387.         break;
  388.         case PANEL_START: {
  389.           int state;
  390.           GetCtrlVal(panel,PANEL_START, &state);
  391.           if (state && eventData1==0) {
  392.             histoinit();
  393.             for (int i=0; i<nsamples; i++) timebins[i]=(i-nbefore)*8*decimation/1000.;
  394.             if (output) foutput = fopen(filename, "wb");
  395.  
  396.             SetCtrlVal(panel,PANEL_CEVE  , 0);
  397.             ctrl_c=0;
  398.             hdr[0] = 0;
  399.             hdr[1] = 7*sizeof(int);
  400.             hdr[2] = delay;
  401.             hdr[3] = decimation;
  402.             hdr[4] = level * 1000;
  403.             sbuff[0] = neve;
  404.             sbuff[1] = nsamples;
  405.             sbuff[2] = 1000; //tout
  406.             cbuff[0] = trigger;
  407.             cbuff[1] = mask;
  408.             ClientTCPWrite(chandle,&hdr[0],hdr[1],5000);  // acquire
  409.           } else {
  410.             hdr[0] = 1;
  411.             hdr[1] = 2*sizeof(int);
  412.             ClientTCPWrite(chandle,&hdr[0],hdr[1],5000);  // stop the transfer
  413.  
  414.             if (foutput) fclose(foutput);
  415.             foutput=NULL;
  416.           }
  417.           break;
  418.         }
  419.         default:
  420.           printf("Unknown command\n");
  421.           break;
  422.       }
  423.  
  424.  
  425.  
  426.  
  427.       ProcessSystemEvents ();
  428.       break;
  429.     }
  430.   }
  431.   return 0;
  432. }
  433.  
  434.  
  435. int CVICALLBACK ExitCB (int panel, int control, int event,
  436.                         void *callbackData, int eventData1, int eventData2) {
  437.   switch (event) {
  438.     case EVENT_COMMIT:
  439.       QuitUserInterface(0);
  440.       break;
  441.   }
  442.   return 0;
  443. }
  444.  
  445.  
  446.  
  447. int CVICALLBACK ExportCB (int panel, int control, int event,
  448.                           void *callbackData, int eventData1, int eventData2) {
  449.   int hid=0;
  450.   switch (event) {
  451.     case EVENT_COMMIT:
  452.       switch (control) {
  453.         case PANEL_EXPORT_1:
  454.           hid=1;
  455.           break;
  456.         case PANEL_EXPORT_2:
  457.           hid=2;
  458.           break;
  459.         case PANEL_EXPORT_3:
  460.           hid=3;
  461.           break;
  462.         case PANEL_EXPORT_4:
  463.           hid=4;
  464.           break;
  465.  
  466.       }
  467.       export_data(hid);
  468.       break;
  469.   }
  470.   return 0;
  471. }
  472.  
  473. int CVICALLBACK ResetCB (int panel, int control, int event,
  474.                          void *callbackData, int eventData1, int eventData2) {
  475.   switch (event) {
  476.     case EVENT_COMMIT:
  477.  
  478.       for (int i=1; i<=4; i++) H1D_Clear(i);
  479.       break;
  480.   }
  481.   return 0;
  482. }
  483.