#!/bin/sh  
# \
exec oagwish "$0" "$@"

set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]
APSDebugPath
APSStandardSetup

# Process command line arguments
set DRYRUN 1
set PowerUser 1
set authorizedUser 1

# Menu bar
set CVSRevisionAuthor "\$Revision: 1.1 $ \$Author: shang $"

set appName "s35bm9mPinholeCam: 9m Pinhole camera Setup"
set displayStatus "This script sets up 9m pinhole camera and VID4 PVs for machine studies."

APSApplication . -name $appName -version $CVSRevisionAuthor \
  -overview "Configure 35-BM 9m pinhole for studies."

# message window output "stdout"
APSScrolledStatus .status -parent .userFrame -width 86 -height 16 -textVariable displayStatus 

# Widgets design: SetupRestore
proc SetupRestoreFrame {widget args} {
  APSStrictParseArguments {parent}    
  global PowerUser
  global abortRun

  set w $parent$widget.frame
  APSFrame $widget -parent $parent \
    -label "Restore settings for the In-Tunnel Pinhole Camera" 
    
  APSSetVarAndUpdate displayStatus "(1) Set up video mux and frame grabber."
  APSSetVarAndUpdate displayStatus "(2) ASk MCR operator to give the shutter permit to S35."
  APSSetVarAndUpdate displayStatus "(3) Open 35-BM-C shutter."
  APSSetVarAndUpdate displayStatus "(4) The beam size information is on VID4 EPICS controls."
  APSSetVarAndUpdate displayStatus " "
  APSSetVarAndUpdate displayStatus "(5) To display 9 m pinhole video, use ezmux to select <SECTOR 35 ==> S35-4>."
  APSSetVarAndUpdate displayStatus "(6) To display VID4 output video, use ezmux to select <SECTOR 35 ==> VID4 Output>."

  APSButton .videoSetup -parent $w \
    -text "Setup 9m pinhole camera and VID4" \
    -packOption "-side left" \
    -command { setupVideo } \
    -contextHelp "set up 9m pinhole camera through VID4."

  APSButton .openShutter -parent $w \
    -text "Open shutter" \
    -packOption "-side left" \
    -command { openShutter } \
    -contextHelp "Open PS2 of 35BM for 9 m pinhole camera."

  return
}


proc setupVideo { } {
    global frameGrabber

	# unlock the VID4 mux 
    setPV -pv VID:ICR:muxOut43C.DISV -value 1

	# Connect 35-BM-C2 CCD to VID4 through 35BM mux channel #125
    APSSetVarAndUpdate displayStatus "\nConnecting VID4 to 35BM-C2 camera through S35-5"
    setPV -pv VID:ICR:muxOut43C -value 125
    setPV -pv VID:S35:muxOut4C  -value 8

    APSSetVarAndUpdate displayStatus "Setting up video frame grabber calibration table"
    setPV -pv S:VID4:CAL:cameraIndexC -value 105
    setPV -pv S:VID4:captureModeC -value 2
    setPV -pv S:VID4:autoBaselineShiftC -value 1
    setPV -pv S:VID4:baselineAvgC -value 1
    setPV -pv S:VID4:plotEnableC -value 0
    setPV -pv S:VID4:displayTextC -value 0
    setPV -pv S:VID4:clearScreenC -value 1
    setPV -pv S:VID4:drawRoiC -value 1
    setPV -pv S:VID4:displayTextC -value 1
   
	# lock the VID4 mux 
    setPV -pv VID:ICR:muxOut43C.DISV -value 0

    APSSetVarAndUpdate displayStatus "Done."
    return
}


proc openShutter { } {
    global frameGrabber

    APSSetVarAndUpdate displayStatus "Opening 35-BM PS2 ... ..."

	# check PS1 
    if { [getPV -pv EPS:35:BM:PS1:POSITION.VAL] != "Open" } {
      APSSetVarAndUpdate displayStatus "PS1 is closed. Call Floor Coordinator."
      return
    }
    
	# open PS2
    for {set iteratn 1} {$iteratn <= 6} {incr iteratn} {
       setPV -pv S35BM:PS2C:SS2C:Open:C -value 1
       after 2000
       if { [getPV -pv EPS:35:BM:PS2C:POSITION.VAL] != "Closed" } {
         APSSetVarAndUpdate displayStatus "35-BM PS2C is OPEN."
         return
       }
    }

    APSSetVarAndUpdate displayStatus "PS2 does not open. Ask MCR operator to help."
    return
}


proc getPV {args} {
    global VERBOSE

    APSParseArguments {pv}

    if {[string length $pv] < 5} {
        if {$VERBOSE} {APSSetVarAndUpdate displayStatus "getPV: PV is not valid: $pv, return 0!"}
        set value 0
    } else {
        set value [caget $pv]
        if {$VERBOSE} {APSSetVarAndUpdate displayStatus "getPV: $pv has value of $value"}
        if [string match *Error* $value] {set value 0}
    }
    
    return $value
}



proc setPV {args} {
    global VERBOSE
    global authorizedUser
    global DRYRUN
    
    set readback 0
    APSParseArguments {pv value readback}

    if {[string length $pv] < 5} {
        APSSetVarAndUpdate displayStatus "setPV: PV is not valid: $pv, No action taken!"
        return
    }
    
    if { ($DRYRUN!=0) || ($authorizedUser==0) } {
        APSSetVarAndUpdate displayStatus "pretend setting $pv to $value"
    } else {
        if $VERBOSE { APSSetVarAndUpdate displayStatus "Setting $pv to $value" }
        exec caput $pv $value
    }   

    if !$readback { return 0 }
    set newValue [getPV -pv $pv]
    return $newValue 
}



#########################################################
#	Define and initialize important global vars	#
#########################################################

proc initGlobalVars {args} {

    	# Program control
    global abortRun
    global PowerUser
    global VERBOSE
    global DRYRUN

    global frameGrabber
    global monitor

    global noInit
    if ![info exists noInit] {
        set noInit 1
        set abortRun 0
        set VERBOSE 0
        set DRYRUN 0

        set frameGrabber VID4
    }   
    return

}


initGlobalVars

# The following switches can be turned on during development

SetupRestoreFrame .setup      -parent .userFrame


.menu.file.menu delete 2
.menu.file.menu add command -label "Quit" -command { 
    APSMenuFrameClose .userFrame.mf
    exit
}

.menu.help.menu add command -label "HTML info file" -command {
    # exec netscape "/home/helios/PHOTODIA/DeviceInfo/SR/Sector35/35BM/35BMFE/Pinhole_Camera/BMPinholeCamera.html" &
}
.menu.help.menu insert 5 separator


