Subversion Repositories f9daq

Rev

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