esrf

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

#%TITLE% SPECGUI.MAC
#%NAME%
#  Interfacing of %B%spec%B% with the graphic user interface
#
#%OVERVIEW%
#
#%EXAMPLE%
#  %DL%
#  %DT%XstartGUI
#  %DD%Initializes %B%spec%B% values in the GUI if it runs.
#  %DT%spipe openclosepanel mypanel open
#  %DD%Sends to the GUI the command to open the panel called `mypanel'.
#  %DT%Xval("ShutterState", "open")
#  %DD%Sets the value `ShutterState' in the GUI to `open'.
#  %DT%Xmval();Xmval("Sample", "SiO2");Xmval("Spos", "UP");Xmval()
#  %DD%Sets the values `Sample' and `Spos' in the GUI to `SiO2' and `UP'
#  respectively.
#  %XDL%

#%IU% <GUIcommand> [<param1> <param2> ....] ...
#%MDESC%
#
def spipe 'Xmess("$*", "begend")'

#%IU% [<GUIcommand> [<param1> <param2> ....]] ...
#%MDESC%
#
def bpipe 'fprintf("pipe", "[B]$*")'
#def bpipe 'Xmess("$*", "beg")'

#%IU% 
#%MDESC%
#
def epipe 'fprintf("pipe", "$*[E]")'
#def epipe 'Xmess("$*", "end")'

#%IU% 
#%MDESC%
#
def Xmess(str, mode) '{
   global XMESS

   if ((whatis("str") & 0x08000000) || mode == "beg") {
      if (XMESS)
         fprintf("pipe", "[B]%s[E]", XMESS)
      XMESS = str
   } else if (mode == "end") {
      fprintf("pipe", "[B]%s %s[E]", XMESS, str)
      XMESS = ""
   } else if (mode == "begend") {
      if (XMESS) {
         fprintf("pipe", "[B]%s[E]", XMESS)
         XMESS = ""
      }
      fprintf("pipe", "[B]%s[E]", str)
   } else if ((whatis("mode") & 0x08000000)) {
      if (XMESS)
         XMESS = sprintf("%s %s", XMESS, str)
      else
         fprintf("pipe", "[B]%s[E]", str)
   } else {
      fprintf("tty", "Wrong parameter for $0")
   }
}'

#%IU% 
#%MDESC%
#
def Xval(name, value) '{
   fprintf("pipe","[B]updateval %s \"%s\"[E]", name, value)
}'

#%IU% 
#%MDESC%
#
def Xmval(name, value) '{
   global XVAL

   if (name) {
      if (XVAL)
         XVAL = sprintf("%s %s \"%s\" ", XVAL, name, value)
      else 
         XVAL = sprintf("[B]updateval %s \"%s\" ", name, value)
   } else {
      if (XVAL) fprintf("pipe", "%s[E]", XVAL)
      XVAL=""
   }
}'

#%IU% 
#%MDESC%
#
def Xmconfig '{
   global XOLDA
   local i	

   for(i = 0; i < MOTORS; i++) {
      XOLDA[i]=1e28
   }
   fprintf("pipe", "[B]mconfig")
   for(i = 0; i < MOTORS; i++) {
      fprintf("pipe"," \"%s\" %s %.10g %.10g %.10g %d %d %d",             \
            motor_name(i), motor_mne(i), user(i,get_lim(i,-1)),           \
            user(i,get_lim(i,+1)), motor_par(i,"step_size"),              \
            (motor_par(i,"status")&0x100)?1:0, motor_par(i,"misc_par_1"), \
            motor_par(i,"disable"));
   }
   fprintf("pipe", "[E]")
}'

#%IU% 
#%MDESC%
#
def Xcconfig '{
  local i type

  fprintf("pipe", "[B]cconfig")
  for(i = 0; i < COUNTERS;i++) {
     type = (i == 0) ? 0 : (i == DET) ? 1 : (i == MON) ? 2 : 1 
     fprintf("pipe", " \"%s\" %s %d %d",     \
            cnt_name(i), cnt_mne(i), counter_par(i,"disable"), type)
  }
  fprintf("pipe", "[E]")
}'

