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

APSStandardSetup

set env(EPICS_CA_AUTO_ADDR_LIST) YES
set env(EPICS_CA_ADDR_LIST) ""

set status ""
proc SetStatus {text} {
    global status
    set status $text
    update
}



APSApplication . -name LMPSStatusDisplay \
  -overview "SRLocalMPSValidation."

APSButton .read -parent .userFrame -text "Refresh" -command Read
set varList ""
for {set sector 1} {$sector<40} {incr sector 2} {
    set sectorf [format %02d $sector]
    set labelVar(S$sectorf) LMPS$sectorf
    lappend varList labelVar(S$sectorf)
}
APSFrame .f0 -parent .userFrame
.userFrame.f0.frame configure -bd 0
APSLabeledOutputFrame .label -parent .userFrame.f0.frame -label "Sector:  " -variableList $varList \
    -width 10 -orientation horizontal
#APSLabel .label -parent .userFrame -text $labelTxt

set scroll [APSScroll .scroll -parent .userFrame]
set scrollFrame .userFrame.scroll

foreach num {1 2 3 4 5 6 7} {
    set ratePVList ""
    set rateVarList ""
    set head1PVList ""
    set head1VarList ""
    set head2PVList ""
    set head2VarList ""
    set head3PVList ""
    set head3VarList ""
    set head4PVList ""
    set head4VarList ""
    for {set sector 1} {$sector<40} {incr sector 2} {
	set sectorf [format %02d $sector]
	lappend ratePVList  S${sectorf}-LMPS:BM_Link${num}RxRate-I
	lappend rateVarList LMPS(S$sectorf.link$num.rate)
	foreach head {1 2 3 4} {
	    lappend head${head}PVList S${sectorf}-LMPS:Link$num:BPM[expr $head -1]:HeadInfo-I
	    lappend head${head}VarList  LMPS(S$sectorf.link$num.head$head)
	}
    }
    if [pv linkw $rateVarList $ratePVList] {
	puts stderr "Error link pvs1: [join $ratePVList]"
	exit 1
    }
    if [pv linkw $head1VarList $head1PVList] {
	puts stderr "Error link pvs2: [join $head1PVList]"
	exit 1
    }
     if [pv linkw $head2VarList $head2PVList] {
	puts stderr "Error link pvs3: [join $head2PVList]"
	exit 1
    }
     if [pv linkw $head3VarList $head3PVList] {
	puts stderr "Error link pvs4: [join $head3PVList]"
	exit 1
    }
     if [pv linkw $head4VarList $head4PVList] {
	puts stderr "Error link pvs5: [join $head4PVList]"
	exit 1
    }
    set LMPS(Link$num.rateVarList) $rateVarList
    set LMPS(Link$num.head1VarList) $head1VarList
    set LMPS(Link$num.head2VarList) $head2VarList
    set LMPS(Link$num.head3VarList) $head3VarList
    set LMPS(Link$num.head4VarList) $head4VarList
   
}

set evenFastMaskPVList ""
set evenFastMaskVarList ""
set evenSlowMaskPVList ""
set evenSlowMaskVarList ""
set oddFastMaskPVList ""
set oddFastMaskVarList ""
set oddSlowMaskPVList ""
set oddSlowMaskVarList ""
for {set sector 1} {$sector<40} {incr sector 2} {
    set sectorf [format %02d $sector]
    lappend evenFastMaskPVList S${sectorf}-LMPS:ExtInFastMask1-SP
    lappend evenFastMaskVarList LMPS(S$sectorf.evenFast)
    lappend evenSlowMaskPVList S${sectorf}-LMPS:ExtInSlowMask1-SP
    lappend evenSlowMaskVarList LMPS(S$sectorf.evenSlow)
    
    lappend oddFastMaskPVList S${sectorf}-LMPS:ExtInFastMask0-SP
    lappend oddFastMaskVarList LMPS(S$sectorf.oddFast)
    lappend oddSlowMaskPVList S${sectorf}-LMPS:ExtInSlowMask0-SP
    lappend oddSlowMaskVarList LMPS(S$sectorf.oddSlow)

    lappend evenInputMaskPVList  S${sectorf}-LMPS:ExtInMask1-SP
    lappend evenInputMaskVarList LMPS(S$sectorf.evenInputMask)

    lappend oddInputMaskPVList  S${sectorf}-LMPS:ExtInMask0-SP
    lappend oddInputMaskVarList LMPS(S$sectorf.oddInputMask)
    
    lappend evenInputPVList  S${sectorf}-LMPS:ExtInData1-I
    lappend evenInputVarList LMPS(S$sectorf.evenInput)
     
    lappend oddInputPVList  S${sectorf}-LMPS:ExtInData0-I
    lappend oddInputVarList LMPS(S$sectorf.oddInput)
    
}
set LMPS(evenInputMaskVarList) $evenInputMaskVarList
set LMPS(oddInputMaskVarList) $oddInputMaskVarList
set LMPS(evenInputVarList) $evenInputVarList
set LMPS(oddInputVarList) $oddInputVarList

set LMPS(evenFastVarList) $evenFastMaskVarList
set LMPS(evenSlowVarList) $evenSlowMaskVarList
set LMPS(oddFastVarList) $oddFastMaskVarList
set LMPS(oddSlowVarList) $oddSlowMaskVarList
if [pv linkw $evenFastMaskVarList $evenFastMaskPVList] {
    puts stderr "Error link even fast mask PVs: [join $evenFastMaskPVList]"
    exit 1
}
if [pv linkw $evenSlowMaskVarList $evenSlowMaskPVList] {
    puts stderr "Error link even slow mask PVs: [join $evenSlowMaskPVList]"
    exit 1
}

