跳转到主要内容

Linux的gpiochip chardev ABI v2的Pythonic API。

项目描述

Linux的gpiochip chardev ABI v2的Pythonic API。

功能

  • 同时管理多个GPIO线,使用位操作一次影响整个线组(|=&=^=)。

  • 获取带时间戳的线事件(上升沿,下降沿)的文件事件通知。

  • 获取gpiochip级和线级事件的文件事件通知(选择,轮询,epoll等)。

  • 控制线参数(上拉,下拉,低电平有效,去抖动,...)。

  • 纯Python模块:无需编译,不受CPython限制。

要求

  • Linux >=5.10.0,用于GPIO chardev ABI v2

  • python stdlib >=3.7.10(未测试早期版本,可能适用)

示例

注意:此示例 应直接执行。根据连接到此处使用的GPIO线(这完全取决于板子),可能会引起各种问题,包括永久性硬件损坏。

此仅为快速了解此模块API的概述。

from gpiochip2 import GPIOChip, GPIO_V2_LINE_FLAG, EXPECT_PRECONFIGURED
with GPIOChip('/dev/gpiochip0', 'w+b') as gpiochip:
    # Get information about the gpio chip itself
    gpiochip.getInfo()
    # Get information about line 20
    gpiochip.getLineInfo(20)
    with gpiochip.openLines(
        line_list=[20, 21, 26],
        flags=GPIO_V2_LINE_FLAG.OUTPUT,
        consumer='sample-name'.encode('ascii'),
        flags_dict={
            # Line 26 is an input and produces event on falling edges
            2: GPIO_V2_LINE_FLAG.INPUT | GPIO_V2_LINE_FLAG.EDGE_FALLING,
        },
        default_dict={
            # Drive line 20 low immediately on opening
            0: False,
        },
        # Expect the GPIO lines to be correctly preconfigured (ex: by board-
        # specific firmware or devicetree).
        expect_preconfigured=EXPECT_PRECONFIGURED.DIRECTION,
        expect_preconfigured_dict={
            # Expect line 26 to have its edge detection preconfigured
            # in addition to its direction
            2: EXPECT_PRECONFIGURED.DIRECTION | EXPECT_PRECONFIGURED.EDGE,
        },
    ) as gpio_lines:
        # Read lines state
        value = gpio_lines.value
        # Change lines state
        gpio_lines.value = 0b11
        # Set line 21
        gpio_lines |= 2 # 1 << 1
        # Clear line 20
        gpio_lines &= 1 # 1 << 0
        # Read event
        lines.getEvent()

位操作说明

gpio_lines.lines |= some_mask 会先读取然后写入GPIO,而 gpio_lines |= some_mask 只需写入,使其更高效。

同样适用于 &=,但不适用于其他运算符。

有关更现实的代码,请参阅 examples 目录。

项目详情


下载文件

下载适用于您的平台文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

gpiochip2-0.9.3.tar.gz (41.7 kB 查看哈希值)

上传时间