Subversion Repositories f9daq

Rev

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

#include "redpitaya_gui.h"
#include <ansi_c.h>
#include <tcpsupp.h>
#include <utility.h>
#include <cvirte.h>
#include <userint.h>
#include "redpitaya_gui.h"

#define NBEFORE 150
#define MAXSAMPLES 16384
#define MINTRGDELAY -8192


static int panelHandle;

static unsigned int chandle = 0;
static int tfID;
int pfreq;
static int plothandle[2]= {0,0};
int debug ;
int initialized = 0;
#define MAX_THREADS 10
int nsamples=0;
float timebins[0XFFFF];
static CmtThreadPoolHandle poolHandle = 0;
int ctrl_c=0;


int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData);


int main (int argc, char *argv[]) {
  if (InitCVIRTE (0, argv, 0) == 0)
    return -1;  /* out of memory */
  if ((panelHandle = LoadPanel (0, "redpitaya_gui.uir", PANEL)) < 0)
    return -1;

 
  SetStdioPort (CVI_STDIO_WINDOW);
  SetSleepPolicy(VAL_SLEEP_MORE);
  CmtNewThreadPool (MAX_THREADS,  &poolHandle);

  DisplayPanel (panelHandle);
  RunUserInterface ();
  DiscardPanel (panelHandle);
  CmtDiscardThreadPool (poolHandle);
  if (chandle!=0) DisconnectFromTCPServer (chandle);
  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(panelHandle,PANEL_STDIO,strbuf);

  if (gLog) {
    flog = fopen ("stdio.log", "a");
    fprintf (flog, "%s", strbuf);
    fclose (flog);
  }
  return(ret);
}


int analyse(int neve, unsigned char *cdata) {

  int *data = (int *) cdata;
  for (int i=0; i<neve; i++) {

    int recid   = *data++;
    int chmask  = *data++;
    if (recid!=0x2) continue;
    for (int id=0;id<2;id++){
      if ( !(chmask & (1 << id)) ) {
        if (i % pfreq == 0)
          if (plothandle[id]) {
            DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW);
            plothandle[id] = 0;
          }
        continue;
      }
      if ( id != *(data++) ) printf("Error\n");
      int nsamples = *(data++);
      //printf("nsamples %d\n", nsamples);
       
      if (i % pfreq == 0) {
          const int col[4]= {VAL_RED,VAL_GREEN,VAL_BLUE,VAL_WHITE};
          if (plothandle[id]) DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW);
          //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]);

          plothandle[id] = PlotXY (panelHandle, PANEL_GRAPH, timebins, data, nsamples, VAL_FLOAT, VAL_INTEGER, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[id]);

          if (debug) for (int k=0; k<10; k++) printf("%d %d (%f , %d)\n", id,k, timebins[k],data[k]);
      }
      data+=nsamples;
   
    }
    recid      = *data++;
    int event  = *data++;
    //printf("recid %d event %d\n",recid, event );
  }  

  return 0;
}

unsigned char data[100000000];
int *idata = (int *) &data[0];

static int ncalls = 0;

int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData) {

  int nb = 0 ;
  static int event = 0;
  unsigned char cmd;
  switch (xType) {
    case TCP_DISCONNECT:
      printf("TCP_DISCONNECT ErrorString %s\n",GetTCPErrorString(errCode));
      printf("TCP_DISCONNECT SystemErrorString %s\n",GetTCPSystemErrorString());
      //DisconnectFromTCPServer (&chandle);
      chandle = 0;
      break;
    case TCP_DATAREADY: {
      printf("Data Ready \n");
      nb = ClientTCPRead(handle,&cmd,1,1000);
      printf("Server Response = %d\n", cmd);
      int hdr[3]= {0,0,0};
     
      switch (cmd) {
        case 1: // init finished
          initialized = 1;
          break;
        case 2:
          break;
        case 3: // data ready
          cmd = 4;
          ClientTCPWrite(handle,&cmd,1,5000);  // read
          break;
        case 4:// read
          {
          ClientTCPRead(handle,&hdr[0],12,5000);
          int neve = hdr[1];
          printf("Client: Data received: %d, %d, %d\n", hdr[0], neve, hdr[2]);
          int size = hdr[0]-12;
          nb = 0;
          while (nb < size) {
            int retval = ClientTCPRead(handle,&data[nb],size-nb,5000);
            if (retval<1) break;
            nb += retval;
          }
          printf("Read nb = %d size %d\n", nb, size);
          analyse(neve, data);
          if (ctrl_c==0) StartCB (panelHandle, PANEL_START,EVENT_COMMIT,NULL, 0, 0 );
                         
          }
         
          break;
        default:
          printf("Unknown command = %d\n", cmd);
          break;
      }
      break;
     
    }
  }
  return 0;
}