#%IU% 
#%MDESC%
#
# ----  create a new data file and set the last scan num
def Xfconfig'
   fprintf("pipe", "[B]finfo %s %s %d[E]", DATAFILE, DATA_DIR, SCAN_N)
'

#%IU% 
#%MDESC%
#
# -------- Prints the motor positions to the pipe
def Xwa '{
   local i list
   global XOLDA 

   list = ""
   for(i = 0; i < MOTORS; i++) {
      if (A[i] != XOLDA[i] || $#) {
         list = sprintf(" %s %d %g %g", list, i, A[i], dial(i, A[i]))
         XOLDA[i] = A[i]
      }
   }
   if (list != "") {
      fprintf("pipe", "[B]position%s[E]", list)
   }
}'

#%IU% 
#%MDESC%
#
def Xlm '
  fprintf("pipe", "[B]limit")
  for(i=0;i<MOTORS;i++) {
    fprintf("pipe", " %g %g", user(i,get_lim(i,-1)), user(i,get_lim(i,+1)))
  }
  fprintf("pipe", "[E]")
'

#%IU% 
#%MDESC%
#
def Xsetdial 'set_dial $1 $2; Xlm'

#%IU% 
#%MDESC%
#
def Xset 'set $1 $2'

#%IU% 
#%MDESC%
#
# ------- Move motors macro (Xmv,Xmvr.......) -----
def Xmva '{
   global XMovingMotor
   local pp mt ii nbpar

   nbpar = split("$*", pp) 
   waitmove; get_angles

   for (mt = 0; mt < (nbpar/2); mt++) {
      ii = motor_num(pp[mt*2])
      XMovingMotor[ii] = 1
      A[ii] = pp[mt*2+1]
   }

   move_em
   XmMoveFinished ii
}'

#%IU% 
#%MDESC%
#
def Xmvr '{
   global XMovingMotor
   local pp mt ii nbpar

   nbpar = split("$*", pp) 
   waitmove; get_angles

   for (mt = 0; mt < nbpar / 2; mt++) {
      ii = motor_num(pp[mt*2])
      XMovingMotor[ii] = 1
      A[ii] += pp[mt*2+1]
   }
  
   move_em
   XmMoveFinished ii
}'

#%IU% 
#%MDESC%
#
def XmMoveFinished '
   global XMovingMotor
   fprintf("pipe", "[B]mfinished %d[E]", $1)
   XMovingMotor[$1] = 0
'

#%IU% 
#%MDESC%
#
def XmFinishedTest '
   global XMovingMotor

   if (!wait(0x21)) {
      for (ii=0;ii<MOTORS;ii++) {
         if (XMovingMotor[ii]) {
            XmMoveFinished ii
         }
      }
   }
'

#%IU% 
#%MDESC%
#
def Xprompt_mac '
   fprintf("pipe", "[B]spec_msg error_reset[E][B]prompt[E]")
   XmFinishedTest
'

#%IU% 
#%MDESC%
#
# ----- counter
def Xsetcounter '
   counter_par($1, "disable", $2)
'

#%IU% 
#%MDESC%
#
def XGet_counts '
   fprintf("pipe", "[B]counts")
   for(i = 0; i < COUNTERS; i++) {
      fprintf("pipe", " %f", S[i])
   }
   fprintf("pipe", "[E]")
'

#%IU% 
#%MDESC%
#
# Fast hack to get moving in real time
def X_update1 '
   global UPDATE_DELAY

   if ( UPDATE_DELAY == 0 ) {
	   while (wait(0x21)) {
	      get_angles 
	      sleep(UPDATE)
	   }
	   get_angles
   }
'


# Special Scans 

