Linux 中的 Python libgpiod 与 gpiod 软件包?

标签 python gpio libgpiod

我用 Python 编写了一个小测试程序来操作 Intel Up Xtreme i11 上的 GPIO 引脚。第一次在 NixOS 下运行,我将软件包作为“libgpiod”引入,一切正常。 (MacOS 包管理器也知道“libgpiod”。)然后我尝试将其移植到同一硬件上的 Ubuntu 世界。但是apt和apt-get对libgpiod一无所知,它们只知道gpiod。 pip3 也是如此。所以我安装了 gpiod,但差异越来越大......

  1. gpiod 有一个成员“chip”而不是“Chip”
  2. 对于我能找到的任何小整数,chip.get_line 都会收到错误 22。

我缺少的是文档。是否有什么东西,在某个地方,可以清楚地解释这两个看似相似但实际上并非如此的包之间的区别?在 Python 中使用 Ubuntu gpiod 包的正确方法实际上是什么?

顺便说一句,我在这两种情况下都以 root 身份运行。这是代码(gpiod 版本):

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()

最佳答案

您所说的“libgpiod”库是基于this C library的系统包。 来自其文档:

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.

该库还提供了可能已经使用过的 python3 绑定(bind)。 在 Ubuntu 上你可以安装你需要的所有东西: apt安装python3-libgpiod。 使用此库的代码应该如下所示:

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()

有关更多使用示例,请参阅 examples section在 repo 协议(protocol)上。

Python 包 gpiod 可通过 pypi.org 中的 pip 获得是,“一个纯Python库,不依赖于其他包”。所以与上面提到的C库没有关系。 另请参阅this question使用其中一种的差异或优势。

a basic example作为文档提供。 要使您的代码使用 python3-gpiod (通过 pip 安装的库)运行,您应该进行如下修改:

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()

或者尝试使用 help(gpiod.line.get_line) 或类似工具对代码进行故障排除。

关于Linux 中的 Python libgpiod 与 gpiod 软件包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74352978/

相关文章:

python - 分词器改变词汇条目

python - 用 beautifulsoup 抓取 html id

python - 树莓派 GPIO :s 上的奇怪 python 错误

python - 在 Python 中运行后,如何在特定时间段内禁用 Raspberry Pi GPIO 事件?

c - 为 libgpiod 模拟 gpios

python - 在网络界面中显示以numpy表示的birmap [Python]

参数中带有函数的 Python 日志记录开销

c - 通过单个 GPIO 引脚转储闪存

gpio - 将 Raspberry 硬件 GPIO 引脚映射到 gpiod 芯片线号