|
| 1 | +# SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +"""A Pin class for use with lgpio.""" |
| 5 | + |
| 6 | +from pathlib import Path |
| 7 | +import lgpio |
| 8 | + |
| 9 | + |
| 10 | +def _get_gpiochip(): |
| 11 | + """ |
| 12 | + Determines the handle of the GPIO chip device to access. |
| 13 | +
|
| 14 | + iterate through sysfs to find a GPIO chip device with a driver known to be |
| 15 | + used for userspace GPIO access. |
| 16 | + """ |
| 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 |
| 20 | + if drivers & { |
| 21 | + "raspberrypi,rp1-gpio", |
| 22 | + "raspberrypi,bcm2835-gpio", |
| 23 | + "raspberrypi,bcm2711-gpio", |
| 24 | + }: |
| 25 | + return lgpio.gpiochip_open(int(dev.name[-1])) |
| 26 | + # return chip0 as a fallback |
| 27 | + return lgpio.gpiochip_open(0) |
| 28 | + |
| 29 | + |
| 30 | +CHIP = _get_gpiochip() |
| 31 | + |
| 32 | + |
| 33 | +class Pin: |
| 34 | + """Pins dont exist in CPython so...lets make our own!""" |
| 35 | + |
| 36 | + LOW = 0 |
| 37 | + HIGH = 1 |
| 38 | + OFF = LOW |
| 39 | + ON = HIGH |
| 40 | + |
| 41 | + # values of lg mode constants |
| 42 | + PULL_NONE = 0x80 |
| 43 | + PULL_UP = 0x20 |
| 44 | + PULL_DOWN = 0x40 |
| 45 | + ACTIVE_LOW = 0x02 |
| 46 | + |
| 47 | + # drive mode lg constants |
| 48 | + OPEN_DRAIN = 0x04 |
| 49 | + IN = 0x0100 |
| 50 | + OUT = 0x0200 |
| 51 | + |
| 52 | + # LG mode constants |
| 53 | + _LG_ALERT = 0x400 |
| 54 | + _LG_GROUP = 0x800 |
| 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 |
| 58 | + |
| 59 | + id = None |
| 60 | + _value = LOW |
| 61 | + _mode = IN |
| 62 | + |
| 63 | + # we want exceptions |
| 64 | + lgpio.exceptions = True |
| 65 | + |
| 66 | + def __init__(self, bcm_number): |
| 67 | + self.id = bcm_number |
| 68 | + |
| 69 | + def __repr__(self): |
| 70 | + return str(self.id) |
| 71 | + |
| 72 | + def __eq__(self, other): |
| 73 | + return self.id == other |
| 74 | + |
| 75 | + def init(self, mode=IN, pull=None): |
| 76 | + """Initialize the Pin""" |
| 77 | + if mode is not None: |
| 78 | + if mode == Pin.IN: |
| 79 | + self._mode = Pin.IN |
| 80 | + self._set_gpio_mode_in() |
| 81 | + elif mode == self.OUT: |
| 82 | + self._mode = Pin.OUT |
| 83 | + Pin._check_result(lgpio.gpio_claim_output(CHIP, self.id, Pin.LOW)) |
| 84 | + else: |
| 85 | + raise RuntimeError(f"Invalid mode for pin: {self.id}") |
| 86 | + if pull is not None: |
| 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) |
| 91 | + else: |
| 92 | + raise RuntimeError(f"Invalid pull for pin: {self.id}") |
| 93 | + |
| 94 | + def value(self, val=None): |
| 95 | + """Set or return the Pin Value""" |
| 96 | + if val is not None: |
| 97 | + if val == Pin.LOW: |
| 98 | + self._value = val |
| 99 | + Pin._check_result(lgpio.gpio_write(CHIP, self.id, val)) |
| 100 | + elif val == Pin.HIGH: |
| 101 | + self._value = val |
| 102 | + Pin._check_result(lgpio.gpio_write(CHIP, self.id, val)) |
| 103 | + else: |
| 104 | + raise RuntimeError("Invalid value for pin") |
| 105 | + return None |
| 106 | + return Pin._check_result(lgpio.gpio_read(CHIP, self.id)) |
| 107 | + |
| 108 | + @staticmethod |
| 109 | + def _check_result(result): |
| 110 | + """ |
| 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 |
| 113 | + argument. |
| 114 | + """ |
| 115 | + if result < 0: |
| 116 | + raise RuntimeError(lgpio.error_text(result)) |
| 117 | + return result |
| 118 | + |
| 119 | + def _set_gpio_mode_in(self, lflags=0): |
| 120 | + """ |
| 121 | + claim a gpio as input, or modify the flags (PULL_UP, PULL_DOWN, ... ) |
| 122 | + """ |
| 123 | + # This gpio_free may seem redundant, but is required when |
| 124 | + # changing the line-flags of an already acquired input line |
| 125 | + try: |
| 126 | + lgpio.gpio_free(CHIP, self.id) |
| 127 | + except lgpio.error: |
| 128 | + pass |
| 129 | + Pin._check_result(lgpio.gpio_claim_input(CHIP, self.id, lFlags=lflags)) |
0 commit comments