/ adafruit_veml7700.py
adafruit_veml7700.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2019 Kattni Rembor for Adafruit Industries 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a copy 6 # of this software and associated documentation files (the "Software"), to deal 7 # in the Software without restriction, including without limitation the rights 8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 # copies of the Software, and to permit persons to whom the Software is 10 # furnished to do so, subject to the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included in 13 # all copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 # THE SOFTWARE. 22 """ 23 `adafruit_veml7700` 24 ================================================================================ 25 26 CircuitPython driver for VEML7700 high precision I2C ambient light sensor. 27 28 29 * Author(s): Kattni Rembor 30 31 Implementation Notes 32 -------------------- 33 34 **Hardware:** 35 36 * `Adafruit VEML7700 <https://www.adafruit.com/products>`_ 37 38 **Software and Dependencies:** 39 40 * Adafruit CircuitPython firmware for the supported boards: 41 https://github.com/adafruit/circuitpython/releases 42 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 45 """ 46 47 from micropython import const 48 import adafruit_bus_device.i2c_device as i2cdevice 49 from adafruit_register.i2c_struct import UnaryStruct, ROUnaryStruct 50 from adafruit_register.i2c_bits import RWBits 51 from adafruit_register.i2c_bit import RWBit, ROBit 52 53 __version__ = "0.0.0-auto.0" 54 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VEML7700.git" 55 56 57 class VEML7700: 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 64 # Ambient light sensor gain settings 65 ALS_GAIN_1 = const(0x0) 66 ALS_GAIN_2 = const(0x1) 67 ALS_GAIN_1_8 = const(0x2) 68 ALS_GAIN_1_4 = const(0x3) 69 70 # Ambient light integration time settings 71 72 ALS_25MS = const(0xC) 73 ALS_50MS = const(0x8) 74 ALS_100MS = const(0x0) 75 ALS_200MS = const(0x1) 76 ALS_400MS = const(0x2) 77 ALS_800MS = const(0x3) 78 79 # Gain value integers 80 gain_values = { 81 ALS_GAIN_2: 2, 82 ALS_GAIN_1: 1, 83 ALS_GAIN_1_4: 0.25, 84 ALS_GAIN_1_8: 0.125, 85 } 86 87 # Integration time value integers 88 integration_time_values = { 89 ALS_25MS: 25, 90 ALS_50MS: 50, 91 ALS_100MS: 100, 92 ALS_200MS: 200, 93 ALS_400MS: 400, 94 ALS_800MS: 800, 95 } 96 97 # ALS - Ambient light sensor high resolution output data 98 light = ROUnaryStruct(0x04, "<H") 99 """Ambient light data. 100 101 This example prints the ambient light data. Cover the sensor to see the values change. 102 103 .. code-block:: python 104 105 import time 106 import board 107 import busio 108 import adafruit_veml7700 109 110 i2c = busio.I2C(board.SCL, board.SDA) 111 veml7700 = adafruit_veml7700.VEML7700(i2c) 112 113 while True: 114 print("Ambient light:", veml7700.light) 115 time.sleep(0.1) 116 """ 117 118 # WHITE - White channel output data 119 white = ROUnaryStruct(0x05, "<H") 120 """White light data. 121 122 This example prints the white light data. Cover the sensor to see the values change. 123 124 .. code-block:: python 125 126 import time 127 import board 128 import busio 129 import adafruit_veml7700 130 131 i2c = busio.I2C(board.SCL, board.SDA) 132 veml7700 = adafruit_veml7700.VEML7700(i2c) 133 134 while True: 135 print("White light:", veml7700.white) 136 time.sleep(0.1) 137 """ 138 139 # ALS_CONF_0 - ALS gain, integration time, interrupt and shutdown. 140 light_shutdown = RWBit(0x00, 0, register_width=2) 141 """Ambient light sensor shutdown. When ``True``, ambient light sensor is disabled.""" 142 light_interrupt = RWBit(0x00, 1, register_width=2) 143 """Enable interrupt. ``True`` to enable, ``False`` to disable.""" 144 light_gain = RWBits(2, 0x00, 11, register_width=2) 145 """Ambient light gain setting. Gain settings are 2, 1, 1/4 and 1/8. Settings options are: 146 ALS_GAIN_2, ALS_GAIN_1, ALS_GAIN_1_4, ALS_GAIN_1_8. 147 148 This example sets the ambient light gain to 2 and prints the ambient light sensor data. 149 150 .. code-block:: python 151 152 import time 153 import board 154 import busio 155 import adafruit_veml7700 156 157 i2c = busio.I2C(board.SCL, board.SDA) 158 veml7700 = adafruit_vcnl4040.VCNL4040(i2c) 159 160 veml7700.light_gain = veml7700.ALS_GAIN_2 161 162 while True: 163 print("Ambient light:", veml7700.light) 164 time.sleep(0.1) 165 166 """ 167 light_integration_time = RWBits(4, 0x00, 6, register_width=2) 168 """Ambient light integration time setting. Longer time has higher sensitivity. Can be: 169 ALS_25MS, ALS_50MS, ALS_100MS, ALS_200MS, ALS_400MS, ALS_800MS. 170 171 This example sets the ambient light integration time to 400ms and prints the ambient light 172 sensor data. 173 174 .. code-block:: python 175 176 import time 177 import board 178 import busio 179 import adafruit_veml7700 180 181 i2c = busio.I2C(board.SCL, board.SDA) 182 veml7700 = adafruit_vcnl4040.VCNL4040(i2c) 183 184 veml7700.light_integration_time = veml7700.ALS_400MS 185 186 while True: 187 print("Ambient light:", veml7700.light) 188 time.sleep(0.1) 189 190 """ 191 192 # ALS_WH - ALS high threshold window setting 193 light_high_threshold = UnaryStruct(0x01, "<H") 194 """Ambient light sensor interrupt high threshold setting.""" 195 # ALS_WL - ALS low threshold window setting 196 light_low_threshold = UnaryStruct(0x02, "<H") 197 """Ambient light sensor interrupt low threshold setting.""" 198 # ALS_INT - ALS INT trigger event 199 light_interrupt_high = ROBit(0x06, 14, register_width=2) 200 """Ambient light high threshold interrupt flag. Triggered when high threshold exceeded.""" 201 light_interrupt_low = ROBit(0x06, 15, register_width=2) 202 """Ambient light low threshold interrupt flag. Triggered when low threshold exceeded.""" 203 204 def __init__(self, i2c_bus, address=0x10): 205 self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address) 206 self.light_shutdown = False # Enable the ambient light sensor 207 208 def integration_time_value(self): 209 """Integration time value in integer form. Used for calculating ``resolution``.""" 210 integration_time = self.light_integration_time 211 return self.integration_time_values[integration_time] 212 213 def gain_value(self): 214 """Gain value in integer form. Used for calculating ``resolution``.""" 215 gain = self.light_gain 216 return self.gain_values[gain] 217 218 def resolution(self): 219 """Calculate the ``resolution`` necessary to calculate lux. Based on integration time and 220 gain settings.""" 221 resolution_at_max = 0.0036 222 gain_max = 2 223 integration_time_max = 800 224 225 if ( 226 self.gain_value() == gain_max 227 and self.integration_time_value() == integration_time_max 228 ): 229 return resolution_at_max 230 return ( 231 resolution_at_max 232 * (integration_time_max / self.integration_time_value()) 233 * (gain_max / self.gain_value()) 234 ) 235 236 @property 237 def lux(self): 238 """Light value in lux. 239 240 This example prints the light data in lux. Cover the sensor to see the values change. 241 242 .. code-block:: python 243 244 import time 245 import board 246 import busio 247 import adafruit_veml7700 248 249 i2c = busio.I2C(board.SCL, board.SDA) 250 veml7700 = adafruit_veml7700.VEML7700(i2c) 251 252 while True: 253 print("Lux:", veml7700.lux) 254 time.sleep(0.1) 255 """ 256 return self.resolution() * self.light