From 8704fa08c79541996d8372b9e6ca6694a5047385 Mon Sep 17 00:00:00 2001 From: Cedar Grove Maker Studios Date: Tue, 16 Mar 2021 11:13:59 -0700 Subject: [PATCH 1/2] add UVI, Lux, and window_factor getters/setters --- TESTS/test_code.py | 44 ++++++++++++++++++++++++ adafruit_ltr390.py | 85 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 108 insertions(+), 21 deletions(-) create mode 100644 TESTS/test_code.py diff --git a/TESTS/test_code.py b/TESTS/test_code.py new file mode 100644 index 0000000..3a24821 --- /dev/null +++ b/TESTS/test_code.py @@ -0,0 +1,44 @@ +# LTR360 test + +import time +import busio +import board +from digitalio import DigitalInOut, Direction, Pull + +from adafruit_ltr390 import LTR390, MeasurementDelay, Resolution, Gain + +i2c = busio.I2C(board.SCL, board.SDA) +ltr = LTR390(i2c) + +outboard_led = DigitalInOut(board.D13) +outboard_led.direction =Direction.OUTPUT + +#ltr.resolution = Resolution.RESOLUTION_20BIT +print("Measurement resolution is", Resolution.string[ltr.resolution]) + +#ltr.gain = Gain.GAIN_18X +print("Measurement gain is", Gain.string[ltr.gain]) + +#ltr.measurement_delay = MeasurementDelay.DELAY_100MS +print("Measurement delay is", MeasurementDelay.string[ltr.measurement_delay]) +print("") +while True: + print() + print(f"resolution: {Resolution.string[ltr.resolution]} gain: {Gain.string[ltr.gain]} delay(rate): {MeasurementDelay.string[ltr.measurement_delay]} resolution.integration: {Resolution.integration[ltr.resolution]} window_factor: {ltr.window_factor}") + outboard_led.value = True + t0 = time.monotonic() + uvs = ltr.uvs + t1 = round(time.monotonic() - t0, 3) + uvi = ltr.uvi + print(f"UVs: {uvs} UVi: {uvi} read_time: {t1} sec ") + + t0 = time.monotonic() + light = round(ltr.light, 3) + t1 = round(time.monotonic() - t0, 3) + lux = ltr.lux + print(f"light: {light} Lux: {lux} read_time: {t1} sec ") + + print((uvi * 100, lux)) + + outboard_led.value = False + time.sleep(0.5) diff --git a/adafruit_ltr390.py b/adafruit_ltr390.py index 2a3878a..5821f56 100644 --- a/adafruit_ltr390.py +++ b/adafruit_ltr390.py @@ -93,12 +93,16 @@ def add_values(cls, value_tuples): """Add CV values to the class""" cls.string = {} cls.lsb = {} + cls.factor = {} # Scale or bit resolution factor + cls.integration = {} # Lux data integration factor for value_tuple in value_tuples: - name, value, string, lsb = value_tuple + name, value, string, lsb, factor, integration = value_tuple setattr(cls, name, value) cls.string[value] = string cls.lsb[value] = lsb + cls.factor[value] = factor + cls.integration[value] = integration @classmethod def is_valid(cls, value): @@ -128,11 +132,11 @@ class Gain(CV): Gain.add_values( ( - ("GAIN_1X", 0, "1X", None), - ("GAIN_3X", 1, "3X", None), - ("GAIN_6X", 2, "6X", None), - ("GAIN_9X", 3, "9X", None), - ("GAIN_18X", 4, "18X", None), + ("GAIN_1X", 0, "1X", None, 1, None), + ("GAIN_3X", 1, "3X", None, 3, None), + ("GAIN_6X", 2, "6X", None, 6, None), + ("GAIN_9X", 3, "9X", None, 9, None), + ("GAIN_18X", 4, "18X", None, 18, None), ) ) @@ -161,12 +165,12 @@ class Resolution(CV): Resolution.add_values( ( - ("RESOLUTION_20BIT", 0, "20 bits", None), - ("RESOLUTION_19BIT", 1, "19 bits", None), - ("RESOLUTION_18BIT", 2, "18 bits", None), - ("RESOLUTION_17BIT", 3, "17 bits", None), - ("RESOLUTION_16BIT", 4, "16 bits", None), - ("RESOLUTION_13BIT", 5, "13 bits", None), + ("RESOLUTION_20BIT", 0, "20 bits", None, 20, 4.0), + ("RESOLUTION_19BIT", 1, "19 bits", None, 19, 2.0), + ("RESOLUTION_18BIT", 2, "18 bits", None, 18, 1.0), + ("RESOLUTION_17BIT", 3, "17 bits", None, 17, 0.5), + ("RESOLUTION_16BIT", 4, "16 bits", None, 16, 0.25), + ("RESOLUTION_13BIT", 5, "13 bits", None, 13, 0.03125), ) ) @@ -197,13 +201,13 @@ class MeasurementDelay(CV): MeasurementDelay.add_values( ( - ("DELAY_25MS", 0, "25", None), - ("DELAY_50MS", 1, "50", None), - ("DELAY_100MS", 2, "100", None), - ("DELAY_200MS", 3, "200", None), - ("DELAY_500MS", 4, "500", None), - ("DELAY_1000MS", 5, "1000", None), - ("DELAY_2000MS", 6, "2000", None), + ("DELAY_25MS", 0, "25", None, 25, None), + ("DELAY_50MS", 1, "50", None, 50, None), + ("DELAY_100MS", 2, "100", None, 100, None), + ("DELAY_200MS", 3, "200", None, 200, None), + ("DELAY_500MS", 4, "500", None, 500, None), + ("DELAY_1000MS", 5, "1000", None, 1000, None), + ("DELAY_2000MS", 6, "2000", None, 2000, None), ) ) @@ -260,6 +264,7 @@ def initialize(self): self._mode = UV self.gain = Gain.GAIN_3X # pylint:disable=no-member self.resolution = Resolution.RESOLUTION_16BIT # pylint:disable=no-member + self._window_factor = 1 # default window transmission factor # ltr.setThresholds(100, 1000); # self.low_threshold = 100 @@ -331,7 +336,7 @@ def resolution(self, value): self._resolution_bits = value def enable_alerts(self, enable, source, persistance): - """The configuraiton of alerts raised by the sensor + """The configuration of alerts raised by the sensor :param enable: Whether the interrupt output is enabled :param source: Whether to use the ALS or UVS data register to compare @@ -351,7 +356,7 @@ def enable_alerts(self, enable, source, persistance): @property def measurement_delay(self): """The delay between measurements. This can be used to set the measurement rate which - affects the sensors power usage.""" + affects the sensor power usage.""" return self._measurement_delay_bits @measurement_delay.setter @@ -359,3 +364,41 @@ def measurement_delay(self, value): if not MeasurementDelay.is_valid(value): raise AttributeError("measurement_delay must be a MeasurementDelay") self._measurement_delay_bits = value + + @property + def uvi(self): + """Read UV count and return calculated UV Index (UVI) value based upon the rated sensitivity + of 1 UVI per 2300 counts at 18X gain factor and 20-bit resolution.""" + return ( + self.uvs + / ( + (Gain.factor[self.gain] / 18) + * (2 ** Resolution.factor[self.resolution]) + / (2 ** 20) + * 2300 + ) + * self._window_factor + ) + + @property + def lux(self): + """Read light level and return calculated Lux value.""" + return ( + (self.light * 0.6) + / (Gain.factor[self.gain] * Resolution.integration[self.resolution]) + ) * self._window_factor + + @property + def window_factor(self): + """Window transmission factor (Wfac) for UVI and Lux calculations. + A factor of 1 (default) represents no window or clear glass; > 1 for a tinted window. + Factor of > 1 requires an emperical calibration with a reference light source.""" + return self._window_factor + + @window_factor.setter + def window_factor(self, factor=1): + if factor < 1: + raise ValueError( + "window transmission factor must be a value of 1.0 or greater" + ) + self._window_factor = factor From b2e6a86939c854e70f5ff1a9b6b5812ad8c86c38 Mon Sep 17 00:00:00 2001 From: Cedar Grove Maker Studios Date: Tue, 16 Mar 2021 12:24:33 -0700 Subject: [PATCH 2/2] update simple example --- TESTS/test_code.py | 44 ----------------------------------- examples/ltr390_simpletest.py | 3 ++- 2 files changed, 2 insertions(+), 45 deletions(-) delete mode 100644 TESTS/test_code.py diff --git a/TESTS/test_code.py b/TESTS/test_code.py deleted file mode 100644 index 3a24821..0000000 --- a/TESTS/test_code.py +++ /dev/null @@ -1,44 +0,0 @@ -# LTR360 test - -import time -import busio -import board -from digitalio import DigitalInOut, Direction, Pull - -from adafruit_ltr390 import LTR390, MeasurementDelay, Resolution, Gain - -i2c = busio.I2C(board.SCL, board.SDA) -ltr = LTR390(i2c) - -outboard_led = DigitalInOut(board.D13) -outboard_led.direction =Direction.OUTPUT - -#ltr.resolution = Resolution.RESOLUTION_20BIT -print("Measurement resolution is", Resolution.string[ltr.resolution]) - -#ltr.gain = Gain.GAIN_18X -print("Measurement gain is", Gain.string[ltr.gain]) - -#ltr.measurement_delay = MeasurementDelay.DELAY_100MS -print("Measurement delay is", MeasurementDelay.string[ltr.measurement_delay]) -print("") -while True: - print() - print(f"resolution: {Resolution.string[ltr.resolution]} gain: {Gain.string[ltr.gain]} delay(rate): {MeasurementDelay.string[ltr.measurement_delay]} resolution.integration: {Resolution.integration[ltr.resolution]} window_factor: {ltr.window_factor}") - outboard_led.value = True - t0 = time.monotonic() - uvs = ltr.uvs - t1 = round(time.monotonic() - t0, 3) - uvi = ltr.uvi - print(f"UVs: {uvs} UVi: {uvi} read_time: {t1} sec ") - - t0 = time.monotonic() - light = round(ltr.light, 3) - t1 = round(time.monotonic() - t0, 3) - lux = ltr.lux - print(f"light: {light} Lux: {lux} read_time: {t1} sec ") - - print((uvi * 100, lux)) - - outboard_led.value = False - time.sleep(0.5) diff --git a/examples/ltr390_simpletest.py b/examples/ltr390_simpletest.py index ebd42ef..f71aae6 100644 --- a/examples/ltr390_simpletest.py +++ b/examples/ltr390_simpletest.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2020 by Bryan Siepert, written for Adafruit Industries +# SPDX-FileCopyrightText: 2021 by Bryan Siepert, written for Adafruit Industries # # SPDX-License-Identifier: Unlicense import time @@ -12,4 +12,5 @@ while True: print("UV:", ltr.uvs, "\t\tAmbient Light:", ltr.light) + print("UVI:", ltr.uvi, "\t\tLux:", ltr.lux) time.sleep(1.0)