
#==================================
#----------------------------------
# pulse.template:
#
# Records to support pulsed RF.
# feed_launcher script executes
# lcls2_rf/software/prc/rf_pulse.py 
# on user request, which runs 
# TGEN program.
#----------------------------------
#==================================

#==================================
# feed_wf_acquire_base.template
# determines waveform period length
# based on number of active waveforms
# and decimation factor. Here, we
# mimic those calculations, but using
# the non-chirp decimation factor
# in order to provide these values
# and other diagnostics independent
# of current mode
#==================================

record(calc, "$(PREF)PULSE_SAMP_PERIOD") {
  field(INPA, "$(PREF)ACQ_DECIM_SEL.C CP")
  field(INPB, "33") # cic_base_period
  field(INPC, "94.2857142857e6") # samp/sec
  field(CALC, "B*2*A/C")
  field(PINI, "YES")
  field(FLNK, "$(PREF)PULSE_PERIOD")
}

record(calcout, "$(PREF)PULSE_PERIOD") {
  field(DESC, "WF acquisition length")
# Number of samples * sample period
  field(CALC, "A*B")
  field(INPA, "$(PREF)ACQ_NORD CP")
  field(INPB, "$(PREF)PULSE_SAMP_PERIOD")
  field(EGU,  "s")
# Set DOPT to force OCAL to be executed
  field(DOPT, "Use OCAL")
  field(OOPT, "Every Time")
  field(OCAL, "A*B*1000")
# OVAL = VAL in ms
  field(FLNK, "$(PREF)PULSE_PERIOD_FO")
}

record(dfanout, "$(PREF)PULSE_PERIOD_FO") {
  field(OMSL, "closed_loop")
  field(DOL,  "$(PREF)PULSE_PERIOD.OVAL")
  field(OUTA, "$(PREF)PULSE_OFFTIME.DRVH NPP")
# ONTIME processes OFFTIME
  field(OUTB, "$(PREF)PULSE_ONTIME.DRVH PP")
}

#==================================
# User parameters. 
# Off time = wf sample period - on time
#==================================

# Dummy PV to allow python API to access actual PV
record(longin, "$(PREF)PULSE_ONTIMEDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(NAME) reg=mbox_$(BIT)_PULSE_ONTIME")
  info(feed:info:input, "$(PREF)PULSE_ONTIME")
  info(feed:info:output, "$(PREF)PULSE_ONTIME")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(PREF)PULSE_ONTIME_HLIMDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(NAME) reg=mbox_$(BIT)_PULSE_ONTIME_HLIM")
  info(feed:info:input, "$(PREF)PULSE_ONTIME.DRVH")
  info(feed:info:output, "$(PREF)PULSE_ONTIME.DRVH")
}

record(ao, "$(PREF)PULSE_ONTIME") {
  field(DESC, "RF Pulse On Time")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(VAL,  "70")
  field(EGU,  "ms")
  field(DRVL, "0")
# DRVH set by ACQ_PERIOD. Set default DRVH to enforce DRVL
  field(DRVH, "1000")
  field(PREC, "3")
  field(FLNK, "$(PREF)PULSE_OFFTIME_CALC")
  info(autosaveFields, "VAL DRVH")
}

record(calc, "$(PREF)PULSE_OFFTIME_CALC") {
  field(DESC, "Calc RF Pulse Off Time")
  field(SCAN, "Passive")
  field(CALC, "A-B")
  field(INPA, "$(PREF)PULSE_PERIOD.OVAL")
  field(INPB, "$(PREF)PULSE_ONTIME")
  field(FLNK, "$(PREF)PULSE_OFFTIME")
}

record(ao, "$(PREF)PULSE_OFFTIME") {
  field(DESC, "RF Pulse Off Time")
  field(SCAN, "Passive")
  field(OMSL, "closed_loop")
  field(DOL,  "$(PREF)PULSE_OFFTIME_CALC")
  field(DRVL, "0")
# DRVH set by ACQ_PERIOD. Set default DRVH to enforce DRVL
  field(DRVH, "1000")
  field(EGU,  "ms")
  field(PREC, "3")
  field(FLNK, "$(PREF)PULSE_ON_DIFF")
  info(autosaveFields, "VAL DRVH")
}

