Skip to content

Commit b9b7b06

Browse files
committed
Documented.
1 parent ea0898e commit b9b7b06

File tree

5 files changed

+148
-42
lines changed

5 files changed

+148
-42
lines changed

README.rst

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ Installing from PyPI
3232
--------------------
3333
.. note:: This library is not available on PyPI yet. Install documentation is included
3434
as a standard element. Stay tuned for PyPI availability!
35-
.. todo:: Remove the above note if PyPI version is/will be available at time of release.
36-
If the library is not planned for PyPI, remove the entire 'Installing from PyPI' section.
35+
3736
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
3837
PyPI <https://pypi.org/project/adafruit-circuitpython-vcnl4040/>`_. To install for current user:
3938

@@ -59,7 +58,22 @@ To install in a virtual environment in your current project:
5958
Usage Example
6059
=============
6160

62-
.. todo:: Add a quick, simple example. It and other examples should live in the examples folder and be included in docs/examples.rst.
61+
.. code-block:: python
62+
63+
import time
64+
import board
65+
import busio
66+
import adafruit_vcnl4040
67+
68+
i2c = busio.I2C(board.SCL, board.SDA)
69+
sensor = adafruit_vcnl4040.VCNL4040(i2c)
70+
71+
while True:
72+
print("Proximity:", sensor.proximity)
73+
print("Light:", sensor.light)
74+
print("White:", sensor.white)
75+
time.sleep(0.3)
76+
6377
6478
Contributing
6579
============

adafruit_vcnl4040.py

