#!/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 
set CVSRevisionAuthor "\$Revision: 1.10 $ \$Author: shang $"

set APSScaleUpSize 5
APSApplication . -name "Big Current Monitor" -version $CVSRevisionAuthor 

APSMenubarAddMenu .font -parent .menu -text Font
foreach font {10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160} {
    .menu.font.menu add command -label $font -command \
      "ChangeFont -font $font" 
}
 
proc ChangeFont {args} {
    set font 20
    APSStrictParseArguments {font}
    option add *font -bitstream-courier-bold-r-normal-*-$font-*-75-75-*-*-*-*
   
    global timeStamp Current Lifetime Emittance Coupling prevFont
    if {$font==$prevFont} {
        return
    }
    set prevFont $font
    if [winfo exist .userFrame.time] {
        destroy .userFrame.time
    }
    entry .userFrame.time -relief flat -width 20 -textvariable timeStamp -background Grey85
    pack .userFrame.time -fill both
    if [winfo exist .userFrame.fg] {
        destroy .userFrame.fg
    }
    APSFrameGrid .fg -parent .userFrame \
      -xList {name value units} \
      -yList {current lifetime dose emittance coupling}
    
    label .userFrame.fg.name.current.label -text   "Current   " -pady 4
    label .userFrame.fg.name.lifetime.label -text  "Lifetime  " -pady 4
    label .userFrame.fg.name.dose.label -text  "Dose      " -pady 4
    label .userFrame.fg.name.emittance.label -text "Emittance " -pady 4
    label .userFrame.fg.name.coupling.label -text  "Coupling  " -pady 4
    
    pack .userFrame.fg.name.current.label .userFrame.fg.name.lifetime.label .userFrame.fg.name.dose.label  \
      .userFrame.fg.name.emittance.label .userFrame.fg.name.coupling.label -fill both
    
    label .userFrame.fg.units.current.label -text "mA" -pady 4
    label .userFrame.fg.units.lifetime.label -text "hr" -pady 4
    label .userFrame.fg.units.dose.label -text "A*h" -pady 4
    label .userFrame.fg.units.emittance.label -text "pm" -pady 4
    label .userFrame.fg.units.coupling.label -text "%" -pady 4
    
    pack .userFrame.fg.units.current.label .userFrame.fg.units.lifetime.label  \
         .userFrame.fg.units.dose.label .userFrame.fg.units.emittance.label .userFrame.fg.units.coupling.label -fill both
    
    APSLabeledOutput .lab -parent .userFrame.fg.value.current -label "" -textVariable Current \
      -width 6 
    .userFrame.fg.value.current.lab.entry configure -relief flat
    APSLabeledOutput .lab -parent .userFrame.fg.value.lifetime -label "" -textVariable Lifetime \
      -width 6 
    .userFrame.fg.value.lifetime.lab.entry configure -relief flat
    APSLabeledOutput .lab -parent .userFrame.fg.value.dose -label "" -textVariable Dose \
      -width 6 
    .userFrame.fg.value.dose.lab.entry configure -relief flat
    APSLabeledOutput .lab -parent .userFrame.fg.value.emittance -label "" -textVariable Emittance \
      -width 6 
    .userFrame.fg.value.emittance.lab.entry configure -relief flat
    APSLabeledOutput .lab -parent .userFrame.fg.value.coupling -label "" -textVariable Coupling \
      -width 6 
    .userFrame.fg.value.coupling.lab.entry configure -relief flat
}

set font 120
set args $argv
if {[APSStrictParseArguments {font}]} {
    return -code error "usage: SRLargeStatusDisplay -font <size>"
}

set Emittance ?
set Coupling ?

if {[pv linkw {Current LifetimeMin Dose} {S-DCCT:CurrentM S-INJ:MPS:LifetimeMinutesM S-DCCT:AmpHoursM}] != 0} {
    APSAlertBox .err[APSTmpString] -errorMessage "Unable to make channel access connections.\n$errorCode" 
    exit
}
set prevFont -1
set first 1

while 1 {
    pv getw {Current LifetimeMin}  30
    if {$Current!="?"} {
	catch {set Current [format %0.2f $Current]}
    }
    if {$LifetimeMin!="?"} {
	catch {set Lifetime [format %0.2f [expr $LifetimeMin/60.0]]}
    }
    if {$Dose!="?"} {
	catch {set Dose [format %0.2f $Dose]}
    }
    if [string compare $Emittance "?"] {
	catch {set Emittance [format %.2f $Emittance]}
    }
    if [string compare $Coupling "?"] {
	catch {set Coupling [format %.2f $Coupling]}
    }
    set timeStamp [clock format [clock seconds] -format "%m/%d/%Y %T"]
    if $first {
        ChangeFont -font $font
    }
    catch {
	if [expr $Current<1.0] {
	    set Emittance ?
	    set Coupling ?
	}
    }
    set first 0
    update
    after 1000
}
