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

#
# $Log: not supported by cvs2svn $
# Revision 1.5  2000/07/24 15:40:06  emery
# Indented code.
#
# Revision 1.4  1998/05/12 20:35:01  borland
# Now works on BTS raw supplies BTS:RSR and BTS:RBO instead of BTS:R.
#
# Revision 1.3  1996/09/19 21:50:49  borland
# Changed from using "exec wish" to "exec oagwish".
#
# Revision 1.2  1996/03/11 18:26:33  borland
# Added CVSRevisionAuthor stuff.
#
# Revision 1.1  1996/03/11  18:13:09  borland
# First version per N. Sereno.
#
#

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)]

set magnetlist {Quads Hcorr Vcorr AB BB BX} 
set magvarlist {Q H V AB BB BX} 

set ControlStatus "Ready."
set CVSRevisionAuthor "\$Revision: 1.6 $ \$Author: sereno $"

# Make the menu and status for the BTS magnet control application.

proc MakeBTSMagnetControlMenuAndStatus {} {

    global ControlStatus CVSRevisionAuthor 

    APSApplication . -name "BTS Magnet Control" -version "$CVSRevisionAuthor (sereno)" \
      -overview "BTS Magnet Control, Standardization, and Degauss Functions."

    APSScrolledStatus .status -parent .userFrame -textVariable ControlStatus -width 50 -packOption {-side top}
}

# Make the bts magnet control frame and associated widgets.

proc MakeBTSMagnetControlFrameWidget {widget args} {

    global ControlStatus
    set parent ""
    APSParseArguments {parent}

    APSFrame $widget -parent $parent -label "BTS Power Supply Control" \
      -height 30 \
      -packOption {-side left -expand 1}

    set w $parent$widget.frame
    MakeRawSupplyControlFrameWidget -parent $w
    MakeBTSMagnetCheckButtonFrameWidget .correctorOnOffFrame -parent $w
}

# Make widgets to control the BTS raw supply.

proc MakeRawSupplyControlFrameWidget {args} {

    global ControlStatus Voltage BXCurrent
    set parent ""
    APSParseArguments {parent}

    APSFrame .onOffFrame -parent $parent -label "BTS Raw Magnet Power Supply On/Off" \
      -height 30 \
      -packOption {-side top}

    set OnOffw $parent.onOffFrame.frame

    APSButton .rawon -parent $OnOffw \
      -text "On" \
      -command {BTSRawOnOff on -statusCallback SetStatus} \
      -packOption {-side left -expand 1} \
      -contextHelp "This button turns the BTS raw power supply On."

    APSButton .rawoff -parent $OnOffw \
      -text "Off" \
      -command {BTSRawOnOff off -statusCallback SetStatus} \
      -packOption {-side left -expand 1} \
      -contextHelp "This button turns the BTS raw power supply Off."

    APSFrame .setVoltageFrame -parent $parent \
      -height 30 \
      -packOption {-side top}

    set Voltagew $parent.setVoltageFrame.frame

    APSLabeledEntry .voltageEntryFrame -parent $Voltagew -width 7 \
      -textVariable Voltage \
      -packOption {-side left} \
      -label "Raw Supply Voltage:" \
      -contextHelp "Use this entry box to set the BTS raw power supply voltage in volts (default = 65 V)."

    APSButton .setVoltage -parent $Voltagew \
      -text "Set" \
      -command {SetBTSRawSupplyVoltage $Voltage -statusCallback SetStatus} \
      -packOption {-side left} \
      -contextHelp "This button sets the BTS raw power supply voltage entered in the entry box."

    APSFrame .bxSetCurrentFrame -parent $parent \
      -height 30 \
      -packOption {-side top}

    set Voltagew $parent.bxSetCurrentFrame.frame

    APSLabeledEntry .currentEntryFrame -parent $Voltagew -width 7 \
      -textVariable BXCurrent  \
      -packOption {-side left} \
      -label "BX Setpoint:" \
      -contextHelp "Use this entry box to set the BTX BX dipole current in Amps (default = 319 Amps)."

    APSButton .setVoltage -parent $Voltagew \
      -text "Set" \
      -command {SetBXCurrentSetpoint $BXCurrent -statusCallback SetStatus} \
      -packOption {-side left} \
      -contextHelp "This button sets the BTS BX current setpoint entered in the entry box."
}

# Make widgets to control and condition individual BTS magnets.

