Subversion Repositories f9daq

Rev

Rev 225 | Rev 229 | 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. #define NBEFORE 150
  10. #define MAXSAMPLES 16384
  11. #define MINTRGDELAY -8192
  12.  
  13.  
  14. static int panelHandle;
  15.  
  16. static unsigned int chandle = 0;
  17. static int tfID;
  18. int pfreq;
  19. static int plothandle[2]= {0,0};
  20. int debug ;
  21. int initialized = 0;
  22. #define MAX_THREADS 10
  23. int nsamples=0;
  24. float timebins[0XFFFF];
  25. static CmtThreadPoolHandle poolHandle = 0;
  26. int ctrl_c=0;
  27.  
  28.  
  29. int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData);
  30.  
  31.  
  32. int main (int argc, char *argv[]) {
  33.   if (InitCVIRTE (0, argv, 0) == 0)
  34.     return -1;  /* out of memory */
  35.   if ((panelHandle = LoadPanel (0, "redpitaya_gui.uir", PANEL)) < 0)
  36.     return -1;
  37.  
  38.  
  39.   SetStdioPort (CVI_STDIO_WINDOW);
  40.   SetSleepPolicy(VAL_SLEEP_MORE);
  41.   CmtNewThreadPool (MAX_THREADS,  &poolHandle);
  42.  
  43.   DisplayPanel (panelHandle);
  44.   RunUserInterface ();
  45.   DiscardPanel (panelHandle);
  46.   CmtDiscardThreadPool (poolHandle);
  47.   if (chandle!=0) DisconnectFromTCPServer (chandle);
  48.   return 0;
  49. }
  50.  
  51. char strbuf[0xFF];
  52.  
  53. int gLog=0;
  54.  
  55. int printf(const char *format, ...) {
  56.   va_list aptr;
  57.   int ret;
  58.   FILE *flog;
  59.  
  60.   va_start(aptr, format);
  61.   ret = vsprintf(strbuf, format, aptr);
  62.   va_end(aptr);
  63.   SetCtrlVal(panelHandle,PANEL_STDIO,strbuf);
  64.  
  65.   if (gLog) {
  66.     flog = fopen ("stdio.log", "a");
  67.     fprintf (flog, "%s", strbuf);
  68.     fclose (flog);
  69.   }
  70.   return(ret);
  71. }
  72.  
  73.  
  74. int analyse(int neve, unsigned char *cdata) {
  75.  
  76.   int *data = (int *) cdata;
  77.   for (int i=0; i<neve; i++) {
  78.  
  79.     int recid   = *data++;
  80.     int chmask  = *data++;
  81.     if (recid!=0x2) continue;
  82.     for (int id=0;id<2;id++){
  83.       if ( !(chmask & (1 << id)) ) {
  84.         if (i % pfreq == 0)
  85.           if (plothandle[id]) {
  86.             DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW);
  87.             plothandle[id] = 0;
  88.           }
  89.         continue;
  90.       }
  91.       if ( id != *(data++) ) printf("Error\n");
  92.       int nsamples = *(data++);
  93.       //printf("nsamples %d\n", nsamples);
  94.        
  95.       if (i % pfreq == 0) {
  96.           const int col[4]= {VAL_RED,VAL_GREEN,VAL_BLUE,VAL_WHITE};
  97.           if (plothandle[id]) DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW);
  98.           //plothandle[id] = PlotXY (panelHandle, PANEL_GRAPH, timebins, (float *) data, nsamples, VAL_FLOAT, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[id]);
  99.  
  100.           plothandle[id] = PlotXY (panelHandle, PANEL_GRAPH, timebins, data, nsamples, VAL_FLOAT, VAL_INTEGER, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[id]);
  101.  
  102.           if (debug) for (int k=0; k<10; k++) printf("%d %d (%f , %d)\n", id,k, timebins[k],data[k]);
  103.       }
  104.       data+=nsamples;
  105.    
  106.     }
  107.     recid      = *data++;
  108.     int event  = *data++;
  109.     //printf("recid %d event %d\n",recid, event );
  110.   }  
  111.  
  112.   return 0;
  113. }
  114.  
  115. unsigned char data[100000000];
  116. int *idata = (int *) &data[0];
  117.  
  118. static int ncalls = 0;
  119.  
  120. int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData) {
  121.  
  122.   int nb = 0 ;
  123.   static int event = 0;
  124.   unsigned char cmd;
  125.   switch (xType) {
  126.     case TCP_DISCONNECT:
  127.       printf("TCP_DISCONNECT ErrorString %s\n",GetTCPErrorString(errCode));
  128.       printf("TCP_DISCONNECT SystemErrorString %s\n",GetTCPSystemErrorString());
  129.       //DisconnectFromTCPServer (&chandle);
  130.       chandle = 0;
  131.       break;
  132.     case TCP_DATAREADY: {
  133.       printf("Data Ready \n");
  134.       nb = ClientTCPRead(handle,&cmd,1,1000);
  135.       printf("Server Response = %d\n", cmd);
  136.       int hdr[3]= {0,0,0};
  137.      
  138.       switch (cmd) {
  139.         case 1: // init finished
  140.           initialized = 1;
  141.           break;
  142.         case 2:
  143.           break;
  144.         case 3: // data ready
  145.           cmd = 4;
  146.           ClientTCPWrite(handle,&cmd,1,5000);  // read
  147.           break;
  148.         case 4:// read
  149.           {
  150.           ClientTCPRead(handle,&hdr[0],12,5000);
  151.           int neve = hdr[1];
  152.           printf("Client: Data received: %d, %d, %d\n", hdr[0], neve, hdr[2]);
  153.           int size = hdr[0]-12;
  154.           nb = 0;
  155.           while (nb < size) {
  156.             int retval = ClientTCPRead(handle,&data[nb],size-nb,5000);
  157.             if (retval<1) break;
  158.             nb += retval;
  159.           }
  160.           printf("Read nb = %d size %d\n", nb, size);
  161.           analyse(neve, data);
  162.           if (ctrl_c==0) StartCB (panelHandle, PANEL_START,EVENT_COMMIT,NULL, 0, 0 );
  163.                          
  164.           }
  165.          
  166.           break;
  167.         default:
  168.           printf("Unknown command = %d\n", cmd);
  169.           break;
  170.       }
  171.       break;
  172.      
  173.     }
  174.   }
  175.   return 0;
  176. }
  177.  
  178.  
  179.  
  180. int CVICALLBACK StartCB (int panel, int control, int event,
  181.                          void *callbackData, int eventData1, int eventData2) {
  182.   char msg[0xFF];
  183.   char ip[0xFF];
  184.   int *hdr   = (int *)&msg[1] ;
  185.   unsigned short *sbuff = (unsigned short *)&msg[1] ;
  186.   unsigned char  *cbuff = (unsigned char  *)&msg[1] ;
  187.   int imask[2];
  188.   unsigned char mask;
  189.   unsigned char trigger;
  190.   unsigned short nsamples;
  191.   unsigned short delay;  
  192.   unsigned short decimation;
  193.   unsigned short neve;
  194.   switch (event) {
  195.     case EVENT_COMMIT: {
  196.       ncalls = 0;
  197.       GetCtrlVal(panel,PANEL_IP, ip);
  198.       GetCtrlVal(panel,PANEL_TRIGGER, &trigger);
  199.       GetCtrlVal(panel,PANEL_SAMPLES, &nsamples);
  200.       GetCtrlVal(panel,PANEL_DECIMATION,&decimation);
  201.       GetCtrlVal(panel,PANEL_NEVE   , &neve);
  202.       GetCtrlVal(panel,PANEL_CH0    , &imask[0] );
  203.       GetCtrlVal(panel,PANEL_CH1    , &imask[1]  );
  204.       GetCtrlVal(panel,PANEL_PFREQ  , &pfreq);
  205.       GetCtrlVal(panel,PANEL_DEBUG  , &debug);
  206.       delay= MINTRGDELAY + nsamples - NBEFORE + 1;
  207.       delay= nsamples - NBEFORE + 1;      
  208.       mask = 0;
  209.       for (int i=0; i<2; i++) {
  210.         if (imask[i]) mask |= (1<<i);
  211.       }
  212.  
  213.       double level =0;
  214.       GetCtrlVal(panel,PANEL_TRGLEVEL , &level);
  215.  
  216.  
  217.       switch (control) {
  218.         case PANEL_CONNECT:    
  219.           ConnectToTCPServerEx (&chandle, 9930, ip, SocketCB, NULL, 0, TCP_ANY_LOCAL_PORT);
  220.           break;
  221.         case PANEL_INIT:
  222.           ctrl_c=0;
  223.           msg[0] = 1;
  224.           hdr[0] = delay;
  225.           hdr[1] = decimation;
  226.           hdr[2] = level * 1000;
  227.           ClientTCPWrite(chandle,&msg[0],13,5000);  // init
  228.           break;
  229.         case PANEL_START:
  230.           msg[0] = 3;
  231.           sbuff[0] = neve;
  232.           sbuff[1] = nsamples;
  233.           sbuff[2] = 1000; //tout
  234.           cbuff[6] = trigger;
  235.           cbuff[7] = mask;
  236.           ClientTCPWrite(chandle,&msg[0],9,5000);  // acquire
  237.  
  238.           break;
  239.  
  240.         case PANEL_END:
  241.           msg[0] = 0;
  242.  
  243.           ClientTCPWrite(chandle,&msg[0],1,5000);  // disconnect
  244.           chandle = 0;
  245.           break;
  246.         default:
  247.           printf("Unknown command\n");
  248.           break;
  249.       }
  250.  
  251.  
  252.       for (int i=0; i<nsamples; i++) timebins[i]=i*8*decimation/1000.;
  253.  
  254.       ProcessSystemEvents ();
  255.       break;
  256.     }
  257.   }
  258.   return 0;
  259. }
  260.  
  261.  
  262.  
  263. int CVICALLBACK OpenGuiCB (int panel, int control, int event,
  264.                            void *callbackData, int eventData1, int eventData2) {
  265.   switch (event) {
  266.     case EVENT_COMMIT:
  267.  
  268.       break;
  269.   }
  270.   return 0;
  271. }
  272.  
  273. int CVICALLBACK ExitCB (int panel, int control, int event,
  274.                         void *callbackData, int eventData1, int eventData2) {
  275.   switch (event) {
  276.     case EVENT_COMMIT:
  277.       QuitUserInterface(0);
  278.       break;
  279.   }
  280.   return 0;
  281. }
  282.  
  283. int CVICALLBACK StopCB (int panel, int control, int event,
  284.                         void *callbackData, int eventData1, int eventData2) {
  285.   switch (event) {
  286.     case EVENT_COMMIT:
  287.       //if (chandle != 0)
  288.       ctrl_c=1;
  289.       break;
  290.   }
  291.   return 0;
  292. }
  293.