#!/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)]
#Agilent 54854A scope command documentation
#http://cp.literature.agilent.com/litweb/pdf/54855-97017.pdf

proc ScopeWaveform {widget args} {
    if {![llength [info commands blt::bgexec]]} {
	package require BLT
    }

    set parent ""
    set packOption "-side top"
    set xmin ""
    set xmax ""
    set width 300
    set height 300
    set waveformVariable1 ""
    set waveformVariable2 ""
    set waveformVariable3 ""
    set waveformVariable4 ""
    APSStrictParseArguments {parent packOption xmin xmax width height waveformVariable1 waveformVariable2 waveformVariable3 waveformVariable4}
    
    frame $parent$widget -bd 2
    eval pack $parent$widget $packOption
    global $waveformVariable1 $waveformVariable2 $waveformVariable3 $waveformVariable4 apsScopeSettings
    set xMax [expr {[llength [set $waveformVariable1]] - 1}]
    if {![llength $xmax]} {
        set xmax $xMax
    }
    if {![llength $xmin]} {
        set xmin 0
    }
    set ymin 99e99
    for {set i 1} {$i <= 4} {incr i} {
        if {$apsScopeSettings(line$i) == "On"} {
            set y [expr {$apsScopeSettings(:CHANNEL${i}:OFFSET) - $apsScopeSettings(:CHANNEL${i}:RANGE) / 2.0}]
            if {$y < $ymin} {
                set ymin $y
            }
        }
    }
    set ymax -99e99
    for {set i 1} {$i <= 4} {incr i} {
        if {$apsScopeSettings(line$i) == "On"} {
            set y [expr {$apsScopeSettings(:CHANNEL${i}:OFFSET) + $apsScopeSettings(:CHANNEL${i}:RANGE) / 2.0}]
            if {$y > $ymax} {
                set ymax $y
            }
        }
    }
    if {$ymin == $ymax} {
        set ymin [expr {$ymin - 1}]
        set ymax [expr {$ymax + 1}]
    }
    set increment [expr {1.0 * ($xmax - $xmin) / $xMax}]
    set p $xmin
    for {set n 0} {$n <= $xMax} {incr n} {
        lappend xdata $p
        set p [expr {$p + $increment}]
    }
    blt::graph $parent$widget.graph -cursor crosshair -width $width -height $height \
      -bg black -fg white -title "" \
      -plotbackground black
    $parent$widget.graph xaxis configure -color white -background black \
      -min $xmin -max $xmax -title "" -titlecolor white
    $parent$widget.graph yaxis configure -color white -background black \
      -min $ymin -max $ymax -title "" -titlecolor white
    $parent$widget.graph legend configure -hide yes
    $parent$widget.graph grid configure -hide no -dashes "2 2"
    $parent$widget.graph element create line1 -xdata $xdata -ydata [set $waveformVariable1] \
      -color yellow -pixels 1 -linewidth 1
    $parent$widget.graph element create line2 -xdata $xdata -ydata [set $waveformVariable2] \
      -color green -pixels 1 -linewidth 1
    $parent$widget.graph element create line3 -xdata $xdata -ydata [set $waveformVariable3] \
      -color SkyBlue -pixels 1 -linewidth 1
    $parent$widget.graph element create line4 -xdata $xdata -ydata [set $waveformVariable4] \
      -color magenta -pixels 1 -linewidth 1
    pack $parent$widget.graph -fill both -expand true
    
    trace variable $waveformVariable1 w "ScopeWaveformUpdate $parent$widget.graph line1"
    trace variable $waveformVariable2 w "ScopeWaveformUpdate $parent$widget.graph line2"
    trace variable $waveformVariable3 w "ScopeWaveformUpdate $parent$widget.graph line3"
    trace variable $waveformVariable4 w "ScopeWaveformUpdate $parent$widget.graph line4"
}