# Access to CW decimation for rf_controls.py
# Dummy PV to allow python API to access actual PV
record(longin, "$(PREF)CWDECIMREQDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(NAME) reg=mbox_$(BIT)_DECIM_REQ_CW")
  info(feed:info:input, "$(PREF)ACQ_DECIM_SEL.A")
  info(feed:info:output, "$(PREF)ACQ_DECIM_SEL.A")
}

# Access to pulsed decimation for rf_controls.py
# Dummy PV to allow python API to access actual PV
record(longin, "$(PREF)PULSEDDECIMREQDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(NAME) reg=mbox_$(BIT)_DECIM_REQ_PULSED")
  info(feed:info:input, "$(PREF)ACQ_DECIM_SEL.C")
  info(feed:info:output, "$(PREF)ACQ_DECIM_SEL.C")
}

#==================================
# Records set by rf_pulse.py
# Status and values currently/last 
# in use.
#==================================
# Dummy PV to allow python API to access actual PV
record(longin, "$(PREF)PULSE_STATUSDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(NAME) reg=mbox_$(BIT)_PULSE_STATUS")
  info(feed:info:input, "$(PREF)PULSE_STATUS")
  info(feed:info:output, "$(PREF)PULSE_STATUS")
}

record(mbbi, "$(PREF)PULSE_STATUS") {
    field(ZRST, "Awaiting request")
    field(ONST, "Pulse request")
    field(TWST, "Pulsing on")
    field(THST, "Stop request")
    field(FRST, "Pulsing off")
    field(ZRVL, "0")
    field(ONVL, "1")
    field(ONSV, "MINOR")
    field(TWVL, "2")
    field(TWSV, "MINOR")
    field(THVL, "3")
    field(THSV, "MINOR")
    field(FRVL, "4")
    field(PINI, "YES")
    info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(PREF)PULSE_INUSE_ONDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(NAME) reg=mbox_$(BIT)_PULSE_INUSE_ON")
  info(feed:info:input, "$(PREF)PULSE_INUSE_ON")
  info(feed:info:output, "$(PREF)PULSE_INUSE_ON")
}

record(ao, "$(PREF)PULSE_INUSE_ON") {
  field(DESC, "In-use RF Pulse On Time")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(EGU,  "ms")
  field(PREC, "3")
  field(PINI, "YES")
  field(FLNK, "$(PREF)PULSE_ON_DIFF")
  info(autosaveFields, "VAL")
}
record(ao, "$(PREF)PULSE_INUSE_OFF") {
  field(DESC, "In-use RF Pulse Off Time")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(EGU,  "ms")
  field(PREC, "3")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(PREF)PULSE_INUSE_ASETDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(NAME) reg=mbox_$(BIT)_PULSE_INUSE_ASET")
  info(feed:info:input, "$(PREF)PULSE_INUSE_ASET")
  info(feed:info:output, "$(PREF)PULSE_INUSE_ASET")
}

record(ao, "$(PREF)PULSE_INUSE_ASET") {
  field(DESC, "In-use RF Pulse ADES")
  field(SCAN, "Passive")
  field(OMSL, "supervisory")
  field(EGU,  "%")
  field(PREC, "3")
  field(PINI, "YES")
  field(FLNK, "$(PREF)PULSE_ADES_DIFF")
  info(autosaveFields, "VAL")
}

record(calc, "$(PREF)PULSE_ON_DIFF") {
  field(DESC, "Diff latest and desired on time")
  field(SCAN, "Passive")
  field(CALC, "A=B?0:1")
  field(INPA, "$(PREF)PULSE_INUSE_ON")
  field(INPB, "$(PREF)PULSE_ONTIME")
  field(HIGH, "1")
  field(HSV,  "MINOR")
  field(PINI, "YES")
}

