|
| 1 | +"""This module defines the `VEML7700Manager` class, which provides a high-level interface |
| 2 | +for interacting with the VEML7700 light sensor. It handles the initialization of the sensor |
| 3 | +and provides methods for reading light levels in various formats. |
| 4 | +
|
| 5 | +**Usage:** |
| 6 | +```python |
| 7 | +logger = Logger() |
| 8 | +i2c = busio.I2C(board.SCL, board.SDA) |
| 9 | +i2c = initialize_i2c_bus(logger, board.SCL, board.SDA, 100000,) |
| 10 | +light_sensor = VEML7700Manager(logger, i2c) |
| 11 | +lux_data = light_sensor.get_lux() |
| 12 | +``` |
| 13 | +""" |
| 14 | + |
| 15 | +import time |
| 16 | + |
| 17 | +from adafruit_tca9548a import TCA9548A_Channel |
| 18 | +from adafruit_veml7700 import VEML7700 |
| 19 | +from busio import I2C |
| 20 | + |
| 21 | +from ....logger import Logger |
| 22 | +from ....protos.light_sensor import LightSensorProto |
| 23 | +from ....sensor_reading.error import ( |
| 24 | + SensorReadingUnknownError, |
| 25 | + SensorReadingValueError, |
| 26 | +) |
| 27 | +from ....sensor_reading.light import Light |
| 28 | +from ....sensor_reading.lux import Lux |
| 29 | +from ...exception import HardwareInitializationError |
| 30 | + |
| 31 | +try: |
| 32 | + from typing import Literal |
| 33 | +except ImportError: |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +class VEML7700Manager(LightSensorProto): |
| 38 | + """Manages the VEML7700 ambient light sensor.""" |
| 39 | + |
| 40 | + def __init__( |
| 41 | + self, |
| 42 | + logger: Logger, |
| 43 | + i2c: I2C | TCA9548A_Channel, |
| 44 | + integration_time: Literal[0, 1, 2, 3, 8, 12] = 12, |
| 45 | + ) -> None: |
| 46 | + """Initializes the VEML7700Manager. |
| 47 | +
|
| 48 | + Args: |
| 49 | + logger: The logger to use. |
| 50 | + i2c: The I2C bus connected to the chip. |
| 51 | + integration_time: The integration time for the light sensor (default is 25ms). |
| 52 | + Integration times can be one of the following: |
| 53 | + - 12: 25ms |
| 54 | + - 8: 50ms |
| 55 | + - 0: 100ms |
| 56 | + - 1: 200ms |
| 57 | + - 2: 400ms |
| 58 | + - 3: 800ms |
| 59 | +
|
| 60 | + Raises: |
| 61 | + HardwareInitializationError: If the light sensor fails to initialize. |
| 62 | + """ |
| 63 | + self._log: Logger = logger |
| 64 | + self._i2c: I2C | TCA9548A_Channel = i2c |
| 65 | + |
| 66 | + try: |
| 67 | + self._log.debug("Initializing light sensor") |
| 68 | + self._light_sensor: VEML7700 = VEML7700(i2c) |
| 69 | + self._light_sensor.light_integration_time = integration_time |
| 70 | + except Exception as e: |
| 71 | + raise HardwareInitializationError( |
| 72 | + "Failed to initialize light sensor" |
| 73 | + ) from e |
| 74 | + |
| 75 | + def get_light(self) -> Light: |
| 76 | + """Gets the light reading of the sensor with default gain and integration time. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + A Light object containing a non-unit-specific light level reading. |
| 80 | +
|
| 81 | + Raises: |
| 82 | + SensorReadingUnknownError: If an unknown error occurs while reading the sensor. |
| 83 | + """ |
| 84 | + try: |
| 85 | + return Light(self._light_sensor.light) |
| 86 | + except Exception as e: |
| 87 | + raise SensorReadingUnknownError("Failed to get light reading") from e |
| 88 | + |
| 89 | + def get_lux(self) -> Lux: |
| 90 | + """Gets the light reading of the sensor with default gain and integration time. |
| 91 | +
|
| 92 | + Returns: |
| 93 | + A Lux object containing the light level in SI lux. |
| 94 | +
|
| 95 | + Raises: |
| 96 | + SensorReadingValueError: If the reading returns an invalid value. |
| 97 | + SensorReadingUnknownError: If an unknown error occurs while reading the sensor. |
| 98 | + """ |
| 99 | + try: |
| 100 | + lux = self._light_sensor.lux |
| 101 | + except Exception as e: |
| 102 | + raise SensorReadingUnknownError("Failed to get lux reading") from e |
| 103 | + |
| 104 | + if self._is_invalid_lux(lux): |
| 105 | + raise SensorReadingValueError("Lux reading is invalid or zero") |
| 106 | + |
| 107 | + return Lux(lux) |
| 108 | + |
| 109 | + def get_auto_lux(self) -> Lux: |
| 110 | + """Gets the auto lux reading of the sensor. This runs the sensor in auto mode |
| 111 | + and returns the lux value by searching through the available gain and integration time |
| 112 | + combinations to find the best match. |
| 113 | +
|
| 114 | + Returns: |
| 115 | + A Lux object containing the light level in SI lux. |
| 116 | +
|
| 117 | + Raises: |
| 118 | + SensorReadingValueError: If the reading returns an invalid value. |
| 119 | + SensorReadingUnknownError: If an unknown error occurs while reading the sensor. |
| 120 | + """ |
| 121 | + try: |
| 122 | + lux = self._light_sensor.autolux |
| 123 | + except Exception as e: |
| 124 | + raise SensorReadingUnknownError("Failed to get auto lux reading") from e |
| 125 | + |
| 126 | + if self._is_invalid_lux(lux): |
| 127 | + raise SensorReadingValueError("Lux reading is invalid or zero") |
| 128 | + |
| 129 | + return Lux(lux) |
| 130 | + |
| 131 | + @staticmethod |
| 132 | + def _is_invalid_lux(lux: float | None) -> bool: |
| 133 | + """Determines if the given lux reading is invalid or zero. |
| 134 | + Args: |
| 135 | + lux (float | None): The lux reading to validate. It can be a float representing |
| 136 | + the light level in SI lux, or None if no reading is available. |
| 137 | + Returns: |
| 138 | + bool: True if the lux reading is invalid (None or zero), False otherwise. |
| 139 | + """ |
| 140 | + return lux is None or lux == 0 |
| 141 | + |
| 142 | + def reset(self) -> None: |
| 143 | + """Resets the light sensor.""" |
| 144 | + try: |
| 145 | + self._light_sensor.light_shutdown = True |
| 146 | + time.sleep(0.1) # Allow time for the sensor to reset |
| 147 | + self._light_sensor.light_shutdown = False |
| 148 | + self._log.debug("Light sensor reset successfully") |
| 149 | + except Exception as e: |
| 150 | + self._log.error("Failed to reset light sensor:", e) |
0 commit comments