Ran update-copyright.py.
[pycomedi.git] / doc / demo / info.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
4 #
5 # This file is part of pycomedi.
6 #
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
10 # version.
11 #
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.
15 #
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/>.
18
19 """Gather and display information about a comedi device.
20 """
21
22 from pycomedi import PyComediError as _PyComediError
23 import pycomedi.constant as _constant
24 from pycomedi.device import Device as _Device
25 from pycomedi.subdevice import StreamingSubdevice as _StreamingSubdevice
26
27
28 def display_maxdata(subdevice):
29     if subdevice.maxdata_is_chan_specific():
30         print('  max data value: (channel specific)')
31         for channel in subdevice.channels():
32             print('    chan{}: {}'.format(
33                     channel.index, channel.get_maxdata()))
34     else:
35         print('  max data value: {}'.format(
36                 subdevice.channel(0).get_maxdata()))
37
38 def display_ranges(subdevice):
39     if subdevice.range_is_chan_specific():
40         print('  ranges: (channel specific)')
41         for channel in subdevice.channels():
42             print('    chan{}: {}'.format(
43                     channel.index,
44                     ', '.join(str(r) for r in channel.ranges())))
45     else:
46         print('  ranges: {}'.format(
47                 ', '.join(str(r) for r in subdevice.channel(0).ranges())))
48
49 def display_max_generic_timed(subdevice):
50     """Fastest supported single-channel command"""
51     try:
52         command = subdevice.get_cmd_generic_timed(chanlist_len=1)
53     except _PyComediError, e:
54         if e.function_name != 'comedi_get_cmd_generic_timed':
55             raise
56         print('  command fast 1chan: (not supported)')
57     else:
58         print('  command fast 1chan:')
59         print('    start: {} {}'.format(command.start_src, command.start_arg))
60         print('    scan_begin: {} {}'.format(
61                 command.scan_begin_src, command.scan_begin_arg))
62         print('    convert: {} {}'.format(
63                 command.convert_src, command.convert_arg))
64         print('    scan_end: {} {}'.format(
65                 command.scan_end_src, command.scan_end_arg))
66         print('    stop: {} {}'.format(command.stop_src, command.stop_arg))
67
68 def display_command(subdevice):
69     try:
70         command = subdevice.get_cmd_src_mask()
71     except _PyComediError, e:
72         if e.function_name != 'comedi_get_cmd_src_mask':
73             raise
74         print('  command: (not supported)')
75     else:
76         print('  command:')
77         print('    start: {}'.format(command.start_src))
78         print('    scan_begin: {}'.format(command.scan_begin_src))
79         print('    convert: {}'.format(command.convert_src))
80         print('    scan_end: {}'.format(command.scan_end_src))
81         print('    stop: {}'.format(command.stop_src))
82         display_max_generic_timed(subdevice=subdevice)
83
84 def display_subdevice(subdevice):
85     print('subdevice {}:'.format(subdevice.index))
86     subtype = subdevice.get_type()
87     print('  type: {}'.format(subtype))
88     if subtype == _constant.SUBDEVICE_TYPE.unused:
89         return
90     print('  flags: {}'.format(subdevice.get_flags()))
91     print('  number of channels: {}'.format(subdevice.get_n_channels()))
92     display_maxdata(subdevice=subdevice)
93     display_ranges(subdevice=subdevice)
94     # For testing commands, assume every device supports streaming and
95     # catch errors later.
96     streaming_subdevice = subdevice.device.subdevice(
97         index=subdevice.index, factory=_StreamingSubdevice)
98     display_command(subdevice=streaming_subdevice)
99
100 def display(device):
101     print('overall info')
102     print('  comedi version: {}'.format(
103             '.'.join(str(x) for x in device.get_version())))
104     print('  driver name: {}'.format(device.get_driver_name()))
105     print('  board name: {}'.format(device.get_board_name()))
106     print('  number of subdevices: {}'.format(device.get_n_subdevices()))
107     for subdevice in device.subdevices():
108         display_subdevice(subdevice=subdevice)
109
110 def run(filename):
111     """
112     >>> run(filename='/dev/comedi0')  # doctest: +ELLIPSIS, +REPORT_UDIFF
113     overall info
114       comedi version: 0.7.76
115       driver name: ni_pcimio
116       board name: pci-6052e
117       number of subdevices: 14
118     subdevice 0:
119       type: ai
120       flags: cmd_read|readable|ground|common|diff|other|dither
121     ...
122     subdevice 13:
123       type: counter
124       flags: readable|writable
125       number of channels: 1
126       max data value: 15
127       ranges: <Range unit:none min:0.0 max:1.0>
128       command: (not supported)
129     """
130     device = _Device(filename=filename)
131     device.open()
132     try:
133         display(device)
134     finally:
135         device.close()
136
137
138 if __name__ == '__main__':
139     import pycomedi_demo_args
140
141     args = pycomedi_demo_args.parse_args(
142         description=__doc__,
143         argnames=['filename', 'verbose'])
144
145     run(filename=args.filename)