Lines changed: 116 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
3434
**Hardware:**
3535
36-
.. * `Adafruit VCNL4040 <url>`_
36+
.. * `Adafruit VCNL4040 <https://www.adafruit.com/products>`_
3737
3838
**Software and Dependencies:**
3939
@@ -46,17 +46,22 @@
4646
"""
4747

4848
from micropython import const
49-
import adafruit_bus_device.i2c_device as i2c_device
49+
import adafruit_bus_device.i2c_device as i2cdevice
5050
from adafruit_register.i2c_struct import UnaryStruct, ROUnaryStruct
51-
from adafruit_register.i2c_bits import RWBits, ROBits
51+
from adafruit_register.i2c_bits import RWBits
5252
from adafruit_register.i2c_bit import RWBit, ROBit
5353
import digitalio
5454

5555
__version__ = "0.0.0-auto.0"
5656
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VCNL4040.git"
5757

5858

59-
class VCNL4040:
59+
class VCNL4040: # pylint: disable=too-few-public-methods
60+
"""Driver for the VCNL4040 proximity and ambient light sensor.
61+
62+
:param busio.I2C i2c_bus: The I2C bus the VCNL4040 is connected to.
63+
64+
"""
6065
# Ambient light sensor integration times
6166
ALS_80MS = const(0x0)
6267
ALS_160MS = const(0x1)
@@ -89,89 +94,165 @@ class VCNL4040:
8994
LED_1_160 = const(0x2)
9095
LED_1_320 = const(0x3)
9196

92-
# Proximity sensor enable/disable options
97+
# Proximity sensor interrupt enable/disable options
9398
PS_INT_DISABLE = const(0x0)
9499
PS_INT_CLOSE = const(0x1)
95100
PS_INT_AWAY = const(0x2)
96101
PS_INT_CLOSE_AWAY = const(0x3)
97102

98103
# ID_LM - Device ID, address
99104
_device_id = UnaryStruct(0x0C, "<H")
100-
"""The device ID"""
105+
"""The device ID."""
101106

102107
# PS_Data_LM - PS output data
103108
proximity = ROUnaryStruct(0x08, "<H")
104-
"""The proximity data"""
109+
"""Proximity data.
110+
111+
This example prints the proximity data. Move your hand towards the sensor to see the values
112+
change.
113+
114+
..code-block:: python
115+
116+
import time
117+
import board
118+
import busio
119+
import adafruit_vcnl4040
120+
121+
i2c = busio.I2C(board.SCL, board.SDA)
122+
sensor = adafruit_vcnl4040.VCNL4040(i2c)
123+
124+
while True:
125+
print("Proximity:", sensor.proximity)
126+
time.sleep(0.1)
127+
"""
105128

106129
# PS_CONF1 - PS duty ratio, integration time, persistence, enable/disable
107130
# PS_CONF2 - PS output resolution selection, interrupt trigger method
108131
# PS_CONF3 - PS smart persistence, active force mode
109132
proximity_shutdown = RWBit(0x03, 0, register_width=2)
110-
"""Proximity sensor shutdown. When True, proximity data is disabled."""
133+
"""Proximity sensor shutdown. When ``True``, proximity data is disabled."""
111134
proximity_integration_time = RWBits(3, 0x03, 1, register_width=2)
112-
"""Proximity sensor integration time setting."""
113-
proximity_persistence = RWBits(2, 0x03, 4, register_width=2)
114-
"""Proximity sensor interrupt persistence setting"""
115-
proximity_smart_persistence = RWBit(0x04, 4, register_width=2)
116-
"""Proximity sensor smart persistence. 0 when disabled, 1 when enabled."""
135+
"""Proximity sensor integration time setting. Integration times are 1T, 1.5T, 2T, 2.5T, 3T,
136+
3.5T, 4T, and 8T. Options are: PS_1T, PS_1_5T, PS_2T, PS_2_5T, PS_3T, PS_3_5T, PS_4T, PS_8T.
137+
"""
117138
proximity_interrupt = RWBits(2, 0x03, 8, register_width=2)
118-
"""Interrupt enable. Options are: (above)"""
119-
proximity_cancellation_level = UnaryStruct(0x05, "<H")
139+
"""Interrupt enable. Interrupt setting are close, away, close and away, or disabled. Options
140+
are: PS_INT_DISABLE, PS_INT_CLOSE, PS_INT_AWAY, PS_INT_CLOSE_AWAY."""
120141
proximity_bits = RWBit(0x03, 11, register_width=2)
121-
"""0 when proximity sensor output is 12 bits, 1 when proximity sensor output is 16 bits."""
122-
proximity_force = RWBit(0x04, 3, register_width=2)
123-
"""0 when active force mode is disabled (default), 1 when active force mode is enabled."""
124-
proximity_force_trigger = RWBit(0x04, 2, register_width=2)
125-
"""0 for no proximity active force mode trigger, 1 for trigger one time cycle. Output one cycle
126-
data every time host writes ``1`` to sensor. State returns to 0 automatically."""
142+
"""Proximity data output setting. ``0`` when proximity sensor output is 12 bits, ``1`` when
143+
proximity sensor output is 16 bits."""
127144

128145
# PS_THDL_LM - PS low interrupt threshold setting
129146
proximity_low_threshold = UnaryStruct(0x06, "<H")
147+
"""Proximity sensor interrupt low threshold setting."""
130148
# PS_THDH_LM - PS high interrupt threshold setting
131149
proximity_high_threshold = UnaryStruct(0x07, "<H")
150+
"""Proximity sensor interrupt high threshold setting."""
132151
# INT_FLAG - PS interrupt flag
133152
proximity_high_interrupt = ROBit(0x0B, 9, register_width=2)
134-
"""If close: proximity rises above high threshold interrupt trigger event"""
153+
"""If interrupt is set to ``PS_INT_CLOSE`` or ``PS_INT_CLOSE_AWAY``, trigger event when
154+
proximity rises above high threshold interrupt."""
135155
proximity_low_interrupt = ROBit(0x0B, 8, register_width=2)
136-
"""If away: proximity drops below low threshold trigger event"""
156+
"""If interrupt is set to ``PS_INT_AWAY`` or ``PS_INT_CLOSE_AWAY``, trigger event when
157+
proximity drops below low threshold."""
137158

138159
led_current = RWBits(3, 0x04, 8, register_width=2)
139-
"""LED current selection setting"""
160+
"""LED current selection setting, in mA. Options are: LED_50MA, LED_75MA, LED_100MA, LED_120MA,
161+
LED_140MA, LED_160MA, LED_180MA, LED_200MA."""
140162
led_duty_cycle = RWBits(2, 0x03, 6, register_width=2)
141-
"""Proximity sensor LED duty ratio setting"""
163+
"""Proximity sensor LED duty ratio setting. Ratios are 1/40, 1/80, 1/160, and 1/320. Options
164+
are: LED_1_40, LED_1_80, LED_1_160, LED_1_320."""
142165

143166
# ALS_Data_LM - ALS output data
144167
light = ROUnaryStruct(0x09, "<H")
145-
"""Ambient light data"""
168+
"""Ambient light data.
169+
170+
This example prints the ambient light data. Cover the sensor to see the values change.
171+
172+
..code-block:: python
173+
174+
import time
175+
import board
176+
import busio
177+
import adafruit_vcnl4040
178+
179+
i2c = busio.I2C(board.SCL, board.SDA)
180+
sensor = adafruit_vcnl4040.VCNL4040(i2c)
181+
182+
while True:
183+
print("Ambient light:", sensor.light)
184+
time.sleep(0.1)
185+
"""
146186

147187
# ALS_CONF - ALS integration time, persistence, interrupt, function enable/disable
148188
light_shutdown = RWBit(0x00, 0, register_width=2)
149-
"""Ambient light sensor shutdown. When True, ambient light data is disabled."""
189+
"""Ambient light sensor shutdown. When ``True``, ambient light data is disabled."""
150190
light_integration_time = RWBits(2, 0x00, 6, register_width=2)
151-
"""Ambient light sensor integration time setting. Longer time has higher sensitivity."""
191+
"""Ambient light sensor integration time setting. Longer time has higher sensitivity. Can be:
192+
ALS_80MS, ALS_160MS, ALS_320MS or ALS_640MS.
193+
194+
This example sets the ambient light integration time to 640ms and prints the ambient light
195+
sensor data.
196+
197+
..code-block:: python
198+
199+
import time
200+
import board
201+
import busio
202+
import adafruit_vcnl4040
203+
204+
i2c = busio.I2C(board.SCL, board.SDA)
205+
sensor = adafruit_vcnl4040.VCNL4040(i2c)
206+
207+
sensor.light_integration_time = sensor.ALS_640MS
208+
209+
while True:
210+
print("Ambient light:", sensor.light)
211+
time.sleep(0.1)
212+
"""
152213
light_interrupt = RWBit(0x00, 1, register_width=2)
153-
"""Ambient light sensor interrupt enable. 0 when disabled, 1 when enabled."""
214+
"""Ambient light sensor interrupt enable. ``True`` to enable, and ``False`` to disable."""
154215

155216
# ALS_THDL_LM - ALS low interrupt threshold setting
156217
light_low_threshold = UnaryStruct(0x02, "<H")
218+
"""Ambient light interrupt low threshold."""
157219
# ALS_THDH_LM - ALS high interrupt threshold setting
158220
light_high_threshold = UnaryStruct(0x01, "<H")
221+
"""Ambient light interrupt high threshold."""
159222
# INT_FLAG - ALS interrupt flag
160223
light_high_interrupt = ROBit(0x0B, 12, register_width=2)
161-
"""If high: ambient light sensor crosses high interrupt threshold trigger event"""
224+
"""High interrupt event. Triggered when ambient light value exceeds high threshold."""
162225
light_low_interrupt = ROBit(0x0B, 13, register_width=2)
163-
"""If low: ambient light sensor crosses low interrupt threshold trigger event"""
226+
"""Low interrupt event. Triggered when ambient light value drops below low threshold."""
164227

165228
# White_Data_LM - White output data
166229
white = ROUnaryStruct(0x0A, "<H")
167-
"""White light data"""
230+
"""White light data.
231+
232+
This example prints the white light data. Cover the sensor to see the values change.
233+
234+
..code-block:: python
235+
236+
import time
237+
import board
238+
import busio
239+
import adafruit_vcnl4040
240+
241+
i2c = busio.I2C(board.SCL, board.SDA)
242+
sensor = adafruit_vcnl4040.VCNL4040(i2c)
243+
244+
while True:
245+
print("White light:", sensor.white)
246+
time.sleep(0.1)
247+
"""
168248

169249
# PS_MS - White channel enable/disable, PS mode, PS protection setting, LED current
170250
# White_EN - PS_MS_H, 7th bit - White channel enable/disable
171-
# white_enable = RWBit(0x04, 15, register_width=2)
251+
white_enable = RWBit(0x04, 15, register_width=2)
252+
"""White data enable. ``True`` when enabled."""
172253

173254
def __init__(self, i2c, address=0x60, interrupt_pin=None):
174-
self.i2c_device = i2c_device.I2CDevice(i2c, address)
255+
self.i2c_device = i2cdevice.I2CDevice(i2c, address)
175256
if self._device_id != 0x186:
176257
raise RuntimeError("Failed to find VCNL4040 - check wiring!")
177258

@@ -181,3 +262,4 @@ def __init__(self, i2c, address=0x60, interrupt_pin=None):
181262

182263
self.proximity_shutdown = False
183264
self.light_shutdown = False
265+
self.white_enable = True

docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
# Uncomment the below if you use native CircuitPython modules such as
2121
# digitalio, micropython and busio. List the modules you use. Without it, the
2222
# autodoc module docs will fail to generate with a warning.
23-
# autodoc_mock_imports = ["digitalio", "busio"]
23+
autodoc_mock_imports = ["adafruit_register.i2c_struct", "adafruit_register.i2c_bits",
24+
"adafruit_register.i2c_bit"]
2425

2526

2627
intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'BusDevice': ('https://circuitpython.readthedocs.io/projects/busdevice/en/latest/', None),'Register': ('https://circuitpython.readthedocs.io/projects/register/en/latest/', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}

docs/index.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ Table of Contents
2323
.. toctree::
2424
:caption: Tutorials
2525

26-
.. todo:: Add any Learn guide links here. If there are none, then simply delete this todo and leave
27-
the toctree above for use later.
2826

2927
.. toctree::
3028
:caption: Related Products
3129

32-
.. todo:: Add any product links here. If there are none, then simply delete this todo and leave
33-
the toctree above for use later.
3430

3531
.. toctree::
3632
:caption: Other Links

examples/vcnl4040_simpletest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import time
2+
import board
3+
import busio
4+
import adafruit_vcnl4040
5+
6+
i2c = busio.I2C(board.SCL, board.SDA)
7+
sensor = adafruit_vcnl4040.VCNL4040(i2c)
8+
9+
while True:
10+
print("Proximity:", sensor.proximity)
11+
print("Light:", sensor.light)
12+
print("White:", sensor.white)
13+
time.sleep(0.3)

0 commit comments

Comments
 (0)