#!/usr/bin/env python
## an adaptation of the inp.c program bundled with the comedilib package
## the inp.c file is found in the package demo directory
## Copyright (C) January 2007 Paul Finnigan
## 
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.

## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.

"""inp.py - Python version of comedi inp demonstration

Call the program supplying parameters to read a particular input.

You can use options to override the subdevice, channel, range and aref to be used.

-f --file      Supply the device to be used default=/dev/comedi0
-s --subdevice Supply the subdevice to be used
-c --channel   Supply the channel to be used
-r --range     Supply the range to be used
-a --aref      Supply the aref to be used
-h --help      Writes this python doc string to the screen
-v --verbose   Writes a message before reading the data

The default areguments supplied are equivalent to:

inp.py -f /dev/comedi0 -s 0 -c 0 -r 0 -a 0 
"""

import comedi as c

def main():
    filename = "/dev/comedi0"
    subdevice = 0
    channel = 0
    rnge = 0
    aref = 0
    verbose = True
    # open the comedi device
    device=c.comedi_open(filename)
    if not device: raise "Error openning Comedi device"
    if verbose:
        print "measuring device=%s subdevice=%d channel=%d range=%d analog reference=%d" % (filename,subdevice,channel,rnge,aref)

    # In python the data read function returns a list
    # The first element (data[0]) is the return code
    # The data is the second element (data[1]) of the list
    data = c.comedi_data_read(device,subdevice,channel,rnge,aref)
    if data[0]<1:
        c.comedi_perror(filename)
        sys.exit(2)
    else:
        print data[1]

if __name__ == "__main__":
    main()
