Skip to main content

Upload Python Package Tests

Python API for Numato GPIO Expanders

This Python API can be used to control Numato USB GPIO expander devices with 8, 16, 32, 64, and 128 Ports.

  • Configure ports as input or output port
  • Write to output ports
  • Read from input ports
  • Read integer values from ADC input ports
  • Register a callback for input port events (edge detection)

See the changelog for details on the releases.

Install uv

Uv is currently by far the fastest and most modern Python project and dependency management tool written in Rust and inspired by Rust's cargo. uv covers the whole Python development environment including but not limited to installation of Python interpreters, dependency-version and package management and transparent isolation in virtual environments. It also enables one-off executions of packages with uvx.


Note:

This readme, only covers the Linux operating system. Refer to the uv documentation to learn how to install and use uv on MacOS or Windows. Keep in mind though that the specifics of device accessibility typically vary across operating systems and numato-gpio is at present only tested on Linux.


To install uv, you'll only need curl and a shell. Both are typically already installed on any Linux installation.

curl -LsSf https://astral.sh/uv/install.sh | sh

Preparation

First, attach the numato device via USB. Ensure you've got read/write access to the device. With dmesg you can find out which device file the board was made available by after attaching it, e.g. /dev/ttyACM0. With ls -l /dev/ttyACM0 you'll then be able to see which user and group the device file belongs to and see the permission. Usually the device will be owned by user root and group dialout. The common way of getting r/w permission for your user is, to join the dialout group like sudo adduser your_username dialout.

One-off execution of numato-gpio's numato-discover with uvx

To test whether your devices can be found, run the command-line interface like:

uvx --from numato-gpio numato-discover

Expected output:

❯ python3 -m numato_gpio
dev: /dev/ttyACM0 | id: 0 | ver: 00000009 | ports: 32 | iodir: 0xffffffff | iomask: 0x00000000 | state: 0x00000000
dev: /dev/ttyACM1 | id: 1 | ver: 00000009 | ports: 32 | iodir: 0xffffffff | iomask: 0x00000000 | state: 0x00000000

Usage API

The API can be used like:

import numato_gpio as gpio

# You can instantiate the device directly from its OS identifier, for instance
# "/dev/ttyACM0" on Linux or "COM5" on Windows.
dev = gpio.NumatoUsbGpio("/dev/ttyACM0")

# Alternatively, you can use the discovery function, but it is limited to
# Linux' /dev/ttyACM* devices. This is because discovery will open and try to
# interact with any device. This can lead to errors in unrelated devices.
# Under windows the naming scheme is entirely flat (COMx) increasing the error
# potential, so no discovery here.
# my_device_id = 0
# gpio.discover()
# dev = gpio.devices[my_device_id]

# Configure port 4 as output and set it to high
dev.setup(4, gpio.OUT)
dev.write(4, value=1)

# Configure port 27 as input and print its logic level
dev.setup(27, gpio.IN)
print(dev.read(27))

# Configure port 2 as input and print its ADC value
dev.setup(2, gpio.IN)
print(dev.adc_read(2))

# Configure port 14 as input and setup notification on logic level changes
dev.setup(14, gpio.IN)
def callback(port, level):
    print("{edge:7s} edge detected on port {port} "
        "-> new logic level is {level}".format(
        edge="Rising" if level else "Falling",
        port=port,
        level="high" if level else "low")
    )

dev.add_event_detect(14, callback, gpio.BOTH)
dev.notify = True

Release Versions

See changelog.

Troubleshooting

In case your device can't be discovered or you even get an error message or stacktrace, please follow the troubleshooting guide.

Known Issues

Though the code works well in a Home Assistant integration since 2018, there are quite some aspects to improve. The following issues are only the ones the author is aware of:

  • Some docstrings are hard to understand
  • Device discovery/registry as module-global dict is sub-optimal
  • Only /dev/ACMx devices are scanned which were mapped on the author's Linux
  • No async API available

Install development environment

If you plan to make a contribution use uv to set-up your development environment. Uv installs the dependencies and the numato-project (editable) in a virtualenv.

uv sync

You can now activate the virtualenv (.venv directory) like

source .venv/bin/activate

If you use VSCode or similar IDEs, ensure that their Python environment is configured to the .venv directory so their tools, like Debugger, Test Explorer etc., work.

Note that all commands of the Makefile are using uv run or uvx, so you don't have to activate the virtualenv before calling them.

System Tests

Unit tests in the tests directory are using a device mockup which mimics a Numato device's responses as far as known at the state of development.

System tests in the sys_tests folder are meant to be run using a real device and will just fail, if no device is connected. They are an important complement to unit tests, because they are the real thing and might behave differently than the mockup device for the unit tests.

If you consider to run system tests you should be aware that it may be dangerous running them.


WARNING

Only run the system tests with gpio ports disconnected! You otherwise risk a short circuit which may lead to damage or destruction of your device or worse.

Metadata

Release files for numato-gpio 0.14.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for numato-gpio 0.14.0
File Size Uploaded
numato_gpio-0.14.0.tar.gz 53.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for numato-gpio 0.14.0
File Interpreter ABI Platform
numato_gpio-0.14.0-py3-none-any.whl Python 3 none any Details

Total release size: 66.6 kB

Release files / numato_gpio-0.14.0.tar.gz

Download URL numato_gpio-0.14.0.tar.gz
Size 53.5 kB
Tags Source
SHA-256 checksum
How to use checksums
63002a83b02973ea98ba7fad667f1be3e79565343d3d8111f63922884f9625be
BLAKE2b-256 checksum
How to use checksums
2c714f4c120859c8408915b38a581ed8deb6d48ccb9e4e5f5421fad900978d1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.17

Release files / numato_gpio-0.14.0-py3-none-any.whl

Download URL numato_gpio-0.14.0-py3-none-any.whl
Size 13.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc866e3d9af83ac1822127e08c8c8830a7e50ad2fb40d8ef2767ce2d1abc88e9
BLAKE2b-256 checksum
How to use checksums
996a56cf21022d80fb136056fd825c41e39a4b9fb63add14f90fc53d02fa4f3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.17

Release history Release notifications | RSS feed

This release

0.14.0 This release

2 release files

0.13.0

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page