# Asyn Diagnostic PVs
record(asyn,"$(BL)GammaAsynControl") {
    field(DTYP, "asynRecordDevice")
    field(PORT, "$(PORT)")
}

record(stringin, "$(BL)GammaString") {
  field(DESC, "$(BL) string output")
  field(DTYP, "stream")
  field(SCAN, "I/O Intr")
  field(INP,  "@$(proto) debug $(PORT)")
}

# Processing this pv allows the profiles to be reloaded
# after a change so that the ioc doesn't have to be rebooted.
# all profiles will be reloaded, and there may be some alarms
# that ocurr, for stream devices, during the reload process.
record(sub,   "$(BL)SWReload") {
  field(DESC, "$(BL) Stream Reload")
  field(SNAM, "streamReload")
}

# HPI 6032 Operations Manual 
# https://slacspace.slac.stanford.edu/sites/controls/specialprojects/Shared%20Documents/KTL/PPS/HPI-6032%20Manual.pdf
#
# Serial Output Setting (RS485)
# The settings are:
# 9600 Baud
# 8 Bits
# 1 Stop Bit 
# No Parity
#
# Two-wire connection 
# Termination by data
#
# HPI 6032 Operations Manual
# https://slacspace.slac.stanford.edu/sites/controls/specialprojects/Shared%20Documents/KTL/PPS/HPI-6032%20Manual.pdf
#
# Radiation Moniitor send out a packet every second, over an RS485 serial connection. 
# The packet of data should look like the example listed below:
#
#  000001 0064 0000 0032 00 00 
#
# The data from left to right is:
#
# Group Config   Digits         Description
#   1   XXXXXX   6 hex digits   Level per second from detector. The same value as on the
#                               display. Upper bit is set if level is negative.
#   2   XXXX     4 hex digits   Alarm level
#   3   XXXX     4 hex digits   Low Level Alarm
#   4   XXXX     4 hex digits   This is the fail. This starts as the fail number and counts down to
#                               zero. At zero it shows FAILURE in the display. Upper bit always 00.
#   5   XX       1 hex digit    This is the 1st status byte. See below for a description.
#   6   XX       1 hex digit    This is the 2nd status byte. See below for a description.
#
# Decimal points are omitted from the output. They are the same as in display. For example a display of
# 123.45 will have an output of 12345.
# 1) The first 6 hex digits are the radiation level. No decimal point but one is implied at: XXX.XX. The highest bit is
#    set if the level is negative. 800001 represents: -0.01 mrem/h
# 2) The next group of 4 hex digits is the alarm level with the same decimal point as 1) above; 03E8 represents an
#    alarm level of 10.00 mrem/h.
# 3) The next group of 4 hex digits is the low level alarm. To calculate the value of the setting, subtract the zero
#    offset from the low level trip. If the value was 0014, its decimal equivalent is 20. If the Zero Offset is 50 then 20-
#    50 = -30. The â30 has the same decimal point as 1) above and would be â0.30 mrem/h.
# 4) The next group of 4 digits is the fail counter. It is only the lower byte. The upper byte is always set to 00. When
#    the instrument detects a failure it starts counting down and when it reaches zero it will activate the failure mode. If
#    you log this value you can see it decrease if it detects a failure but if the failure goes away during the count down
#    period it means that the reason for the failure went away. 003C represents a fail counter setting of 60 seconds.
#
# This group of 4 hex digits is the alarm level where
# 0x03E8(hex)=1000(dec), represents an alarm level of 10.00 mrem/h,
# using the formula: 
#    if data<32768 then 
#      alarm level=32768*0.01=10.0mrem/h
#    else  
#      alarm level(mrem/hr)= 0.01*(32768-data)=
# where, data=1000(dec)=0x3e8(hex) 
#
record(calcout, "$(BL)GammaDoseRate") {
  field(DESC, "gamma dose rate")
  field(DTYP, "stream")
  field(OUT,  "@$(proto) read($(BL)) $(PORT)")
  field(SCAN, "I/O Intr")
  field(CALC, "A<8388608 ? 0.01*A : 0.01*(8388608-A)")
# .A is filled in by $(proto)
# SDIS is for alarm propogation only
  field(SDIS, "$(BL)GammaCheckIO CP MS")
  field(EGU,  "mR/hr")
  field(PREC, "2")
  field(FLNK, "$(BL)GammaDoseFanout")
}

record(calcout, "$(BL)GammaTripLevel") { 
  field(DESC, "gamma trip level")
  field(CALC, "A*B")
# .A is filled in by $(proto)
# SDIS is for alarm propogation only
  field(SDIS, "$(BL)GammaCheckIO CP MS")
  field(INPB, "0.01")
  field(EGU,  "mR/hr")
  field(PREC, "2")
}

# The is the low level alarm. Converting the raw data:
# zero offset - data
# ex) where, data is 0x0014(hex)=20(dec) and zero offset is 50
#    trip = (20-50)*0.01=-0.30 mrem/h
#
record(calcout, "$(BL)GammaTripLowLevel") { 
  field(DESC, "low trip level ")
  field(CALC, "(A*B)-C")
# .A is filled in by $(proto)
# SDIS is for alarm propogation only
  field(SDIS, "$(BL)GammaCheckIO CP MS")
  field(INPB, "0.01")
  field(INPC, "$(BL)GammaOffset")
  field(EGU,  "mR/hr")
  field(PREC, "2")
}

# This group of 4-digits is the fail counter. It is only the lower byte. 
# The upper byte is always set to 00. When the instrument detects a
# failure it starts counting down and when it reaches zero it will activate 
# the failure mode. If you log this value you can see it decrease if it 
# detects a failure but if the  failure goes away during the count down 
# period it means that the reason for the failure went away. 003C represents
# a fail counter setting of 60 seconds.
record(calcout,"$(BL)GammaFailCounter")  {
  field(DESC, "HPI Fail Counter")
  field(CALC, "A&255")
# .A is filled in by $(proto)
# SDIS is for alarm propogation only
  field(SDIS, "$(BL)GammaCheckIO CP MS")
  field(EGU,  "sec")
}

# Note: must have updated hpi manual V1.31 Rev.C March 2017
# Status Byte #1 BIT FUNCTION
# Bit 0: 0=positive radiation level, 1=negative radiation level
# Bit 1: 0=no overflow, 1=radiation level > 1000 mrem/h
# Bit 2: 0=no alarm trip Hi, 1=alarm Hi has tripped
# Bit 3: not used 
# Bit 4: 0=radiation level below trip level, 1=radiation level above trip level
#
record(calcout, "$(BL)GammaDoseStatus") { 
  field(DESC, "gamma status byte 1")
  field(CALC, "A&23")
# .A is filled in by $(proto)
# SDIS is for alarm propogation only
  field(SDIS, "$(BL)GammaCheckIO CP MS")
}

# Status Byte #2 BIT FUNCTION
# Bit 0: 0=have HV on detector, 1=no HV on detector
# Bit 1: 0=HV pulse on detector is working, 1=no HV pulse on detector (failure)
# Bit 2: 0=no low level alarm, 1=low level alarm has tripped
# Bit 3: 0=latch for alarm trip turned off, 1=latch for alarm trip turned on
# Bit 4: 0=no failure, 1=failure
#
record(calcout, "$(BL)GammaHVStatus") { 
  field(DESC, "gamma status byte 2")
  field(CALC, "A&31")
# .A is filled in by $(proto)
# SDIS is for alarm propogation only
  field(SDIS, "$(BL)GammaCheckIO CP MS")
  field(FLNK, "$(BL)GammaStatusFanout")
}