if [pv linkw $oddFastMaskVarList $oddFastMaskPVList] {
    puts stderr "Error link odd fast mask PVs: [join $oddFastMaskPVList]"
    exit 1
}
if [pv linkw $oddSlowMaskVarList $oddSlowMaskPVList] {
    puts stderr "Error link odd slow mask PVs: [join $oddSlowMaskPVList]"
    exit 1
}

if [pv linkw  $evenInputMaskVarList $evenInputMaskPVList] {
    puts stderr "Error link even input mask PVs: [join $evenInputMaskPVList]"
    exit 1
}

if [pv linkw  $oddInputMaskVarList $oddInputMaskPVList] {
    puts stderr "Error link odd input mask PVs: [join $evenInputMaskPVList]"
    exit 1
}

if [pv linkw  $evenInputVarList $evenInputPVList] {
    puts stderr "Error link even input  PVs: [join $evenInputPVList]"
    exit 1
}

if [pv linkw  $oddInputVarList $oddInputMaskPVList] {
    puts stderr "Error link odd input  PVs: [join $evenInputPVList]"
    exit 1
}



proc Read {args} {
    global LMPS
    foreach item {Link1 Link2 Link3 Link4 Link5 Link6 Link7} {
	pv getw $LMPS($item.rateVarList)
	update
	foreach num {1 2 3 4} {
	    pv getw $LMPS($item.head${num}VarList)
	    update
	    foreach var $LMPS($item.head${num}VarList) {
		set $var [string toupper 0x[format %x [set $var]]]
	    }
	}
    }
    foreach nm {even odd} {
	foreach item {Fast Slow} {
	    pv getw $LMPS(${nm}${item}VarList)
	    update
	    foreach var $LMPS(${nm}${item}VarList) {
		set $var [string toupper 0X[format %x [set $var]]]
	    }
	}
	foreach item {InputMask Input}  {
	    pv getw $LMPS(${nm}${item}VarList)
	    update
	    foreach var $LMPS(${nm}${item}VarList) {
		set $var [string toupper 0X[format %x [set $var]]]
	    }
	}
    }
}

set width 10
set itemList {Link1 Link2 Link3 Link4 Link5 Link6 Link7}
set i 0
foreach item $itemList {
    if [expr $i % 2]==1 {
	APSFrame .f$item -parent $scroll -label $item  -packOption "-side top -fill x -fill y" -labelFont bold
    } else {
	APSFrame .f$item -parent $scroll -label $item  -packOption "-side top -fill x -fill y"
    }
    incr i
    set w0 $scroll.f$item.frame
    APSLabeledEntryFrame .rate -parent $w0 -label "Rx Rate:" -variableList $LMPS($item.rateVarList) \
	-width $width -orientation horizontal 
    APSLabeledEntryFrame .head1 -parent $w0 -label "BPM1 Head:" -variableList $LMPS($item.head1VarList) \
	-width $width -orientation horizontal -fg blue
    APSLabeledEntryFrame .head2 -parent $w0 -label "BPM2 Head:" -variableList $LMPS($item.head2VarList) \
	-width $width -orientation horizontal -fg green
    APSLabeledEntryFrame .head3 -parent $w0 -label "BPM3 Head:" -variableList $LMPS($item.head3VarList) \
	-width $width -orientation horizontal -fg black
    APSLabeledEntryFrame .head4 -parent $w0 -label "BPM4 Head:" -variableList $LMPS($item.head4VarList) \
	-width $width -orientation horizontal -fg red
   
}

APSFrame .input -parent $scroll -label "MPS Input&Mask" -labelFont bold
APSLabeledEntryFrame .even2 -parent $scroll.input.frame -label "Even Input:" -width $width \
    -orientation horizontal -variableList $LMPS(evenInputVarList)  -fg black
APSLabeledEntryFrame .even1 -parent $scroll.input.frame -label "Even Mask:" -width $width \
    -orientation horizontal -variableList $LMPS(evenInputMaskVarList) -fg red
APSLabeledEntryFrame .odd2 -parent $scroll.input.frame -label "Odd Input:" -width $width \
    -orientation horizontal -variableList $LMPS(oddInputVarList)  -fg black
APSLabeledEntryFrame .odd1 -parent $scroll.input.frame -label "Odd Mask:" -width $width \
    -orientation horizontal -variableList $LMPS(oddInputMaskVarList) -fg red 

APSFrame .abort -parent $scroll -label "MPS Fast/Slow Config" -labelFont bold
APSLabeledEntryFrame .even1 -parent $scroll.abort.frame -label "Even Fast:" -width $width \
    -orientation horizontal -variableList $LMPS(evenFastVarList) -fg black
APSLabeledEntryFrame .even2 -parent $scroll.abort.frame -label "Even Slow:" -width $width \
    -orientation horizontal -variableList $LMPS(evenSlowVarList)  -fg red
APSLabeledEntryFrame .odd1 -parent $scroll.abort.frame -label "Odd Fast:" -width $width \
    -orientation horizontal -variableList $LMPS(oddFastVarList)  -fg black
APSLabeledEntryFrame .odd2 -parent $scroll.abort.frame -label "Odd Slow:" -width $width \
    -orientation horizontal -variableList $LMPS(oddSlowVarList)  -fg red 

    

tkwait visibility $scroll.abort.frame.odd2

APSScrollAdjust $scrollFrame  -numVisible 20
Read
    