int CVICALLBACK StartCB (int panel, int control, int event,
                         void *callbackData, int eventData1, int eventData2) {
  char msg[0xFF];
  char ip[0xFF];
  int *hdr   = (int *)&msg[1] ;
  unsigned short *sbuff = (unsigned short *)&msg[1] ;
  unsigned char  *cbuff = (unsigned char  *)&msg[1] ;
  int imask[2];
  unsigned char mask;
  unsigned char trigger;
  unsigned short nsamples;
  unsigned short delay;  
  unsigned short decimation;
  unsigned short neve;
  switch (event) {
    case EVENT_COMMIT: {
      ncalls = 0;
      GetCtrlVal(panel,PANEL_IP, ip);
      GetCtrlVal(panel,PANEL_TRIGGER, &trigger);
      GetCtrlVal(panel,PANEL_SAMPLES, &nsamples);
      GetCtrlVal(panel,PANEL_DECIMATION,&decimation);
      GetCtrlVal(panel,PANEL_NEVE   , &neve);
      GetCtrlVal(panel,PANEL_CH0    , &imask[0] );
      GetCtrlVal(panel,PANEL_CH1    , &imask[1]  );
      GetCtrlVal(panel,PANEL_PFREQ  , &pfreq);
      GetCtrlVal(panel,PANEL_DEBUG  , &debug);
      delay= MINTRGDELAY + nsamples - NBEFORE + 1;
      delay= nsamples - NBEFORE + 1;      
      mask = 0;
      for (int i=0; i<2; i++) {
        if (imask[i]) mask |= (1<<i);
      }

      double level =0;
      GetCtrlVal(panel,PANEL_TRGLEVEL , &level);


      switch (control) {
        case PANEL_CONNECT:    
          ConnectToTCPServerEx (&chandle, 9930, ip, SocketCB, NULL, 0, TCP_ANY_LOCAL_PORT);
          break;
        case PANEL_INIT:
          ctrl_c=0;
          msg[0] = 1;
          hdr[0] = delay;
          hdr[1] = decimation;
          hdr[2] = level * 1000;
          ClientTCPWrite(chandle,&msg[0],13,5000);  // init
          break;
        case PANEL_START:
          msg[0] = 3;
          sbuff[0] = neve;
          sbuff[1] = nsamples;
          sbuff[2] = 1000; //tout
          cbuff[6] = trigger;
          cbuff[7] = mask;
          ClientTCPWrite(chandle,&msg[0],9,5000);  // acquire

          break;

        case PANEL_END:
          msg[0] = 0;

          ClientTCPWrite(chandle,&msg[0],1,5000);  // disconnect
          chandle = 0;
          break;
        default:
          printf("Unknown command\n");
          break;
      }


      for (int i=0; i<nsamples; i++) timebins[i]=i*8*decimation/1000.;

      ProcessSystemEvents ();
      break;
    }
  }
  return 0;
}



int CVICALLBACK OpenGuiCB (int panel, int control, int event,
                           void *callbackData, int eventData1, int eventData2) {
  switch (event) {
    case EVENT_COMMIT:

      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 StopCB (int panel, int control, int event,
                        void *callbackData, int eventData1, int eventData2) {
  switch (event) {
    case EVENT_COMMIT:
      //if (chandle != 0)
      ctrl_c=1;
      break;
  }
  return 0;
}