proc ScopeWaveformUpdate {W line waveformVariable tmp op} {
    global $waveformVariable apsScopeSettings
    set ymin 99e99
    set ymax -99e99
    for {set i 1} {$i <= 4} {incr i} {
        if {$apsScopeSettings(line$i) == "On"} {
            set y [expr {$apsScopeSettings(:CHANNEL${i}:OFFSET) - $apsScopeSettings(:CHANNEL${i}:RANGE) / 2.0}]
            if {$y < $ymin} {
                set ymin $y
            }
            set y [expr {$apsScopeSettings(:CHANNEL${i}:OFFSET) + $apsScopeSettings(:CHANNEL${i}:RANGE) / 2.0}]
            if {$y > $ymax} {
                set ymax $y
            }
            $W element configure line$i -hide no
        } else {
            $W element configure line$i -hide yes
        }
    }
    $W yaxis configure -min $ymin -max $ymax
    $W element configure $line -ydata [set $waveformVariable]
}

proc Export {} {
    global doRun apsScopeSettings voltsList timeList
    if {[winfo exists .export]} {
	set apsScopeSettings(exportFile) ""
	destroy .export
    }
    toplevel .export
    if {$doRun} {
	frame .export.top
	pack .export.top
	APSLabeledEntry .pages -parent .export.top -label "Frames to save:" -textVariable apsScopeSettings(framesToSave)
    } else {
	set apsScopeSettings(framesToSave) 1
    }
    set apsScopeSettings(exportFile) [APSFileSelectDialog .select -parent .export -title "Save File" -checkValidity 0]
    destroy .export
    if {[llength $apsScopeSettings(exportFile)] == 0} {
        return
    }
    set apsScopeSettings(doExport) $apsScopeSettings(framesToSave)
    if {$doRun == 0} {
	UpdateParameters
	set minpoints [expr int($apsScopeSettings(:TIMEBASE:RANGE) * $apsScopeSettings(:ACQUIRE:SRATE))]
	set apsScopeSettings(:ACQUIRE:POINTS) $minpoints
        APSInitializeScope
        APSAcquireDataFromScope
        APSMeasurementsFromScope
        APSTransferDataFromScope \
          -waveformVariable1 voltsList1 \
          -waveformVariable2 voltsList2 \
          -waveformVariable3 voltsList3 \
          -waveformVariable4 voltsList4 \
          -timeVariable timeList
        .userFrame.top.waveform.graph element configure line1 -xdata $timeList
        .userFrame.top.waveform.graph element configure line2 -xdata $timeList
        .userFrame.top.waveform.graph element configure line3 -xdata $timeList
        .userFrame.top.waveform.graph element configure line4 -xdata $timeList
        .userFrame.top.waveform.graph xaxis configure -min [lindex $timeList 0] -max [lindex $timeList end]

        APSSetValueOnScope :CHANNEL1:DISPLAY ON
        APSSetValueOnScope :ACQ:SRAT:AUTO 1
        APSSetValueOnScope :ACQ:POINTS:AUTO 1
        APSSendCommandToScope :RUN
    }
}

proc Run {} {
    bell
    global doRun parameterUpdateRequested w
    if {$doRun} {
        return
    }
    set parameterUpdateRequested 1
    $w.update.button configure -bg grey85
    set doRun 1
    .userFrame.b1 configure -bg green
    .userFrame.b2 configure -bg red
    Update
}

proc Stop {} {
    bell
    global doRun
    if {!$doRun} {
        return
    }
    .userFrame.b1 configure -bg yellow
    .userFrame.b2 configure -bg yellow
    set doRun 0
}

