Based on David A. Schleef's `comedilib/demo/cmd.c`.
"""
-import logging as _logging
import sys as _sys
import time as _time
if __name__ == '__main__':
- import argparse
-
- parser = argparse.ArgumentParser(description=__doc__)
- parser.add_argument(
- '-f', '--filename', default='/dev/comedi0',
- help='path to comedi device file')
- parser.add_argument(
- '-s', '--subdevice', type=int, help='subdevice for analog input')
- parser.add_argument(
- '-c', '--channel', type=int, action='append',
- help='add a channel for analog input')
- parser.add_argument(
- '-a', '--analog-reference', dest='aref', default='ground',
- choices=['diff', 'ground', 'other', 'common'],
- help='reference for analog input')
- parser.add_argument(
- '-r', '--range', type=int, default=0, help='range for analog input')
- parser.add_argument(
- '-N', '--num-scans', type=int, default=10,
- help='number of analog input scans')
- parser.add_argument(
- '-F', '--frequency', type=float, help='scan frequency in hertz')
- parser.add_argument(
- '-p', '--physical', default=False, action='store_const', const=True,
- help='convert input to physical values before printing')
- parser.add_argument(
- '-v', '--verbose', default=0, action='count')
-
- args = parser.parse_args()
-
- if args.verbose >= 3:
- _LOG.setLevel(_logging.DEBUG)
- elif args.verbose >= 2:
- _LOG.setLevel(_logging.INFO)
- elif args.verbose >= 1:
- _LOG.setLevel(_logging.WARN)
+ import pycomedi_demo_args
+
+ args = pycomedi_demo_args.parse_args(
+ description=__doc__,
+ argnames=['filename', 'subdevice', 'channels', 'aref', 'range', 'num-scans',
+ 'frequency', 'physical', 'verbose'])
_LOG.info(('measuring device={0.filename} subdevice={0.subdevice} '
- 'channel={0.channel} range={0.range} analog reference={0.aref}'
+ 'channels={0.channels} range={0.range} '
+ 'analog-reference={0.aref}'
).format(args))
- channel_indexes = args.channel
- if not channel_indexes:
- channel_indexes = [0] # user gave no channels on the command line
- aref = _constant.AREF.index_by_name(args.aref)
- if args.frequency is None:
- period = 0
- else:
- period = 1/args.frequency
-
device = _Device(filename=args.filename)
device.open()
try:
read(
- device=device, subdevice=args.subdevice,
- channels=channel_indexes, range=args.range, aref=aref,
- period=period, num_scans=args.num_scans, physical=args.physical)
+ 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)
finally:
device.close()
if __name__ == '__main__':
- import argparse
+ import pycomedi_demo_args
- parser = argparse.ArgumentParser(description=__doc__)
- parser.add_argument(
- '-f', '--filename', default='/dev/comedi0',
- help='path to comedi device file')
-
- args = parser.parse_args()
+ args = pycomedi_demo_args.parse_args(description=__doc__, argnames=['filename'])
device = _Device(filename=args.filename)
device.open()
input, and do another ``gettimeofday()`` call.
"""
-import logging as _logging
-
from pycomedi import LOG as _LOG
import pycomedi.constant as _constant
from pycomedi.device import Device as _Device
if __name__ == '__main__':
import argparse
+ import pycomedi_demo_args
- parser = argparse.ArgumentParser(description=__doc__)
- parser.add_argument(
- '-f', '--filename', default='/dev/comedi0',
- help='path to comedi device file')
- parser.add_argument(
- '-s', '--subdevice', type=int, help='subdevice for analog input')
- parser.add_argument(
- '-c', '--channel', type=int, default=0,
- help='channel for analog input')
- parser.add_argument(
- '-a', '--analog-reference', dest='aref', default='ground',
- choices=['diff', 'ground', 'other', 'common'],
- help='reference for analog input')
- parser.add_argument(
- '-r', '--range', type=int, default=0, help='range for analog input')
- parser.add_argument(
- '-N', '--num-scans', type=int, default=10,
- help='number of analog input scans')
- parser.add_argument(
- '-v', '--verbose', default=0, action='count')
-
- args = parser.parse_args()
-
- if args.verbose >= 3:
- _LOG.setLevel(_logging.DEBUG)
- elif args.verbose >= 2:
- _LOG.setLevel(_logging.INFO)
- elif args.verbose >= 1:
- _LOG.setLevel(_logging.WARN)
+ args = pycomedi_demo_args.parse_args(
+ description=__doc__,
+ argnames=['filename', 'subdevice', 'channel', 'aref', 'range', 'num-scans',
+ 'verbose'])
_LOG.info(('measuring device={0.filename} subdevice={0.subdevice} '
'channel={0.channel} range={0.range} analog reference={0.aref}'
insns[0].data = insns[2].data = [0, 0]
insns[1].insn = _constant.INSN.read
insns[1].data = [0] * args.num_scans
- aref = _constant.AREF.index_by_name(args.aref)
insns[1].chanspec = _ChanSpec(
- chan=args.channel, range=args.range, aref=aref)
+ chan=args.channel, range=args.range, aref=args.aref)
device.do_insnlist(insns)
device.close()
t1 = insns[0].data[0] + insns[1].data[1]/1e6
t2 = insns[2].data[0] + insns[2].data[1]/1e6
- print('initial time: {}'.format(t1))
- print('final time: {}'.format(t2))
- print('difference: {}'.format(t2-t1))
- print('data:')
+ _LOG.info('initial time: {}'.format(t1))
+ _LOG.info('final time: {}'.format(t2))
+ _LOG.info('difference: {}'.format(t2-t1))
for x in insns[1].data:
print(x)
--- /dev/null
+# Copyright
+
+import argparse as _argparse
+import logging as _logging
+
+from pycomedi import LOG as _LOG
+import pycomedi.constant as _constant
+
+
+class _SetFrequencyAction (_argparse.Action):
+ def __call__(self, parser, namespace, values, option_string=None):
+ setattr(namespace, self.dest, values)
+ setattr(namespace, 'period', 1.0/values)
+
+
+class _IncrementVerbosityAction (_argparse.Action):
+ def __call__(self, parser, namespace, values, option_string=None):
+ level = _LOG.level
+ level -= 10 # e.g. logging.INFO -> logging.DEBUG
+ if level >= _logging.DEBUG:
+ _LOG.setLevel(level)
+
+
+# name -> (args, kwargs)
+ARGUMENTS = {
+ 'filename':(
+ ['-f', '--filename'],
+ {'default':'/dev/comedi0',
+ 'help':'path to comedi device file'}),
+ 'subdevice':(
+ ['-s', '--subdevice'],
+ {'type':int,
+ 'help':'subdevice for analog input/output'}),
+ 'channel':(
+ ['-c', '--channel'],
+ {'type':int,
+ 'default':0,
+ 'help':'channel for analog input/output'}),
+ 'channels':(
+ ['-c', '--channels'],
+ {'type':lambda x: [int(i) for i in x.split(',')],
+ 'default':0,
+ 'help':'comma-separated channels for analog input/output'}),
+ 'aref':(
+ ['-a', '--analog-reference'],
+ {'dest':'aref',
+ 'default':_constant.AREF.ground,
+ 'type':lambda x: _constant.AREF.index_by_name(x),
+ 'choices':_constant.AREF,
+ 'help':'reference for analog input/output'}),
+ 'range':(
+ ['-r', '--range'],
+ {'type':int,
+ 'default':0,
+ 'help':'range for analog input/output'}),
+ 'num-scans':(
+ ['-N', '--num-scans'],
+ {'type':int,
+ 'default':10,
+ 'help':'number of input/output scans'}),
+ 'frequency':(
+ ['-F', '--frequency'],
+ {'type':float,
+ 'action':_SetFrequencyAction,
+ 'help':'scan frequency in hertz'}),
+ 'physical':(
+ ['-p', '--physical'],
+ {'default':False,
+ 'action':'store_const',
+ 'const':True,
+ 'help':'convert input to physical values before printing'}),
+ 'verbose':(
+ ['-v', '--verbose'],
+ {'action':_IncrementVerbosityAction}),
+ }
+
+def parse_args(description, argnames):
+ parser = _argparse.ArgumentParser(description=description)
+ for argument in ['filename', 'subdevice', 'channels', 'aref', 'range',
+ 'num-scans', 'frequency', 'physical', 'verbose']:
+ args,kwargs = ARGUMENTS[argument]
+ parser.add_argument(*args, **kwargs)
+ return parser.parse_args()