Skip to main content

Pythonic API for Linux's gpiochip chardev ABI v2.

Project description

Pythonic API for Linux’s gpiochip chardev ABI v2.

Features

  • Manage multiple GPIO lines at the same time, with bit operation affecting the entire line group at once (|=, &=, ^=).

  • Get file event notification of timestamped line events (rising edge, falling edge).

  • Get file event notifications (select, poll, epoll…) of gpiochip-level and line-level events.

  • Control line parameters (pull-up, pull-down, active-low, debouncing, …).

  • Pure python module: no compilation needed, not limited to CPython.

Requirements

  • Linux >=5.10.0 for GPIO chardev ABI v2

  • python stdlib >=3.7.10 (not tested with earlier versions, they may work)

Examples

Warning: this example is not meant to be executed as-is. Depending on what is connected to the GPIO lines used here (which is entirely board-dependent), this could cause all sort of problems, including permanent hardware damage.

This is only to be taken as a quick overview of this module’s API.

from gpiochip2 import GPIOChip, GPIO_V2_LINE_FLAG
with GPIOChip('/dev/gpiochip0', 'w+b') as gpiochip:
    # Get information about the gpio chip itself
    gpiochip.getInfo()
    # Get information about line 20
    gpiochip.getLineInfo(20)
    with gpiochip.openLines(
        line_list=[20, 21, 26],
        flags=GPIO_V2_LINE_FLAG.OUTPUT,
        consumer='sample-name'.encode('ascii'),
        flags_dict={
            # Line 26 is an input and produces event on falling edges
            2: GPIO_V2_LINE_FLAG.INPUT | GPIO_V2_LINE_FLAG.EDGE_FALLING,
        },
        default_dict={
            # Drive line 20 low immediately on opening
            0: False,
        },
    ) as gpio_lines:
        # Read lines state
        value = gpio_lines.lines
        # Change lines state
        gpio_lines.lines = 0b11
        # Invert line 20
        gpio_lines ^= 1 # 1 << 0
        # Set line 21
        gpio_lines |= 2 # 1 << 1
        # Clear line 21
        gpio_lines &= 2 # 1 << 1
        # Read event
        lines.getEvent()

Notes on bit operations:

gpio_lines.lines |= some_mask will read then write the GPIO, while gpio_lines |= some_mask only needs to write, making it more efficient.

The same applies to &=, but not to other operators.

See also the examples directory for more realistic code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gpiochip2-0.9.2.tar.gz (40.7 kB view details)

Uploaded Source

File details

Details for the file gpiochip2-0.9.2.tar.gz.

File metadata

  • Download URL: gpiochip2-0.9.2.tar.gz
  • Upload date:
  • Size: 40.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.4.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.7

File hashes

Hashes for gpiochip2-0.9.2.tar.gz
Algorithm Hash digest
SHA256 98824d410e45fd2fe94c40d80376e38ff35330b01f70edee9866c1dcd335bd27
MD5 b2f09600dd2573c3a8539021c56015bc
BLAKE2b-256 45a28a42aa0a271469e3a033dce83f3dc12b7d2c1aa638e29b944c54cf3f7660

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page