proc Update {} {
    global doRun apsScopeSettings voltsList timeList parameterUpdateRequested
#    set origPoints $apsScopeSettings(:ACQUIRE:POINTS)
    while {$doRun} {
        if {$parameterUpdateRequested} {
            UpdateParameters
	    set minpoints [expr int($apsScopeSettings(:TIMEBASE:RANGE) * $apsScopeSettings(:ACQUIRE:SRATE))]
	    set apsScopeSettings(:ACQUIRE:POINTS) $minpoints
	    APSInitializeScope
	    set rescale 1
	    set tVar timeList
        } else {
	    set rescale 0
	    set tVar ""
	}
        APSAcquireDataFromScope
        update
	if {$apsScopeSettings(:TRIGGER:SWEEP) == "TRIG"} {
	    while {1} {
		if {[catch {exec agilentcomm -ip $apsScopeSettings(ip) -c ":TER?"} trigEventOccurred]} {
		    set trigEventOccurred 0
		} else {
		    set trigEventOccurred [string trim $trigEventOccurred +]
		}
		if {$trigEventOccurred} {
		    break
		} else {
		    update
		    puts "NO TRIGGER"
		    if {!$doRun} {
			break
		    }
		}
	    }
	}
        APSMeasurementsFromScope
        update
        APSTransferDataFromScope \
          -waveformVariable1 voltsList1 \
          -waveformVariable2 voltsList2 \
          -waveformVariable3 voltsList3 \
          -waveformVariable4 voltsList4 \
          -timeVariable $tVar
	if {$rescale} {
	    .userFrame.top.waveform.graph element configure line1 -xdata $timeList
	    .userFrame.top.waveform.graph element configure line2 -xdata $timeList
	    .userFrame.top.waveform.graph element configure line3 -xdata $timeList
	    .userFrame.top.waveform.graph element configure line4 -xdata $timeList
	    .userFrame.top.waveform.graph xaxis configure -min [lindex $timeList 0] -max [lindex $timeList end]
	}
        APSWaitWithUpdate -waitSeconds .1
    }
    APSSetValueOnScope :CHANNEL1:DISPLAY ON

    APSSetValueOnScope :ACQ:SRAT:AUTO 1
    APSSetValueOnScope :ACQ:POINTS:AUTO 1
    APSSendCommandToScope :RUN
    .userFrame.b1 configure -bg grey
    .userFrame.b2 configure -bg grey
}

proc Save {} {
    global apsScopeSettingsFlux
    set saveFile [APSFileSelectDialog .select -title "Save File" -checkValidity 0]
    if {[llength $saveFile] == 0} {
        return
    }
    set fid [open $saveFile w]
    puts $fid "[array get apsScopeSettingsFlux]"
    close $fid
}

proc Load {} {
    global apsScopeSettings apsScopeSettingsFlux
    set loadFile [APSFileSelectDialog .select -title "Save File" -checkValidity 1]
    if {[llength $loadFile] == 0} {
        return
    }
    set fid [open $loadFile r]
    array set apsScopeSettingsFlux [read $fid]
    close $fid
}

