# Temporary, until integrated in with LCLS-II alarms

# checks an incrementor PV to see if it's incrementing.
# the incrementors are updated by matlab processes.
# Joe's matlab code
# this will be 0 if old = current (i.e. not incrementing
#function running = check_running(new_string, old_string)
#   running = abs(sign(str2double(new_string)-str2double(old_string)));
#end
# =======================================================
# A has current, B has previous
record(calcout, "ALRM:SYS2:RFDMN1:SCAN") {
  field(CALC, "A=B?0:A+1")
  field(SCAN, "1 second")
  field(INPA, "ALRM:SYS2:RFDMN1:SCAN.VAL")
  field(INPB, "1")
  field(OUT, "ALRM:SYS2:RFDMN1:ALHBCURR.VAL PP")
  field(DOPT, "Use CALC")
  field(OOPT, "Transition To Zero")
}

record(ai, "ALRM:SYS2:RFDMN1:ALHBCURR") {
  field(INP, "ACCL:L1B:1:RFDMN_HEARTBEAT NPP MS")
  field(SCAN, "Passive")
  field(FLNK, "ALRM:SYS2:RFDMN1:ALHBCHK")
}

record(calc, "ALRM:SYS2:RFDMN1:ALHBCHK") {
  field(CALC, "C>2?2:(A=B?1:0)")
  field(INPA, "ALRM:SYS2:RFDMN1:ALHBCURR NPP MS")
  field(INPB, "ALRM:SYS2:RFDMN1:ALHBPREV.VAL NPP MS")
  field(INPC, "ALRM:SYS2:RFDMN1:ALHBCURR.SEVR")
  field(FLNK, "ALRM:SYS2:RFDMN1:ALHBERR")
}

# MS will cause this to be invalid if the HBCHK is
record(mbbi, "ALRM:SYS2:RFDMN1:ALHBERR") {
  field(DESC, "RFDMN1 monitoring")
  field(INP, "ALRM:SYS2:RFDMN1:ALHBCHK.VAL NPP MS")
  field(ZRSV, "NO_ALARM")
  field(ONSV, "MAJOR")
  field(TWSV, "INVALID")
  field(ZRST, "RUNNING")
  field(ONST, "NOT_RUNNING")
  field(TWST, "INVALID")
  field(FLNK, "ALRM:SYS2:RFDMN1:ALHBPREV")
}

record(ai, "ALRM:SYS2:RFDMN1:ALHBPREV") {
  field(INP, "ALRM:SYS2:RFDMN1:ALHBCURR NPP NMS")
}
