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 if Path(dev / "of_node/compatible").is_file():
19 drivers = set((dev / "of_node/compatible").read_text().split("\0"))
20 # check if driver names are intended for userspace control
22 "raspberrypi,rp1-gpio",
23 "raspberrypi,bcm2835-gpio",
24 "raspberrypi,bcm2711-gpio",
26 return lgpio.gpiochip_open(int(dev.name[-1]))
27 # return chip0 as a fallback
28 return lgpio.gpiochip_open(0)
31 CHIP = _get_gpiochip()
35 """Pins dont exist in CPython so...lets make our own!"""
42 # values of lg mode constants
48 # drive mode lg constants
56 _LG_MODES = IN | OUT | _LG_ALERT | _LG_GROUP
57 _LG_PULLS = PULL_NONE | PULL_UP | PULL_NONE | ACTIVE_LOW
58 _LG_DRIVES = OPEN_DRAIN
65 lgpio.exceptions = True
67 def __init__(self, bcm_number):
73 def __eq__(self, other):
74 return self.id == other
76 def init(self, mode=IN, pull=None):
77 """Initialize the Pin"""
81 self._set_gpio_mode_in()
82 elif mode == self.OUT:
84 Pin._check_result(lgpio.gpio_claim_output(CHIP, self.id, Pin.LOW))
86 raise RuntimeError(f"Invalid mode for pin: {self.id}")
88 if self._mode != Pin.IN:
89 raise RuntimeError("Can only set pull resistor on input")
90 if pull in {Pin.PULL_UP, Pin.PULL_DOWN, Pin.PULL_NONE}:
91 self._set_gpio_mode_in(lflags=pull)
93 raise RuntimeError(f"Invalid pull for pin: {self.id}")
95 def value(self, val=None):
96 """Set or return the Pin Value"""
100 Pin._check_result(lgpio.gpio_write(CHIP, self.id, val))
101 elif val == Pin.HIGH:
103 Pin._check_result(lgpio.gpio_write(CHIP, self.id, val))
105 raise RuntimeError("Invalid value for pin")
107 return Pin._check_result(lgpio.gpio_read(CHIP, self.id))
110 def _check_result(result):
112 convert any result other than zero to a text message and pass it back
113 as a runtime exception. Typical usage: use the lgpio call as the
117 raise RuntimeError(lgpio.error_text(result))
120 def _set_gpio_mode_in(self, lflags=0):
122 claim a gpio as input, or modify the flags (PULL_UP, PULL_DOWN, ... )
124 # This gpio_free may seem redundant, but is required when
125 # changing the line-flags of an already acquired input line
127 lgpio.gpio_free(CHIP, self.id)
130 Pin._check_result(lgpio.gpio_claim_input(CHIP, self.id, lFlags=lflags))