I wrote a little test program in Python to manipulate GPIO pins on an an Intel Up Xtreme i11. First running under NixOS, I brought in the package as "libgpiod" and things are working. (MacOS package managers also know "libgpiod".) Then I tried to port this to an Ubuntu world on the same hardware. But apt and apt-get know nothing of libgpiod, they only know gpiod. pip3, too. So I installed gpiod, but the discrepancies mount up…
What I lack is documentation. Is there something, somewhere, that clearly explains the distinction between these two packages that appear to be similar but are not? And what is actually the correct way of using the Ubuntu gpiod package in Python?
BTW I am running as root in both cases. Here's the code (gpiod version):
import gpiod, time
# pins
POWER = 9
chip=gpiod.chip('gpiochip0')
power=chip.get_line(POWER)
power.request(consumer="motor_movement", type=gpiod.LINE_REQ_DIR_OUT)
def run():
delay = 1.0
try:
#power.set_value(0)
while True:
power.set_value(1)
time.sleep(delay)
power.set_value(0)
time.sleep(delay)
finally:
cleanup()
def cleanup():
power.release()
if __name__ == "__main__":
run()
What you refer to as "libgpiod" library are system packages based on this C library. From its documentation:
libgpiod
========
libgpiod - C library and tools for interacting with the linux GPIO
character device (gpiod stands for GPIO device)
Since linux 4.8 the GPIO sysfs interface is deprecated. User space should use
the character device instead. This library encapsulates the ioctl calls and
data structures behind a straightforward API.
The library also provides python3 bindings, which have probably been using.
On Ubuntu you would install everything you need issuing:
apt install python3-libgpiod
.
Your code using this library should have looked like this:
import gpiod, time
# pins
POWER = 9
chip = gpiod.Chip('0')
power = chip.get_line(POWER)
power.request(consumer="motor_movement", type=gpiod.LINE_REQ_DIR_OUT)
def run():
delay = 1.0
try:
#power.set_value(0)
while True:
power.set_value(1)
time.sleep(delay)
power.set_value(0)
time.sleep(delay)
finally:
cleanup()
def cleanup():
power.release()
if __name__ == "__main__":
run()
For further usage examples see the examples section on the repo.
The python package gpiod
available through pip
from pypi.org is, "a pure Python library and has no dependencies on other packages". So no relation to the C library mentioned above.
See also this question for differences or advantages using one or the other.
There is a basic example provided as documentation.
To make your code working using python3-gpiod
(the library installed through pip), you should modify as follows:
import gpiod, time
# pins
POWER = 9
chip=gpiod.chip('gpiochip0')
power=chip.get_line(POWER)
power_config = gpiod.line_request()
power_config.consumer = "motor_movement"
power_config.request_type = gpiod.line_request.DIRECTION_OUTPUT
power.request(power_config)
def run():
delay = 1.0
try:
#power.set_value(0)
while True:
power.set_value(1)
time.sleep(delay)
power.set_value(0)
time.sleep(delay)
finally:
cleanup()
def cleanup():
power.release()
if __name__ == "__main__":
run()
Alternatively try to use help(gpiod.line.get_line)
or similar to troubleshoot your code.