MaskedBridge.g

MaskedBridge.g — copies a data point, applying a mask and shift operation.

Code

[Note]

The code for this and other example scripts can be found in the DataHub distribution archive, typically at this location:

C:\Program Files\Cogent\Cogent DataHub\scripts\

Please refer to Section 3.1, “How to Run a Script” for more information on using scripts.

/*
 * Copy a data point to another data point, applying a mask and shift
 * operation during the copy.  The mask is applied before the shift,
 * allowing you to mask any bits and then shift them right or left.
 * A negative shift will shift left, and a positive shift will shift
 * right.  The shift indicates the number of bits to shift, where 0
 * indicates no shift.
 *
 * You can create any number of operations simply by reproducing the
 * .OnChange method call with different pairs of points.  The
 * datahub_command call is simply to ensure that the destination
 * point exists, which may not be necessary depending on your
 * application.
 */ 

require ("Application");

class MaskedBridge Application
{
}

method MaskedBridge.WriteBits (value, pointname, mask, shift)
{
    if (shift < 0)
        set(pointname, (value & mask) << -shift);
    else
        set(pointname, (value & mask) >> shift);
}

method MaskedBridge.constructor ()
{
    datahub_command("(create DataPid:TwoBits 1)", 1);
    .OnChange(#$DataPid:PID1.Mv,
	      `(@self).WriteBits(value, #$DataPid:TwoBits, 0x03, 0));
}

/* Start the program by instantiating the class. */
ApplicationSingleton (MaskedBridge);