Skip to content

Commit 6cc65a0

Browse files
authored
Merge pull request #3 from CedarGroveStudios/main
Add UVI, Lux, and Window Transmission Factor
2 parents 837976d + b2e6a86 commit 6cc65a0

File tree

2 files changed

+66
-22
lines changed

2 files changed

+66
-22
lines changed

adafruit_ltr390.py

+64-21
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,16 @@ def add_values(cls, value_tuples):
9393
"""Add CV values to the class"""
9494
cls.string = {}
9595
cls.lsb = {}
96+
cls.factor = {} # Scale or bit resolution factor
97+
cls.integration = {} # Lux data integration factor
9698

9799
for value_tuple in value_tuples:
98-
name, value, string, lsb = value_tuple
100+
name, value, string, lsb, factor, integration = value_tuple
99101
setattr(cls, name, value)
100102
cls.string[value] = string
101103
cls.lsb[value] = lsb
104+
cls.factor[value] = factor
105+
cls.integration[value] = integration
102106

103107
@classmethod
104108
def is_valid(cls, value):
@@ -128,11 +132,11 @@ class Gain(CV):
128132

129133
Gain.add_values(
130134
(
131-
("GAIN_1X", 0, "1X", None),
132-
("GAIN_3X", 1, "3X", None),
133-
("GAIN_6X", 2, "6X", None),
134-
("GAIN_9X", 3, "9X", None),
135-
("GAIN_18X", 4, "18X", None),
135+
("GAIN_1X", 0, "1X", None, 1, None),
136+
("GAIN_3X", 1, "3X", None, 3, None),
137+
("GAIN_6X", 2, "6X", None, 6, None),
138+
("GAIN_9X", 3, "9X", None, 9, None),
139+
("GAIN_18X", 4, "18X", None, 18, None),
136140
)
137141
)
138142

@@ -161,12 +165,12 @@ class Resolution(CV):
161165

162166
Resolution.add_values(
163167
(
164-
("RESOLUTION_20BIT", 0, "20 bits", None),
165-
("RESOLUTION_19BIT", 1, "19 bits", None),
166-
("RESOLUTION_18BIT", 2, "18 bits", None),
167-
("RESOLUTION_17BIT", 3, "17 bits", None),
168-
("RESOLUTION_16BIT", 4, "16 bits", None),
169-
("RESOLUTION_13BIT", 5, "13 bits", None),
168+
("RESOLUTION_20BIT", 0, "20 bits", None, 20, 4.0),
169+
("RESOLUTION_19BIT", 1, "19 bits", None, 19, 2.0),
170+
("RESOLUTION_18BIT", 2, "18 bits", None, 18, 1.0),
171+
("RESOLUTION_17BIT", 3, "17 bits", None, 17, 0.5),
172+
("RESOLUTION_16BIT", 4, "16 bits", None, 16, 0.25),
173+
("RESOLUTION_13BIT", 5, "13 bits", None, 13, 0.03125),
170174
)
171175
)
172176

@@ -197,13 +201,13 @@ class MeasurementDelay(CV):
197201

198202
MeasurementDelay.add_values(
199203
(
200-
("DELAY_25MS", 0, "25", None),
201-
("DELAY_50MS", 1, "50", None),
202-
("DELAY_100MS", 2, "100", None),
203-
("DELAY_200MS", 3, "200", None),
204-
("DELAY_500MS", 4, "500", None),
205-
("DELAY_1000MS", 5, "1000", None),
206-
("DELAY_2000MS", 6, "2000", None),
204+
("DELAY_25MS", 0, "25", None, 25, None),
205+
("DELAY_50MS", 1, "50", None, 50, None),
206+
("DELAY_100MS", 2, "100", None, 100, None),
207+
("DELAY_200MS", 3, "200", None, 200, None),
208+
("DELAY_500MS", 4, "500", None, 500, None),
209+
("DELAY_1000MS", 5, "1000", None, 1000, None),
210+
("DELAY_2000MS", 6, "2000", None, 2000, None),
207211
)
208212
)
209213

@@ -260,6 +264,7 @@ def initialize(self):
260264
self._mode = UV
261265
self.gain = Gain.GAIN_3X # pylint:disable=no-member
262266
self.resolution = Resolution.RESOLUTION_16BIT # pylint:disable=no-member
267+
self._window_factor = 1 # default window transmission factor
263268

264269
# ltr.setThresholds(100, 1000);
265270
# self.low_threshold = 100
@@ -331,7 +336,7 @@ def resolution(self, value):
331336
self._resolution_bits = value
332337

333338
def enable_alerts(self, enable, source, persistance):
334-
"""The configuraiton of alerts raised by the sensor
339+
"""The configuration of alerts raised by the sensor
335340
336341
:param enable: Whether the interrupt output is enabled
337342
:param source: Whether to use the ALS or UVS data register to compare
@@ -351,11 +356,49 @@ def enable_alerts(self, enable, source, persistance):
351356
@property
352357
def measurement_delay(self):
353358
"""The delay between measurements. This can be used to set the measurement rate which
354-
affects the sensors power usage."""
359+
affects the sensor power usage."""
355360
return self._measurement_delay_bits
356361

357362
@measurement_delay.setter
358363
def measurement_delay(self, value):
359364
if not MeasurementDelay.is_valid(value):
360365
raise AttributeError("measurement_delay must be a MeasurementDelay")
361366
self._measurement_delay_bits = value
367+
368+
@property
369+
def uvi(self):
370+
"""Read UV count and return calculated UV Index (UVI) value based upon the rated sensitivity
371+
of 1 UVI per 2300 counts at 18X gain factor and 20-bit resolution."""
372+
return (
373+
self.uvs
374+
/ (
375+
(Gain.factor[self.gain] / 18)
376+
* (2 ** Resolution.factor[self.resolution])
377+
/ (2 ** 20)
378+
* 2300
379+
)
380+
* self._window_factor
381+
)
382+
383+
@property
384+
def lux(self):
385+
"""Read light level and return calculated Lux value."""
386+
return (
387+
(self.light * 0.6)
388+
/ (Gain.factor[self.gain] * Resolution.integration[self.resolution])
389+
) * self._window_factor
390+
391+
@property
392+
def window_factor(self):
393+
"""Window transmission factor (Wfac) for UVI and Lux calculations.
394+
A factor of 1 (default) represents no window or clear glass; > 1 for a tinted window.
395+
Factor of > 1 requires an emperical calibration with a reference light source."""
396+
return self._window_factor
397+
398+
@window_factor.setter
399+
def window_factor(self, factor=1):
400+
if factor < 1:
401+
raise ValueError(
402+
"window transmission factor must be a value of 1.0 or greater"
403+
)
404+
self._window_factor = factor

examples/ltr390_simpletest.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2020 by Bryan Siepert, written for Adafruit Industries
1+
# SPDX-FileCopyrightText: 2021 by Bryan Siepert, written for Adafruit Industries
22
#
33
# SPDX-License-Identifier: Unlicense
44
import time
@@ -12,4 +12,5 @@
1212

1313
while True:
1414
print("UV:", ltr.uvs, "\t\tAmbient Light:", ltr.light)
15+
print("UVI:", ltr.uvi, "\t\tLux:", ltr.lux)
1516
time.sleep(1.0)

0 commit comments

Comments
 (0)