summaryrefslogtreecommitdiff
path: root/bindings/python/examples/iio_info.py
blob: e29b29756d3faa711e4cbfb4bf11cb491e214feb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
#
# Copyright (C) 2015 Analog Devices, Inc.
# Author: Paul Cercueil <paul.cercueil@analog.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

import iio
from sys import argv

def main():
	print('Library version: %u.%u (git tag: %s)' % iio.version)

	if len(argv) == 3 and argv[1] == '--uri':
		uri = argv[2]
	else:
		contexts = iio.scan_contexts()
		if len(contexts) > 1:
			print('Multiple contexts found. Please select one using --uri:')
			for index, each in enumerate(contexts):
				print('\t%d: %s [%s]' % (index, contexts[each], each))
			return

		uri = next(iter(contexts), None)

	ctx = iio.Context(uri)

	if uri is not None:
		print('Using auto-detected IIO context at URI \"%s\"' % uri)

	print('IIO context created: ' + ctx.name)
	print('Backend version: %u.%u (git tag: %s)' % ctx.version)
	print('Backend description string: ' + ctx.description)

	if len(ctx.attrs) > 0:
		print('IIO context has %u attributes:' % len(ctx.attrs))
	for attr, value in ctx.attrs.items():
		print('\t' + attr + ': ' + value)

	print('IIO context has %u devices:' % len(ctx.devices))

	for dev in ctx.devices:
		print('\t' + dev.id + ': ' + dev.name)

		if dev is iio.Trigger:
			print('Found trigger! Rate: %u Hz' % dev.frequency)

		print('\t\t%u channels found:' % len(dev.channels))

		for chn in dev.channels:
			print('\t\t\t%s: %s (%s)' % (chn.id, chn.name or "", 'output' if chn.output else 'input'))

			if len(chn.attrs) != 0:
				print('\t\t\t%u channel-specific attributes found:' % len(chn.attrs))

			for attr in chn.attrs:
				try:
					print('\t\t\t\t' + attr + ', value: ' + chn.attrs[attr].value)
				except OSError as e:
					print('Unable to read ' + attr + ': ' + e.strerror)

		if len(dev.attrs) != 0:
			print('\t\t%u device-specific attributes found:' % len(dev.attrs))

		for attr in dev.attrs:
			try:
				print('\t\t\t' + attr + ', value: ' + dev.attrs[attr].value)
			except OSError as e:
				print('Unable to read ' + attr + ': ' + e.strerror)

		if len(dev.debug_attrs) != 0:
			print('\t\t%u debug attributes found:' % len(dev.debug_attrs))

		for attr in dev.debug_attrs:
			try:
				print('\t\t\t' + attr + ', value: ' + dev.debug_attrs[attr].value)
			except OSError as e:
				print('Unable to read ' + attr + ': ' + e.strerror)

if __name__ == '__main__':
	main()