Skip to content

Commit 0b98b52

Browse files
authored
Merge pull request #39 from fivesixzero/cleanup-and-doc
Gesture refactor, memory optimization, major doc updates
2 parents 7ae5dc3 + 72949af commit 0b98b52

File tree

5 files changed

+809
-338
lines changed

5 files changed

+809
-338
lines changed

README.rst

+125-48
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ Introduction
1414
:target: https://github.com/adafruit/Adafruit_CircuitPython_APDS9960/actions/
1515
:alt: Build Status
1616

17-
The APDS9960 is a specialized chip that detects hand gestures, proximity
18-
and ambient light color over I2C. Its available on
19-
`Adafruit as a breakout <https://www.adafruit.com/product/3595>`_.
17+
The APDS-9960 is a specialized chip that detects hand gestures, proximity
18+
and ambient light color over I2C. Its available from
19+
`Adafruit as a breakout <https://www.adafruit.com/product/3595>`_ and as a built-in sensor on
20+
several Adafruit development boards.
2021

22+
* `Adafruit CLUE <https://www.adafruit.com/product/4500>`_
23+
* `Adafruit Feather nRF52840 Sense <https://www.adafruit.com/product/4516>`_
24+
* `Adafruit Proximity Trinkey <https://www.adafruit.com/product/5022>`_
25+
26+
This driver provides easy access to proximity, gesture and color data from the APDS-9960 sensor
27+
with a minimal footprint to allow it to work on all CircuitPython platforms.
2128

2229
Installation and Dependencies
2330
=============================
@@ -26,13 +33,16 @@ This driver depends on:
2633
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
2734

2835
Please ensure all dependencies are available on the CircuitPython filesystem.
36+
2937
This is easily achieved by downloading
3038
`the Adafruit library and driver bundle <https://github.com/adafruit/Adafruit_CircuitPython_Bundle>`_.
3139

3240
Installing from PyPI
3341
--------------------
3442

35-
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from PyPI <https://pypi.org/project/adafruit-circuitpython-apds9960/>`_. To install for current user:
43+
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from PyPI <https://pypi.org/project/adafruit-circuitpython-apds9960/>`_.
44+
45+
To install for current user:
3646

3747
.. code-block:: shell
3848
@@ -64,98 +74,165 @@ Usage Example
6474
6575
i2c = board.I2C()
6676
int_pin = digitalio.DigitalInOut(board.D5)
77+
int_pin.switch_to_input(pull=digitalio.Pull.UP)
6778
apds = APDS9960(i2c)
6879
69-
apds.enable_proximity = True
70-
apds.proximity_interrupt_threshold = (0, 175)
7180
apds.enable_proximity_interrupt = True
81+
apds.proximity_interrupt_threshold = (0, 175)
82+
apds.enable_proximity = True
7283
7384
while True:
85+
if not int_pin.value:
7486
print(apds.proximity)
7587
apds.clear_interrupt()
7688
7789
Hardware Set-up
7890
---------------
7991

80-
Connect Vin to 3.3 V or 5 V power source, GND to ground, SCL and SDA to the appropriate pins.
92+
If you're using a board with a built-in APDS-9960, no hardware setup will be required.
93+
94+
If you're using a breakout board via the pin header, connect ``Vin`` to a 3.3 V or 5 V power source,
95+
connect ``GND`` to ground, then connect ``SCL`` and ``SDA`` to the appropriate pins.
96+
97+
Optionally, if you'd like to use the sensor's interrupt pin connect ``INT`` to any available
98+
digital I/O pin.
8199

82100
Basics
83101
------
84102

85-
Of course, you must import i2c bus device, board pins, and the library:
103+
To get started, import ``board`` and, and this library:
86104

87105
.. code:: python3
88106
89107
90-
import board
91-
from adafruit_apds9960.apds9960 import APDS9960
92-
import digitalio
108+
import board
109+
from adafruit_apds9960.apds9960 import APDS9960
93110
94-
To set-up the device to gather data, initialize the I2CDevice using SCL
95-
and SDA pins. Then initialize the library. Optionally provide an interrupt
96-
pin for proximity detection.
111+
To set up the sensor to gather data, initialize the I2C bus via ``board.I2C()``
112+
then initialize the APDS-9960 library.
97113

98114
.. code:: python3
99115
100-
int_pin = digitalio.DigitalInOut(board.A1)
101-
i2c = board.I2C()
102-
apds = APDS9960(i2c)
116+
i2c = board.I2C()
117+
apds = APDS9960(i2c)
118+
119+
Proximity
120+
---------
121+
122+
To get a proximity result, enable the proximity engine then read the `proximity` value.
123+
124+
This will return a value between 0 and 255, with higher values indicating that something is close
125+
to the sensor.
126+
127+
.. code:: python3
128+
129+
apds.enable_proximity = True
130+
131+
while True:
132+
print(apds.proximity)
103133
104134
Gestures
105135
--------
106136