proc UpdateParameters {} {
    global apsScopeSettings apsScopeSettingsFlux parameterUpdateRequested
    set apsScopeSettings(ip) $apsScopeSettingsFlux(ip) 
    set apsScopeSettings(:TIMEBASE:REFERENCE) $apsScopeSettingsFlux(:TIMEBASE:REFERENCE) 
    set apsScopeSettings(:TIMEBASE:RANGE) $apsScopeSettingsFlux(:TIMEBASE:RANGE)
    set apsScopeSettings(:TIMEBASE:POSITION) $apsScopeSettingsFlux(:TIMEBASE:POSITION) 
    set apsScopeSettings(:CHANNEL1:RANGE) $apsScopeSettingsFlux(:CHANNEL1:RANGE) 
    set apsScopeSettings(:CHANNEL1:OFFSET) $apsScopeSettingsFlux(:CHANNEL1:OFFSET) 
    set apsScopeSettings(:CHANNEL2:RANGE) $apsScopeSettingsFlux(:CHANNEL2:RANGE) 
    set apsScopeSettings(:CHANNEL2:OFFSET) $apsScopeSettingsFlux(:CHANNEL2:OFFSET) 
    set apsScopeSettings(:CHANNEL3:RANGE) $apsScopeSettingsFlux(:CHANNEL3:RANGE) 
    set apsScopeSettings(:CHANNEL3:OFFSET) $apsScopeSettingsFlux(:CHANNEL3:OFFSET) 
    set apsScopeSettings(:CHANNEL4:RANGE) $apsScopeSettingsFlux(:CHANNEL4:RANGE) 
    set apsScopeSettings(:CHANNEL4:OFFSET) $apsScopeSettingsFlux(:CHANNEL4:OFFSET) 
    set apsScopeSettings(:TRIGGER:SWEEP) $apsScopeSettingsFlux(:TRIGGER:SWEEP) 
    set apsScopeSettings(:TRIGGER:EDGE:SOURCE) $apsScopeSettingsFlux(:TRIGGER:EDGE:SOURCE) 
    set apsScopeSettings(:TRIGGER:EDGE:SLOPE) $apsScopeSettingsFlux(:TRIGGER:EDGE:SLOPE) 
    set apsScopeSettings(:TRIGGER:LEVEL) $apsScopeSettingsFlux(:TRIGGER:LEVEL) 
    set apsScopeSettings(pointsToAverage) $apsScopeSettingsFlux(pointsToAverage) 
#    set apsScopeSettings(:ACQUIRE:POINTS) $apsScopeSettingsFlux(:ACQUIRE:POINTS) 
    set apsScopeSettings(:ACQUIRE:SRATE) $apsScopeSettingsFlux(:ACQUIRE:SRATE) 
    set apsScopeSettings(line1) $apsScopeSettingsFlux(line1) 
    set apsScopeSettings(line2) $apsScopeSettingsFlux(line2) 
    set apsScopeSettings(line3) $apsScopeSettingsFlux(line3) 
    set apsScopeSettings(line4) $apsScopeSettingsFlux(line4) 
    set parameterUpdateRequested 0
}
set parameterUpdateRequested 1
proc RequestUpdateParameters {} {
    global parameterUpdateRequested w
    set parameterUpdateRequested 1
    $w.update.button configure -bg grey85
}

################## Setup GUI #####################
APSApplication . -name ScopeControl 

frame .userFrame.top
pack .userFrame.top


set apsScopeSettingsFlux(ip) 164.54.3.74
set apsScopeSettingsFlux(:TIMEBASE:REFERENCE) LEFT
set apsScopeSettingsFlux(:TIMEBASE:RANGE) 5E-04
set apsScopeSettingsFlux(:TIMEBASE:POSITION) 0
set apsScopeSettingsFlux(:CHANNEL1:RANGE) 1.6
set apsScopeSettingsFlux(:CHANNEL1:OFFSET) 0
set apsScopeSettingsFlux(:CHANNEL2:RANGE) 1.6
set apsScopeSettingsFlux(:CHANNEL2:OFFSET) 0
set apsScopeSettingsFlux(:CHANNEL3:RANGE) 1.6
set apsScopeSettingsFlux(:CHANNEL3:OFFSET) 0
set apsScopeSettingsFlux(:CHANNEL4:RANGE) 1.6
set apsScopeSettingsFlux(:CHANNEL4:OFFSET) 0
set apsScopeSettingsFlux(:TRIGGER:SWEEP) AUTO
set apsScopeSettingsFlux(:TRIGGER:EDGE:SOURCE) CHAN1
set apsScopeSettingsFlux(:TRIGGER:EDGE:SLOPE) POS
set apsScopeSettingsFlux(:TRIGGER:LEVEL) 0.0
set apsScopeSettingsFlux(pointsToAverage) 1
set apsScopeSettings(:ACQUIRE:POINTS) 500
set apsScopeSettingsFlux(:ACQUIRE:SRATE) 1e6
set apsScopeSettingsFlux(line1) On
set apsScopeSettingsFlux(line2) Off
set apsScopeSettingsFlux(line3) Off
set apsScopeSettingsFlux(line4) Off
#set apsScopeSettings(measurement1label) FREQUENCY
#set apsScopeSettings(measurement2label) VMAX
#set apsScopeSettings(measurement3label) VMIN
#set apsScopeSettings(measurement4label) PWIDTH
set apsScopeSettings(measurement1label) ""
set apsScopeSettings(measurement2label) ""
set apsScopeSettings(measurement3label) ""
set apsScopeSettings(measurement4label) ""
set apsScopeSettings(doExport) 0
set apsScopeSettings(framesToSave) 1
UpdateParameters

