Source code for drivers.LTR390.LTR390

"""Driver for the Lite-On LTR-390UV-01 UV and ambient light sensor.

Hardware:
    Interface: I2C (via Adafruit bus device abstraction)
    Address: 0x53 (default, managed by adafruit_ltr390 library)
    Supply: 1.7V - 3.6V
    Measures: UV index, UVS raw counts, ambient light (lux), raw light counts

Typical usage::

    from drivers.LTR390.LTR390 import LTR390

    sensor = LTR390(port=1)
    lux = sensor.getLux()
    uv_index = sensor.getUV()

Note:
    Requires ``adafruit-circuitpython-ltr390`` library. The Adafruit library handles
    register-level communication; this wrapper provides simplified read methods with
    error handling and logging.
"""

import adafruit_ltr390
import board
import busio

from utils.oizom_logger import OizomLogger

# -----------------------------------------------------------------------------
# Configure logging
# -----------------------------------------------------------------------------
basic_logger = OizomLogger(__name__).get()
context_logger = OizomLogger(__name__)


[docs] class LTR390: """Driver wrapper for the LTR-390UV-01 UV and ambient light sensor. Wraps the Adafruit LTR390 CircuitPython library to provide simplified read methods with error handling and logging. Attributes: sensor: Adafruit LTR390 sensor instance, or None if initialization failed. i2c: busio.I2C instance for the selected port. """ sensor = None
[docs] def __init__(self, port: int = 0) -> None: """Initialize the LTR390 sensor on the specified I2C port. Args: port: I2C port number (0 for D1/D0, 1 for SCL/SDA). """ try: if port not in (0, 1): context_logger.error_with_context( "LTR390", "Invalid port number. Must be 0 or 1." ) return if port == 0: self.i2c = busio.I2C(board.D1, board.D0) else: self.i2c = busio.I2C(board.SCL, board.SDA) self.sensor = adafruit_ltr390.LTR390(i2c=self.i2c) except Exception as e: context_logger.error_with_context( "LTR390", f"Failed to initialize LTR390 sensor. Error: {e}" )
[docs] def getLux(self) -> float: """Read the ambient light level in lux. Returns: Lux value as a float, or 0.0 if the sensor is uninitialized or read fails. """ try: if self.sensor is None: context_logger.error_with_context( "LTR390", "Sensor not initialized properly." ) return 0.0 lux = self.sensor.lux context_logger.debug_with_context("LTR390", f"Lux value retrieved: {lux}") return lux except Exception: context_logger.error_with_context( "LTR390", "Failed to retrieve Lux value from LTR390 sensor." ) return 0.0
[docs] def getUV(self) -> float: """Read the UV index value. Returns: UV index as a float, or 0.0 if the sensor is uninitialized or read fails. """ try: if self.sensor is None: context_logger.error_with_context( "LTR390", "Sensor not initialized properly." ) return 0.0 uv = self.sensor.uvi context_logger.debug_with_context("LTR390", f"UV Index retrieved: {uv}") return uv except Exception: context_logger.error_with_context( "LTR390", "Failed to retrieve UV Index from LTR390 sensor." ) return 0.0
[docs] def getLight(self) -> float: """Read the raw ambient light sensor count. Returns: Raw light count as a float, or 0.0 if the sensor is uninitialized or read fails. """ try: if self.sensor is None: context_logger.error_with_context( "LTR390", "Sensor not initialized properly." ) return 0.0 light = self.sensor.light context_logger.debug_with_context( "LTR390", f"Light value retrieved: {light}" ) return light except Exception: context_logger.error_with_context( "LTR390", "Failed to retrieve Light value from LTR390 sensor." ) return 0.0
[docs] def getUVS(self) -> float: """Read the raw UVS (UV sensor) count. Returns: Raw UVS count as a float, or 0.0 if the sensor is uninitialized or read fails. """ try: if self.sensor is None: context_logger.error_with_context( "LTR390", "Sensor not initialized properly." ) return 0.0 uvs = self.sensor.uvs context_logger.debug_with_context("LTR390", f"UVS value retrieved: {uvs}") return uvs except Exception: context_logger.error_with_context( "LTR390", "Failed to retrieve UVS value from LTR390 sensor." ) return 0.0