33
33
34
34
**Hardware:**
35
35
36
- .. * `Adafruit VCNL4040 <url >`_
36
+ .. * `Adafruit VCNL4040 <https://www.adafruit.com/products >`_
37
37
38
38
**Software and Dependencies:**
39
39
46
46
"""
47
47
48
48
from micropython import const
49
- import adafruit_bus_device .i2c_device as i2c_device
49
+ import adafruit_bus_device .i2c_device as i2cdevice
50
50
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
52
52
from adafruit_register .i2c_bit import RWBit , ROBit
53
53
import digitalio
54
54
55
55
__version__ = "0.0.0-auto.0"
56
56
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VCNL4040.git"
57
57
58
58
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
+ """
60
65
# Ambient light sensor integration times
61
66
ALS_80MS = const (0x0 )
62
67
ALS_160MS = const (0x1 )
@@ -89,89 +94,165 @@ class VCNL4040:
89
94
LED_1_160 = const (0x2 )
90
95
LED_1_320 = const (0x3 )
91
96
92
- # Proximity sensor enable/disable options
97
+ # Proximity sensor interrupt enable/disable options
93
98
PS_INT_DISABLE = const (0x0 )
94
99
PS_INT_CLOSE = const (0x1 )
95
100
PS_INT_AWAY = const (0x2 )
96
101
PS_INT_CLOSE_AWAY = const (0x3 )
97
102
98
103
# ID_LM - Device ID, address
99
104
_device_id = UnaryStruct (0x0C , "<H" )
100
- """The device ID"""
105
+ """The device ID. """
101
106
102
107
# PS_Data_LM - PS output data
103
108
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
+ """
105
128
106
129
# PS_CONF1 - PS duty ratio, integration time, persistence, enable/disable
107
130
# PS_CONF2 - PS output resolution selection, interrupt trigger method
108
131
# PS_CONF3 - PS smart persistence, active force mode
109
132
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."""
111
134
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
+ """
117
138
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."""
120
141
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."""
127
144
128
145
# PS_THDL_LM - PS low interrupt threshold setting
129
146
proximity_low_threshold = UnaryStruct (0x06 , "<H" )
147
+ """Proximity sensor interrupt low threshold setting."""
130
148
# PS_THDH_LM - PS high interrupt threshold setting
131
149
proximity_high_threshold = UnaryStruct (0x07 , "<H" )
150
+ """Proximity sensor interrupt high threshold setting."""
132
151
# INT_FLAG - PS interrupt flag
133
152
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."""
135
155
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."""
137
158
138
159
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."""
140
162
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."""
142
165
143
166
# ALS_Data_LM - ALS output data
144
167
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
+ """
146
186
147
187
# ALS_CONF - ALS integration time, persistence, interrupt, function enable/disable
148
188
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."""
150
190
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
+ """
152
213
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 ."""
154
215
155
216
# ALS_THDL_LM - ALS low interrupt threshold setting
156
217
light_low_threshold = UnaryStruct (0x02 , "<H" )
218
+ """Ambient light interrupt low threshold."""
157
219
# ALS_THDH_LM - ALS high interrupt threshold setting
158
220
light_high_threshold = UnaryStruct (0x01 , "<H" )
221
+ """Ambient light interrupt high threshold."""
159
222
# INT_FLAG - ALS interrupt flag
160
223
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. """
162
225
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. """
164
227
165
228
# White_Data_LM - White output data
166
229
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
+ """
168
248
169
249
# PS_MS - White channel enable/disable, PS mode, PS protection setting, LED current
170
250
# 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."""
172
253
173
254
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 )
175
256
if self ._device_id != 0x186 :
176
257
raise RuntimeError ("Failed to find VCNL4040 - check wiring!" )
177
258
@@ -181,3 +262,4 @@ def __init__(self, i2c, address=0x60, interrupt_pin=None):
181
262
182
263
self .proximity_shutdown = False
183
264
self .light_shutdown = False
265
+ self .white_enable = True
0 commit comments