proc ChangedParameter {args} {
    global w
    $w.update.button configure -bg yellow
}
trace add variable apsScopeSettingsFlux(ip) write ChangedParameter
trace add variable apsScopeSettingsFlux(:TIMEBASE:REFERENCE) write ChangedParameter
trace add variable apsScopeSettingsFlux(:TIMEBASE:RANGE) write ChangedParameter
trace add variable apsScopeSettingsFlux(:TIMEBASE:POSITION) write ChangedParameter
trace add variable apsScopeSettingsFlux(:CHANNEL1:RANGE) write ChangedParameter
trace add variable apsScopeSettingsFlux(:CHANNEL1:OFFSET) write ChangedParameter
trace add variable apsScopeSettingsFlux(:CHANNEL2:RANGE) write ChangedParameter
trace add variable apsScopeSettingsFlux(:CHANNEL2:OFFSET) write ChangedParameter
trace add variable apsScopeSettingsFlux(:CHANNEL3:RANGE) write ChangedParameter
trace add variable apsScopeSettingsFlux(:CHANNEL3:OFFSET) write ChangedParameter
trace add variable apsScopeSettingsFlux(:CHANNEL4:RANGE) write ChangedParameter
trace add variable apsScopeSettingsFlux(:CHANNEL4:OFFSET) write ChangedParameter
trace add variable apsScopeSettingsFlux(:TRIGGER:SWEEP) write ChangedParameter
trace add variable apsScopeSettingsFlux(:TRIGGER:EDGE:SOURCE) write ChangedParameter
trace add variable apsScopeSettingsFlux(:TRIGGER:EDGE:SLOPE) write ChangedParameter
trace add variable apsScopeSettingsFlux(:TRIGGER:LEVEL) write ChangedParameter
trace add variable apsScopeSettingsFlux(pointsToAverage) write ChangedParameter
#trace add variable apsScopeSettingsFlux(:ACQUIRE:POINTS) write ChangedParameter
trace add variable apsScopeSettingsFlux(:ACQUIRE:SRATE) write ChangedParameter
trace add variable apsScopeSettingsFlux(line1) write ChangedParameter
trace add variable apsScopeSettingsFlux(line2) write ChangedParameter
trace add variable apsScopeSettingsFlux(line3) write ChangedParameter
trace add variable apsScopeSettingsFlux(line4) write ChangedParameter

set voltsList1 "0 0"
set voltsList2 "0 0"
set voltsList3 "0 0"
set voltsList4 "0 0"
ScopeWaveform .waveform -parent .userFrame.top -packOption "-side left" \
  -waveformVariable1 voltsList1 \
  -waveformVariable2 voltsList2 \
  -waveformVariable3 voltsList3 \
  -waveformVariable4 voltsList4 \
  -width 730 -height 730 \
  -xmin 0 -xmax 1

pack [frame .userFrame.top.right] -side left -anchor nw
APSFrame .settings -parent .userFrame.top.right -packOption "-side top -anchor nw"
APSFrame .measurements -parent .userFrame.top.right -packOption "-side top -anchor nw"
set w .userFrame.top.right.settings.frame
set u .userFrame.top.right.measurements.frame


APSLabeledEntry .le0 -parent $w -label "Scope IP address:" -textVariable apsScopeSettingsFlux(ip)

APSLabeledEntry .le1a -parent $w -label "Scope Channel \#1" -textVariable apsScopeSettingsFlux(line1)
pack forget ${w}.le1a.entry
${w}.le1a.label configure -fg yellow -bg black
tk_optionMenu ${w}.le1a.om apsScopeSettingsFlux(line1) On Off
pack ${w}.le1a.om -side right -fill x

APSLabeledEntry .le1b -parent $w -label "Scope Channel \#2" -textVariable apsScopeSettingsFlux(line2)
pack forget ${w}.le1b.entry
${w}.le1b.label configure -fg green -bg black
tk_optionMenu ${w}.le1b.om apsScopeSettingsFlux(line2) On Off
pack ${w}.le1b.om -side right -fill x

