Skip to content

Commit 4ab3a33

Browse files
committed
More documentation.
1 parent ad22281 commit 4ab3a33

File tree

3 files changed

+196
-17
lines changed

3 files changed

+196
-17
lines changed

adafruit_veml7700.py

Lines changed: 185 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,208 @@
3333
3434
**Hardware:**
3535
36-
.. todo:: Add links to any specific hardware product page(s), or category page(s). Use unordered list & hyperlink rST
37-
inline format: "* `Link Text <url>`_"
36+
* `Adafruit VEML7700 <https://www.adafruit.com/products>`_
3837
3938
**Software and Dependencies:**
4039
4140
* Adafruit CircuitPython firmware for the supported boards:
4241
https://github.com/adafruit/circuitpython/releases
4342
44-
.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies based on the library's use of either.
45-
46-
# * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
47-
# * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
43+
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
44+
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
4845
"""
4946

5047
from micropython import const
51-
import adafruit_bus_device.i2c_device as i2c_device
48+
import adafruit_bus_device.i2c_device as i2cdevice
5249
from adafruit_register.i2c_struct import UnaryStruct, ROUnaryStruct
53-
from adafruit_register.i2c_bits import RWBits, ROBits
50+
from adafruit_register.i2c_bits import RWBits
5451
from adafruit_register.i2c_bit import RWBit, ROBit
5552

5653
__version__ = "0.0.0-auto.0"
5754
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VEML7700.git"
5855

5956

6057
class VEML7700:
61-
light = UnaryStruct(0x04, "<H")
58+
"""Driver for the VEML7700 ambient light sensor.
59+
60+
:param busio.I2C i2c_bus: The I2C bus the VEML7700 is connected to.
61+
62+
"""
63+
# Ambient light sensor gain settings
64+
ALS_GAIN_1 = const(0x0)
65+
ALS_GAIN_2 = const(0x1)
66+
ALS_GAIN_1_8 = const(0x2)
67+
ALS_GAIN_1_4 = const(0x3)
68+
69+
# Ambient light integration time settings
70+
ALS_25MS = const(0x0)
71+
ALS_50MS = const(0x1)
72+
ALS_100MS = const(0x2)
73+
ALS_200MS = const(0x3)
74+
ALS_400MS = const(0x4)
75+
ALS_800MS = const(0x5)
76+
77+
# Gain value integers
78+
gain_values = {
79+
ALS_GAIN_2: 2,
80+
ALS_GAIN_1: 1,
81+
ALS_GAIN_1_4: 0.25,
82+
ALS_GAIN_1_8: 0.125
83+
}
84+
85+
# Integration time value integers
86+
integration_time_values = {
87+
ALS_25MS: 25,
88+
ALS_50MS: 50,
89+
ALS_100MS: 100,
90+
ALS_200MS: 200,
91+
ALS_400MS: 400,
92+
ALS_800MS: 800
93+
}
94+
95+
light = ROUnaryStruct(0x04, "<H")
96+
"""Ambient light data.
97+
98+
This example prints the ambient light data. Cover the sensor to see the values change.
99+
100+
..code-block:: python
101+
102+
import time
103+
import board
104+
import busio
105+
import adafruit_veml7700
106+
107+
i2c = busio.I2C(board.SCL, board.SDA)
108+
veml7700 = adafruit_veml7700.VEML7700(i2c)
109+
110+
while True:
111+
print("Ambient light:", veml7700.light)
112+
time.sleep(0.1)
113+
"""
114+
white = ROUnaryStruct(0x05, "<H")
115+
"""White light data.
116+
117+
This example prints the white light data. Cover the sensor to see the values change.
118+
119+
..code-block:: python
120+
121+
import time
122+
import board
123+
import busio
124+
import adafruit_veml7700
125+
126+
i2c = busio.I2C(board.SCL, board.SDA)
127+
veml7700 = adafruit_veml7700.VEML7700(i2c)
128+
129+
while True:
130+
print("White light:", veml7700.white)
131+
time.sleep(0.1)
132+
"""
62133
light_shutdown = RWBit(0x00, 0, register_width=2)
134+
"""Ambient light sensor shutdown. When ``True``, ambient light sensor is disabled."""
135+
light_interrupt = RWBit(0x00, 1, register_width=2)
136+
"""Enable interrupt. ``True`` to enable, ``False`` to disable."""
137+
light_gain = RWBits(2, 0x00, 11, register_width=2)
138+
"""Ambient light gain setting. Gain settings are 2, 1, 1/4 and 1/8. Can be:
139+
ALS_GAIN_2, ALS_GAIN_1, ALS_GAIN_1_4, ALS_GAIN_1_8.
140+
141+
This example sets the ambient light gain to 2 and prints the ambient light sensor data.
142+
143+
..code-block:: python
144+
145+
import time
146+
import board
147+
import busio
148+
import adafruit_veml7700
149+
150+
i2c = busio.I2C(board.SCL, board.SDA)
151+
veml7700 = adafruit_vcnl4040.VCNL4040(i2c)
152+
153+
veml7700.light_gain = veml7700.ALS_GAIN_2
154+
155+
while True:
156+
print("Ambient light:", veml7700.light)
157+
time.sleep(0.1)
158+
159+
"""
160+
light_integration_time = RWBits(4, 0x00, 6, register_width=2)
161+
"""Ambient light integration time setting. Longer time has higher sensitivity. Can be:
162+
ALS_25MS, ALS_50MS, ALS_100MS, ALS_200MS, ALS_400MS, ALS_800MS.
163+
164+
This example sets the ambient light integration time to 400ms and prints the ambient light
165+
sensor data.
166+
167+
..code-block:: python
168+
169+
import time
170+
import board
171+
import busio
172+
import adafruit_veml7700
173+
174+
i2c = busio.I2C(board.SCL, board.SDA)
175+
veml7700 = adafruit_vcnl4040.VCNL4040(i2c)
176+
177+
veml7700.light_integration_time = veml7700.ALS_400MS
178+
179+
while True:
180+
print("Ambient light:", veml7700.light)
181+
time.sleep(0.1)
182+
183+
"""
184+
185+
light_high_threshold = UnaryStruct(0x01, "<H")
186+
"""Ambient light sensor interrupt high threshold setting."""
187+
light_low_threshold = UnaryStruct(0x02, "<H")
188+
"""Ambient light sensor interrupt low threshold setting."""
189+
light_interrupt_high = ROBit(0x06, 14, register_width=2)
190+
"""Ambient light high threshold interrupt flag. Triggered when high threshold exceeded."""
191+
light_interrupt_low = ROBit(0x06, 15, register_width=2)
192+
"""Ambient light low threshold interrupt flag. Triggered when low threshold exceeded."""
193+
194+
def __init__(self, i2c_bus, address=0x10):
195+
self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address)
196+
self.light_shutdown = False # Enable the ambient light sensor
197+
198+
def integration_time_value(self):
199+
"""Integration time value in integer form. Used for calculating ``resolution``."""
200+
integration_time = self.light_integration_time
201+
return self.integration_time_values[integration_time]
202+
203+
def gain_value(self):
204+
"""Gain value in integer form. Used for calculating ``resolution``."""
205+
gain = self.light_gain
206+
return self.gain_values[gain]
207+
208+
def resolution(self):
209+
"""Calculate the ``resolution`` necessary to calculate lux. Based on integration time and
210+
gain settings."""
211+
resolution_at_max = 0.0036
212+
gain_max = 2
213+
integration_time_max = 800
214+
215+
if self.gain_value() == gain_max and self.integration_time_value() == integration_time_max:
216+
return resolution_at_max
217+
return resolution_at_max * (integration_time_max / self.integration_time_value()) * \
218+
(gain_max / self.gain_value())
219+
220+
@property
221+
def lux(self):
222+
"""Light value in lux.
223+
224+
This example prints the light data in lux. Cover the sensor to see the values change.
225+
226+
..code-block:: python
227+
228+
import time
229+
import board
230+
import busio
231+
import adafruit_veml7700
63232
64-
def __init__(self, i2c, address=0x10):
65-
self.i2c_device = i2c_device.I2CDevice(i2c, address)
233+
i2c = busio.I2C(board.SCL, board.SDA)
234+
veml7700 = adafruit_veml7700.VEML7700(i2c)
66235
67-
self.light_shutdown = False
236+
while True:
237+
print("Lux:", veml7700.lux)
238+
time.sleep(0.1)
239+
"""
240+
return self.resolution() * self.light

docs/index.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,9 @@ 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.
28-
2926
.. toctree::
3027
:caption: Related Products
3128

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.
3429

3530
.. toctree::
3631
:caption: Other Links

examples/veml7700_simpletest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import time
2+
import board
3+
import busio
4+
import adafruit_veml7700
5+
6+
i2c = busio.I2C(board.SCL, board.SDA)
7+
veml7700 = adafruit_veml7700.VEML7700(i2c)
8+
9+
while True:
10+
print("Ambient light:", veml7700.light)
11+
time.sleep(0.1)

0 commit comments

Comments
 (0)