IntToBit.g

IntToBit.g — converts an integer data point into a set of single-bit points.

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.

/*
 * Given an integer value, convert it to a set of data points where
 * each point represents a single bit in the original integer.
 *
 * Usage:
 * Modify the IntToBit.constructor to reflect the data points that
 * need to be broken into individual bits.  This consists of one or
 * more calls to
 *      .initPoint(point, format, nbits)
 *  where
 *      point - a symbol or string representing the input integer
 *              data point specify symbols with #$domain:symbol_name
 *              or as a string
 *      format - a format string defining how to create the point
 *               names for the individual bits, given the symbol name
 *               and a bit number (starting at 0). Use %a for the
 *               format specifier for input point name
 *      nbits - the number of bits to extract from the input,
 *              starting at the LSB
 */ 

require ("Application");

class IntToBit Application
{
}

/* Add any data points that you want to split into bits here */
method IntToBit.constructor ()
{
    .initPoint(#$default:test, "%a_%d", 8);
    .initPoint("default:test2", "%a_%02d", 16);
    .initPoint(format("default:test%d", 3), "%a_%02d", 32);
}

/* ------- Implementation: No need to change beyond here ---------- */

/* A callback that runs whenever the input integer changes value */
method IntToBit.processNewValue (inputsym, outputformat, value, nbits)
{
    if (string_p(inputsym))
        inputsym = symbol(inputsym);

    local bitsym, bitvalue, i;
    local valueinfo = PointMetadata(inputsym);
    
    if (valueinfo)
    {
        for (i=0; i<nbits; i++)
        {
            bitsym = format(outputformat, inputsym, i);
            bitvalue = (value >> i) % 2;
            datahub_write(bitsym, bitvalue, nil, valueinfo.quality,
			  valueinfo.timestamp);
        }
    }
}

/* Set up the event handler and initial state for the output points */
method IntToBit.initPoint (inputsym, outputformat, nbits)
{
    local bitsym, i, curvalue;

    if (string_p(inputsym))
        inputsym = symbol(inputsym);

    for (i=0; i<nbits; i++)
    {
        bitsym = format(outputformat, inputsym, i);
        datahub_command (format("(create %s 1)", stringc(bitsym)), 1);
        datahub_command (format("(set_canonical %s BOOL)",
				stringc(bitsym)), 1);
    }
    .OnChange(inputsym, `(@self).processNewValue(this, @outputformat,
						 value, @nbits));
    
    if (!undefined_p(curvalue = eval(inputsym)) && number_p(curvalue))
    {
        .processNewValue(inputsym, outputformat, curvalue, nbits);
    }
}

/* Any code to be run when the program gets shut down. */
method IntToBit.destructor ()
{
}

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