========
Examples
========

Runnable example scripts live in the repository under `python/example
<https://github.com/intrepidcs/libneoradio2/tree/master/python/example>`_,
including the neoRAD-IO2-BADGE demos (LEDs, digital output, analog input) and
live plotting with matplotlib.

Reading analog input
====================

.. code-block:: python

    import neoradio2

    devices = neoradio2.find()
    if not devices:
        raise SystemExit("No neoRAD-IO2 devices found")

    handle = neoradio2.open(devices[0])
    neoradio2.chain_identify(handle)
    neoradio2.app_start(handle, 0, 0xFF)

    # Request calibrated sensor data for all banks of device 0, then read each
    neoradio2.request_sensor_data(handle, 0, 0xFF, neoradio2.CalType.ENABLED)
    for bank in range(8):
        print(f"bank {bank}: {neoradio2.read_sensor_float(handle, 0, bank):.4f}")

    neoradio2.close(handle)

Walking a device chain
======================

.. code-block:: python

    import neoradio2

    handle = neoradio2.open(neoradio2.find()[0])
    neoradio2.chain_identify(handle)
    count = neoradio2.get_chain_count(handle, True)
    print(f"{count} device(s) in the chain")

    for device in range(count):
        neoradio2.app_start(handle, device, 0xFF)
        dev_type = neoradio2.get_device_type(handle, device, 0)
        serial = neoradio2.get_serial_number(handle, device, 0)
        major, minor = neoradio2.get_firmware_version(handle, device, 0)
        print(f"device {device}: type={dev_type} serial={serial} fw={major}.{minor}")

    neoradio2.close(handle)
