Skip to content

⚡ GPIO API

linuxpy.gpio.device

Human friendly interface to linux GPIO subsystem.

The heart of linuxpy GPIO library is the Device class. The recommended way is to use one of the find methods to create a Device object and use it within a context manager like:

from linuxpy.gpio.device import find, LineFlag

with find() as gpio:
    # request lines 5 and 6
    lines = gpio[5, 6]
    lines.flags = LineFlag.ACTIVE_LOW
    with lines:
        print(lines[:])

Request(device, config: dict, blocking: bool = False)

Bases: ReentrantOpen

A lazy request to reserve lines on a chip

Prefered creation from the Device.request() method.

name: str property writable

Requestor name

Change the requestor name must be called before the request is open to take effect

RETURNS DESCRIPTION
str

consumer name

TYPE: str

min_line: int property

The smallest line number in the request

RETURNS DESCRIPTION
int

The smallest line number in the request

TYPE: int

max_line: int property

The biggest line number in the request

RETURNS DESCRIPTION
int

The biggest line number in the request

TYPE: int

__getitem__(key: Union[int, tuple, slice]) -> Union[int, dict]

Reads lines values

PARAMETER DESCRIPTION
key

a line number, a slice, or a list of line numbers or slices

TYPE: Union[int, tuple, slice]

RETURNS DESCRIPTION
Union[int, dict]

Union[int, dict]: a dict where key is line number and value its value

__setitem__(key: Union[int, tuple, slice], value: Union[int, Sequence[int]])

Sets the given lines values

PARAMETER DESCRIPTION
key

a line number, a slice, or a list of line numbers or slices

TYPE: Union[int, tuple, slice]

value

the value(s) to write for the given lines

TYPE: Union[int, Sequence[int]]

RAISES DESCRIPTION
ValueError

if key is a line number and value is not a number (0 or 1)

__iter__() -> Iterable[LineEvent]

Infinite stream of line events

RETURNS DESCRIPTION
Iterable[LineEvent]

Iterable[LineEvent]: the stream of events

__aiter__() -> AsyncIterator[LineEvent]

Asynchronous stream of line events

RETURNS DESCRIPTION
AsyncIterator[LineEvent]

AsyncIterator[LineEvent]: the asynchronous stream of events

filenos() -> list[int]

List of underlying request file numbers

RETURNS DESCRIPTION
list[int]

list[int]: List of underlying request file numbers

close()

Closes the underling request files. If request is not open nothing is done.

open()

Opens the underling request files effectively reserving the lines

get_values(lines: Optional[Sequence[int]] = None) -> dict[int, int]

Reads values for the given lines

PARAMETER DESCRIPTION
lines

A collection of lines. Defaults to None. Default means read all lines

TYPE: Optional[Sequence[int]] DEFAULT: None

RETURNS DESCRIPTION
dict[int, int]

dict[int, int]: line values. Key is line number and value its value

set_values(values: dict[int, Union[int, bool]])

Writes new values on the given lines

PARAMETER DESCRIPTION
values

key is line number and value its value

TYPE: dict[int, Union[int, bool]]

Device(*args, **kwargs)

Bases: BaseDevice

A device represents a connection to the underlying gpio chip

__len__() -> int

The number of lines in this chip

RETURNS DESCRIPTION
int

The number of lines in this chip

TYPE: int

__getitem__(key: Union[int, tuple, slice]) -> Request

create a request for the given lines. Equivalent to device.request(key)

Note

The request is not active after this call. You need to use the request object returned by this method in a context manager or manually call open/close.

PARAMETER DESCRIPTION
key

the line number, slice or a list of line numbers, or slices

TYPE: Union[int, tuple, slice]

RETURNS DESCRIPTION
Request

A new request object

TYPE: Request

__iter__() -> Iterable[LineInfoEvent]

Infinite stream of line info events

RETURNS DESCRIPTION
Iterable[LineInfoEvent]

Iterable[LineInfoEvent]: the stream of line info events

__aiter__() -> AsyncIterator[LineInfoEvent]

Asynchronous stream of line events

RETURNS DESCRIPTION
AsyncIterator[LineInfoEvent]

AsyncIterator[LineInfoEvent]: the asynchronous stream of line info events

get_info() -> Info

