#!/usr/bin/python
#
# arch-tag: 8a58313c-de66-4ef2-82d9-f1e95dc76000
# Time-stamp: <2005-01-31 18:10:42 mike>
#
# XML helper functions.
#
from elementtree import ElementTree

def sensor(type, adc, name=None, units=None, cal=[0,1]):
    """Return an XML node describing a sensor connected to
    an A/D channel. The description uses the following DTD:

    <!ELEMENT sensor (calibration)>
    <!ELEMENT calibration EMPTY>
    <!ATTLIST sensor type CDATA #REQUIRED
                     board CDATA #REQUIRED
                     channel CDATA #REQUIRED
                     name CDATA #IMPLIED>
    <!ATTLIST calibration units CDATA #REQUIRED
                          coeff CDATA #REQUIRED>
                          
    """
    sens = ElementTree.Element('sensor')
    sens.set('type', type)
    if name:
        sens.set('name', name)
    sens.set('board', str(adc.board))
    sens.set('channel', str(adc.chan))
    child = ElementTree.SubElement(sens, 'calibration')
    child.set('units', units or 'V')
    child.set('coeff', ','.join([str(c) for c in cal]))
    return sens

def switch(iop, name):
    """Return an XML node describing a power switch. The description uses
    the following DTD:

    <!ELEMENT switch EMPTY>
    <!ATTLIST switch name CDATA #REQUIRED
                     board CDATA #REQUIRED
                     port CDATA #REQUIRED
                     bit CDATA #REQUIRED>

    """
    sw = ElementTree.Element('switch')
    sw.set('name', name)
    sw.set('board', str(iop.board))
    sw.set('port', str(iop.port))
    sw.set('bit', str(iop.bit))
    return sw
