Skip to main content

用于 Numato GPIO 扩展器的 Python API

项目描述

上传 Python 包

用于 Numato GPIO 扩展器的 Python API

这个 Python API 可用于控制Numato USB GPIO 扩展器

  • 将端口配置为输入或输出端口
  • 写入输出端口
  • 从输入端口读取
  • 从 ADC 输入端口 (1 - 7) 读取整数值
  • 为输入端口事件注册回调(边缘检测)

有关版本的详细信息,请参阅更改日志

安装

安装最新的开发版本:

pip install --user git+https://github.com/clssn/numato-gpio.git

安装最新版本:

pip install --user numato-gpio

使用 CLI

测试是否可以找到运行命令行界面(如 python3 -m numato_gpio. 请记住让您的用户加入该dialout组,因为这些设备已注册为 /dev/ttyACMx(即调制解调器设备)。

预期输出:

❯ python3 -m numato_gpio
dev: /dev/ttyACM0 | id: 0 | ver: 9 | ports: 32 | eol: '\r\n' | iodir: 0xffffffff | iomask: 0x00000000 | state: 0x00000000
dev: /dev/ttyACM1 | id: 1 | ver: 9 | ports: 32 | eol: '\r\n' | iodir: 0xffffffff | iomask: 0x00000000 | state: 0x00000000

使用 API

API 可以像这样使用:

import numato_gpio as gpio

my_device_id = 0
gpio.discover()
dev = gpio.devices[my_device_id]

# configure port 4 as output and set it to high
dev.setup(4, gpio.OUT)
dev.write(4, 1)

# configure port 27 as input and print its logic level
dev.setup(27, gpio.IN)
print(dev.read(27))

# configure port 2 as input and print its ADC value
dev.setup(2, gpio.IN)
print(dev.adc_read(2))

# configure port 14 as input and setup notification on logic level changes
dev.setup(14, gpio.IN)
def callback(port, level):
    print("{edge:7s} edge detected on port {port} "
        "-> new logic level is {level}".format(
        edge="Rising" if level else "Falling",
        port=port,
        level="high" if level else "low")
    )

dev.add_event_detect(14, callback, gpio.BOTH)
dev.notify = True

发布版本

请参阅更改日志

故障排除

如果您的设备无法被发现,或者您甚至收到错误消息或堆栈跟踪,请按照故障排除指南进行操作。

已知的问题

尽管该代码自 2018 年以来在Home Assistant 集成中运行良好,但仍有很多方面需要改进。以下问题只是作者知道的问题:

  • 有些文档字符串很难理解
  • 设备发现/注册作为模块全局字典不是最佳的
  • /dev/ACMx扫描在作者的 Linux 上映射的设备
  • 没有可用的异步 API

系统测试

目录中的单元测试tests使用了一个设备模型,该模型模拟了 Numato 设备在开发状态下的响应。

文件夹中的系统测试sys_tests旨在使用真实设备运行,如果没有连接设备,它将失败。它们是单元测试的重要补充,因为它们是真实的,并且可能与单元测试的模型设备不同。

如果您考虑运行系统测试,您应该意识到运行它们可能很危险。


警告

仅在断开 gpio 端口的情况下运行系统测试!否则,您将面临短路的风险,这可能导致您的设备损坏或毁坏,甚至引发火灾。

项目详情


下载文件

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

源分布

numato-gpio-0.10.0.tar.gz (11.5 kB 查看哈希

已上传 source

内置分布

numato_gpio-0.10.0-py3-none-any.whl (11.1 kB 查看哈希

已上传 py3