# 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)
  CdevGPIO

    alias of `Cdev1GPIO`

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:

      SysfsGPIO

    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 periphery.GPIO(*args, **kwargs)

  Bases: `object`

  read()

    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)

    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)

    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()

    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:

      EdgeEvent

    Raises:

      - GPIOError – if an I/O or OS error occurs.

      - NotImplementedError – if called on a sysfs GPIO.

  static poll_multiple(gpios, timeout=None)

    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.

  close()

    Close the GPIO.

    Raises:

      GPIOError – if an I/O or OS error occurs.

  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

  property drive

    Get or set the GPIO’s line drive. Can be “default” (for push-pull), “open_drain”, “open_source”.

    This property is not supported by sysfs GPIOs.

    Raises:

      - GPIOError – if an I/O or OS error occurs.

      - TypeError – if drive type is not str.

      - ValueError – if drive value is invalid.

    Type:

      str

  property inverted

    Get or set the GPIO’s inverted (active low) property.

    Raises:

      - GPIOError – if an I/O or OS error occurs.

      - TypeError – if inverted type is not bool.

    Type:

      bool

class periphery.EdgeEvent(edge, timestamp)

  Bases: `EdgeEvent`

  EdgeEvent containing the event edge and event time reported by Linux.

  Parameters:

    - edge (str) – event edge, either “rising” or “falling”.

    - timestamp (int) – event time in nanoseconds.

class periphery.GPIOError

  Bases: `OSError`

  Base class for GPIO errors.