esrf

Beamline Instrument Software Support
SPEC Macro documentation: [ Macro Index | BCU Home ]

#%TITLE% ADC.MAC
#%NAME%
#  Macros to work with an ADAS ICV150 from spec
#
#%CATEGORY% Generic I/O
#
#%DESCRIPTION%
#  This macro updates the old adc.mac macros.
#  The macros improves the setup macros from the old file
#  and substitutes the old adcsetup with adcadd and new syntax
#
# It deletes obsolete macros and the implementation become simpler.
#
#%EXAMPLE%
#%DL%
#%DT%adcadd id3/icvadc/1 ad11,ad12,ad13
#%DD% It configures the device id3/icvadc/1 and links it to counters
#    ad11,ad12 and ad13. Channel assignment and scale must be done
#    in config for those counters
#%XDL%
#
#%END%


#%UU%
#%MDESC%
#
def adcadd '{
   global ADCON ADCLIST ADC_NREAD ADC_AVER 

   local npar param
   local dev cntlist

   adcprecount   # clears counter values

   list_test ADCLIST
  
   if ($# < 2) {
      print "Usage: adcadd device-name cnt-list (comma separated list)"
   } else {
      dev = "$1"
      cntlist="$2"
      list_add(ADCLIST,dev)
      list_setpar(ADCLIST,dev,"cntlist",cntlist)

      list_setpar(ADCLIST,dev,"used",0) 
      cmin=-1; cmax = -1

      ncnt=split(cntlist,cntarr,",")
      for (jj=0;jj<ncnt;jj++) {
          if ( (cnum = cnt_num(cntarr[jj])) != -1 ) {
             list_setpar(ADCLIST,dev,"used",1) 
             cntcha = counter_par(cnum,"channel")
             if ( cntcha < cmin || cmin == -1 )   cmin = cntcha
             if ( cntcha > cmax )   cmax = cntcha
          }
      }
      
      list_setpar(ADCLIST,dev,"cmin",cmin) 
      list_setpar(ADCLIST,dev,"cmax",cmax) 

      if (whatis("ADCON") & 0x8000000 ) 
         adcon

      if (whatis("blmenu") & 0x2 )
         adcblon()
      

      setup_tail("adc",dev)
   }    
}'

def adcblon()  '{
     blmenuadd("ADC/ICV150 counters","ADC Info","adcbody","_adc_")
}'

#%UU%
#%MDESC%
#  Activates the counting on ADCs
def adcon '{
    ADCON=1
    cdef("user_prepcount",  "adcprecount\n","adc")
    cdef("user_pollcounts", "adcgetcounts\n","adc")
    cdef("user_getcounts",  "adcgetcounts\n","adc")
}'

#%IU%
#%MDESC%
#
def adcunsetup '{
    list_remove(ADCLIST,"$1")
    adcoff
}'

#%UU%
#%MDESC%
#   Disable counting on ADCs
def adcoff '{
    ADCON=0
    cdef("","","adc","delete")
}'

#%IU%
#%MDESC%
#  Resets all variables to zero. 
def adcprecount '{
   for (it=1;it <= list_n(ADCLIST);it++) {
       cntstr = list_getpar(ADCLIST,it,"cntlist")
       ncnt=split(cntstr,cntarr,",")
       for (kk=0;kk<ncnt;kk++) {
           if ((cnum=cnt_num(cntarr[kk])) != -1 ) {
               S[cnum]=0
               ADC_NREAD[cnum]=0
               ADC_AVER[cnum]=0
           }
       }
   }
}'

#%IU%
#%MDESC%
#  Do the reading. 
def adcgetcounts '{
   local channel[]
   for (it=1;it <= list_n(ADCLIST);it++) {
       if  ( list_getpar(ADCLIST,it,"used") == 1 ) {
           channel[0] = list_getpar(ADCLIST,it,"cmin")
           channel[1] = list_getpar(ADCLIST,it,"cmax")
           esrf_io(list_item(ADCLIST,it),"DevReadValues",channel,values)

           if (ESRF_ERR) {
                 printf("Error reading %s\n",list_item(ADCLIST,it))
                 continue
           }

           cntstr=list_getpar(ADCLIST,it,"cntlist")
           ncnt=split(cntstr,cntlist,",")
           for ( kk=0;kk<ncnt;kk++) { 
              cnum = cnt_num(cntlist[kk]) 
              if (cnum == -1 ) {
                   continue
              }
              idx = counter_par(cnum,"channel") - channel[0]
              ADC_AVER[cnum] += values[idx]
              ADC_NREAD[cnum]++
              S[cnum] = ADC_AVER[cnum] * counter_par(cnum,"scale") / ADC_NREAD[cnum]
           }
       }
   }
}'

#%UU%  cnt gain
#%MDESC%
#
def adcgain '{
   local cntlist cntstr cnt gain it done

   cnt = "$1"
   gain = $2 
   done = 0

   for (it=1;it<=list_n(ADCLIST);it++) {
       cntstr = list_getpar(ADCLIST,it,"adclist")
       ncnt = split(cntstr,cntlist,",")
       for (kk=0;kk<ncnt;kk++) {
           if ( cnt == cntlist[kk] ) {
                cnum = cnt_num(cnt)
                if (cnum != -1 ) {
                   cntcha = counter_par(cnum,"channel") 
                   argin[0] = cntcha;  argin[1] = gain
                   esrf_io(list_item(ADCLIST,it),"DevSetChanGain",argin)
                   done=1
                   break
                }
           }
       }
       if (done) break
   }
   if (!done) printf( "Counter \"%s\" not an ADC counter\n",cnt)
}'

#%UU%
#%MDESC%
# Shows current configuration and state of ADCs
def adcshow '{
    printf("\n\t")

    tty_cntl("us")
    printf("Current ADC/ICV150 configuration\n\n")
    tty_cntl("ue")

    for (it=1;it <= list_n(ADCLIST); it++) {
        printf("Device:  ")
        tty_cntl("md")
        printf("%s\n\n",list_item(ADCLIST,it))
        tty_cntl("me")
        cntstr=list_getpar(ADCLIST,it,"cntlist")
        ncnt = split(cntstr,cntlist,",")
        for (kk=0;kk<ncnt;kk++) {
             if (kk%3 == 0 && kk !=0 ) printf("\n")
             cnum=cnt_num(cntlist[kk]) 
             tty_cntl("md")
             printf("%6s: ",cntlist[kk])
             tty_cntl("me")
             if (cnum != -1 ) {
                 sc = counter_par(cnum,"scale") 
                 ch = counter_par(cnum,"channel") 
                 printf("%4s,%4s ",sprintf("ch=%d",ch),sprintf("sc=%d",sc))
             } else {
                 printf("Not config")
             }
             printf("  ||")
        }
        printf("\n\n")
    }
}'

#%UU%
#%MDESC%
#
def adcbody(mode) '{
   if (mode == 1) {
      if (ADCON) {
         adcoff
      } else {
         adcon
      }
   }

   if (mode == 2) {
      adcshow
   }

   return(ADCON?"On":"Off")
}'

#%MACROS%
#%IMACROS%
#%DEPENDENCIES%
#%AUTHOR% V.Rey (BLISS/ESRF) Jan.99
#%TOC%