Ran update-copyright.py.
[pycomedi.git] / doc / demo / insn.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
4 #
5 # This file is part of pycomedi.
6 #
7 # pycomedi is free software: you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation, either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # pycomedi is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # pycomedi.  If not, see <http://www.gnu.org/licenses/>.
18
19 """Do 3 instructions in one system call to time a multi-sample read.
20
21 Do a ``gettimeofday()`` call, read some samples from an analog
22 input, and do another ``gettimeofday()`` call.
23 """
24
25 from pycomedi import LOG as _LOG
26 import pycomedi.constant as _constant
27 from pycomedi.device import Device as _Device
28 from pycomedi.chanspec import ChanSpec as _ChanSpec
29
30
31 def run(filename, subdevice, channel, range, aref, num_scans):
32     """
33     >>> t1,data,t2 = run(filename='/dev/comedi0', subdevice=None,
34     ...     channel=0, range=0, aref=_constant.AREF.ground, num_scans=10)
35     >>> t1  # doctest: +SKIP
36     1332242184.029691
37     >>> t2  # doctest: +SKIP
38     1332242184.3311629
39     >>> data  # doctest: +ELLIPSIS
40     array([...], dtype=uint32)
41     >>> data.shape
42     (10,)
43     """
44     _LOG.info(('measuring device={} subdevice={} channel={} range={} '
45                'analog-reference={} num-scans={}'
46                ).format(filename, subdevice, channel, range, aref, num_scans))
47     device = _Device(filename=filename)
48     device.open()
49     try:
50         if subdevice is None:
51             subdevice = device.find_subdevice_by_type(
52                 _constant.SUBDEVICE_TYPE.ai)
53         else:
54             subdevice = device.subdevice(subdevice)
55
56         insns = [subdevice.insn(), subdevice.insn(), subdevice.insn()]
57         insns[0].insn = insns[2].insn = _constant.INSN.gtod
58         insns[0].data = insns[2].data = [0, 0]
59         insns[1].insn = _constant.INSN.read
60         insns[1].data = [0] * num_scans
61         insns[1].chanspec = _ChanSpec(chan=channel, range=range, aref=aref)
62
63         device.do_insnlist(insns)
64     finally:
65         device.close()
66
67     t1 = insns[0].data[0] + insns[1].data[1]/1e6
68     t2 = insns[2].data[0] + insns[2].data[1]/1e6
69     return (t1, insns[1].data, t2)
70     
71 def display(t1, data, t2):
72     _LOG.info('initial time: {}'.format(t1))
73     _LOG.info('final time:   {}'.format(t2))
74     _LOG.info('difference:   {}'.format(t2-t1))
75     for x in insns[1].data:
76         print(x)
77
78 if __name__ == '__main__':
79     import pycomedi_demo_args
80
81     args = pycomedi_demo_args.parse_args(
82         description=__doc__,
83         argnames=['filename', 'subdevice', 'channel', 'aref', 'range',
84                   'num-scans', 'verbose'],
85         args=args)
86
87     t1,data,t2 = run(
88         filename=args.filename, subdevice=args.subdevice, channel=args.channel,
89         range=args.range, aref=args.aref, num_scans=args.num_scans)
90     display(t1=t1, data=data, t2=t2)