# This program turns on or off horizontal and/or vertical 
# booster corrector power supplies or raw supplies.
# This program turns the booster corrector raw supplies on
# at 65 Volts, loads zero ramp tables, and either turns
# selected correctors on or off.  If off is selected, the 
# raw supplies are also turned off.  "Fast" mode does not
# load zero ramps when turning the supplies off.  "Fast"
# mode will not perform INIT/AUTO/ACTIVATE procedure before
# turning on.  "Raw Only" turns on only the raw supplies,
# loads 65 Volts as their setpoint and does this in "Fast" mode.

proc APSMpBoosterSetCorrPowerSuppliesOnOffInfo {} {

    return "This program turns on or off horizontal and/or vertical\
booster corrector power supplies or raw supplies.\
This program turns the booster corrector raw supplies on\
at 65 Volts, loads zero ramp tables, and either turns\
selected correctors on or off.  If off is selected, the\
raw supplies are also turned off.  \"Fast\" mode does not\
load zero ramps when turning the supplies off.  \"Fast\"\
mode will not perform INIT/AUTO/ACTIVATE procedure before\
turning on.  \"Raw Only\" turns on only the raw supplies,\
loads 65 Volts as their setpoint and does this in \"Fast\" mode."

    return
}

proc APSMpBoosterSetCorrPowerSuppliesOnOffDialog {frame} {
    APSSetVarAndUpdate status "Set Booster Corrector Power Supply Options"
    label $frame.la1 -text "Set Booster Corrector Power Supply Options"
    pack $frame.la1 -side top
    APSRadioButtonFrame .rb1 -parent $frame -label "" \
	-variable APSMpBoosterSetCorrPowerSuppliesOnOff(state1) \
	-buttonList {On Off} \
	-valueList {on off} \
	-packOption {-side top} \
	-contextHelp "These buttons turn the booster corrector raw supplies on at 65 Volts, load zero ramp tables, and either turn selected correctors on or off.  If off is selected, the raw supplies are also turned off."
    
    APSRadioButtonFrame .rb2 -parent $frame -label "" \
	-variable APSMpBoosterSetCorrPowerSuppliesOnOff(selection) \
	-buttonList {Horizontal Vertical Both} \
	-valueList {h v b} \
	-packOption {-side top} \
	-contextHelp "These buttons select various combinations of booster corrector supplies."
    
    APSCheckButtonFrame .cb1 -parent $frame -label "" \
	-packOption {-side top} \
	-buttonList {Fast "Raw Only"} \
	-variableList "APSMpBoosterSetCorrPowerSuppliesOnOff(Fast) APSMpBoosterSetCorrPowerSuppliesOnOff(RawOnly)" \
	-contextHelp "\"Fast\" mode does not load zero ramps when turning the supplies off.  \"Fast\" mode will not perform INIT/AUTO/ACTIVATE procedure before turning on.  \"Raw Only\" turns on only the raw supplies, loads 65 Volts as their setpoint and does this in \"Fast\" mode."
    
    return
}

proc APSMpBoosterSetCorrPowerSuppliesOnOff {args} {
    set state1 off
    set selection b
    set Fast 0
    set RawOnly 0

    APSStrictParseArguments {state1 selection Fast RawOnly}

    APSMpStep init \
	-promptDialog APSMpBoosterSetCorrPowerSuppliesOnOffDialog -vars {state1 selection Fast RawOnly}

    APSMpStep "Turning $state1 supplies" -desc "Turn on or off raw supplies and/or power supplies" -proc APSMpBoosterSetCorrPowerSuppliesOnOffNormal
    if $RawOnly {
	APSSetVarAndUpdate status "Turning $state1 raw supplies in \"Fast\" mode..."
	if [catch {APSMpBoosterSetCorrPowerSuppliesOnOffNormal -state1 $state1 -selection $selection -RawOnly 1} done] {
	    APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOff: $done"
	}
	APSSetVarAndUpdate status "Done."
    } elseif $Fast {
	APSSetVarAndUpdate status "Turning $state1 $selection power supplies and raw supplies in \"Fast\" mode..."
	if [catch {APSMpBoosterSetCorrPowerSuppliesOnOffNormal -state1 $state1 -selection $selection -Fast 1} done] {
	    APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOff: $done"
	}
	APSSetVarAndUpdate status "Done."
    } else {
	APSSetVarAndUpdate status "Turning $state1 $selection power supplies and raw supplies and loading zero ramps..."
	if [catch {APSMpBoosterSetCorrPowerSuppliesOnOffNormal -state1 $state1 -selection $selection} done] {
	    APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOff: $done"
	}
	APSSetVarAndUpdate status "Done."
    }
    after 2000

    APSMpReturn ok
}

