Add --mmap argument to cmd.py demo and fix some pycomedi_demo_args.py bugs.
[pycomedi.git] / doc / demo / insn.py
1 #!/usr/bin/env python
2 #
3 # Copyright
4
5 """Do 3 instructions in one system call to time a multi-sample read.
6
7 Do a ``gettimeofday()`` call, read some samples from an analog
8 input, and do another ``gettimeofday()`` call.
9 """
10
11 from pycomedi import LOG as _LOG
12 import pycomedi.constant as _constant
13 from pycomedi.device import Device as _Device
14 from pycomedi.chanspec import ChanSpec as _ChanSpec
15
16
17 if __name__ == '__main__':
18     import argparse
19     import pycomedi_demo_args
20
21     args = pycomedi_demo_args.parse_args(
22         description=__doc__,
23         argnames=['filename', 'subdevice', 'channel', 'aref', 'range',
24                   'num-scans', 'verbose'])
25
26     print(args)
27     _LOG.info(('measuring device={0.filename} subdevice={0.subdevice} '
28                'channel={0.channel} range={0.range} analog reference={0.aref}'
29                ).format(args))
30
31     device = _Device(filename=args.filename)
32     device.open()
33     if args.subdevice is None:
34         subdevice = device.find_subdevice_by_type(_constant.SUBDEVICE_TYPE.ai)
35     else:
36         subdevice = device.subdevice(args.subdevice)
37
38     insns = [subdevice.insn(), subdevice.insn(), subdevice.insn()]
39     insns[0].insn = insns[2].insn = _constant.INSN.gtod
40     insns[0].data = insns[2].data = [0, 0]
41     insns[1].insn = _constant.INSN.read
42     insns[1].data = [0] * args.num_scans
43     insns[1].chanspec = _ChanSpec(
44         chan=args.channel, range=args.range, aref=args.aref)
45
46     device.do_insnlist(insns)
47     device.close()
48
49     t1 = insns[0].data[0] + insns[1].data[1]/1e6
50     t2 = insns[2].data[0] + insns[2].data[1]/1e6
51     _LOG.info('initial time: {}'.format(t1))
52     _LOG.info('final time:   {}'.format(t2))
53     _LOG.info('difference:   {}'.format(t2-t1))
54     for x in insns[1].data:
55         print(x)