APSLabeledEntry .le1c -parent $w -label "Scope Channel \#3" -textVariable apsScopeSettingsFlux(line3)
pack forget ${w}.le1c.entry
${w}.le1c.label configure -fg SkyBlue -bg black
tk_optionMenu ${w}.le1c.om apsScopeSettingsFlux(line3) On Off
pack ${w}.le1c.om -side right -fill x

APSLabeledEntry .le1d -parent $w -label "Scope Channel \#4" -textVariable apsScopeSettingsFlux(line4)
pack forget ${w}.le1d.entry
${w}.le1d.label configure -fg magenta -bg black
tk_optionMenu ${w}.le1d.om apsScopeSettingsFlux(line4) On Off
pack ${w}.le1d.om -side right -fill x

APSLabeledEntry .le2 -parent $w -label "Time base reference:" -textVariable apsScopeSettingsFlux(:TIMEBASE:REFERENCE)
pack forget ${w}.le2.entry
tk_optionMenu ${w}.le2.om apsScopeSettingsFlux(:TIMEBASE:REFERENCE) LEFT CENTER RIGHT
pack ${w}.le2.om -side right -fill x

APSLabeledEntry .le3 -parent $w -label "Time base range:" -textVariable apsScopeSettingsFlux(:TIMEBASE:RANGE)
APSLabeledEntry .le4 -parent $w -label "Time base position:" -textVariable apsScopeSettingsFlux(:TIMEBASE:POSITION)
APSLabeledEntry .le5 -parent $w -label "Channel \#1 range (V):" -textVariable apsScopeSettingsFlux(:CHANNEL1:RANGE)
APSLabeledEntry .le6 -parent $w -label "Channel \#1 offset (V):" -textVariable apsScopeSettingsFlux(:CHANNEL1:OFFSET)
APSLabeledEntry .le7 -parent $w -label "Channel \#2 range (V):" -textVariable apsScopeSettingsFlux(:CHANNEL2:RANGE)
APSLabeledEntry .le8 -parent $w -label "Channel \#2 offset (V):" -textVariable apsScopeSettingsFlux(:CHANNEL2:OFFSET)
APSLabeledEntry .le9 -parent $w -label "Channel \#3 range (V):" -textVariable apsScopeSettingsFlux(:CHANNEL3:RANGE)
APSLabeledEntry .le10 -parent $w -label "Channel \#3 offset (V):" -textVariable apsScopeSettingsFlux(:CHANNEL3:OFFSET)
APSLabeledEntry .le11 -parent $w -label "Channel \#4 range (V):" -textVariable apsScopeSettingsFlux(:CHANNEL4:RANGE)
APSLabeledEntry .le12 -parent $w -label "Channel \#4 offset (V):" -textVariable apsScopeSettingsFlux(:CHANNEL4:OFFSET)

APSLabeledEntry .le12b -parent $w -label "Trigger sweep:" -textVariable apsScopeSettingsFlux(:TRIGGER:SWEEP)
pack forget ${w}.le12b.entry
tk_optionMenu ${w}.le12b.om apsScopeSettingsFlux(:TRIGGER:SWEEP) AUTO TRIG SING
pack ${w}.le12b.om -side right -fill x

APSLabeledEntry .le13 -parent $w -label "Trigger edge source:" -textVariable apsScopeSettingsFlux(:TRIGGER:EDGE:SOURCE)
pack forget ${w}.le13.entry
tk_optionMenu ${w}.le13.om apsScopeSettingsFlux(:TRIGGER:EDGE:SOURCE) CHAN1 CHAN2 CHAN3 CHAN4 AUX LINE
pack ${w}.le13.om -side right -fill x

