|
| 1 | +# SPDX-FileCopyrightText: 2019 Kattni Rembor for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +`adafruit_veml7700` |
| 7 | +================================================================================ |
| 8 | +
|
| 9 | +CircuitPython driver for VEML7700 high precision I2C ambient light sensor. |
| 10 | +
|
| 11 | +
|
| 12 | +* Author(s): Kattni Rembor |
| 13 | +
|
| 14 | +Implementation Notes |
| 15 | +-------------------- |
| 16 | +
|
| 17 | +**Hardware:** |
| 18 | +
|
| 19 | +* `Adafruit VEML7700 Lux Sensor - I2C Light Sensor |
| 20 | + <https://www.adafruit.com/product/4162>`_ (Product ID: 4162) |
| 21 | +
|
| 22 | +**Software and Dependencies:** |
| 23 | +
|
| 24 | +* Adafruit CircuitPython firmware for the supported boards: |
| 25 | + https://circuitpython.org/downloads |
| 26 | +
|
| 27 | +* Adafruit's Bus Device library: |
| 28 | + https://github.com/adafruit/Adafruit_CircuitPython_BusDevice |
| 29 | +
|
| 30 | +* Adafruit's Register library: |
| 31 | + https://github.com/adafruit/Adafruit_CircuitPython_Register |
| 32 | +""" |
| 33 | + |
| 34 | +from micropython import const |
| 35 | +import adafruit_bus_device.i2c_device as i2cdevice |
| 36 | +from adafruit_register.i2c_struct import ROUnaryStruct |
| 37 | +from adafruit_register.i2c_bits import RWBits |
| 38 | +from adafruit_register.i2c_bit import RWBit |
| 39 | + |
| 40 | +try: |
| 41 | + import typing # pylint: disable=unused-import |
| 42 | + from busio import I2C |
| 43 | +except ImportError: |
| 44 | + pass |
| 45 | + |
| 46 | +__version__ = "2.0.2" |
| 47 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VEML7700.git" |
| 48 | + |
| 49 | + |
| 50 | +class VEML7700: |
| 51 | + """Driver for the VEML7700 ambient light sensor. |
| 52 | +
|
| 53 | + :param ~busio.I2C i2c_bus: The I2C bus the device is connected to |
| 54 | + :param int address: The I2C device address. Defaults to :const:`0x10` |
| 55 | +
|
| 56 | + """ |
| 57 | + |
| 58 | + # Ambient light sensor gain settings |
| 59 | + ALS_GAIN_1 = const(0x0) |
| 60 | + ALS_GAIN_2 = const(0x1) |
| 61 | + ALS_GAIN_1_8 = const(0x2) |
| 62 | + ALS_GAIN_1_4 = const(0x3) |
| 63 | + |
| 64 | + # Ambient light integration time settings |
| 65 | + |
| 66 | + ALS_25MS = const(0xC) |
| 67 | + ALS_50MS = const(0x8) |
| 68 | + ALS_100MS = const(0x0) |
| 69 | + ALS_200MS = const(0x1) |
| 70 | + ALS_400MS = const(0x2) |
| 71 | + ALS_800MS = const(0x3) |
| 72 | + |
| 73 | + # Gain value integers |
| 74 | + gain_values = { |
| 75 | + ALS_GAIN_2: 2, |
| 76 | + ALS_GAIN_1: 1, |
| 77 | + ALS_GAIN_1_4: 0.25, |
| 78 | + ALS_GAIN_1_8: 0.125, |
| 79 | + } |
| 80 | + |
| 81 | + # Integration time value integers |
| 82 | + integration_time_values = { |
| 83 | + ALS_25MS: 25, |
| 84 | + ALS_50MS: 50, |
| 85 | + ALS_100MS: 100, |
| 86 | + ALS_200MS: 200, |
| 87 | + ALS_400MS: 400, |
| 88 | + ALS_800MS: 800, |
| 89 | + } |
| 90 | + |
| 91 | + # ALS - Ambient light sensor high resolution output data |
| 92 | + light = ROUnaryStruct(0x04, "<H") |
| 93 | + """Ambient light data. |
| 94 | +
|
| 95 | + This example prints the ambient light data. Cover the sensor to see the values change. |
| 96 | +
|
| 97 | + .. code-block:: python |
| 98 | +
|
| 99 | + import time |
| 100 | + import board |
| 101 | + import adafruit_veml7700 |
| 102 | +
|
| 103 | + i2c = board.I2C() # uses board.SCL and board.SDA |
| 104 | + veml7700 = adafruit_veml7700.VEML7700(i2c) |
| 105 | +
|
| 106 | + while True: |
| 107 | + print("Ambient light:", veml7700.light) |
| 108 | + time.sleep(0.1) |
| 109 | + """ |
| 110 | + |
| 111 | + # WHITE - White channel output data |
| 112 | + white = ROUnaryStruct(0x05, "<H") |
| 113 | + """White light data. |
| 114 | +
|
| 115 | + This example prints the white light data. Cover the sensor to see the values change. |
| 116 | +
|
| 117 | + .. code-block:: python |
| 118 | +
|
| 119 | + import time |
| 120 | + import board |
| 121 | + import adafruit_veml7700 |
| 122 | +
|
| 123 | + i2c = board.I2C() # uses board.SCL and board.SDA |
| 124 | + veml7700 = adafruit_veml7700.VEML7700(i2c) |
| 125 | +
|
| 126 | + while True: |
| 127 | + print("White light:", veml7700.white) |
| 128 | + time.sleep(0.1) |
| 129 | + """ |
| 130 | + |
| 131 | + # ALS_CONF_0 - ALS gain, integration time, shutdown. |
| 132 | + light_shutdown = RWBit(0x00, 0, register_width=2) |
| 133 | + """Ambient light sensor shutdown. When ``True``, ambient light sensor is disabled.""" |
| 134 | + light_gain = RWBits(2, 0x00, 11, register_width=2) |
| 135 | + """Ambient light gain setting. Gain settings are 2, 1, 1/4 and 1/8. Settings options are: |
| 136 | + ALS_GAIN_2, ALS_GAIN_1, ALS_GAIN_1_4, ALS_GAIN_1_8. |
| 137 | +
|
| 138 | + This example sets the ambient light gain to 2 and prints the ambient light sensor data. |
| 139 | +
|
| 140 | + .. code-block:: python |
| 141 | +
|
| 142 | + import time |
| 143 | + import board |
| 144 | + import adafruit_veml7700 |
| 145 | +
|
| 146 | + i2c = board.I2C() # uses board.SCL and board.SDA |
| 147 | + veml7700 = adafruit_vcnl4040.VCNL4040(i2c) |
| 148 | +
|
| 149 | + veml7700.light_gain = veml7700.ALS_GAIN_2 |
| 150 | +
|
| 151 | + while True: |
| 152 | + print("Ambient light:", veml7700.light) |
| 153 | + time.sleep(0.1) |
| 154 | +
|
| 155 | + """ |
| 156 | + light_integration_time = RWBits(4, 0x00, 6, register_width=2) |
| 157 | + """Ambient light integration time setting. Longer time has higher sensitivity. Can be: |
| 158 | + ALS_25MS, ALS_50MS, ALS_100MS, ALS_200MS, ALS_400MS, ALS_800MS. |
| 159 | +
|
| 160 | + This example sets the ambient light integration time to 400ms and prints the ambient light |
| 161 | + sensor data. |
| 162 | +
|
| 163 | + .. code-block:: python |
| 164 | +
|
| 165 | + import time |
| 166 | + import board |
| 167 | + import adafruit_veml7700 |
| 168 | +
|
| 169 | + i2c = board.I2C() # uses board.SCL and board.SDA |
| 170 | + veml7700 = adafruit_vcnl4040.VCNL4040(i2c) |
| 171 | +
|
| 172 | + veml7700.light_integration_time = veml7700.ALS_400MS |
| 173 | +
|
| 174 | + while True: |
| 175 | + print("Ambient light:", veml7700.light) |
| 176 | + time.sleep(0.1) |
| 177 | +
|
| 178 | + """ |
| 179 | + |
| 180 | + def __init__(self, i2c_bus: I2C | TCA9548A , address: int = 0x10) -> None: |
| 181 | + self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address) |
| 182 | + for _ in range(3): |
| 183 | + try: |
| 184 | + # Set lowest gain to keep from overflow on init if bright light |
| 185 | + self.light_gain = self.ALS_GAIN_1_8 |
| 186 | + # |
| 187 | + self.light_shutdown = False # Enable the ambient light sensor |
| 188 | + break |
| 189 | + except OSError: |
| 190 | + pass |
| 191 | + else: |
| 192 | + raise RuntimeError("Unable to enable VEML7700 device") |
| 193 | + |
| 194 | + def integration_time_value(self) -> int: |
| 195 | + """Integration time value in integer form. Used for calculating :meth:`resolution`.""" |
| 196 | + integration_time = self.light_integration_time |
| 197 | + return self.integration_time_values[integration_time] |
| 198 | + |
| 199 | + def gain_value(self) -> float: |
| 200 | + """Gain value in integer form. Used for calculating :meth:`resolution`.""" |
| 201 | + gain = self.light_gain |
| 202 | + return self.gain_values[gain] |
| 203 | + |
| 204 | + def resolution(self) -> float: |
| 205 | + """Calculate the :meth:`resolution`` necessary to calculate lux. Based on |
| 206 | + integration time and gain settings.""" |
| 207 | + resolution_at_max = 0.0036 |
| 208 | + gain_max = 2 |
| 209 | + integration_time_max = 800 |
| 210 | + |
| 211 | + if ( |
| 212 | + self.gain_value() == gain_max |
| 213 | + and self.integration_time_value() == integration_time_max |
| 214 | + ): |
| 215 | + return resolution_at_max |
| 216 | + return ( |
| 217 | + resolution_at_max |
| 218 | + * (integration_time_max / self.integration_time_value()) |
| 219 | + * (gain_max / self.gain_value()) |
| 220 | + ) |
| 221 | + |
| 222 | + @property |
| 223 | + def lux(self) -> float: |
| 224 | + """Light value in lux. |
| 225 | +
|
| 226 | + This example prints the light data in lux. Cover the sensor to see the values change. |
| 227 | +
|
| 228 | + .. code-block:: python |
| 229 | +
|
| 230 | + import time |
| 231 | + import board |
| 232 | + import adafruit_veml7700 |
| 233 | +
|
| 234 | + i2c = board.I2C() # uses board.SCL and board.SDA |
| 235 | + veml7700 = adafruit_veml7700.VEML7700(i2c) |
| 236 | +
|
| 237 | + while True: |
| 238 | + print("Lux:", veml7700.lux) |
| 239 | + time.sleep(0.1) |
| 240 | + """ |
| 241 | + return self.resolution() * self.light |
0 commit comments