1 # SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams for Adafruit Industries
 
   3 # SPDX-License-Identifier: MIT
 
   4 """A Pin class for use with lgpio."""
 
   6 from pathlib import Path
 
  12     Determines the handle of the GPIO chip device to access.
 
  14     iterate through sysfs  to find a GPIO chip device with a driver known to be
 
  15     used for userspace GPIO access.
 
  17     for dev in Path("/sys/bus/gpio/devices").glob("gpiochip*"):
 
  18         drivers = set((dev / "of_node/compatible").read_text().split("\0"))
 
  19         #   check if driver names are intended for userspace control
 
  21             "raspberrypi,rp1-gpio",
 
  22             "raspberrypi,bcm2835-gpio",
 
  23             "raspberrypi,bcm2711-gpio",
 
  25             return lgpio.gpiochip_open(int(dev.name[-1]))
 
  26     # return chip0 as a fallback
 
  27     return lgpio.gpiochip_open(0)
 
  30 CHIP = _get_gpiochip()
 
  34     """Pins dont exist in CPython so...lets make our own!"""
 
  41     # values of lg mode constants
 
  47     # drive mode lg constants
 
  55     _LG_MODES = IN | OUT | _LG_ALERT | _LG_GROUP
 
  56     _LG_PULLS = PULL_NONE | PULL_UP | PULL_NONE | ACTIVE_LOW
 
  57     _LG_DRIVES = OPEN_DRAIN
 
  64     lgpio.exceptions = True
 
  66     def __init__(self, bcm_number):
 
  72     def __eq__(self, other):
 
  73         return self.id == other
 
  75     def init(self, mode=IN, pull=None):
 
  76         """Initialize the Pin"""
 
  80                 self._set_gpio_mode_in()
 
  81             elif mode == self.OUT:
 
  83                 Pin._check_result(lgpio.gpio_claim_output(CHIP, self.id, Pin.LOW))
 
  85                 raise RuntimeError(f"Invalid mode for pin: {self.id}")
 
  87             if self._mode != Pin.IN:
 
  88                 raise RuntimeError("Can only set pull resistor on input")
 
  89             if pull in {Pin.PULL_UP, Pin.PULL_DOWN, Pin.PULL_NONE}:
 
  90                 self._set_gpio_mode_in(lflags=pull)
 
  92                 raise RuntimeError(f"Invalid pull for pin: {self.id}")
 
  94     def value(self, val=None):
 
  95         """Set or return the Pin Value"""
 
  99                 Pin._check_result(lgpio.gpio_write(CHIP, self.id, val))
 
 100             elif val == Pin.HIGH:
 
 102                 Pin._check_result(lgpio.gpio_write(CHIP, self.id, val))
 
 104                 raise RuntimeError("Invalid value for pin")
 
 106         return Pin._check_result(lgpio.gpio_read(CHIP, self.id))
 
 109     def _check_result(result):
 
 111         convert any result other than zero to a text message and pass it back
 
 112         as a runtime exception.  Typical usage:  use the lgpio call as the
 
 116             raise RuntimeError(lgpio.error_text(result))
 
 119     def _set_gpio_mode_in(self, lflags=0):
 
 121         claim a gpio as input, or modify the flags (PULL_UP, PULL_DOWN, ... )
 
 123         # This gpio_free may seem redundant, but is required when
 
 124         #  changing the line-flags of an already acquired input line
 
 126             lgpio.gpio_free(CHIP, self.id)
 
 129         Pin._check_result(lgpio.gpio_claim_input(CHIP, self.id, lFlags=lflags))