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)¶ 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”).
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: - 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”.
Returns: GPIO object.
Return type: Raises: GPIOError– if an I/O or OS error occurs.TypeError– if path, line, or direction types are invalid.ValueError– if direction 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: Truefor high state,Falsefor 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) –
Truefor high state,Falsefor 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.
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, 0 for a non-blocking poll, or negative or None for a blocking poll. Defaults to blocking poll.
Parameters: timeout (int, float, None) – timeout duration in seconds.
Returns: Trueif an edge event occurred,Falseon 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.
Returns: a namedtuple containing the string edge event that occurred (either
"rising"or"falling"), and the event time reported by Linux in nanoseconds.Return type: Raises: GPIOError– if an I/O or OS error occurs.NotImplementedError– if called on a sysfs GPIO.
-
devpath¶ Get the device path of the underlying GPIO device.
Type: str
-
fd¶ Get the line file descriptor of the GPIO object.
Type: int
-
line¶ Get the GPIO object’s line number.
Type: int
-
name¶ Get the line name of the GPIO.
his method is intended for use with character device GPIOs and always returns the empty string for sysfs GPIOs.
Type: str
-
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
-
chip_name¶ Get the name of the GPIO chip associated with the GPIO.
Type: str
-
chip_label¶ Get the label of the GPIO chip associated with the GPIO.
Type: str
-
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
-