Subversion Repositories f9daq

Rev

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