APSLabeledEntry .le14 -parent $w -label "Trigger edge slope:" -textVariable apsScopeSettingsFlux(:TRIGGER:EDGE:SLOPE)
pack forget ${w}.le14.entry
tk_optionMenu ${w}.le14.om apsScopeSettingsFlux(:TRIGGER:EDGE:SLOPE) POS NEG
pack ${w}.le14.om -side right -fill x
APSLabeledEntry .le15 -parent $w -label "Trigger level (V):" -textVariable apsScopeSettingsFlux(:TRIGGER:LEVEL)

APSLabeledEntry .le16 -parent $w -label "Points to average:" -textVariable apsScopeSettingsFlux(pointsToAverage)
APSLabeledEntry .le17 -parent $w -label "Acquire points (Calc):" -textVariable apsScopeSettings(:ACQUIRE:POINTS)
APSLabeledEntry .le18 -parent $w -label "Sample rate:" -textVariable apsScopeSettingsFlux(:ACQUIRE:SRATE)
pack forget ${w}.le18.entry
tk_optionMenu ${w}.le18.om apsScopeSettingsFlux(:ACQUIRE:SRATE) 1 2.5 5 10 25 50 100 250 500 1e3 2.5e3 5e3 10e3 20e3 25e3 40e3 50e3 100e3 200e3 250e3 400e3 500e3 1e6 2e6 2.5e6 4e6 5e6 10e6 20e6 25e6 40e6 50e6 100e6 200e6 250e6 400e6 500e6 1e9 2e9 2.5e9 4e9 5e9 10e9 20e9
pack ${w}.le18.om -side right -fill x
APSButton .update -parent $w -text "Update Parameters" -packOption "-anchor e" -command RequestUpdateParameters



#############################

for {set i 1} {$i <= 4} {incr i} {
    APSLabeledOutput .lo$i -parent $u -label "" -textVariable apsScopeSettings(measurement${i})
    pack forget ${u}.lo${i}.label
    tk_optionMenu ${u}.lo${i}.om apsScopeSettings(measurement${i}label) "" AREA FALLTIME FREQUENCY NWIDTH PWIDTH RISETIME TMAX TMIN VAMPLITUDE VAVERAGE VBASE VLOWER VMAX VMIDDLE VMIN VPP VRMS-AC VRMS-DC VTOP VUPPER 
    pack ${u}.lo${i}.om -side left -fill x
}

#############################

set doRun 0
APSButton .b0 -parent .userFrame -text "Reset" -command "Stop ; APSResetScope"
APSButton .b1 -parent .userFrame -text "Run" -command "Run"
APSButton .b2 -parent .userFrame -text "Stop" -command "Stop"
APSButton .b3 -parent .userFrame -text "Export" -command "Export"
APSButton .b4 -parent .userFrame -text "Save Config" -command "Save"
APSButton .b5 -parent .userFrame -text "Load Config" -command "Load"




proc APSCopyScopeParameters {} {
    global apsScopeSettingsFlux apsScopeSettings
    set names ":TIMEBASE:REFERENCE :TIMEBASE:RANGE :TIMEBASE:POSITION :CHANNEL1:RANGE :CHANNEL1:OFFSET :CHANNEL2:RANGE :CHANNEL2:OFFSET :CHANNEL3:RANGE :CHANNEL3:OFFSET :CHANNEL4:RANGE :CHANNEL4:OFFSET :TRIGGER:EDGE:SOURCE :TRIGGER:SWEEP :TRIGGER:EDGE:SLOPE :ACQUIRE:SRATE"
    foreach name $names {
	set apsScopeSettingsFlux($name) [APSGetValueFromScope $name]
    }
    set names ":ACQUIRE:POINTS"
    foreach name $names {
	set apsScopeSettings($name) [APSGetValueFromScope $name]
    }

    if {[catch {exec agilentcomm -ip $apsScopeSettings(ip) -c ":TRIGGER:LEVEL? $apsScopeSettingsFlux(:TRIGGER:EDGE:SOURCE)"} results]} {
        puts "Error: $results"
    } else {
        set apsScopeSettingsFlux(:TRIGGER:LEVEL) $results
    }
    UpdateParameters
}

APSCopyScopeParameters
$w.update.button configure -bg grey85

#APSResetScope