107-
To get a gesture, see if a gesture is available first, then get the gesture Code
137+
First, enable both the proximity and gesture engines. The gesture engine relies on the proximity
138+
engine to determine when to start itself up and, as a result, proximity readings won't be reliable
139+
while the gesture engine is enabled.
140+
141+
To get a gesture, use the `gesture()` function to see if a gesture has been detected. If a value
142+
greater than 0 is returned, a gesture has been detected.
108143

109144
.. code:: python3
110145
111-
gesture = apds.gesture()
112-
if gesture == 1:
113-
print("up")
114-
if gesture == 2:
115-
print("down")
116-
if gesture == 3:
117-
print("left")
118-
if gesture == 4:
119-
print("right")
146+
# Uncomment and set the rotation if depending on how your sensor is mounted.
147+
# apds.rotation = 270 # 270 for CLUE
120148
121-
Color Measurement
122-
-----------------
149+
apds.enable_proximity = True
150+
apds.enable_gesture = True
123151
124-
To get a color measure, enable color measures, wait for color data,
125-
then get the color data.
152+
while True:
153+
gesture = apds.gesture()
154+
if gesture == 1:
155+
print("up")
156+
if gesture == 2:
157+
print("down")
158+
if gesture == 3:
159+
print("left")
160+
if gesture == 4:
161+
print("right")
162+
163+
Color/Light Measurement
164+
-----------------------
165+
166+
To get a color measurement, first enable the color/light engine, wait for color data to arrive,
167+
then read the `color_data` values.
126168

127169
.. code:: python3
128170
129-
apds.enable_color = True
171+
apds.enable_color = True
172+
173+
while True:
174+
while not apds.color_data_ready:
175+
time.sleep(0.005)
176+
177+
r, g, b, c = apds.color_data
178+
print("r: {}, g: {}, b: {}, c: {}".format(r, g, b, c))
130179
131-
while not apds.color_data_ready:
132-
time.sleep(0.005)
180+
Interrupt Pin
181+
-------------
133182

134-
r, g, b, c = apds.color_data
135-
print("r: {}, g: {}, b: {}, c: {}".format(r, g, b, c))
183+
This sensor has an interrupt pin can be asserted (pulled low) if proximity is detected outside of a
184+
specified window of values.
185+
186+
For boards with a built-in APDS-9960 this interupt pin will already be defined. For example, on the
187+
Clue and Feather nRF52840 Sense boards this pin is mapped to ``board.PROXIMITY_LIGHT_INTERRUPT``
188+
and on the Proximity Trinkey it is mapped to ``board.INTERRUPT``.
189+
190+
.. code:: python3
191+
192+
int_pin = digitalio.DigitalInOut(board.D5)
193+
int_pin.switch_to_input(pull=digitalio.Pull.UP)
136194
137195
Proximity Detection
138-
---------------------
196+
-------------------
197+
198+
With the interrupt pin set up we can define a threshold and enable the assertion of the sensor's
199+
interrupt pin by the proximity engine before enabling the proximity engine itself.
139200

140-
To check for a object in proximity, see if a gesture is available first, then get the gesture Code
201+
In this configuration, the sensor's interrupt pin will be asserted when an object is close to the
202+
sensor. After checking on the interrupt it can be cleared using `clear_interrupt()`
141203

142204
.. code:: python3
143205
144-
apds.enable_proximity = True
206+
apds.enable_proximity = True
207+
208+
# set the interrupt threshold to fire when proximity reading goes above 175
209+
apds.proximity_interrupt_threshold = (0, 175)
145210
146-
# set the interrupt threshold to fire when proximity reading goes above 175
147-
apds.proximity_interrupt_threshold = (0, 175)
211+
# assert interrupt pin on internal proximity interrupt
212+
apds.enable_proximity_interrupt = True
148213
149-
# enable the proximity interrupt
150-
apds.enable_proximity_interrupt = True
214+
# enable the sensor's proximity engine
215+
apds.enable_proximity = True
151216
152-
while True:
153-
if not interrupt_pin.value:
154-
print(apds.proximity)
217+
while True:
218+
if not interrupt_pin.value:
219+
print(apds.proximity)
220+
221+
# clear the interrupt
222+
apds.clear_interrupt()
223+
224+
Initiaization Options
225+
----------------------
155226

156-
# clear the interrupt
157-
apds.clear_interrupt()
227+
By default, when the driver is initialized, the APDS-9960 sensor's internal settings are reset and
228+
sensible defaults are applied to several low-level settings that should work well for most use cases.
229+
230+
If either the "reset" or "set defaults" behaviors (or both) aren't desired, they can be individually
231+
disabled via init kwargs.
232+
233+
.. code:: python3
158234
235+
apds = APDS9960(i2c, reset=False, set_defaults=False)
159236
160237
Documentation
161238
=============

0 commit comments

Comments
 (0)