record(calc, "$(PREF)PULSE_ADES_DIFF") {
  field(DESC, "Diff latest and desired ADES")
  field(SCAN, "Passive")
  field(CALC, "A=B?0:1")
  field(INPA, "$(PREF)PULSE_INUSE_ASET")
  field(INPB, "$(PREF)SEL_ASET CP")
  field(HIGH, "1")
  field(HSV,  "MINOR")
  field(PINI, "YES")
}

record(calc, "$(PREF)PULSE_DIFF_SUM") {
  field(DESC, "Pulse parms need updating")
  field(SCAN, "1 second")
  field(CALC, "C#4?0:D#1?0:A?1:B?1:0")
  field(INPA, "$(PREF)PULSE_ON_DIFF")
  field(INPB, "$(PREF)PULSE_ADES_DIFF")
  field(INPC, "$(PREF)RFMODECTRL")
  field(INPD, "$(PREF)RFCTRL")
  field(HIGH, "1")
  field(HSV,  "MINOR")
}

record(stringin, "$(PREF)PULSE_MSG") {
  field(DESC, "Pulse message")
  field(SCAN, "Passive")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

# Intermediate record, as put to busy record results in CA timeout
record(bo, "$(PREF)SETPULSEONSTRT_") {
  field(DESC, "Internal pulse on control")
  field(FLNK, "$(PREF)PULSEONSTRT")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(PREF)PULSEONSTRTDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(NAME) reg=mbox_$(BIT)_PULSE_ONSTRT")
  info(feed:info:input, "$(PREF)SETPULSEONSTRT_")
  info(feed:info:output, "$(PREF)SETPULSEONSTRT_")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(PREF)PULSEOFFSTRTDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(NAME) reg=mbox_$(BIT)_PULSEOFFSTRT")
  info(feed:info:input, "$(PREF)PULSEOFFSTRT")
  info(feed:info:output, "$(PREF)PULSEOFFSTRT")
}

#==================================
# Support graphic for pulse set up
#==================================
record(aSub, "$(PREF)PULSESUB") {
  field(DESC, "Pulse display graphic")
  field(INAM, "")
  field(SNAM, "RF Pulse Diag")
  field(SCAN, "Passive")
  field(FTA, "SHORT") 
  field(NOA, "1")
  field(INPA, "$(PREF)PULSE_STATUS")
  field(FTB, "DOUBLE") 
  field(NOB, "1")
  field(INPB, "$(PREF)PULSE_PERIOD.OVAL CP")
  field(FTC, "DOUBLE") 
  field(NOC, "1")
  field(INPC, "$(PREF)SEL_ASET CP")
  field(FTD, "DOUBLE") 
  field(NOD, "1")
  field(INPD, "$(PREF)PULSE_INUSE_ASET CP")
  field(FTE, "DOUBLE") 
  field(NOE, "1")
  field(INPE, "$(PREF)PULSE_ONTIME")
  field(FTF, "DOUBLE") 
  field(NOF, "1")
  field(INPF, "$(PREF)PULSE_INUSE_ON CP")
  field(FTG, "DOUBLE") 
# Process when OFFTIME updates (not ONTIME) so will
# get consistent set of on/off values
  field(NOG, "1")
  field(INPG, "$(PREF)PULSE_OFFTIME CP")
  field(FTH, "DOUBLE") 
  field(NOH, "1")
  field(INPH, "$(PREF)PULSE_INUSE_OFF CP")
# VALA = amplitude for pulse graphic - new 
  field(FTVA, "DOUBLE")
  field(NOVA, "5")
# VALB = amplitude for pulse graphic - in use
  field(FTVB, "DOUBLE")
  field(NOVB, "5")
# VALC = time for pulse graphic - new
  field(FTVC, "DOUBLE")
  field(NOVC, "5")
# VALD = time for pulse graphic - in use
  field(FTVD, "DOUBLE")
  field(NOVD, "5")
}