proc MakeBTSMagnetCheckButtonFrameWidget {widget args} {

    global ControlStatus magnetlist magvarlist        
    set parent ""
    APSParseArguments {parent}

    APSFrame $widget -parent $parent -label "BTS Magnet\nPower Supplies" -width 50 \
      -height 30 \
      -packOption {-side left -expand 1}

    set w $parent$widget.frame

    APSFrame .magsOnOff -parent $w \
      -height 30 \
      -packOption {-side top}

    set onoffw $w.magsOnOff.frame

    APSButton .magson -parent $onoffw \
      -text "On" \
      -command {BTSMagsOnOff on -statusCallback SetStatus} \
      -packOption {-side left} \
      -contextHelp "This button turns the BTS magnet power supplies selected On."

    APSButton .magsoff -parent $onoffw \
      -text "Off" \
      -command {BTSMagsOnOff off -statusCallback SetStatus} \
      -packOption {-side left} \
      -contextHelp "This button turns the BTS magnet power supplies selected Off."

    APSCheckButtonFrame .bmags -parent $w -label "" \
      -packOption {-side top} \
      -buttonList $magnetlist -variableList "$magvarlist" \
      -allNone 1 \
      -contextHelp "These checkbuttons are used to select the BTS power supplies to be turned on/off and conditioned."

    APSFrame .condition -parent $w \
      -height 30 \
      -packOption {-side top}

    set conditionw $w.condition.frame

    APSButton .conditionMagnets -parent $conditionw \
      -text "Condition" \
      -command {exec BoosterPem} \
      -packOption {-side left -expand 1} \
      -contextHelp "This activates conditioning of the BTS magnets selected.  The procedure will standardize dipoles and quadrupoles, and degauss correctors."

#    APSButton .abortMagnetConditioning -parent $conditionw \
#      -text "Abort" \
#      -command {ConditionBTSMagnets abort -statusCallback SetStatus} \
#      -packOption {-side left -expand 1} \
#      -contextHelp "This button aborts conditioning of the BTS magnets selected."
}

proc SetStatus {text} {
    global ControlStatus
    set ControlStatus $text
    update
}

# Procedure to turn on and off the BTS raw supply.  Returns 1 if set state is on, 0 if it is off.

proc BTSRawOnOff {state args} {

    global ControlStatus
    APSParseArguments {statusCallback}

    if {$state=="on"} {
        if {$statusCallback!=""} {
            $statusCallback "Turning on BTS raw supply..."
        }
        catch {exec cavput -list=BTS:R -list=BO,SR -list=:ResetSEQ=1}
        after 2000
        if {$statusCallback!=""} {
            $statusCallback "Done."
        }
        return 1
    } elseif {$state=="off"} {
        if {$statusCallback!=""} {
            $statusCallback "Turning off BTS raw supply..."
        }
        catch {exec cavput -list=BTS:R -list=BO,SR -list=:OffBO=1}
        after 2000
        if {$statusCallback!=""} {
            $statusCallback "Done."
        }
        return 0
    } else {return 0}
}

# Set the BTS raw supply voltage.

proc SetBTSRawSupplyVoltage {voltage args} {

    global ControlStatus
    APSParseArguments {statusCallback}

    if {$statusCallback!=""} {
        $statusCallback "Setting the BTS raw supply voltage to $voltage volts..."
    }
    catch {exec cavput -list=BTS:R -list=BO,SR -list=:VoltageAO=$voltage -pendIoTime=3}
    after 1000
    if {$statusCallback!=""} {
        $statusCallback "Done."
    }
    return 1
}

# Set the BTS BX current setpoint

proc SetBXCurrentSetpoint {bxcurrent args} {

    global ControlStatus
    APSParseArguments {statusCallback}

    if {$statusCallback!=""} {
        $statusCallback "Setting the BTS BX current setpoint to $bxcurrent Amps..."
    }
    catch {exec cavput -list=BTS:BX:CurrentAO=$bxcurrent -pendIoTime=3}
    after 1000
    if {$statusCallback!=""} {
        $statusCallback "Done."
    }
    return 1
}

# Procedure to turn on individual BTS magnet supplies.

proc BTSMagsOnOff {state args} {

    global ControlStatus magvarlist 
    global Q H V AB BB BX
    APSParseArguments {statusCallback}
    set listindex 0

    if {$state=="on"} {
        foreach magnet $magvarlist {
            set magnetchoice [subst $[lindex $magvarlist $listindex]]
            if {$magnetchoice==1} {
                if {$statusCallback!=""} {
                    $statusCallback "Turning on BTS magnet $magnet..."
                }
                catch {exec devSend BTS:$magnet on}
            }
            incr listindex
        }
        after 1000
        if {$statusCallback!=""} {
            $statusCallback "Done."
        }
        return 1
    } elseif {$state=="off"} {
        foreach magnet $magvarlist {
            set magnetchoice [subst $[lindex $magvarlist $listindex]]
            if {$magnetchoice==1} {
                if {$statusCallback!=""} {
                    $statusCallback "Turning off BTS magnet $magnet..."
                }
                catch {exec devSend BTS:$magnet off}
            }
            incr listindex
        }
        after 1000
        if {$statusCallback!=""} {
            $statusCallback "Done."
        }
        return 0
    } else {return 0}
}

