-
Notifications
You must be signed in to change notification settings - Fork 9
/
adafruit_veml6070.py
322 lines (262 loc) · 10.5 KB
/
adafruit_veml6070.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# SPDX-FileCopyrightText: 2018 Michael Schroeder(sommersoft)
#
# SPDX-License-Identifier: MIT
# This CircuitPython library is based on Limor Fried's Arduino
# library for the Adafruit VEML6070 UV Sensor Breakout.
# License information from that library is included below.
#
# Designed specifically to work with the VEML6070 sensor from Adafruit
# ----> https://www.adafruit.com/products/2899
#
# These sensors use I2C to communicate, 2 pins are required to
# interface.
#
# Adafruit invests time and resources providing this open source code,
# please support Adafruit and open-source hardware by purchasing
# products from Adafruit!
#
# Arduino Library: Written by Limor Fried/Ladyada for Adafruit Industries.
# MIT license, all text above must be included in any redistribution
# https://github.com/adafruit/Adafruit_VEML6070
"""
`adafruit_veml6070`
====================================================
CircuitPython library to support VEML6070 UV Index sensor.
* Author(s): Limor Fried & Michael Schroeder
Implementation Notes
--------------------
**Hardware:**
* Adafruit `VEML6070 UV Index Sensor Breakout
<https://www.adafruit.com/products/2899>`_ (Product ID: 2899)
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://circuitpython.org/downloads
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
**Notes:**
#. Datasheet: https://cdn-learn.adafruit.com/assets/assets/000/032/482/original/veml6070.pdf
"""
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VEML6070.git"
from adafruit_bus_device.i2c_device import I2CDevice
from micropython import const
try:
import typing # pylint: disable=unused-import
from busio import I2C
except ImportError:
pass
# Set I2C addresses:
_VEML6070_ADDR_ARA = const(0x18 >> 1)
_VEML6070_ADDR_CMD = const(0x70 >> 1)
_VEML6070_ADDR_LOW = const(0x71 >> 1)
_VEML6070_ADDR_HIGH = const(0x73 >> 1)
# Integration Time dictionary. [0] is the byte setting; [1] is the risk
# level divisor.
_VEML6070_INTEGRATION_TIME = {
"VEML6070_HALF_T": [0x00, 0],
"VEML6070_1_T": [0x01, 1],
"VEML6070_2_T": [0x02, 2],
"VEML6070_4_T": [0x03, 4],
}
# UV Risk Level dictionary. [0],[1] are the lower and uppper bounds of the range
_VEML6070_RISK_LEVEL = {
"LOW": [0, 560],
"MODERATE": [561, 1120],
"HIGH": [1121, 1494],
"VERY HIGH": [1495, 2054],
"EXTREME": [2055, 9999],
}
class VEML6070:
"""
Driver base for the VEML6070 UV Light Sensor
:param ~busio.I2C i2c_bus: The I2C bus the device is connected to
:param str _veml6070_it: The integration time you'd like to set initially. Available
options: :const:`VEML6070_HALF_T`, :const:`VEML6070_1_T`,
:const:`VEML6070_2_T`, and
:const:`VEML6070_4_T`. The higher the '_x_' value, the more accurate
the reading is (at the cost of less samples per reading).
Defaults to :const:`VEML6070_1_T` if parameter not passed. To change
setting after initialization,
``VEML6070.set_integration_time(new_it)``.
:param bool ack: The initial setting of ``ACKnowledge`` on alert. Defaults to `False`
if parameter not passed. To change setting after initialization,
use ``VEML6070.set_ack(new_ack)``.
**Quickstart: Importing and using the device VEML6070**
Here is an example of using the :class:`VEML6070` class.
First you will need to import the libraries to use the sensor
.. code-block:: python
import board
import adafruit_veml6070
Once this is done you can define your `board.I2C` object and define your sensor object
.. code-block:: python
i2c = board.I2C() # uses board.SCL and board.SDA
uv = adafruit_veml6070.VEML6070(i2c)
Now you have access to the :attr:`uv_raw` attribute and the calculate the risk level
.. code-block:: python
uv_raw = uv.uv_raw
risk_level = uv.get_index(uv_raw)
"""
def __init__(
self, i2c_bus: I2C, _veml6070_it: str = "VEML6070_1_T", ack: bool = False
) -> None:
# Check if the IT is valid
if _veml6070_it not in _VEML6070_INTEGRATION_TIME:
raise ValueError(
"Integration Time invalid. Valid values are: ",
_VEML6070_INTEGRATION_TIME.keys(),
)
# Check if ACK is valid
if ack not in (True, False):
raise ValueError("ACK must be 'True' or 'False'.")
# Passed checks; set self values
self._ack = int(ack)
self._ack_thd = 0x00
self._it = _veml6070_it
# Latch the I2C addresses
self.i2c_cmd = I2CDevice(i2c_bus, _VEML6070_ADDR_CMD)
self.i2c_low = I2CDevice(i2c_bus, _VEML6070_ADDR_LOW)
self.i2c_high = I2CDevice(i2c_bus, _VEML6070_ADDR_HIGH)
# Initialize the VEML6070
ara_buf = bytearray(1)
try:
with I2CDevice(i2c_bus, _VEML6070_ADDR_ARA) as ara:
ara.readinto(ara_buf)
except ValueError: # the ARA address is never valid? datasheet error?
pass
self.buf = bytearray(1)
self.buf[0] = (
self._ack << 5 | _VEML6070_INTEGRATION_TIME[self._it][0] << 2 | 0x02
)
with self.i2c_cmd as i2c_cmd:
i2c_cmd.write(self.buf)
@property
def uv_raw(self) -> int:
"""
Reads and returns the value of the UV intensity.
"""
buffer = bytearray(2)
with self.i2c_low as i2c_low:
i2c_low.readinto(buffer, end=1)
with self.i2c_high as i2c_high:
i2c_high.readinto(buffer, start=1)
return buffer[1] << 8 | buffer[0]
@property
def ack(self) -> int:
"""
Turns on or off the ACKnowledge function of the sensor. The ACK function will send
a signal to the host when the value of the sensed UV light changes beyond the
programmed threshold.
"""
return self._ack
@ack.setter
def ack(self, new_ack: int) -> None:
if new_ack != bool(new_ack):
raise ValueError("ACK must be '1' (On) or '0' (Off).")
self._ack = int(new_ack)
self.buf[0] = (
self._ack << 5
| self._ack_thd << 4
| _VEML6070_INTEGRATION_TIME[self._it][0] << 2
| 0x02
)
with self.i2c_cmd as i2c_cmd:
i2c_cmd.write(self.buf)
@property
def ack_threshold(self) -> int:
"""
The ACKnowledge Threshold, which alerts the host controller to value changes
greater than the threshold. Available settings are: :const:`0` = 102 steps;
:const:`1` = 145 steps. :const:`0` is the default setting.
"""
return self._ack_thd
@ack_threshold.setter
def ack_threshold(self, new_ack_thd: int) -> None:
if new_ack_thd not in (0, 1):
raise ValueError("ACK Threshold must be '0' or '1'.")
self._ack_thd = int(new_ack_thd)
self.buf[0] = (
self._ack << 5
| self._ack_thd << 4
| _VEML6070_INTEGRATION_TIME[self._it][0] << 2
| 0x02
)
with self.i2c_cmd as i2c_cmd:
i2c_cmd.write(self.buf)
@property
def integration_time(self) -> str:
"""
The Integration Time of the sensor, which is the refresh interval of the
sensor. The higher the refresh interval, the more accurate the reading is (at
the cost of less sampling). The available settings are: :const:`VEML6070_HALF_T`,
:const:`VEML6070_1_T`, :const:`VEML6070_2_T`, :const:`VEML6070_4_T`.
"""
return self._it
@integration_time.setter
def integration_time(self, new_it: str) -> None:
if new_it not in _VEML6070_INTEGRATION_TIME:
raise ValueError(
"Integration Time invalid. Valid values are: ",
_VEML6070_INTEGRATION_TIME.keys(),
)
self._it = new_it
self.buf[0] = (
self._ack << 5
| self._ack_thd << 4
| _VEML6070_INTEGRATION_TIME[new_it][0] << 2
| 0x02
)
with self.i2c_cmd as i2c_cmd:
i2c_cmd.write(self.buf)
def sleep(self) -> None:
"""
Puts the VEML6070 into sleep ('shutdown') mode. Datasheet claims a current draw
of 1uA while in shutdown.
"""
self.buf[0] = 0x03
with self.i2c_cmd as i2c_cmd:
i2c_cmd.write(self.buf)
def wake(self) -> None:
"""
Wakes the VEML6070 from sleep. :class:`VEML6070.uv_raw` will also wake from sleep.
"""
self.buf[0] = (
self._ack << 5
| self._ack_thd << 4
| _VEML6070_INTEGRATION_TIME[self._it][0] << 2
| 0x02
)
with self.i2c_cmd as i2c_cmd:
i2c_cmd.write(self.buf)
def get_index(self, _raw: int) -> str:
"""
Calculates the UV Risk Level based on the captured UV reading. Requires the ``_raw``
argument (from :meth:`veml6070.uv_raw`). Risk level is available for Integration Times (IT)
1, 2, & 4. The result is automatically scaled to the current IT setting.
========= ========
LEVEL* UV Index
========= ========
LOW 0-2
MODERATE 3-5
HIGH 6-7
VERY HIGH 8-10
EXTREME >=11
========= ========
* Not to be considered as accurate condition reporting.
Calculation is based on VEML6070 Application Notes:
http://www.vishay.com/docs/84310/designingveml6070.pdf
"""
# get the divisor for the current IT
div = _VEML6070_INTEGRATION_TIME[self._it][1]
if div == 0:
raise ValueError(
"[veml6070].get_index only available for Integration Times 1, 2, & 4.",
"Use [veml6070].set_integration_time(new_it) to change the Integration Time.",
)
# adjust the raw value using the divisor, then loop through the Risk Level dict
# to find which range the adjusted raw value is in.
raw_adj = int(_raw / div)
for levels, val in _VEML6070_RISK_LEVEL.items():
tmp_range = range(val[0], val[1])
if raw_adj in tmp_range:
risk = levels
break
return risk