proc APSMpBoosterSetCorrPowerSuppliesOnOffNormal {args} {
    set state1 off
    set selection b
    set Fast 0
    set RawOnly 0
    set horizontal 1
    set vertical 1
    APSStrictParseArguments {state1 selection Fast RawOnly}
    if {$state1=="on"} {
	set on 1
    } else {
	set on 0
    }
    if {$selection=="v"} {
	set horizontal 0
    } elseif {$selection=="h"} {
	set vertical 0
    }
    if $RawOnly {
	set Fast 1
    }
    #catch {exec medm -x -attach /net/helios/iocapps/adlsys/booster/psApp/BoosterRampControl.adl &}
    # Initialization procedure -- no longer needed for new AFG (h. Shang Sept, 2015)
    
    catch {exec medm -x -attach /net/helios/iocapps/adlsys/booster/psApp/bPSStatus.adl &}
    # Zero ramps
    if {!$on && !$Fast} {
	APSMpStep "Zeroing Ramps" -desc "(!on && !Fast) Zeroing Ramps" -proc APSMpBoosterSetCorrPowerSuppliesOnOffZero
	if {$horizontal && $vertical}  {
	    APSSetVarAndUpdate status "Zeroing ramps..."
	    if [catch {APSMpBoosterSetCorrPowerSuppliesOnOffZero -selection b} done] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $done"
	    }
	} elseif $horizontal { 
	    APSSetVarAndUpdate status "Zeroing horizontal ramps..."
	    if [catch {APSMpBoosterSetCorrPowerSuppliesOnOffZero -selection h} done] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $done"
	    }
	} elseif $vertical {
	    APSSetVarAndUpdate status "Zeroing vertical ramps..."
	    if [catch {APSMpBoosterSetCorrPowerSuppliesOnOffZero -selection v} done] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $done"
	    }
	}
    }
    # Horizontal
    if $horizontal {
	if $on {
	    # Raw supplies
	    if [catch {exec caput BRH412:ResetSEQ 1} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	    if [catch {exec caput BRH420:ResetSEQ 1} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	    update
	    after 5000
	    if [catch {exec caput BRH412:VoltageAO 65} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	    if [catch {exec caput BRH420:VoltageAO 65} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	}
	if {!$RawOnly} {
	    # Individual supplies
	    update
	    after 5000
	    if $on {
		# Individual supplies
                if {[catch {exec cavput -list=B -range=begin=1,end=4 -list=C -range=begin=0,end=9 -list=H:ResetSEQ=1} results]} {
                    APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
                }
#		if [catch {exec devSend B:H on} results] {
#		    APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
#		}
	    } else {
                if {[catch {exec cavput -list=B -range=begin=1,end=4 -list=C -range=begin=0,end=9 -list=H:OffBO=1} results]} {
                    APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
                }
#		if [catch {exec devSend B:H off} results] {
#		    APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
#		}
	    }
	    update
	    after 10000
	}
	if {!$on} {
	    # Raw supplies
	    if [catch {exec caput BRH412:VoltageAO 0} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	    if [catch {exec caput BRH420:VoltageAO 0} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	    if [catch {exec caput BRH412:OffBO 1} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	    if [catch {exec caput BRH420:OffBO 1} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	}
    }
    
    # Vertical
    if $vertical { 
	if $on {
	    # Raw supplies
	    if [catch {exec caput BRV412:ResetSEQ 1} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	    if [catch {exec caput BRV420:ResetSEQ 1} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	    update
	    after 5000
	    if [catch {exec caput BRV412:VoltageAO 65} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	    if [catch {exec caput BRV420:VoltageAO 65} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	}
	if {!$RawOnly} {
	    # Individual supplies
	    update
	    after 5000
	    if $on {
		# Individual supplies
                if {[catch {exec cavput -list=B -range=begin=1,end=4 -list=C -range=begin=0,end=9 -list=V:ResetSEQ=1} results]} {
                    APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
                }
#		if [catch {exec devSend B:V on} results] {
#		    APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
#		}
	    } else {
                if {[catch {exec cavput -list=B -range=begin=1,end=4 -list=C -range=begin=0,end=9 -list=V:OffBO=1} results]} {
                    APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
                }
#		if [catch {exec devSend B:V off} results] {
#		    APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
#		}
	    }
	    update
	    after 10000
	}
	if {!$on} {
	    # Raw supplies
	    if [catch {exec caput BRV412:VoltageAO 0} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	    if [catch {exec caput BRV420:VoltageAO 0} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	    if [catch {exec caput BRV412:OffBO 1} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	    if [catch {exec caput BRV420:OffBO 1} results] {
		APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffNormal: $results"
	    }
	}
    }
    APSMpReturn ok
}

proc APSMpBoosterSetCorrPowerSuppliesOnOffZero {args} {
    set horizontal 1
    set vertical 1
    set selection b
    APSStrictParseArguments {selection}

    if {$selection=="v"} {
	set horizontal 0
    } elseif {$selection=="h"} {
	set vertical 0
    }
    set dir /home/helios/oagData/booster/orbitCorr
    if {$horizontal && $vertical} { 
	set filename boosterzero.ramp
    } elseif $horizontal {
	set filename boosterzero.h.ramp
    } elseif $vertical {
	set filename boosterzero.v.ramp
    }
    file delete -force -- $filename
    if [catch {eval exec ramploadnew $dir/$filename} results] {
        APSMpReturn error "APSMpBoosterSetCorrPowerSuppliesOnOffZero: $results"
    }
    APSMpReturn ok
}
