跳转到主要内容

APSD9960手势扩展板的CircuitPython驱动程序

项目描述

简介

Documentation Status Discord Build Status Code Style: Black

APDS-9960是一种专用的芯片,可以检测手部手势、接近度和环境光颜色,通过I2C进行。它可以从Adafruit作为扩展板购买,也可以作为多个Adafruit开发板上的内置传感器。

此驱动程序提供了对APDS-9960传感器的接近度、手势和颜色数据的轻松访问,占用空间最小,以便它在所有CircuitPython平台上运行。

安装和依赖项

此驱动程序依赖于

请确保CircuitPython文件系统中所有依赖项均可用。

这可以通过下载 Adafruit库和驱动程序包轻松实现。

从PyPI安装

在支持GNU/Linux系统(如Raspberry Pi)上,您可以从PyPI 本地安装驱动程序

为当前用户安装

pip3 install adafruit-circuitpython-apds9960

全局安装(在某些情况下可能需要)

sudo pip3 install adafruit-circuitpython-apds9960

在当前项目中安装虚拟环境

mkdir project-name && cd project-name
python3 -m venv .venv
source .venv/bin/activate
pip3 install adafruit-circuitpython-apds9960

使用示例

import board
import digitalio
from adafruit_apds9960.apds9960 import APDS9960

i2c = board.I2C()
int_pin = digitalio.DigitalInOut(board.D5)
int_pin.switch_to_input(pull=digitalio.Pull.UP)
apds = APDS9960(i2c)

apds.enable_proximity_interrupt = True
apds.proximity_interrupt_threshold = (0, 175)
apds.enable_proximity = True

while True:
    if not int_pin.value:
        print(apds.proximity)
        apds.clear_interrupt()

硬件设置

如果您使用的是内置APDS-9960的板,则无需进行硬件设置。

如果您通过引脚头使用分线板,请将Vin连接到3.3 V或5 V电源,将GND连接到地,然后将SCLSDA连接到相应的引脚。

可选地,如果您想使用传感器的中断引脚,请将INT连接到任何可用的数字I/O引脚。

基础知识

要开始,导入board以及这个库

import board
from adafruit_apds9960.apds9960 import APDS9960

要设置传感器以收集数据,通过board.I2C()初始化I2C总线,然后初始化APDS-9960库。

i2c = board.I2C()
apds = APDS9960(i2c)

接近度

要获取接近度结果,启用接近度引擎,然后读取proximity值。

这将返回一个介于0到255之间的值,值越大表示物体越接近传感器。

apds.enable_proximity = True

while True:
  print(apds.proximity)

手势

首先,启用接近度和手势引擎。手势引擎依赖于接近度引擎来确定何时启动自身,因此,在启用手势引擎时,接近度读数将不可靠。

要获取手势,使用gesture()函数检查是否检测到手势。如果返回值大于0,则表示已检测到手势。

# Uncomment and set the rotation if depending on how your sensor is mounted.
# apds.rotation = 270 # 270 for CLUE

apds.enable_proximity = True
apds.enable_gesture = True

while True:
  gesture = apds.gesture()
  if gesture == 1:
    print("up")
  if gesture == 2:
    print("down")
  if gesture == 3:
    print("left")
  if gesture == 4:
    print("right")

颜色/光照测量

要获取颜色测量值,首先启用颜色/光照引擎,等待颜色数据到达,然后读取color_data值。

apds.enable_color = True

while True:
    while not apds.color_data_ready:
        time.sleep(0.005)

    r, g, b, c = apds.color_data
    print("r: {}, g: {}, b: {}, c: {}".format(r, g, b, c))

中断引脚

此传感器有一个中断引脚,如果检测到超出指定值范围的接近度,则可以置位(拉低)。

对于内置APDS-9960的板,此中断引脚已定义。例如,在Clue和Feather nRF52840 Sense板上,此引脚映射到board.PROXIMITY_LIGHT_INTERRUPT,在Proximity Trinkey板上映射到board.INTERRUPT

int_pin = digitalio.DigitalInOut(board.D5)
int_pin.switch_to_input(pull=digitalio.Pull.UP)

接近度检测

在中断引脚设置后,我们可以定义一个阈值,在启用接近度引擎之前,通过接近度引擎启用传感器的中断引脚的置位。

在此配置中,当物体靠近传感器时,传感器的中断引脚将被置位。在检查中断后,可以使用clear_interrupt()清除中断。

apds.enable_proximity = True

# set the interrupt threshold to fire when proximity reading goes above 175
apds.proximity_interrupt_threshold = (0, 175)

# assert interrupt pin on internal proximity interrupt
apds.enable_proximity_interrupt = True

# enable the sensor's proximity engine
apds.enable_proximity = True

while True:
  if not interrupt_pin.value:
    print(apds.proximity)

    # clear the interrupt
    apds.clear_interrupt()

初始化选项

默认情况下,当驱动器初始化时,APDS-9960传感器的内部设置会被重置,并为几个低级别设置应用合理的默认值,这些设置应该适用于大多数用例。

如果不需要“重置”或“设置默认值”行为(或两者都不需要),则可以通过init kwargs单独禁用它们。

apds = APDS9960(i2c, reset=False, set_defaults=False)

文档

此库的API文档可在Read the Docs上找到。

有关构建库文档的信息,请参阅此指南

贡献

欢迎贡献!在为此项目做出贡献之前,请阅读我们的行为准则,以帮助保持项目的友好性。

本地构建

要本地构建此库,您需要安装circuitpython-travis-build-tools包。

安装后,请确保您处于虚拟环境中

然后运行构建

Sphinx文档

Sphinx用于根据rST文件和代码中的注释构建文档。首先,安装依赖项(您可以重复使用上面的虚拟环境)

python3 -m venv .venv
source .venv/bin/activate
pip install Sphinx sphinx-rtd-theme

现在,一旦激活了虚拟环境

cd docs
sphinx-build -E -W -b html . _build/html

这将把文档输出到docs/_build/html。在浏览器中打开index.html以查看它们。由于-W选项,它还会在出现任何警告时出错,就像Travis会做的那样。这是一种在本地验证其能否通过的好方法。

项目详情


下载文件

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

源代码分布

adafruit-circuitpython-apds9960-3.1.11.tar.gz (40.2 kB 查看哈希值)

上传时间 源代码

构建分布

adafruit_circuitpython_apds9960-3.1.11-py3-none-any.whl (16.2 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面