# Procedure to standardize BTS quads and dipoles and degauss correctors and abort standardization and degaussing.
# Returns a 1 after initiate and abort standardization procedures.  Returns 0 for anything else.

proc ConditionBTSMagnets {state args} {

    global ControlStatus magvarlist 
    global Q H V AB BB BX
    set directory "/home/helios/oagData/controlFiles/BTS"
    set listindex 0
    APSParseArguments {statusCallback}

    if {$state=="initiate"} {
        foreach magnet $magvarlist {
            set magnetchoice [subst $[lindex $magvarlist $listindex]]
            if {$magnetchoice==1 && $magnet=="Q"} {
                catch {exec standardize $directory/bts_23.34949Q.std}
                if {$statusCallback!=""} {
                    $statusCallback "BTS quadrupole standardization initiated."
                }
            } elseif {$magnetchoice==1 && $magnet=="H"} {
                catch {exec degauss $directory/BTSH_degauss.sdds}
                if {$statusCallback!=""} {
                    $statusCallback "BTS horizontal corrector degaussing initiated."
                }
            } elseif {$magnetchoice==1 && $magnet=="V"} {
                catch {exec degauss $directory/BTSV_degauss.sdds} 
                if {$statusCallback!=""} {
                    $statusCallback "BTS vertical corrector degaussing initiated."
                }
            } elseif {$magnetchoice==1 && $magnet=="AB"} {
                catch {exec standardize $directory/bts_23.34949AB.std}
                if {$statusCallback!=""} {
                    $statusCallback "BTS AB dipole standardization initiated."
                }
            } elseif {$magnetchoice==1 && $magnet=="BB"} {
                catch {exec standardize $directory/bts_23.34949BB.std}
                if {$statusCallback!=""} {
                    $statusCallback "BTS BB dipole standardization initiated."
                }
            } elseif {$magnetchoice==1 && $magnet=="BX"} {
                catch {exec standardize $directory/bts_23.34949BX.std}
                if {$statusCallback!=""} {
                    $statusCallback "BTS BX dipole standardization initiated."
                }
            }         
            incr listindex                
        }
        if {$statusCallback!=""} {
            $statusCallback "Done."                                
        }
        return 1
    } elseif {$state=="abort"} {
        foreach magnet $magvarlist {
            set magnetchoice [subst $[lindex $magvarlist $listindex]]
            if {$magnetchoice==1 && $magnet=="Q"} {
                catch {exec standardize $directory/bts_23.34949Q.std -stop}
                if {$statusCallback!=""} {
                    $statusCallback "BTS quadrupole standardization aborted."
                }
            } elseif {$magnetchoice==1 && $magnet=="H"} {
                catch {exec degauss $directory/BTSH_degauss.sdds -stop}
                if {$statusCallback!=""} {
                    $statusCallback "BTS horizontal corrector degaussing aborted."
                }
            } elseif {$magnetchoice==1 && $magnet=="V"} {
                catch {exec degauss $directory/BTSV_degauss.sdds -stop} 
                if {$statusCallback!=""} {
                    $statusCallback "BTS vertical corrector degaussing aborted."
                }
            } elseif {$magnetchoice==1 && $magnet=="AB"} {
                catch {exec standardize $directory/bts_23.34949AB.std -stop}
                if {$statusCallback!=""} {
                    $statusCallback "BTS AB dipole standardization aborted."
                }
            } elseif {$magnetchoice==1 && $magnet=="BB"} {
                catch {exec standardize $directory/bts_23.34949BB.std -stop}
                if {$statusCallback!=""} {
                    $statusCallback "BTS BB dipole standardization aborted."
                }
            } elseif {$magnetchoice==1 && $magnet=="BX"} {
                catch {exec standardize $directory/bts_23.34949BX.std -stop}
                if {$statusCallback!=""} {
                    $statusCallback "BTS BX dipole standardization aborted."
                }
            }         
            incr listindex
        }
        if {$statusCallback!=""} {
            $statusCallback "Done."                                
        }
        after 1000
        return 1
    } else {return 0}
}

set Voltage 65
set BXCurrent 319

# Build Application

MakeBTSMagnetControlMenuAndStatus
MakeBTSMagnetControlFrameWidget .btsMagnetControl -parent .userFrame
