waveform80 / rpi-lgpio

A compatibility shim for lgpio emulating the RPi.GPIO API
https://rpi-lgpio.readthedocs.io/
Other
9 stars 5 forks source link

Fix `pull` variable does not exist + Allow configuring GPIO 0/1 #8

Closed cquick01 closed 4 months ago

cquick01 commented 4 months ago

Fixes an exception seen when trying to initialize pins

File "/home/user/my_project/venv/lib/python3.11/site-packages/RPi/GPIO/__init__.py", line 659, in setup
    if _warnings and gpio in (2, 3) and pull in (PUD_UP, PUD_DOWN):
                                        ^^^^
NameError: name 'pull' is not defined

Also updates the pin map to allow configuring GPIO 0 and 1. I've been using a little patch at the top of my script using rpi-lgpio, which works perfectly with my Pi 5

# patch RPi.GPIO (rpi-lgpio) to allow configuring ID_SD and ID_SC as GPIO pins.
GPIO._BOARD_MAP[27] = 0  # ID_SD = GPIO 0
GPIO._BOARD_MAP[28] = 1  # ID_SC = GPIO 1
GPIO._BCM_MAP = {channel: gpio for (gpio, channel) in GPIO._BOARD_MAP.items()}
waveform80 commented 4 months ago

Argh, the first commit is definitely a bug that needs fixing. Kicking myself that I didn't spot that one.

Unfortunately, I can't accept the second commit because it's not "bug compatible" with RPi.GPIO. If you have a look at RPi.GPIO's code it doesn't include GPIO0 or 1 in the BOARD mapping and if you try and use those GPIOs from that mode it fails (the following is the "real" RPi.GPIO on a Pi 4 under Ubuntu jammy):

>>> from RPi import GPIO
>>> GPIO.setmode(GPIO.BOARD)
>>> GPIO.gpio_function(27)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The channel sent is invalid on a Raspberry Pi

You can still use GPIO 0 and 1 from the BCM mode quite happily of course, and the same is true under rpi-lgpio, but I can't justify making any deliberate differences between this and RPi.GPIO.

cquick01 commented 4 months ago

Thanks for grabbing the first commit, and for that info! I'll see about using BCM mode in my code instead