GPIO¶
Code Example¶
from periphery import GPIO
# Open GPIO /dev/gpiochip0 line 10 with input direction
gpio_in = GPIO("/dev/gpiochip0", 10, "in")
# Open GPIO /dev/gpiochip0 line 12 with output direction
gpio_out = GPIO("/dev/gpiochip0", 12, "out")
value = gpio_in.read()
gpio_out.write(not value)
gpio_in.close()
gpio_out.close()
API¶
-
class
periphery.
GPIO
(path, line, direction)¶ -
class
CdevGPIO
(path, line, direction, edge='none', bias='default', drive='default', inverted=False, label=None)¶ Character device GPIO
Instantiate a GPIO object and open the character device GPIO with the specified line and direction at the specified GPIO chip path (e.g. “/dev/gpiochip0”). Defaults properties can be overridden with keyword arguments.
- Parameters
path (str) – GPIO chip character device path.
line (int, str) – GPIO line number or name.
direction (str) – GPIO direction, can be “in”, “out”, “high”, or “low”.
edge (str) – GPIO interrupt edge, can be “none”, “rising”, “falling”, or “both”.
bias (str) – GPIO line bias, can be “default”, “pull_up”, “pull_down”, or “disable”.
drive (str) – GPIO line drive, can be “default”, “open_drain”, or “open_source”.
inverted (bool) – GPIO is inverted (active low).
label (str, None) – GPIO line consumer label.
- Returns
GPIO object.
- Return type
- Raises
GPIOError – if an I/O or OS error occurs.
TypeError – if path, line, direction, edge, bias, drive, inverted, or label types are invalid.
ValueError – if direction, edge, bias, or drive value is invalid.
LookupError – if the GPIO line was not found by the provided name.
-
class
-
class
periphery.
GPIO
(line, direction) -
class
SysfsGPIO
(line, direction)¶ Sysfs GPIO
Instantiate a GPIO object and open the sysfs GPIO with the specified line and direction.
direction can be “in” for input; “out” for output, initialized to low; “high” for output, initialized to high; or “low” for output, initialized to low.
- Parameters
line (int) – GPIO line number.
direction (str) – GPIO direction, can be “in”, “out”, “high”, or “low”,
- Returns
GPIO object.
- Return type
- Raises
GPIOError – if an I/O or OS error occurs.
TypeError – if line or direction types are invalid.
ValueError – if direction value is invalid.
TimeoutError – if waiting for GPIO export times out.
-
class
-
class
periphery.
GPIO
[source] Bases:
object
-
read
()[source]¶ Read the state of the GPIO.
- Returns
True
for high state,False
for low state.- Return type
bool
- Raises
GPIOError – if an I/O or OS error occurs.
-
write
(value)[source]¶ Set the state of the GPIO to value.
- Parameters
value (bool) –
True
for high state,False
for low state.- Raises
GPIOError – if an I/O or OS error occurs.
TypeError – if value type is not bool.
-
poll
(timeout=None)[source]¶ Poll a GPIO for the edge event configured with the .edge property with an optional timeout.
For character device GPIOs, the edge event should be consumed with read_event(). For sysfs GPIOs, the edge event should be consumed with read().
timeout can be a positive number for a timeout in seconds, zero for a non-blocking poll, or negative or None for a blocking poll. Default is a blocking poll.
- Parameters
timeout (int, float, None) – timeout duration in seconds.
- Returns
True
if an edge event occurred,False
on timeout.- Return type
bool
- Raises
GPIOError – if an I/O or OS error occurs.
TypeError – if timeout type is not None or int.
-
read_event
()[source]¶ Read the edge event that occurred with the GPIO.
This method is intended for use with character device GPIOs and is unsupported by sysfs GPIOs.
-
static
poll_multiple
(gpios, timeout=None)[source]¶ Poll multiple GPIOs for the edge event configured with the .edge property with an optional timeout.
For character device GPIOs, the edge event should be consumed with read_event(). For sysfs GPIOs, the edge event should be consumed with read().
timeout can be a positive number for a timeout in seconds, zero for a non-blocking poll, or negative or None for a blocking poll. Default is a blocking poll.
- Parameters
gpios (list) – list of GPIO objects to poll.
timeout (int, float, None) – timeout duration in seconds.
- Returns
list of GPIO objects for which an edge event occurred.
- Return type
list
- Raises
GPIOError – if an I/O or OS error occurs.
TypeError – if timeout type is not None or int.
-
property
devpath
¶ Get the device path of the underlying GPIO device.
- Type
str
-
property
fd
¶ Get the line file descriptor of the GPIO object.
- Type
int
-
property
line
¶ Get the GPIO object’s line number.
- Type
int
-
property
name
¶ Get the line name of the GPIO.
This method is intended for use with character device GPIOs and always returns the empty string for sysfs GPIOs.
- Type
str
-
property
label
¶ Get the line consumer label of the GPIO.
This method is intended for use with character device GPIOs and always returns the empty string for sysfs GPIOs.
- Type
str
-
property
chip_fd
¶ Get the GPIO chip file descriptor of the GPIO object.
This method is intended for use with character device GPIOs and is unsupported by sysfs GPIOs.
- Raises
NotImplementedError – if accessed on a sysfs GPIO.
- Type
int
-
property
chip_name
¶ Get the name of the GPIO chip associated with the GPIO.
- Type
str
-
property
chip_label
¶ Get the label of the GPIO chip associated with the GPIO.
- Type
str
-
property
direction
¶ Get or set the GPIO’s direction. Can be “in”, “out”, “high”, “low”.
Direction “in” is input; “out” is output, initialized to low; “high” is output, initialized to high; and “low” is output, initialized to low.
- Raises
GPIOError – if an I/O or OS error occurs.
TypeError – if direction type is not str.
ValueError – if direction value is invalid.
- Type
str
-
property
edge
¶ Get or set the GPIO’s interrupt edge. Can be “none”, “rising”, “falling”, “both”.
- Raises
GPIOError – if an I/O or OS error occurs.
TypeError – if edge type is not str.
ValueError – if edge value is invalid.
- Type
str
-
property
bias
¶ Get or set the GPIO’s line bias. Can be “default”, “pull_up”, “pull_down”, “disable”.
This property is not supported by sysfs GPIOs.
- Raises
GPIOError – if an I/O or OS error occurs.
TypeError – if bias type is not str.
ValueError – if bias value is invalid.
- Type
str
-