#=================================================================
#
#    Auth:  9-Jul-2008, Sonya Hoobler (sonya)
#
#    Rem:  Contains records associated with individual knob box.
#          See more comments below.
#
#-----------------------------------------------------------------
#
#    Mod:  
#
#=================================================================
# We use an infinite loop to process the knobs
# on this box. Each knob has a corresponding :PROCESS
# record that does one of four things, depending on the
# value of .SELN. :STATE writes its value to .SELN each
# time it is processed. The options are:
# 
# SELN=1 Knob is newly attached. Process :ATTACH record
# which will launch all the required attach functions.
# Then :STATE is set to 2.
# 
# SELN=2 Knob is plain old attached. Request turns from knob
# box. If turns = 0, check to see if device value has
# changed since last time around. If so, write new value
# to legend. If turns != 0, calculate new device value,
# put that value to attached device.
# 
# SELN=3 Knob is newly detached. Write 6 to :DETACHSEQ,
# which will launch all the required detach functions.
# SELN  will remain 3 until all detach functions are
# complete. Then :STATE is set to 4.
# 
# SELN=4 Knob is not attached. Skip this knob and move on to
# the next.
# 
# Initialize .SELN to be 4
record(seq, "KNOB:$(LOCA):$(B):K0_PROCESS") {
  field(SELN, "4")
  field(SELM, "Specified")
  field(LNK1, "KNOB:$(LOCA):$(B):K0_ATTACH PP")
  field(LNK2, "KNOB:$(LOCA):$(B):K0_GETTURNS PP")
#  field(TPRO, "1")
  field(DOL3, "6")
  field(LNK3, "KNOB:$(LOCA):$(B):K0_DETACHSEQ PP")
  field(LNK4, "KNOB:$(LOCA):$(B):K1_PROCESS PP")
  field(PINI, "YES")
  field(FLNK, "KNOB:$(LOCA):$(B):PROCTIME")
}

# See :0:PROCESS
record(seq, "KNOB:$(LOCA):$(B):K1_PROCESS") {
  field(SELN, "4")
  field(SELM, "Specified")
  field(LNK1, "KNOB:$(LOCA):$(B):K1_ATTACH PP")
  field(LNK2, "KNOB:$(LOCA):$(B):K1_GETTURNS PP")
#  field(TPRO, "1")
  field(DOL3, "6")
  field(LNK3, "KNOB:$(LOCA):$(B):K1_DETACHSEQ PP")
  field(LNK4, "KNOB:$(LOCA):$(B):K2_PROCESS.VAL PP")
}

# See :0:PROCESS
record(seq, "KNOB:$(LOCA):$(B):K2_PROCESS") {
  field(SELN, "4")
  field(SELM, "Specified")
  field(LNK1, "KNOB:$(LOCA):$(B):K2_ATTACH PP")
  field(LNK2, "KNOB:$(LOCA):$(B):K2_GETTURNS PP")
#  field(TPRO, "1")
  field(DOL3, "6")
  field(LNK3, "KNOB:$(LOCA):$(B):K2_DETACHSEQ PP")
  field(LNK4, "KNOB:$(LOCA):$(B):K3_PROCESS.VAL PP")
}

# See :0:PROCESS
record(seq, "KNOB:$(LOCA):$(B):K3_PROCESS") {
  field(SELN, "4")
  field(SELM, "Specified")
  field(LNK1, "KNOB:$(LOCA):$(B):K3_ATTACH PP")
  field(LNK2, "KNOB:$(LOCA):$(B):K3_GETTURNS PP")
#  field(TPRO, "1")
  field(DOL3, "6")
  field(LNK3, "KNOB:$(LOCA):$(B):K3_DETACHSEQ PP")
  field(LNK4, "KNOB:$(LOCA):$(B):WAIT PP")
}

# Uses DTYP Soft Timestamp to read in Unix time in seconds.
record(stringin, "KNOB:$(LOCA):$(B):GETTIME") {
  field(DTYP, "Soft Timestamp")
#  field(TPRO, "1")
  field(INP, "@%s")
  field(SCAN, "5 second")
  field(PINI, "YES")
}

# Used to check that box infinite loop doesn't stop.
# Is processed each time knob 0 :PROCESS is. Reads
# the time from GETTTIME. Seq program box_errors uses
# this value to check that infinite loop is still alive.
record(longin, "KNOB:$(LOCA):$(B):PROCTIME") {
  field(PINI, "YES")
#  field(TPRO, "1")
  field(INP, "KNOB:$(LOCA):$(B):GETTIME")
}

# Increments each time box_errors seq program has to jumpstart the
# infinite loop.
record(calc, "KNOB:$(LOCA):$(B):ERR_LOOP") {
  field(VAL, "0")
  field(CALC, "A + 1")
  field(INPA, "KNOB:$(LOCA):$(B):ERR_LOOP.VAL")
}

# Clears all four knob legends when IOC starts up
record(bo, "KNOB:$(LOCA):$(B):CLRLGNDS") {
  field(DTYP, "stream")
  field(OUT, "@miranova.proto.db clearLegendInit(0x$(ADDR),$(LOCA)) KNOBBOX$(B)")
#  field(TPRO, "1")
  field(PINI, "YES")
}

record(calcout, "KNOB:$(LOCA):$(B):WAIT") {
  field(VAL, "1")
  field(CALC, "A+B+C+D=16?1:2")
  field(INPA, "KNOB:$(LOCA):$(B):K0_STATE ")
  field(INPB, "KNOB:$(LOCA):$(B):K1_STATE")
  field(INPC, "KNOB:$(LOCA):$(B):K2_STATE ")
  field(INPD, "KNOB:$(LOCA):$(B):K3_STATE")
  field(OUT, "KNOB:$(LOCA):$(B):WAITSEQ.SELN PP")
  field(OOPT, "Every Time")
  field(DOPT, "Use CALC")
}

record(seq, "KNOB:$(LOCA):$(B):WAITSEQ") {
  field(SELM, "Specified")
  field(DLY1, "1")
  field(LNK1, "KNOB:$(LOCA):$(B):K0_PROCESS PP")
  field(LNK2, "KNOB:$(LOCA):$(B):K0_PROCESS PP")
}

record(ai, "KNOB:$(LOCA):$(B):BOX") {
  field(VAL, "$(B)")
  field(PINI, "YES")
}
