Subversion Repositories f9daq

Rev

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

  1. #include <utility.h>
  2. #include <ansi_c.h>
  3. #include <cvirte.h>            
  4. #include <userint.h>
  5. #include "drs4.h"
  6. #include "drsread.h"
  7.  
  8. static int daq_on;
  9. static int ph, plothandle[4];
  10. static int tfID;
  11. static int controlID;
  12.  
  13. #define MAX_THREADS 10  
  14.  
  15. static CmtThreadPoolHandle poolHandle = 0;  
  16.  
  17. int main (int argc, char *argv[]) {
  18.   if (InitCVIRTE (0, argv, 0) == 0)
  19.     return -1;  /* out of memory */
  20.   if ((ph = LoadPanel (0, "drs4.uir", PANEL)) < 0)
  21.     return -1;
  22.   SetStdioPort (CVI_STDIO_WINDOW);
  23.   SetSleepPolicy(VAL_SLEEP_MORE);
  24.   CmtNewThreadPool (MAX_THREADS,  &poolHandle);
  25.  
  26.   DisplayPanel (ph);
  27.   RunUserInterface ();
  28.   DiscardPanel (ph);
  29.    CmtDiscardThreadPool (poolHandle);
  30.   return 0;
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37. static void start_timer (double tout) {
  38.   SetCtrlAttribute (ph, PANEL_TIMER, ATTR_INTERVAL, tout);
  39.   SetCtrlAttribute (ph, PANEL_TIMER, ATTR_ENABLED, 1);
  40. }
  41.  
  42. static void stop_timer ( void ) {
  43.   SetCtrlAttribute (ph, PANEL_TIMER, ATTR_ENABLED, 0);
  44.    DRSSetTimeout();      
  45. }
  46.  
  47.  
  48.  
  49.  
  50. void CVICALLBACK EndOfThread ( CmtThreadPoolHandle poolhandle,
  51.                                CmtThreadFunctionID functionID, unsigned int event,
  52.                                int value, void *callbackData  ) {
  53.  
  54.   daq_on=0;
  55.   //SetDimming(0);
  56.   printf("End of Thread \n");
  57.   return ;
  58.  
  59. }
  60.  
  61.    
  62. int CVICALLBACK daq(void *functionData) {
  63.  
  64.  
  65.   int neve;
  66.   char filename[0xff];      
  67.   char smask[0xff];      
  68.   unsigned long mask;
  69.   int frequency;
  70.   double trgdelay;  
  71.   double trglevel;  
  72.   int trgtype;
  73.   int trgchannel;
  74.   int trgpolarity;  
  75.   int verbose;  
  76.   double range;
  77.  
  78.  
  79.   GetCtrlVal(ph, PANEL_FILENAME, filename );
  80.   GetCtrlVal(ph, PANEL_MASK, smask );      
  81.   mask = strtoul (smask,NULL,0);
  82.   GetCtrlVal(ph,PANEL_NEVE, &neve);
  83.  
  84.   GetCtrlVal(ph,PANEL_FREQUENCY, &frequency);
  85.   GetCtrlVal(ph,PANEL_TRGDELAY, &trgdelay);
  86.   GetCtrlVal(ph,PANEL_TRGCHANNEL, &trgchannel);
  87.   GetCtrlVal(ph,PANEL_TRGTYPE, &trgtype);
  88.   GetCtrlVal(ph,PANEL_TRGLEVEL, &trglevel);
  89.   GetCtrlVal(ph,PANEL_TRGPOLARITY, &trgpolarity);
  90.   GetCtrlVal(ph,PANEL_RANGE, &range);
  91.   GetCtrlVal(ph,PANEL_DEBUG, &verbose);
  92.  
  93.   printf("mask=0x%x\n",mask);
  94.    
  95.   DRSSetMask( (unsigned char)( mask & 0xF ) );
  96.  
  97.   DRSSetFrequency( frequency  );
  98.   DRSSetTriggerPolarity(trgpolarity);
  99.   DRSSetTriggerLevel(trglevel);
  100.   DRSSetRange (  range );
  101.   DRSSetTriggerType( trgtype );
  102.   DRSSetTriggerChannel(trgchannel );
  103.          
  104.   FILE *fp=fopen(filename,"wb");
  105.  
  106.  
  107.   static unsigned char *buffer;
  108.  
  109.   int buffer_size = 0;
  110. const int nBoards=1;
  111. const int waveDepth=1024;
  112. if (buffer_size == 0) {
  113.          buffer_size =  4 +  nBoards * (4 + 4*(4+waveDepth*4));
  114.          buffer_size += 24 + nBoards * (8 + 4*(4+waveDepth*2));
  115.          buffer = (unsigned char *)malloc(buffer_size);
  116. }
  117.    
  118. time_t t=0,told=0, tstart=0;
  119.  
  120. if (!DRSInit()){
  121.   time(&tstart);
  122.   told=tstart;
  123.   int i=0;
  124.   for (i=0; i<neve; i++) {
  125.     start_timer(1);// 1 s timeout  
  126.     int retval = DRSRead(0);
  127.     stop_timer();
  128.     int nb =  ( retval == 0 && fp ) ? DRSToBuffer( buffer , i ) : 0;
  129.     SetCtrlVal(ph,PANEL_CEVE,i);       
  130.           if (retval) i--;
  131.     if (!daq_on) break;
  132.     time(&t);
  133.     if (t!=told ) {
  134.                   printf("%d events in %2.2f min (%d s) %s",i+1, (double)(t-tstart)/60.,(t-tstart), ctime(&t));
  135.           }    
  136.     told=t;
  137. // Save data   
  138.         if (nb>0 && fp) fwrite(buffer, 1,nb ,fp);
  139. // Plot Data
  140.         for (int k=0;k<4;k++){
  141.         if (! (mask & ( 0x1<<k ))  ) continue;
  142.         float *t=DRSGetTime(k);
  143.         float *x=DRSGetWave(k);        
  144.        
  145.         const int col[4]={VAL_WHITE,VAL_RED,VAL_GREEN,VAL_BLUE};
  146.         if (plothandle[k])      DeleteGraphPlot (ph, PANEL_GRAPH, plothandle[k], VAL_IMMEDIATE_DRAW);
  147.         plothandle[k] = PlotXY (ph, PANEL_GRAPH, t, x, 1024, VAL_FLOAT, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[k]);
  148.          
  149.               for (int i=0 ; i<1024 ; i++) {
  150.            if (verbose) printf("[%d] %d. x= %3.2f  y=%3.2f\n", k, i, t[i], x[i] );
  151.            //h[k]->Fill( t[i], x[i]*1e-3);
  152.         }
  153.   }
  154.        
  155.  
  156.         }
  157.         time(&t);
  158.   printf("%d events in %2.2f min (%d s) %s",i+1, (double)(t-tstart)/60.,t-tstart, ctime(&t));
  159.   DRSEnd();
  160. }
  161.  
  162. if (fp) fclose(fp);
  163.  
  164. free(buffer);
  165.  
  166.   return 0;
  167.  
  168. }
  169.  
  170. int CVICALLBACK StartCB (int panel, int control, int event,
  171.                          void *callbackData, int eventData1, int eventData2) {
  172.   ThreadFunctionPtr mythread = NULL;  
  173.   switch (event) {
  174.      
  175.     case EVENT_COMMIT:
  176.      
  177.       mythread = daq;
  178.       if (mythread!=NULL) {
  179.       printf("New Thread panel=%d button=%d\n", panel, control);
  180.      
  181.      // SetDimming(1);
  182.       controlID= control;
  183.       daq_on=1;
  184.         CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &controlID,
  185.                                         DEFAULT_THREAD_PRIORITY,
  186.                                         EndOfThread,
  187.                                         EVENT_TP_THREAD_FUNCTION_END,
  188.                                         NULL, RUN_IN_SCHEDULED_THREAD,
  189.                                         &tfID);
  190.     }
  191.       break;
  192.   }
  193.   return 0;
  194. }
  195.  
  196. int CVICALLBACK StopCB (int panel, int control, int event,
  197.                         void *callbackData, int eventData1, int eventData2) {
  198.   switch (event) {
  199.     case EVENT_COMMIT:
  200.       daq_on=0;        
  201.       break;
  202.   }
  203.   return 0;
  204. }
  205.  
  206. int CVICALLBACK ExitCB (int panel, int control, int event,
  207.                         void *callbackData, int eventData1, int eventData2) {
  208.   switch (event) {
  209.     case EVENT_COMMIT:
  210.       QuitUserInterface (0);
  211.       break;
  212.   }
  213.   return 0;
  214. }
  215.