##############################################################
#
# A record to sit along side each motor record and give direct
# access to the driver without any motor record logic
# may be used with virtual and real motors
#
# This template requires that all real motors mentioned have
# a real underlying motor record (to determine MRES) this is
# not required for virtual motors
#
# This currently only supports demand but could be extended
# to totally replace motor record (perhaps)
#
##############################################################

# % macro, PORT, The asyn port (motor controller or CS controller)
# % macro, ADDR, The asyn address (real or CS axis number)
# % macro, DESC, Description
# % macro, PMAC, PV Prefix for the motor controller
# % macro, P, PV Prefix for direct motors
# % macro, M, Motor PV suffix ('Mx' for real and 'CSx:My' for CS axes)

##############################################################
# Set immediate demand for this motor
##############################################################
#% archiver 0.5 Monitor
record(ao, "$(P)$(M):DirectDemand")
{
    field(FLNK, "$(P)$(M):DirectCountsCalc.PROC")
    field(PREC, 4)
}

record(scalcout, "$(P)$(M):DirectCountsCalc")
{
    field(INPA, "$(P)$(M):DirectDemand")
    field(INPB, "$(P)$(M):Mres PP")
    field(INPC, "$(P)$(M):Offset PP")
    field(INPD, "$(P)$(M):SCALE")
    field(CALC, "(A-C)/B*D")
    field(OUT, "$(P)$(M):RawDirectDemand PP")
}

record(ao, "$(P)$(M):RawDirectDemand")
{
    field(DTYP, "asynFloat64")
    field(FLNK, "$(P)$(M):DirectMotorSeq")
    field(OUT,  "@asyn($(PORT),$(ADDR),4)MOTOR_MOVE_ABS")
    field(DESC, "$(DESC)")
}

# busy record to make ca_put_callback return only once the motor is done moving
record(seq, "$(P)$(M):DirectMotorSeq")
{
    field(LNK1, "$(P)$(M):DirectMotorBusy PP")
    field(DO1, "1")
}

record(busy, "$(P)$(M):DirectMotorBusy")
{
   field(DTYP, "asynInt32")
   field(OUT, "@asyn($(PORT),$(ADDR),4)MOTOR_STATUS_MOVING")
   field(ZNAM, "Idle")
   field(ONAM, "Moving")
   field(PINI, "1")
   field(VAL,  "0")
}


# record that holds the SCALE
# this overrides the definition in basic_asyn_motor.template and connects it to the
# asyn parameter that tracks the scale factor in the driver. This is set by calling
# pmacSetAxisScale (for CS axes this function is exposed to startup via
# pmacSetCoordStepsPerUnit)

record(ao, "$(P)$(M):SCALE") {
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),$(ADDR),4)PMAC_MOTOR_SCALE")
}

##############################################################
# Database logic to set the correct ASLO and OFF on
# DirectDemand so that it can be commanded in EGUs
#
# The Mres and Offset are copied into ALSO and OFF from
# the appropriate real motor determined by DirectMotorNumber.
#
# For CS motors DirectMotorNumber is set by the driver when it
# maps a CS motor directly to a real motor.
#
# If as CS motor is not directly mapped then it wants an
# mres of 1 but the scaling means this usually translates
# to 0.0001.
#
# For real motors DirectMotorNumber is always reported as the
# motor's own number
#
# The calculation in MresSource handles linking in the
# correct real_motor mres or the value 1 for kinematic CS axes
#
##############################################################
record(longin, "$(P)$(M):DirectMotorNumber")
{
    field(PINI, 1)
    field(VAL, 0)
    field(DTYP, "asynInt32")
    field(SCAN, "I/O Intr")
    field(INP,  "@asyn($(PORT),$(ADDR),4)PMAC_REAL_MOTOR_NUMBER")
    field(FLNK, "$(P)$(M):MresSource")
}

record(scalcout, $(P)$(M):MresSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "$(P)$(M):DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(PMAC):M%d.MRES CPP',A)):1")
    field(OUT, "$(P)$(M):Mres.INPA CA")
    field(FLNK, "$(P)$(M):OffsetSource")
}

record(scalcout, "$(P)$(M):Mres")
{
    field(INPA, "1") # to be overwritten by MresSource
    field(CALC, "A")
    field(VAL, 1)
    field(PINI, "YES")
    field(OUT,  "$(P)$(M):MresDrv PP")
    field(PREC, 6)
}

record(ao,"$(P)$(M):MresDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),$(ADDR),4)PMAC_MRES")
}

# todo non zero offset in CS kinematic axes no supported
record(scalcout, $(P)$(M):OffsetSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "$(P)$(M):DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(PMAC):M%d.OFF CPP',A)):0")
    field(OUT, "$(P)$(M):Offset.INPA CA")
}


record(scalcout, $(P)$(M):Offset)
{
    field(INPA, "0") # to be overwritten by OffsetSource
    field(CALC, "A")
    field(PINI, "YES")
    field(OUT,  "$(P)$(M):OffsetDrv PP")
    field(PREC, 4)
}


record(ao,"$(P)$(M):OffsetDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),$(ADDR),4)PMAC_OFFSET")
}