#!/bin/bash -norc
#==============================================================
#
#  Abs:  Starts  ff -> matlab feedback switching script
#
#  Name:switchFFToMatlab
#   $1:  ff feedback
#   $2 :  matlab feedback
#   $3, $4, $5: numbers for ML02 PVs
#  
#  eg: switchFFToMatlab FBO1:TR02 INL1 101 102 103 INL
#  eg: switchFFToMatlab FB03:TR01 LTL0 104 105 106 LTL
# ------------------------------------------------------------
# This script is called from lclshome .
# ------------------------------------------------------------
#
#  Facility:  SLAC
#
#  Auth: nov 5 2010 dfairley
#
# Mod 
#
# 05-NOV-2010 d. fairley created
#
#==============================================================
#
#
# disable ML01 calc records to that point STATE,  COUNTER PVs
#

# turn off FF feedback and disable STATE PV so it cannot be started
#
caput FBCK:$1:STATE 0
caput FBCK:$1:INSTALLED 0
caput FBCK:$1:STATE.DISV 0
# enable the matlab feedback general STATE PV so it can be started
caput FBCK:$6:1:STATE.DISV 1

# disable no longer write fast fb STATE into matlab STATE so other apps can read it
#caput SIOC:SYS0:ML01:CALCOUT$3.DISV 0 

# disable: no longer write fast fb COUNTER into matlab CNTDISP so other apps can read it
#caput SIOC:SYS0:ML01:CALCOUT$5.DISV 0
#
# disable the fast fb alarms
#
disableALHGroup ACT$2
disableALHGroup SENS$2
disableALHGroup STATE$2
disableALHGroup STAT$2
#
# enable the matlab fb alarms
#
enableALHGroup XCORGb$2
enableALHGroup YCORGb$2
enableALHGroup FBCKGb$2
enableALHGroup BPMSGb$2

# write instructions to status strings for edm display
#
# swaps strings for matlab feedback ON 1=off, 2=on
# matlab string
caput SIOC:SYS0:ML02:AO$3.DESC "RESERVED FOR FF"
caput SIOC:SYS0:ML02:AO$3 2
# ff string
caput SIOC:SYS0:ML02:AO$4.DESC "RESERVED FOR FF"
caput SIOC:SYS0:ML02:AO$4 1

# If the feedback is the Bunch Change, change Sonyas PV
if [ "$1" == "FB02:GN01" ]; then
    caput IOC:IN20:BP01:QANNH.INPA FBCK:BCI0:1:CHRGSP CPP
    caput IOC:IN20:BP01:QANNL.INPA FBCK:BCI0:1:CHRGSP CPP
fi
