APSD9960手势扩展板的CircuitPython驱动程序
项目描述
简介
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连接到地,然后将SCL和SDA连接到相应的引脚。
可选地,如果您想使用传感器的中断引脚,请将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的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | c21703ebefb2e9141ba1f9eccc121166a5f5c80ff9f6d426d1da148981eb27ed |
|
MD5 | 1822dca7d9f327d8bbf90184d29147a3 |
|
BLAKE2b-256 | a76ec12b013151de94fd9c8a22b2dec60312a9d79127c0b8dbae706031914c87 |
adafruit_circuitpython_apds9960-3.1.11-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 18a31130ec27eb873995ea940815278290b5108e729adf0c0ec2df7ae892855d |
|
MD5 | 522ed4bfdb6dac9170c9ca6ac59af1bc |
|
BLAKE2b-256 | a17410c519aef9420b967a41b8fece0a4d030c1669d8d54dcb8cd6e40aaffd0b |