Skip to main content

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, EXPECT_PRECONFIGURED
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,
        },
        # Expect the GPIO lines to be correctly preconfigured (ex: by board-
        # specific firmware or devicetree).
        expect_preconfigured=EXPECT_PRECONFIGURED.DIRECTION,
        expect_preconfigured_dict={
            # Expect line 26 to have its edge detection preconfigured
            # in addition to its direction
            2: EXPECT_PRECONFIGURED.DIRECTION | EXPECT_PRECONFIGURED.EDGE,
        },
    ) as gpio_lines:
        # Read lines state
        value = gpio_lines.value
        # Change lines state
        gpio_lines.value = 0b11
        # Set line 21
        gpio_lines |= 2 # 1 << 1
        # Clear line 20
        gpio_lines &= 1 # 1 << 0
        # 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.

Metadata

Release files for gpiochip2 0.9.3

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

Source distribution (sdist)

Source distribution for gpiochip2 0.9.3
File Size Uploaded
gpiochip2-0.9.3.tar.gz 41.7 kB Details

Release files / gpiochip2-0.9.3.tar.gz

Download URL gpiochip2-0.9.3.tar.gz
Size 41.7 kB
Tags Source
SHA-256 checksum
How to use checksums
aaef14f6711e0ff1c1f5421d8c2e1a7e6728fca1355e59d55af8d0cb70187ad2
BLAKE2b-256 checksum
How to use checksums
e39b75261c60d6b7e6db3397440fd9b3689407381266506e0256068db60a20f4
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.9.3 This release

1 release file

0.9.2

1 release file

0.9.1

1 release file

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