#----------------------------------------------------------------------------
# This file implements the collimator commands for SANS. It requires an
# SPS named sps2 within SICS. 
#
# Mark Koennecke, March 1999
#----------------------------------------------------------------------------
proc coll args {
#-------- set case
    if { [llength $args] > 0 ] } {
       set length [lindex $args 0]
       switch $length {
         18 {
            set command "sps2 push 200 0"
            break
         }         
         15 {
            set command "sps2 push 200 1"
            break
         }         
         11 {
            set command "sps2 push 200 2"
            break
         }         
         8 {
            set command "sps2 push 200 3"
            break
         }         
         6 {
            set command "sps2 push 200 4"
            break
         }         
         4.5 {
            set command "sps2 push 200 5"
            break
         }         
         3 {
            set command "sps2 push 200 6"
            break
         }         
         2 {
            set command "sps2 push 200 7"
            break
         }         
         1.4 {
            set command "sps2 push 201 0"
            break
         }         
         1 {
            set command "sps2 push 201 1"
            break
         } 
         default {
           append text \
             [format "ERROR: collimation length %s invalid\n" $length]
           append text "Possible length are: 18,15,11,8,6,4.5,3,2,1.4,1\n"
           append text \
           "Extraneous . or other characters will yield this error too\n"
           append text "SPS programming courtesy Enzo Manfrin\n"
           return $text
         }        
#------- command has been built, execute it!
         set ret [catch {$command} msg]
         if {$ret != 0} {
           error $msg
         } 
         setstatus Driving
#------- wait till finish, check for interrupts on the way
         set exe 1
         while {$exe} {
	     set ret [catch {sps2 colli} msg]
             if {$ret != 0 } {
               setstatus Eager
               error $msg
             }
             set l [split $msg =]
             set cval [lindex $l 1]
             if { [expr $cval - $length] < 0.2 } {
               set exe 0
             }
             set rupt [getint]
             if {[string compare $rupt continue] != 0 } {
               setstatus Eager
               error "ERROR: driving collimator interrupted"
             }
         }   
         setstatus Eager
         return OK    
    } else {
#-------- get case
	set ret [catch {sps2 colli} msg]
        if {$ret != 0} {
           error $msg
        }
        return $msg        
    }
}
#--------------------------------------------------------------------------
# Another procedure for handling the attenuator.
#
# Mark Koennecke, March 1999
#--------------------------------------------------------------------------
proc findatt { } {
#----------- find the current attenuator
    set ret [catch {sps2 stat2 9 5} msg]
    if { $ret != 0 } {
      error $msg
    }
    set l [split $msg =]
    if { [lindex $l 1] == 1} {
       return 0
    }
    set ret [catch {sps2 stat2 9 6} msg]
    if { $ret != 0 } {
      error $msg
    }
    set l [split $msg =]
    if { [lindex $l 1] == 1} {
       return 1
    }
    set ret [catch {sps2 stat2 9 7} msg]
    if { $ret != 0 } {
      error $msg
    }
    set l [split $msg =]
    if { [lindex $l 1] == 1} {
       return 2
    }
    set ret [catch {sps2 stat2 10 0} msg]
    if { $ret != 0 } {
      error $msg
    }
    set l [split $msg =]
    if { [lindex $l 1] == 1} {
       return 3
    }
    set ret [catch {sps2 stat2 10 1} msg]
    if { $ret != 0 } {
      error $msg
    }
    set l [split $msg =]
    if { [lindex $l 1] == 1} {
       return 4
    }
    set ret [catch {sps2 stat2 10 2} msg]
    if { $ret != 0 } {
      error $msg
    }
    set l [split $msg =]
    if { [lindex $l 1] == 1} {
       return 5
    }
}
#--------------------------------------------------------------------------
proc att args { 
    if [ llength $args] > 0} {
#------- set case
       set aat [lindex $args 0]
       switch $aat {
        0 {
          set command "sps2 push 210 7"
          break
        }
        1 {
          set command "sps2 push 220 0"
          break
        }
        2 {
          set command "sps2 push 220 1"
          break
        }
        3 {
          set command "sps2 push 230 0"
          break
        }
        4 {
          set command "sps2 push 230 1"
          break
        }
        5 {
          set command "sps2 push 230 2"
          break
        }
        default {
           error [format "ERROR: attenuator %s unknown" $aat]
        }
      }
#-----send command
      set ret [catch {$command} msg]
      if {$ret != 0} {
        error $msg
      }
#------ wait till done 
        setstatus Driving
         set exe 1
         while {$exe} {
	     set ret [catch {findatt} msg]
             if {$ret != 0 } {
               setstatus Eager
               error $msg
             }
             if { [expr $msg - $aat] < 0.2 } {
               set exe 0
             }
             set rupt [getint]
             if {[string compare $rupt continue] != 0 } {
               setstatus Eager
               error "ERROR: driving attenuator interrupted"
             }
         }   
         setstatus Eager
         return OK    
    } else {
#----------- get case
	set ret [catch {findatt} msg]
        if {$ret != 0 } {
           error $msg
        } else {
           return [format "att = %s" $msg]
        }
    }
}








