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 哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | aaef14f6711e0ff1c1f5421d8c2e1a7e6728fca1355e59d55af8d0cb70187ad2 |
|
MD5 | e82c19e853ace010eb4842725d3989da |
|
BLAKE2b-256 | e39b75261c60d6b7e6db3397440fd9b3689407381266506e0256068db60a20f4 |