#%IU% 
#%MDESC%
#
def XPreparescan '{
   global XSCAN
   local _pp ii

   _pp[0] = $1 ; _pp[1] = $2 ; _pp[2]=$3 ; _pp[3] = $4; _pp[4]=$5 ; _pp[5] = $6
   Xmess("scanhead", "beg")
   Xmess(_pp[0])
   XSCAN["NOMOT"] = _pp[1]
   for (ii=0; ii < XSCAN["NOMOT"]; ii++) { 
      Xmess(sprintf("%d %d", XSCAN[ii] = _pp[ii+2], 1))
   }
   for (ii=0; ii < COUNTERS; ii++) {
      type = (ii == 0) ? 2 : (ii == DET) ? 3 : (ii == MON) ? 4 : 5 
      Xmess(sprintf("%d %d", ii, type))
   }
   Xmess()
   XSCAN["START"] = 1
}'


#%IU% 
#%MDESC%
#
def XScanpoint '{
   local ii

   Xmess("scanpoint", "beg")
   for (ii=0; ii < XSCAN["NOMOT"]; ii++) { 
      Xmess(sprintf("%.10g", A[XSCAN[ii]]))
   }
   for (ii=0; ii < COUNTERS; ii++) {
      Xmess(sprintf("%.10g ", S[ii]))
   }
   Xmess()
}
'

#%IU% 
#%MDESC%
#
def XEndscan '
   if (XSCAN["START"]) {
      spipe scanend scan finished
      XSCAN["START"]=0
   }
'

#%IU% 
#%MDESC%
#
def Xascan '
   XPreparescan ($4+1) 1 $1 
   ascan $*
   XEndscan 
'

#%IU% 
#%MDESC%
#
def Xa2scan '
   XPreparescan ($7+1) 2 $1 $4
   a2scan $*
   XEndscan
'



#%IU% 
#%MDESC%
#
# ---- set the plot flags $1=MODE, $2=bg_pts, $3=GTERM, $4=splot_col
def Xsetplot '
   offt
   setplot $1
   bg_pts = $2
   GTERM = "$3"
   splot_col = "$4"
   rplot_col = "$5"
   ont
'

#%IU%
#%MDESC%
#
def Xnewfile '
   newfile $1 $2
'

#%IU%
#%MDESC%
#
def Xchg_dial '
  XMovingMotor[$1] = 1
  chg_dial($1, "$2")
'

#%IU%
#%MDESC%
#
def X_config '{
   get_angles
   cdef("prompt_mac","Xprompt_mac\n","_X",0x20)
   cdef("user_getpangles","Xwa\n","_X",0x20)
   cdef("user_getcounts","XGet_counts\n","_X",0x20)
   cdef("user_moveall","X_update1\n","_X",0x20)
   cdef("measure1","XScanpoint\n","_X",0x10)
   cdef("user_cleanup2","XEndscan\n","_X",0)
   cdef("user_set","Xlm\n", "_X",0x20)
   cdef("mca_user_key", "Xmca_key\n", "_X")
   cdef("mca_user_waitcounts", "Xmca_waitcounts\n", "_X")

   Xmconfig
   Xcconfig
   Xfconfig
   Xwa 0
   user_X_config
}'

# input_Xclean is not necessary if one sends SB_SPECRAW instead of SB_SPECCMD
def Xmca_key '
   local Xmcastate

   if (key) {
      key = input_Xclean(input())
      user_Xmca_key
   }
'

#%IU%
#%MDESC%
#
def input_Xclean(line) '{
   for (i=length(line); i>0 && (c=asc(substr(line,i,1))) != 11 && c!=1; i--);
   return substr(line, i+1)
}'


if (!(whatis("Xmca_waitcounts") & 2)) rdef Xmca_waitcounts ""
if (!(whatis("user_X_config") & 2)) rdef user_X_config ""
if (!(whatis("user_Xmca_key") & 2)) rdef user_Xmca_key ""

#%IU%
#%MDESC%
#
# this is my macros for test
def XupdateGUI 'spipe updateval $*'

#%IU%
#%MDESC%
#
def XstartGUI '
   global GUI_RUN
   if (GUI_RUN = (getenv("SP_RUNFROMINTERFACE") == "yes")) {
      X_config
   } else {
      cdef("","","_X","delete")
   }
'

#%IMACROS%
#%SETUP%
#%AUTHOR% 
#  $Revision: 3.0 $ / $Date: 1999/03/17 10:12:27 $
#%TOC%