GPIO chip

class gpiod.Chip(path: str)

Represents a GPIO chip.

Chip object manages all resources associated with the GPIO chip it represents.

The gpiochip device file is opened during the object’s construction. The Chip object’s constructor takes the path to the GPIO chip device file as the only argument.

Callers must close the chip by calling the close() method when it’s no longer used.

Example:

chip = gpiod.Chip("/dev/gpiochip0")
do_something(chip)
chip.close()

The gpiod.Chip class also supports controlled execution (‘with’ statement).

Example:

with gpiod.Chip(path="/dev/gpiochip0") as chip:
    do_something(chip)

Synchronization: objects of this class require external synchronization. Protect calls with a lock when sharing an instance across threads.

close() None

Close the associated GPIO chip descriptor. The chip object must no longer be used after this method is called.

property fd: int

File descriptor associated with this chip.

fileno() int

Return the underlying file descriptor.

get_info() ChipInfo

Get the information about the chip.

Returns:

New gpiod.ChipInfo object.

get_line_info(line: int | str) LineInfo

Get the snapshot of information about the line at given offset.

Args:
line:

Offset or name of the GPIO line to get information for.

Returns:

New LineInfo object.

line_offset_from_id(id: str | int) int

Map a line’s identifier to its offset within the chip.

Args:
id:

Name of the GPIO line, its offset as a string or its offset as an integer.

Returns:

If id is an integer - it’s returned as is (unless it’s out of range for this chip). If it’s a string, the method tries to interpret it as the name of the line first and tries too perform a name lookup within the chip. If it fails, it tries to convert the string to an integer and check if it represents a valid offset within the chip and if so - returns it.

property path: str

Filesystem path used to open this chip.

read_info_event() InfoEvent

Read a single line status change event from the chip.

Returns:

New gpiod.InfoEvent object.

Note:

This function may block if there are no available events in the queue.

request_lines(config: dict[Iterable[int | str] | int | str, LineSettings | None], consumer: str | None = None, event_buffer_size: int | None = None, output_values: dict[int | str, Value] | None = None) LineRequest

Request a set of lines for exclusive usage.

Args:
config:

Dictionary mapping offsets or names (or tuples thereof) to LineSettings. If None is passed as the value of the mapping, default settings are used.

consumer:

Consumer string to use for this request.

event_buffer_size:

Size of the kernel edge event buffer to configure for this request.

output_values:

Dictionary mapping offsets or names to line.Value. This can be used to set the desired output values globally while reusing LineSettings for more lines.

Returns:

New LineRequest object.

unwatch_line_info(line: int | str) None

Stop watching a line for status changes.

Args:
line:

Offset or name of the line to stop watching.

wait_info_event(timeout: timedelta | float | None = None) bool

Wait for line status change events on any of the watched lines on the chip.

Args:
timeout:

Wait time limit represented as either a datetime.timedelta object or the number of seconds stored in a float. If set to 0, the method returns immediately, if set to None it blocks indefinitely.

Returns:

True if an info event is ready to be read from the chip, False if the wait timed out without any events.

watch_line_info(line: int | str) LineInfo

Get the snapshot of information about the line at given offset and start watching it for future changes.

Args:
line:

Offset or name of the GPIO line to get information for.

Returns:

New gpiod.LineInfo object.