Subversion Repositories f9daq

Rev

Rev 57 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <windows.h>
#include <userint.h>
#include <utility.h>
#include <formatio.h>
#include <ansi_c.h>
#include "usb.h"
#include "xxusbdll.h"

#include <cvirte.h>    /* Needed if linking in external compiler; harmless otherwise */
#include "uklon_ui.h"


#define NDIS 21
#define NSCA 22
#define NTGG 23

int nc[]={P1_NC0, P1_NC1, P1_NC2, P1_NC3,
          P1_NC4, P1_NC5, P1_NC6, P1_NC7,
          P1_NC8, P1_NC9, P1_NC10,P1_NC11,
          P1_NC12,P1_NC13,P1_NC14,P1_NC15 } ;


#define C_I CAMAC_I
#define C_Z CAMAC_Z
#define C_C CAMAC_C
//#define C_write        WUSBXX_CAMAC_write
//#define C_read             WUSBXX_CAMAC_read
#define C_write          CAMAC_write
#define C_read       CAMAC_read

//********************************************************************
usb_dev_handle *udev;
 int Q;
 int X;
static HINSTANCE DLLHandle;

int  WUSBXX_load (char* module_path)
{

 
  if (module_path == NULL)
    DLLHandle = LoadLibrary("libxxusb.dll");
   else
    DLLHandle = LoadLibrary(module_path);
   
  if (!(xxusb_register_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_register_read"))) return __LINE__;
  if (!(xxusb_stack_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_read"))) return __LINE__;
  if (!(xxusb_stack_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_write"))) return __LINE__;
  if (!(xxusb_stack_execute_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_stack_execute"))) return __LINE__;
  if (!(xxusb_register_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_register_write"))) return __LINE__;
  if (!(xxusb_usbfifo_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_usbfifo_read"))) return __LINE__;
  if (!(xxusb_bulk_read_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_bulk_read"))) return __LINE__;
  if (!(xxusb_bulk_write_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_bulk_write"))) return __LINE__;
  if (!(xxusb_reset_toggle_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_reset_toggle"))) return __LINE__;

  if (!(xxusb_devices_find_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_devices_find"))) return __LINE__;
  if (!(xxusb_device_close_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_device_close"))) return __LINE__;
  if (!(xxusb_device_open_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_device_open"))) return __LINE__;
  if (!(xxusb_flash_program_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_flash_program"))) return __LINE__;
  if (!(xxusb_flashblock_program_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_flashblock_program"))) return __LINE__;
  if (!(xxusb_serial_open_Ptr = (void*) GetProcAddress(DLLHandle,"xxusb_serial_open"))) return __LINE__;

  if (!(VME_register_write_Ptr = (void*) GetProcAddress(DLLHandle,"VME_register_write"))) return __LINE__;
  if (!(VME_register_read_Ptr = (void*) GetProcAddress(DLLHandle,"VME_register_read"))) return __LINE__;
  if (!(VME_LED_settings_Ptr = (void*) GetProcAddress(DLLHandle,"VME_LED_settings"))) return __LINE__;
  if (!(VME_DGG_Ptr = (void*) GetProcAddress(DLLHandle,"VME_DGG"))) return __LINE__;
  if (!(VME_Output_settings_Ptr = (void*) GetProcAddress(DLLHandle,"VME_Output_settings"))) return __LINE__;
  if (!(VME_read_16_Ptr = (void*) GetProcAddress(DLLHandle,"VME_read_16"))) return __LINE__;
  if (!(VME_read_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_read_32"))) return __LINE__;
  if (!(VME_BLT_read_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_BLT_read_32"))) return __LINE__;
  if (!(VME_write_16_Ptr = (void*) GetProcAddress(DLLHandle,"VME_write_16"))) return __LINE__;
  if (!(VME_write_32_Ptr = (void*) GetProcAddress(DLLHandle,"VME_write_32"))) return __LINE__;

  if (!(CAMAC_DGG_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_DGG"))) return __LINE__;
  if (!(CAMAC_register_read_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_register_read"))) return __LINE__;
  if (!(CAMAC_register_write_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_register_write"))) return __LINE__;
  if (!(CAMAC_LED_settings_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_LED_settings"))) return __LINE__;
  if (!(CAMAC_Output_settings_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_Output_settings"))) return __LINE__;
  if (!(CAMAC_read_LAM_mask_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_read_LAM_mask"))) return __LINE__;
  if (!(CAMAC_write_LAM_mask_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_write_LAM_mask"))) return __LINE__;
  if (!(CAMAC_write_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_write"))) return __LINE__;
  if (!(CAMAC_read_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_read"))) return __LINE__;
  if (!(CAMAC_Z_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_Z"))) return __LINE__;
  if (!(CAMAC_C_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_C"))) return __LINE__;
  if (!(CAMAC_I_Ptr = (void*) GetProcAddress(DLLHandle,"CAMAC_I"))) return __LINE__;
  return 0;
}


void  WUSBXX_open (char *serial)
{
  if (serial != NULL)
        udev = xxusb_serial_open(serial);
  if (!udev)
    MessagePopup ("Error", "Cannot connect to USB. ! Check the connection!");
}

void  WUSBXX_close (void)
{
  if (udev) xxusb_device_close(udev);
}

int WUSBXX_CAMAC_write( usb_dev_handle *udev, int N, int A, int F, long Data, int *Q, int *X);
int WUSBXX_CAMAC_read( usb_dev_handle *udev,int N, int A, int F, long *Data, int *Q, int *X);
int WUSBXX_CAMAC_Z( usb_dev_handle *udev );
int WUSBXX_CAMAC_C( usb_dev_handle *udev );
int WUSBXX_CAMAC_I(usb_dev_handle *udev, int inhibit);

int WUSBXX_CAMAC_write( usb_dev_handle *udev,int N, int A, int F, long Data, int *Q, int *X)
{
    long intbuf[4];  
    int ret;
// CAMAC direct write function
    intbuf[0]=1;
    intbuf[1]=(long)(F+A*32+N*512 + 0x4000);
    if ((F > 15) && (F < 24))  // orig controla
    //if ((F > 15) && (F < 28))  // orig controla
    {  
        intbuf[0]=3;
        intbuf[2]=(Data & 0xffff);
        intbuf[3]=((Data >>16) & 255);
        ret = xxusb_stack_execute(udev, intbuf);
        *Q = (intbuf[0] & 1);
        *X = ((intbuf[0] >> 1) & 1);
    } else {
      printf("Error N%d A%d F%d",N,A,F);
    }
    return ret;
}

int WUSBXX_CAMAC_read( usb_dev_handle *udev,int N, int A, int F, long *Data, int *Q, int *X)
{
    long intbuf[4];  
    int ret;
    // CAMAC direct read function
    intbuf[0]=1;
    intbuf[1]=(long)(F+A*32+N*512 + 0x4000);
//    intbuf[1]=(long)(F+A*32+N*512);
    ret = xxusb_stack_execute(udev, intbuf);
//    printf ("ret = %d\n",ret);
    if (F < 16)  // orig controla
    {
//      *Data = intbuf[0];   //16-bit word
        *Data = intbuf[0] + (intbuf[1] & 0xFF) * 0x10000;   //24-bit word
        *Q = ((intbuf[1] >> 8) & 1);
        *X = ((intbuf[1] >> 9) & 1);
    }
//    else {
//      printf("Error N%d A%d F%d",N,A,F);
//    }
    return ret;
}

int WUSBXX_CAMAC_Z(usb_dev_handle *udev)
{
    long intbuf[4];  
    int  ret;
//  CAMAC Z = N(28) A(8) F(29)
    intbuf[0]=1;
    intbuf[1]=(long)(29+8*32+28*512 + 0x4000);
    ret = xxusb_stack_execute(udev, intbuf);
    return ret;
}

int WUSBXX_CAMAC_C(usb_dev_handle *udev)
{
    long intbuf[4];  
    int ret;
//  CAMAC C = N(28) A(9) F(29)
    intbuf[0]=1;
    intbuf[1]=(long)(29+9*32+28*512 + 0x4000);
    ret = xxusb_stack_execute( udev, intbuf);
    return ret;
}

int WUSBXX_CAMAC_I(usb_dev_handle *udev, int inhibit)
{
    long intbuf[4];  
    int  ret;
//      Set Inhibit             = N(29) A(9) F(24)
//      Clear Inhibit   = N(29) A(9) F(26)
    intbuf[0]=1;
    if (inhibit) intbuf[1]=(long)(24+9*32+29*512 + 0x4000);
    else intbuf[1]=(long)(26+9*32+29*512 + 0x4000);
    ret = xxusb_stack_execute(udev, intbuf);
    return ret;
}

int init_CAMAC () {

  long dum;
  C_Z(udev);
  C_C(udev);
  C_I(udev,1);
 
  C_read ( udev,NTGG, 0 , 9, &dum,  &Q,&X); /* init TGG */
  C_write ( udev,NTGG, 0, 16, 1000,  &Q,&X); /* Set preset counting value */
  C_write ( udev,NTGG, 0 ,17, 0x2,  &Q,&X); /* Set Load&Clock Modes */
  C_write ( udev,NDIS, 0, 16, 0xffff,  &Q,&X); /* enable all ch. */
  C_read ( udev,NDIS, 0, 26, &dum, &Q, &X);
 
  C_I(udev, 0);  

  return 0 ;
}

//********************************************************************

int plot_graph ( int p1h, int logsc, int data[16] ) {
  int max, max_I2 ;
  double plot_data[16], plot_max ;
  int ch ;

  max = 100 ;
  for ( ch=0 ; ch<16 ; ch ++ ) {
    if ( data[ch]>max ) max=data[ch] ;
  }

  DeleteGraphPlot (p1h, P1_HISTO, -1, VAL_IMMEDIATE_DRAW);
 
  max = ((max*1.2)/1000)*1000 ;
  GetCtrlVal (p1h, P1_I2, &max_I2 ) ;
  if ( max_I2>0 ) max=max_I2 ;
 
  if ( logsc ) {
               
    for ( ch=0 ; ch<16 ; ch ++ ) {  
          plot_data[ch] = log10((double)data[ch]) ;
          plot_max = log10((double)max) ;
        }
         
        SetAxisScalingMode (p1h, P1_HISTO, VAL_LEFT_YAXIS, VAL_MANUAL, 0.0, plot_max);
        PlotY (p1h, P1_HISTO, plot_data, 16, VAL_DOUBLE, VAL_VERTICAL_BAR,
                   VAL_NO_POINT, VAL_SOLID, 1, VAL_DK_BLUE);
     
 
  } else {
    SetAxisScalingMode (p1h, P1_HISTO, VAL_LEFT_YAXIS, VAL_MANUAL, 0.0, max);
    PlotY (p1h, P1_HISTO, data, 16, VAL_INTEGER, VAL_VERTICAL_BAR,
                   VAL_NO_POINT, VAL_SOLID, 1, VAL_DK_BLUE);
     
    for ( ch=0; ch<16; ch++ ) {
      SetCtrlVal (p1h, nc[ch], data[ch] ) ;
    }
  }


  return 0 ;
}


//********************************************************************
//********************************************************************

int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                           LPSTR lpszCmdLine, int nCmdShow)
{
  int rph, rID, p1h, p2h ;
  int data[16], sum, max, max_I2=0 ;
  long val, dum;
  double plot_data[16], splot_data[10][32], plot_max ;
  int thr_set, time_set;
  int ch, i, j,ii;
 
  FILE *fp ;
  char filename[256] ;
  int logscale ;
  int os[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} ;
 
  logscale = 0 ;

  if (InitCVIRTE (hInstance, 0, 0) == 0)    /* Needed if linking in external compiler; harmless otherwise */
      return -1;    /* out of memory */

  for ( i=0; i<16; data[i++]=0 ) ;
 

 
  dum=WUSBXX_load (NULL);
  if (dum) MessagePopup ("Error", "Cannot load dll!");
  WUSBXX_open ("CC0130");                              

  //ii=80000000/i-40;
  //if (ii<72) ii=72;
  //CAMAC_DGG(udev,0,7,0,ii,40,0,0);
  //CAMAC_DGG(udev,1,1,1,12,8,0,0);
 
 
  init_CAMAC();  
 
  p1h = LoadPanel (0, "uklon_ui.uir", P1);
//  p2h = LoadPanel (0, "uklon_ui.uir", P2);
  DisplayPanel (p1h);
//  DisplayPanel (p2h);

  GetUserEvent (1, &rph, &rID);


  while ((rID!=P1_QUIT)||(rph!=p1h))
  {
    if ( rph == p1h ) {
                if ( rID==P1_START ) {
              GetCtrlVal (p1h, P1_THR, &thr_set ) ;   /* read desired thr. */  
          C_write ( udev, NDIS, 0, 17,  thr_set, &Q, &X);   /* set the threshold */
                  C_write ( udev, NDIS, 1, 17, 0,  &Q,&X);         /*                                   */
               
              C_read ( udev, NSCA,0,9,&dum,  &Q,&X);

              GetCtrlVal (p1h, P1_TIME, &time_set ) ; /* read time (ms) */  
             
                  SetCtrlAttribute (p1h, P1_TIMER, ATTR_ENABLED, 0);
          C_write ( udev, NTGG, 0, 16, time_set,  &Q,&X);
          C_read ( udev, NTGG, 0, 15, &dum,  &Q,&X);
                                                                                                                                         
          SetCtrlVal (p1h, P1_LEDON,1);  
                  SetCtrlAttribute (p1h, P1_TIMER, ATTR_INTERVAL,time_set*0.001 );
          SetCtrlAttribute (p1h, P1_TIMER, ATTR_ENABLED, 1);
          ResetTimer (p1h, P1_TIMER);
                  Delay (0.1+(time_set/1000.));
                }
   
        if ( rID==P1_COUNT ) {
                  sum = 0 ;
                  max = 100 ;
                  C_I(udev, 1);
              for ( ch=0 ; ch<16 ; ch ++ ) {
                Delay(0.01);
                C_read (  udev, NSCA, ch, 0, &val,  &Q,&X);
                C_read (  udev, NSCA, ch, 0, &val,  &Q,&X);  
                // CC USB ne precita v redu CAEN C257 scalerja,
                // kanali so zamaknjeni za 1, ce je klic samo en
                // vrednost kanala 0 se pristeje h kanalom 8..16, zato ga ne uporabljamo.
                // vajo iyvedemo le s kanali 1..15.
                data[ch]=val;  
               
                if (data[ch] > 0) data[ch]--;
               //  if (data[ch] && 0x80000000) {
                 //   data[ch] &= 0x00FFFFFF;
                   
            //}
           
            sum += data[ch] ;
          }
                  C_I(udev, 0);
              SetCtrlVal (p1h, P1_I1, sum ) ;
                  plot_graph ( p1h, logscale, data ) ;
                }
        if ( rID==P1_INIT ) {
               init_CAMAC ( ) ;  
            }
   
            if ( rID==P1_SAVE ) {
              if ( ( fp = fopen ( "scaler.txt", "w+" ) ) == NULL ) printf ( "ERROR" ) ;
              for ( ch=0; ch<16 ; ch++ ) {  
                fprintf ( fp, "%d\t%d\n", ch, data[ch] ) ;
              }
              fclose ( fp ) ;
            }

            if ( rID==P1_APPEND ) {
                  GetCtrlVal (p1h, P1_APFILE, filename);

              if ( ( fp = fopen ( filename, "a" ) ) == NULL ) printf ( "ERROR" ) ;
              for ( ch=0; ch<16 ; ch++ ) {  
                fprintf ( fp, "%d\t", data[ch] ) ;
              }
              fprintf ( fp, "\n" ) ;
              fclose ( fp ) ;
            }
   
                if ( rID==P1_REPLOT ) {
                  plot_graph ( p1h, logscale, data ) ;
                }
   
   
            if ( rID == P1_PRINT ) {
              SetCtrlVal (p1h, P1_L1,1);
                  PrintPanel (p1h, "", 1, VAL_VISIBLE_AREA, 1);
              SetCtrlVal (p1h, P1_L1,0);
            }


                if ( rID == P1_LOG_SCALE ) {
                  GetCtrlVal (p1h, P1_LOG_SCALE, &logscale) ;
         
          plot_graph ( p1h, logscale, data ) ;
            }          
    }
   
    if ( rph == p2h ) {
            switch ( rID ) {
              case P2_OFS1: GetCtrlVal (p2h, P2_OFS1, &os[0] ) ; i=0 ; break ;
              case P2_OFS2: GetCtrlVal (p2h, P2_OFS2, &os[1] ) ; i=1 ; break ;
              case P2_OFS3: GetCtrlVal (p2h, P2_OFS3, &os[2] ) ; i=2 ; break ;
              case P2_OFS4: GetCtrlVal (p2h, P2_OFS4, &os[3] ) ; i=3 ; break ;
              case P2_OFS5: GetCtrlVal (p2h, P2_OFS5, &os[4] ) ; i=4 ; break ;
              case P2_OFS6: GetCtrlVal (p2h, P2_OFS6, &os[5] ) ; i=5 ; break ;
              case P2_OFS7: GetCtrlVal (p2h, P2_OFS7, &os[6] ) ; i=6 ; break ;
              case P2_OFS8: GetCtrlVal (p2h, P2_OFS8, &os[7] ) ; i=7 ; break ;
              case P2_OFS9: GetCtrlVal (p2h, P2_OFS9, &os[8] ) ; i=8 ; break ;
              case P2_OFS10: GetCtrlVal (p2h, P2_OFS10, &os[9] ) ; i=9 ; break ;
            }
   
   
    }
   
    GetUserEvent (1, &rph, &rID);
  }
     
 
   WUSBXX_close ();    
  //lc8901a_close (hdev);
  return 0;
}

int CVICALLBACK TimerCB (int panel, int control, int event,
                void *callbackData, int eventData1, int eventData2)
{
        switch (event)
                {
                case EVENT_TIMER_TICK:
                                                                 
                            SetCtrlVal (panel, P1_LEDON,0);
                            SetCtrlAttribute (panel, P1_TIMER, ATTR_ENABLED, 0);
                        break;
                }
        return 0;
}