From 160773412e8ae5558c8ceaf9823bd40ca19b1188 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 19 Mar 2012 21:16:06 -0400 Subject: [PATCH] Add --mmap argument to cmd.py demo and fix some pycomedi_demo_args.py bugs. --- doc/demo/cmd.py | 17 +++++++++++------ doc/demo/insn.py | 5 +++-- doc/demo/pycomedi_demo_args.py | 14 +++++++++++--- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/doc/demo/cmd.py b/doc/demo/cmd.py index 3ee20fd..0ea1e5a 100755 --- a/doc/demo/cmd.py +++ b/doc/demo/cmd.py @@ -22,7 +22,7 @@ from pycomedi.device import Device as _Device from pycomedi.subdevice import StreamingSubdevice as _StreamingSubdevice from pycomedi.channel import AnalogChannel as _AnalogChannel from pycomedi.chanspec import ChanSpec as _ChanSpec -from pycomedi.utility import Reader as _Reader +import pycomedi.utility as _utility def open_channels(device, subdevice, channels, range, aref): @@ -76,7 +76,7 @@ def print_data(channels, data, physical=False): print '\t'.join(str(x) for x in data[row,:]) def read(device, subdevice=None, channels=[0], range=0, aref=0, period=0, - num_scans=2, physical=False): + num_scans=2, reader=_utility.Reader, physical=False): """Read ``num_scans`` samples from each specified channel. """ subdevice,channels = open_channels( @@ -89,7 +89,7 @@ def read(device, subdevice=None, channels=[0], range=0, aref=0, period=0, read_buffer = _numpy.zeros( (num_scans, len(channels)), dtype=subdevice.get_dtype()) - reader = _Reader(subdevice, read_buffer) + reader = reader(subdevice=subdevice, buffer=read_buffer, name='Reader') start = _time.time() _LOG.info('start time: {}'.format(start)) subdevice.command() @@ -106,20 +106,25 @@ if __name__ == '__main__': args = pycomedi_demo_args.parse_args( description=__doc__, - argnames=['filename', 'subdevice', 'channels', 'aref', 'range', 'num-scans', - 'frequency', 'physical', 'verbose']) + argnames=['filename', 'subdevice', 'channels', 'aref', 'range', + 'num-scans', 'mmap', 'frequency', 'physical', 'verbose']) _LOG.info(('measuring device={0.filename} subdevice={0.subdevice} ' 'channels={0.channels} range={0.range} ' 'analog-reference={0.aref}' ).format(args)) + if args.mmap: + reader = _utility.MMapReader + else: + reader = _utility.Reader + device = _Device(filename=args.filename) device.open() try: read( device=device, subdevice=args.subdevice, channels=args.channels, range=args.range, aref=args.aref, period=args.period, - num_scans=args.num_scans, physical=args.physical) + num_scans=args.num_scans, reader=reader, physical=args.physical) finally: device.close() diff --git a/doc/demo/insn.py b/doc/demo/insn.py index 52133cf..8d84d54 100755 --- a/doc/demo/insn.py +++ b/doc/demo/insn.py @@ -20,9 +20,10 @@ if __name__ == '__main__': args = pycomedi_demo_args.parse_args( description=__doc__, - argnames=['filename', 'subdevice', 'channel', 'aref', 'range', 'num-scans', - 'verbose']) + argnames=['filename', 'subdevice', 'channel', 'aref', 'range', + 'num-scans', 'verbose']) + print(args) _LOG.info(('measuring device={0.filename} subdevice={0.subdevice} ' 'channel={0.channel} range={0.range} analog reference={0.aref}' ).format(args)) diff --git a/doc/demo/pycomedi_demo_args.py b/doc/demo/pycomedi_demo_args.py index 83f9f5d..cfa0d89 100644 --- a/doc/demo/pycomedi_demo_args.py +++ b/doc/demo/pycomedi_demo_args.py @@ -69,6 +69,12 @@ ARGUMENTS = { 'action':'store_const', 'const':True, 'help':'convert input to physical values before printing'}), + 'mmap':( + ['--mmap'], + {'default':False, + 'action':'store_const', + 'const':True, + 'help':'use a memory-mapped reader rather than reading the input subdevice directly'}), 'verbose':( ['-v', '--verbose'], {'action':_IncrementVerbosityAction}), @@ -76,8 +82,10 @@ ARGUMENTS = { def parse_args(description, argnames): parser = _argparse.ArgumentParser(description=description) - for argument in ['filename', 'subdevice', 'channels', 'aref', 'range', - 'num-scans', 'frequency', 'physical', 'verbose']: + for argument in argnames: args,kwargs = ARGUMENTS[argument] parser.add_argument(*args, **kwargs) - return parser.parse_args() + args = parser.parse_args() + if 'frequency' in argnames and not hasattr(args, 'period'): + args.period = 0 + return args -- 2.26.2