Reads all information available including chip info and detailed information about each chip line information

RETURNS DESCRIPTION
Info

The full chip information

TYPE: Info

request(config: Optional[Union[Collection, list]] = None, blocking: Union[bool, object] = sentinel) -> Request

Create a request to reserve a list of lines on this chip

Note

The request is not active after this call. You need to use the request object returned by this method in a context manager or manually call open/close.

PARAMETER DESCRIPTION
config

lines configuration

TYPE: dict DEFAULT: None

lines

blocking mode

TYPE: Union[bool, sentinel]

RETURNS DESCRIPTION
Request

A new request object

TYPE: Request

get_chip_info(fd: FDLike) -> ChipInfo

Reads the chip information

PARAMETER DESCRIPTION
fd

a gpiochip file number or file like object

TYPE: FDLike

RETURNS DESCRIPTION
ChipInfo

chip info of the given file descriptor

TYPE: ChipInfo

get_line_info(fd: FDLike, line: int) -> LineInfo

Reads the given line information

PARAMETER DESCRIPTION
fd

a gpiochip file number or file like object

TYPE: FDLike

line

desired line to get information

TYPE: int

RETURNS DESCRIPTION
LineInfo

information for the given line and chip

TYPE: LineInfo

get_info(fd: FDLike) -> Info

Reads the given chip the full information

PARAMETER DESCRIPTION
fd

a gpiochip file number or file like object

TYPE: FDLike

RETURNS DESCRIPTION
Info

information for the given chip including all its lines

TYPE: Info

request(fd: FDLike, config: dict, blocking: bool = False) -> raw.gpio_v2_line_request

Make a request to reserve the given line(s)

PARAMETER DESCRIPTION
fd

a gpiochip file number or file like object

TYPE: FDLike

config

line config as specified in GPIO line configuration request

TYPE: dict

blocking

Make the return FD blocking. Defaults to False.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
gpio_v2_line_request

raw.gpio_v2_line_request: The details of the request. Field fd contains the new open file descritor

get_values(req_fd: FDLike, mask: int) -> raw.gpio_v2_line_values

Read lines values.

PARAMETER DESCRIPTION
req_fd

a gpiochip file number or file like object

TYPE: FDLike

mask

a bitmap identifying the lines, with each bit number corresponding to the index of the line reserved in the given req_fd.

TYPE: int

RETURNS DESCRIPTION
gpio_v2_line_values

raw.gpio_v2_line_values: the current line values

set_values(req_fd: FDLike, mask: int, bits: int) -> raw.gpio_v2_line_values

Set lines values.

PARAMETER DESCRIPTION
req_fd

a gpiochip file number or file like object

TYPE: FDLike

mask

The mask is a bitmap identifying the lines, with each bit number corresponding to the index of the line reserved in the given req_fd.

TYPE: int

bits

The bits is a bitmap containing the value of the lines, set to 1 for active and 0 for inactive.

TYPE: int

RETURNS DESCRIPTION
gpio_v2_line_values

raw.gpio_v2_line_values: the underlying object sent to the ioctl call

read_one_event(req_fd: FDLike) -> LineEvent

Read one event from the given request file descriptor

PARAMETER DESCRIPTION
req_fd

description

TYPE: FDLike

RETURNS DESCRIPTION
LineEvent

description

TYPE: LineEvent

expand_from_list(key: Union[int, slice, tuple], minimum, maximum) -> list[int]

Used internally in getitem to expand the given key

iter_gpio_files(path: PathLike = '/dev') -> Iterable[pathlib.Path]

Returns an iterator over all GPIO chip files.

Warning

Only files for which the current user has read and write access are returned

PARAMETER DESCRIPTION
path

root path. Defaults to "/dev".

TYPE: PathLike DEFAULT: '/dev'

RETURNS DESCRIPTION
Iterable[Path]

Iterable[pathlib.Path]: an iterator over the gpiochip files found on the system

iter_devices(path: PathLike = '/dev', **kwargs) -> Iterable[Device]

Returns an iterator over all GPIO chip devices

PARAMETER DESCRIPTION
path

root path. Defaults to "/dev".

TYPE: PathLike DEFAULT: '/dev'

RETURNS DESCRIPTION
Iterable[Device]

Iterable[Device]: an iterator over the gpiochip devices found on the system