Skip to content

Commit

Permalink
python: iio_info.py rewrite.
Browse files Browse the repository at this point in the history
I have rewritten the iio_info.py module in a similar fashion as the iio_readdev, iio_writedev and iio_attr modules.

Signed-off-by: Cristi Iacob <[email protected]>
  • Loading branch information
cristi-iacob committed Jun 2, 2020
1 parent 3df16ee commit edef5b9
Showing 1 changed file with 88 additions and 59 deletions.
147 changes: 88 additions & 59 deletions bindings/python/examples/iio_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,82 +18,111 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""

from sys import argv
import sys
import iio


def main():
"""Dump iio devices, list all iio attributes."""
print("Library version: %u.%u (git tag: %s)" % iio.version)

if len(argv) == 3 and argv[1] == "--uri":
uri = argv[2]
def _create_context():
if len(sys.argv) == 3 and sys.argv[1] == "--uri":
uri = sys.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
for uri, description in contexts.items():
print("\t%s: %s" % (uri, description))
sys.exit(0)

return iio.Context(uri)


class Information:
"""Class for retrieving the requested information."""

uri = next(iter(contexts), None)
def __init__(self, context):
"""
Class constructor.
ctx = iio.Context(uri)
Args:
context: type=iio.Context
Context used for retrieving the information.
"""
self.context = context

if uri is not None:
print('Using auto-detected IIO context at URI "%s"' % uri)
def write_information(self):
"""Write the information about the current context."""
self._context_info()

print("IIO context created: " + ctx.name)
print("Backend version: %u.%u (git tag: %s)" % ctx.version)
print("Backend description string: " + ctx.description)
def _context_info(self):
print("IIO context created: " + self.context.name)
print("Backend version: %u.%u (git tag: %s" % self.context.version)
print("Backend description string: " + self.context.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)
if len(self.context.attrs) > 0:
print("IIO context has %u attributes: " % len(self.context.attrs))

print("IIO context has %u devices:" % len(ctx.devices))
for attr, value in self.context.attrs.items():
print("\t" + attr + ": " + value)

for dev in ctx.devices:
print("IIO context has %u devices:" % len(self.context.devices))

for dev in self.context.devices:
self._device_info(dev)

def _device_info(self, dev):
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 err:
print("Unable to read " + attr + ": " + err.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 err:
print("Unable to read " + attr + ": " + err.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 err:
print("Unable to read " + attr + ": " + err.strerror)
print("Found trigger! Rate: %u HZ" % dev.frequency)

print("\t\t%u channels found: " % len(dev.channels))
for channel in dev.channels:
self._channel_info(channel)

if len(dev.attrs) > 0:
print("\t\t%u device-specific attributes found: " % len(dev.attrs))
for device_attr in dev.attrs:
self._device_attribute_info(dev, device_attr)

if len(dev.debug_attrs) > 0:
print("\t\t%u debug attributes found: " % len(dev.debug_attrs))
for debug_attr in dev.debug_attrs:
self._device_debug_attribute_info(dev, debug_attr)

def _channel_info(self, channel):
print("\t\t\t%s: %s (%s)" % (channel.id, channel.name or "", "output" if channel.output else "input"))
if len(channel.attrs) > 0:
print("\t\t\t%u channel-specific attributes found: " % len(channel.attrs))
for channel_attr in channel.attrs:
self._channel_attribute_info(channel, channel_attr)

@staticmethod
def _channel_attribute_info(channel, channel_attr):
try:
print("\t\t\t\t" + channel_attr + ", value: " + channel.attrs[channel_attr].value)
except OSError as err:
print("Unable to read " + channel_attr + ": " + err.strerror)

@staticmethod
def _device_attribute_info(dev, device_attr):
try:
print("\t\t\t" + device_attr + ", value: " + dev.attrs[device_attr].value)
except OSError as err:
print("Unable to read " + device_attr + ": " + err.strerror)

@staticmethod
def _device_debug_attribute_info(dev, debug_attr):
try:
print("\t\t\t" + debug_attr + ", value: " + dev.debug_attrs[debug_attr].value)
except OSError as err:
print("Unable to read " + debug_attr + ": " + err.strerror)


def main():
"""Module's main method."""
context = _create_context()
information = Information(context)
information.write_information()


if __name__ == "__main__":
Expand Down

0 comments on commit edef5b9

Please sign in to comment.