From 9f0359e7c888f7ba73f9067c159fa303d68beb79 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 26 Apr 2012 13:21:42 -0400 Subject: [PATCH] Add --callback option to cmd.py demo to use CallbackReader. --- doc/demo/cmd.py | 43 +++++++++++++++++++++++++++------- doc/demo/pycomedi_demo_args.py | 7 ++++++ 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/doc/demo/cmd.py b/doc/demo/cmd.py index b8508e9..3d57d03 100755 --- a/doc/demo/cmd.py +++ b/doc/demo/cmd.py @@ -91,6 +91,19 @@ def write_data(stream, channels, data, physical=False): stream.write('\t'.join(str(x) for x in data[row,:])) stream.write('\n') +class DataWriter (object): + def __init__(self, stream, channels, physical=False): + self.stream = stream + self.channels = channels + self.physical = physical + + def __call__(self, data): + d = _numpy.ndarray(shape=(1,data.size), dtype=data.dtype, buffer=data) + write_data( + stream=self.stream, channels=self.channels, data=d, + physical=self.physical) + + def read(device, subdevice=None, channels=[0], range=0, aref=0, period=0, num_scans=2, reader=_utility.Reader, physical=False, stream=_sys.stdout): @@ -103,10 +116,19 @@ def read(device, subdevice=None, channels=[0], range=0, aref=0, period=0, subdevice=subdevice, channels=channels, period=period, num_scans=num_scans) rc = test_command(subdevice=subdevice) - read_buffer = _numpy.zeros( - (num_scans, len(channels)), - dtype=subdevice.get_dtype()) - reader = reader(subdevice=subdevice, buffer=read_buffer, name='Reader') + kwargs = {} + if reader == _utility.CallbackReader: + read_buffer_shape = (len(channels),) + kwargs = { + 'callback': DataWriter( + stream=stream, channels=channels, physical=physical), + 'count': num_scans, + } + else: + read_buffer_shape = (num_scans, len(channels)) + read_buffer = _numpy.zeros(read_buffer_shape, dtype=subdevice.get_dtype()) + reader = reader( + subdevice=subdevice, buffer=read_buffer, name='Reader', **kwargs) start = _time.time() _LOG.info('start time: {}'.format(start)) subdevice.command() @@ -115,8 +137,10 @@ def read(device, subdevice=None, channels=[0], range=0, aref=0, period=0, stop = _time.time() _LOG.info('stop time: {}'.format(stop)) _LOG.info('time: {}'.format(stop - start)) - write_data( - stream=stream, channels=channels, data=read_buffer, physical=physical) + if not isinstance(reader, _utility.CallbackReader): + write_data( + stream=stream, channels=channels, data=read_buffer, + physical=physical) def run(filename, **kwargs): @@ -149,14 +173,17 @@ if __name__ == '__main__': args = pycomedi_demo_args.parse_args( description=__doc__, argnames=['filename', 'subdevice', 'channels', 'aref', 'range', - 'num-scans', 'mmap', 'frequency', 'physical', 'verbose']) + 'num-scans', 'mmap', 'callback', '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: + if args.callback: + reader = _utility.CallbackReader + elif args.mmap: reader = _utility.MMapReader else: reader = _utility.Reader diff --git a/doc/demo/pycomedi_demo_args.py b/doc/demo/pycomedi_demo_args.py index 4b59b31..5a23c12 100644 --- a/doc/demo/pycomedi_demo_args.py +++ b/doc/demo/pycomedi_demo_args.py @@ -90,6 +90,13 @@ ARGUMENTS = { 'const':True, 'help':('use a memory-mapped reader/writer rather than ' 'reading/writing the input/output subdevice directly')}), + 'callback':( + ['--callback'], + {'default':False, + 'action':'store_const', + 'const':True, + 'help':('use a callback reader/writer rather than ' + 'reading/writing the input/output subdevice directly')}), 'verbose':( ['-v', '--verbose'], {'action':_IncrementVerbosityAction, -- 2.26.2