3 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
5 # This file is part of pycomedi.
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
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.
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/>.
19 """Do 3 instructions in one system call to time a multi-sample read.
21 Do a ``gettimeofday()`` call, read some samples from an analog
22 input, and do another ``gettimeofday()`` call.
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
31 def run(filename, subdevice, channel, range, aref, num_scans):
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
37 >>> t2 # doctest: +SKIP
39 >>> data # doctest: +ELLIPSIS
40 array([...], dtype=uint32)
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)
51 subdevice = device.find_subdevice_by_type(
52 _constant.SUBDEVICE_TYPE.ai)
54 subdevice = device.subdevice(subdevice)
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)
63 device.do_insnlist(insns)
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)
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:
78 if __name__ == '__main__':
79 import pycomedi_demo_args
81 args = pycomedi_demo_args.parse_args(
83 argnames=['filename', 'subdevice', 'channel', 'aref', 'range',
84 'num-scans', 'verbose'],
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)