Skip to content

Commit 8704fa0

Browse files
add UVI, Lux, and window_factor getters/setters
1 parent 842e585 commit 8704fa0

File tree

2 files changed

+108
-21
lines changed

2 files changed

+108
-21
lines changed

TESTS/test_code.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# LTR360 test
2+
3+
import time
4+
import busio
5+
import board
6+
from digitalio import DigitalInOut, Direction, Pull
7+
8+
from adafruit_ltr390 import LTR390, MeasurementDelay, Resolution, Gain
9+
10+
i2c = busio.I2C(board.SCL, board.SDA)
11+
ltr = LTR390(i2c)
12+
13+
outboard_led = DigitalInOut(board.D13)
14+
outboard_led.direction =Direction.OUTPUT
15+
16+
#ltr.resolution = Resolution.RESOLUTION_20BIT
17+
print("Measurement resolution is", Resolution.string[ltr.resolution])
18+
19+
#ltr.gain = Gain.GAIN_18X
20+
print("Measurement gain is", Gain.string[ltr.gain])
21+
22+
#ltr.measurement_delay = MeasurementDelay.DELAY_100MS
23+
print("Measurement delay is", MeasurementDelay.string[ltr.measurement_delay])
24+
print("")
25+
while True:
26+
print()
27+
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}")
28+
outboard_led.value = True
29+
t0 = time.monotonic()
30+
uvs = ltr.uvs
31+
t1 = round(time.monotonic() - t0, 3)
32+
uvi = ltr.uvi
33+
print(f"UVs: {uvs} UVi: {uvi} read_time: {t1} sec ")
34+
35+
t0 = time.monotonic()
36+
light = round(ltr.light, 3)
37+
t1 = round(time.monotonic() - t0, 3)
38+
lux = ltr.lux
39+
print(f"light: {light} Lux: {lux} read_time: {t1} sec ")
40+
41+
print((uvi * 100, lux))
42+
43+
outboard_led.value = False
44+
time.sleep(0.5)

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

0 commit comments

Comments
 (0)