Skip to content

Commit 7263ff8

Browse files
authored
Merge pull request #4 from siddacious/add_lux
adding lux and white scaling w/ gain/IT
2 parents 55a924e + e971b5e commit 7263ff8

File tree

1 file changed

+78
-47
lines changed

1 file changed

+78
-47
lines changed

adafruit_vcnl4040.py

Lines changed: 78 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -171,59 +171,85 @@ def proximity_low_interrupt(self):
171171

172172

173173
led_current = RWBits(3, 0x04, 8, register_width=2)
174-
"""LED current selection setting, in mA. Options are: LED_50MA, LED_75MA, LED_100MA, LED_120MA,
174+
"""LED current selection setting, in mA. Options are LED_50MA, LED_75MA, LED_100MA, LED_120MA,
175175
LED_140MA, LED_160MA, LED_180MA, LED_200MA."""
176+
176177
led_duty_cycle = RWBits(2, 0x03, 6, register_width=2)
177178
"""Proximity sensor LED duty ratio setting. Ratios are 1/40, 1/80, 1/160, and 1/320. Options
178179
are: LED_1_40, LED_1_80, LED_1_160, LED_1_320."""
179180

180-
# ALS_Data_LM - ALS output data
181181
light = ROUnaryStruct(0x09, "<H")
182-
"""Ambient light data.
182+
"""Raw ambient light data. The raw ambient light data which will change with integration time
183+
and gain settings changes. Use ``lux`` to get the correctly scaled value for the current
184+
integration time and gain settings
185+
"""
183186

184-
This example prints the ambient light data. Cover the sensor to see the values change.
187+
@property
188+
def lux(self):
189+
"""Ambient light data in lux. Represents the raw sensor data scaled according to the current
190+
integration time and gain settings.
185191
186-
.. code-block:: python
192+
This example prints the ambient light data. Cover the sensor to see the values change.
187193
188-
import time
189-
import board
190-
import busio
191-
import adafruit_vcnl4040
194+
.. code-block:: python
192195
193-
i2c = busio.I2C(board.SCL, board.SDA)
194-
sensor = adafruit_vcnl4040.VCNL4040(i2c)
196+
import time
197+
import board
198+
import busio
199+
import adafruit_vcnl4040
200+
201+
i2c = busio.I2C(board.SCL, board.SDA)
202+
sensor = adafruit_vcnl4040.VCNL4040(i2c)
203+
204+
while True:
205+
print("Ambient light: %.2f lux"%sensor.lux)
206+
time.sleep(0.1)
207+
"""
208+
return self.light * (0.1 /(1 << self.light_integration_time))
195209

196-
while True:
197-
print("Ambient light:", sensor.light)
198-
time.sleep(0.1)
199-
"""
200210

201211
# ALS_CONF - ALS integration time, persistence, interrupt, function enable/disable
202212
light_shutdown = RWBit(0x00, 0, register_width=2)
203213
"""Ambient light sensor shutdown. When ``True``, ambient light data is disabled."""
204-
light_integration_time = RWBits(2, 0x00, 6, register_width=2)
205-
"""Ambient light sensor integration time setting. Longer time has higher sensitivity. Can be:
206-
ALS_80MS, ALS_160MS, ALS_320MS or ALS_640MS.
207214

208-
This example sets the ambient light integration time to 640ms and prints the ambient light
209-
sensor data.
215+
_light_integration_time = RWBits(2, 0x00, 6, register_width=2)
216+
@property
217+
def light_integration_time(self):
218+
"""Ambient light sensor integration time setting. Longer time has higher sensitivity.
219+
Can be: ALS_80MS, ALS_160MS, ALS_320MS or ALS_640MS.
210220
211-
.. code-block:: python
221+
This example sets the ambient light integration time to 640ms and prints the ambient light
222+
sensor data.
212223
213-
import time
214-
import board
215-
import busio
216-
import adafruit_vcnl4040
224+
.. code-block:: python
217225
218-
i2c = busio.I2C(board.SCL, board.SDA)
219-
sensor = adafruit_vcnl4040.VCNL4040(i2c)
226+
import time
227+
import board
228+
import busio
229+
import adafruit_vcnl4040
230+
231+
i2c = busio.I2C(board.SCL, board.SDA)
232+
sensor = adafruit_vcnl4040.VCNL4040(i2c)
233+
234+
sensor.light_integration_time = sensor.ALS_640MS
235+
236+
while True:
237+
print("Ambient light:", sensor.light)
238+
"""
239+
return self._light_integration_time
240+
241+
@light_integration_time.setter
242+
def light_integration_time(self, new_it):
243+
from time import sleep
244+
# IT values are in 0-3 -> 80-640ms
245+
old_it_ms = ((8<< self._light_integration_time)*10)
246+
new_it_ms = ((8<< new_it)*10)
247+
it_delay_seconds = (old_it_ms + new_it_ms + 1) * 0.001
248+
249+
self._light_integration_time = new_it
250+
sleep(it_delay_seconds)
220251

221-
sensor.light_integration_time = sensor.ALS_640MS
222252

223-
while True:
224-
print("Ambient light:", sensor.light)
225-
time.sleep(0.1)
226-
"""
227253
light_interrupt = RWBit(0x00, 1, register_width=2)
228254
"""Ambient light sensor interrupt enable. ``True`` to enable, and ``False`` to disable."""
229255

@@ -245,26 +271,31 @@ def light_low_interrupt(self):
245271
"""Low interrupt event. Triggered when ambient light value drops below low threshold."""
246272
return self._get_and_clear_cached_interrupt_state(self.ALS_IF_L)
247273

248-
# White_Data_LM - White output data
249-
white = ROUnaryStruct(0x0A, "<H")
250-
"""White light data.
251274

252-
This example prints the white light data. Cover the sensor to see the values change.
275+
_raw_white = ROUnaryStruct(0x0A, "<H")
276+
@property
253277

254-
.. code-block:: python
278+
def white(self):
279+
"""White light data scaled according to the current integration time and gain settings.
255280
256-
import time
257-
import board
258-
import busio
259-
import adafruit_vcnl4040
281+
This example prints the white light data. Cover the sensor to see the values change.
260282
261-
i2c = busio.I2C(board.SCL, board.SDA)
262-
sensor = adafruit_vcnl4040.VCNL4040(i2c)
283+
.. code-block:: python
284+
285+
import time
286+
import board
287+
import busio
288+
import adafruit_vcnl4040
289+
290+
i2c = busio.I2C(board.SCL, board.SDA)
291+
sensor = adafruit_vcnl4040.VCNL4040(i2c)
292+
293+
while True:
294+
print("White light:", sensor.white)
295+
time.sleep(0.1)
296+
"""
297+
return self._raw_white * (0.1 /(1 << self.light_integration_time))
263298

264-
while True:
265-
print("White light:", sensor.white)
266-
time.sleep(0.1)
267-
"""
268299

269300
# PS_MS - White channel enable/disable, PS mode, PS protection setting, LED current
270301
# White_EN - PS_MS_H, 7th bit - White channel enable/disable

0 commit comments

Comments
 (0)