#!/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)]

set OptionList {Fast RawOnly} 

set ControlStatus "Ready."

# Make the menu and status for the booster corrector magnet control application.

proc MakeBCorrMagnetControlMenuAndStatus {} {
        
        global ControlStatus

        APSApplication . -name "Booster Corrector Magnet Control" \
                -overview "Booster corrector basic control and and rampload functions."

        APSMenubarAddMenu .timing -parent .menu -text "Timing"
                .menu.timing.menu add command -label "Booster Timing Control" -command {exec btiming &}

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

# This procedure makes a button widget to set the booster corrector power supplies based on selected checkbuttons and/or
# radiobuttons.
        
proc MakeSetPowerSupplyWidget {widget args} {

        global ControlStatus
        set parent ""
        APSParseArguments {parent}

        APSButton $widget -parent $parent \
                -text "Set Booster Corrector Power Supplies" \
                -command {SetBCorrPowerSupplies -statusCallback SetStatus} \
                -packOption {-side top} \
                -contextHelp "This button turns on or off horizontal and/or vertical booster corrector power supplies or raw supplies."
}

# This procedure makes the booster corrector/raw supply On/Off radiobutton widget.

proc MakeBCorrOnOffRadioButtonFrameWidget {widget args} {

        global ControlStatus state
        set parent ""
        APSParseArguments {parent}

        APSRadioButtonFrame $widget -parent $parent -label "" \
                -variable state \
                -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."
}

# This procedure makes the booster horizontal/vertical corrector selection radiobutton widget.

proc MakeBCorrPSSelectionRadioButtonFrameWidget {widget args} {

        global ControlStatus selection
        set parent ""
        APSParseArguments {parent}

        APSRadioButtonFrame $widget -parent $parent -label "" \
                -variable selection \
                -buttonList {Horizontal Vertical Both} \
                -valueList {h v b} \
                -packOption {-side top} \
                -contextHelp "These buttons select various combinations of booster corrector supplies."
}

# This procedure makes the booster horizontal/vertical corrector options widget.  "Fast" mode allows for skipping loading 
# the zero ramp tables when turning off and skipping doing the initialization procedure when turning on.  "Raw Only" turns # on only the raw supplies, loads 65 Volts as their setpoint and does this in "Fast" mode. 

proc MakeBCorrPSOptionsCheckButtonFrameWidget {widget args} {

        global ControlStatus state Fast RawOnly
        set parent ""
        APSParseArguments {parent}

        APSCheckButtonFrame $widget -parent $parent -label "Options" \
                -packOption {-side top} \
                -buttonList {Fast "Raw Only"} -variableList "Fast 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."
}

# This procedure initializes and loads zero ramps.

proc MakeSetBCorrInitProcedureWidget {widget args} {

        global ControlStatus state selection
        set parent ""
        APSParseArguments {parent}

        APSButton $widget -parent $parent \
                -text "INIT/AUTO/ACTIVATE Procedure" \
                -command {SetBCorrInitPS -statusCallback SetStatus} \
                -packOption {-side top} \
                -contextHelp "This button first initiallizes all booster correctors, then loads a zero ramp table and activates auto mode, then loads the zero ramptable again and activates it."
}

proc MakeSetBCorrRampLoadProcedureWidget {widget args} {

        global ControlStatus apsLatticeState
        set parent ""
        APSParseArguments {parent}

        set apsLatticeState standard
        if {[file exists /home/helios/oagData/booster/ramps/correctors/HVCorr.ramp]} {
            set type [file type /home/helios/oagData/booster/ramps/correctors/HVCorr.ramp]
            if {$type == "link"} {
                set fname [file readlink /home/helios/oagData/booster/ramps/correctors/HVCorr.ramp]
                if {$fname == "/home/helios/oagData/booster/ramps/correctors/lattices/x12.75-y9.80/HVCorr.ramp"} {
                    set apsLatticeState low
                }
            }
        }

        APSRadioButtonFrame .lattice -parent $parent -label "" \
                -variable apsLatticeState \
                -buttonList {"High Emittance Lattice" "Low Emittance Lattice"} \
                -valueList {standard low} \
                -packOption {-side top}

        APSButton $widget -parent $parent \
                -text "Load Corrector Ramps" \
                -command {LoadBCorrRamps -statusCallback SetStatus} \
                -packOption {-side top} \
                -contextHelp "This button loads the booster corrector ramps for both horizontal and vertical planes."
}

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

# Procedure to turn on raw and selected booster corrector supplies.

proc SetBCorrPowerSupplies {args} { 
        
        global ControlStatus state selection 
        global Fast RawOnly
        APSParseArguments {statusCallback}

        set FastChoice [subst $Fast]
        set RawOnlyChoice [subst $RawOnly]

        if {(${FastChoice}==1) && (${RawOnlyChoice}==0)} {
                if {$statusCallback!=""} {
                        $statusCallback "Turning $state $selection power supplies and raw supplies in \"Fast\" mode..."
                }
                set OnOffCommand "setbcorrps -${state} -${selection} -f"
                APSExecLog .setCorrState -unixCommand $OnOffCommand
                after 2000
                if {$statusCallback!=""} {
                        $statusCallback "Done."
                }
        } elseif {((${FastChoice}==0) && (${RawOnlyChoice}==1)) || ((${FastChoice}==1) && (${RawOnlyChoice}==1))} {
                if {$statusCallback!=""} {
                        $statusCallback "Turning $state raw supplies in \"Fast\" mode..."
                }
                set OnOffCommand "setbcorrps -${state} -${selection} -r"
                APSExecLog .setCorrState -unixCommand $OnOffCommand
                after 2000
                if {$statusCallback!=""} {
                        $statusCallback "Done."
                }
        } else {
                if {$statusCallback!=""} {
                        $statusCallback "Turning $state $selection power supplies and raw supplies and loading zero ramps..."
                }
                set OnOffCommand "setbcorrps -${state} -${selection}"
                APSExecLog .setCorrState -unixCommand $OnOffCommand
                after 2000
                if {$statusCallback!=""} {
                        $statusCallback "Done.  This procedure will take a couple of minutes to complete."
                }
        }
}

# Procedure to run the booster corrector initialization procedure

proc SetBCorrInitPS {args} {

        global ControlStatus selection 
        APSParseArguments {statusCallback}

        if {$statusCallback!=""} {
                $statusCallback "Starting corrector ramp initialization procedure for $selection supplies..."
        }
        set BCorrInitCommand "setbcorrinit -${selection}"
        APSExecLog .setCorrState -unixCommand $BCorrInitCommand
        after 2000
        if {$statusCallback!=""} {
                $statusCallback "Done.  The initialization procedure will take a few minutes to complete."
        }
}

proc LoadBCorrRamps {args} {

        global ControlStatus apsLatticeState
        APSParseArguments {statusCallback}

        set BCorrRampDir /home/helios/oagData/booster/ramps/correctors
        set BCorrRampLoadCommand "loadbramp $BCorrRampDir/HVCorr.ramp"

        if {$apsLatticeState == "low"} {
            file delete -force /home/helios/oagData/booster/ramps/correctors/HVCorr.ramp
            file link -symbolic /home/helios/oagData/booster/ramps/correctors/HVCorr.ramp /home/helios/oagData/booster/ramps/correctors/lattices/x12.75-y9.80/HVCorr.ramp
        } else {
            file delete -force /home/helios/oagData/booster/ramps/correctors/HVCorr.ramp
            file link -symbolic /home/helios/oagData/booster/ramps/correctors/HVCorr.ramp /home/helios/oagData/booster/ramps/correctors/lattices/x11.75-y9.80/HVCorr.ramp
        }
        
        if {$statusCallback!=""} {
                $statusCallback "Loading the booster corrector ramps..."
        }
        APSExecLog .bCorrRampLoad -unixCommand $BCorrRampLoadCommand
        after 2000
        if {$statusCallback!=""} {
                $statusCallback "This procedure will take a couple of minutes to complete."
        }
}

# Build Application

set state off
set selection b

MakeBCorrMagnetControlMenuAndStatus
MakeSetPowerSupplyWidget .setBCorrPSFrame -parent .userFrame
MakeBCorrOnOffRadioButtonFrameWidget .psOnOffFrame -parent .userFrame
MakeBCorrPSSelectionRadioButtonFrameWidget .psSelectFrame -parent .userFrame
MakeBCorrPSOptionsCheckButtonFrameWidget .psOptionFrame -parent .userFrame
MakeSetBCorrInitProcedureWidget .psInitializeProcFrame -parent .userFrame
MakeSetBCorrRampLoadProcedureWidget .bCorrRampLoadFrame -parent .userFrame


option add *font -adobe-courier-bold-r-normal-*-*-120-75-75-*-*-*-*

set tcl_precision 17

set pi [expr acos(-1.)]
set twopi [expr 2*$pi]

# Set Booster constants
setbconstants

# Defaults
set framepad 2

# Set all the plane-specific variables
setplane -b

# Frame 1
frame .frame1
pack .frame1 -in .userFrame -side top -fill x -fill y -padx $framepad -pady $framepad

# Frame 2
frame .frame2
pack .frame2 -in .userFrame -side top -fill x -fill y -padx $framepad -pady $framepad

# Set PSCU sample time
#frame .bpscusampletimeframe -relief raised -bd 2
#pack .bpscusampletimeframe -in .frame1 -side left -fill x -fill y -padx $framepad -pady $framepad
#makebpscusampletime .bpscusampletimeframe

# Corrector evolution
frame .bcorrevolframe -relief raised -bd 2
pack .bcorrevolframe -in .frame2 -side left -fill x -fill y -padx $framepad -pady $framepad
makebcorrevol .bcorrevolframe

# **************************** Emacs Editing Sequences *****************
# Local Variables:
# mode: tcl
# End:
