Skip to content

Commit db5a5b2

Browse files
committed
adding rate/delay
1 parent e169714 commit db5a5b2

File tree

2 files changed

+86
-6
lines changed

2 files changed

+86
-6
lines changed

adafruit_ltr390.py

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,55 @@ class Resolution(CV):
156156
| :py:const:`Resolution.RESOLUTION_20BIT` | 20 bits |
157157
+-----------------------------------------+------------------------------------------------------+
158158
159+
159160
"""
160161

161162

162163
Resolution.add_values(
163164
(
164-
("RESOLUTION_20BIT", 0, "20", None),
165-
("RESOLUTION_19BIT", 1, "19", None),
166-
("RESOLUTION_18BIT", 2, "18", None),
167-
("RESOLUTION_17BIT", 3, "17", None),
168-
("RESOLUTION_16BIT", 4, "16", None),
169-
("RESOLUTION_13BIT", 5, "13", None),
165+
("RESOLUTION_20BIT", 0, "20 bits", None),
166+
("RESOLUTION_19BIT", 1, "19 bits", None),
167+
("RESOLUTION_18BIT", 2, "18 bits", None),
168+
("RESOLUTION_17BIT", 3, "17 bits", None),
169+
("RESOLUTION_16BIT", 4, "16 bits", None),
170+
("RESOLUTION_13BIT", 5, "13 bits", None),
171+
)
172+
)
173+
174+
175+
class MeasurementDelay(CV):
176+
"""Options for `measurement_delay`
177+
178+
+-------------------------------------------+--------------------------------------+
179+
| MeasurementDelay | Time Between Measurement Cycles (ms) |
180+
+===========================================+======================================+
181+
| :py:const:`MeasurementDelay.DELAY_25MS` | 25 |
182+
+-------------------------------------------+--------------------------------------+
183+
| :py:const:`MeasurementDelay.DELAY_50MS` | 50 |
184+
+-------------------------------------------+--------------------------------------+
185+
| :py:const:`MeasurementDelay.DELAY_100MS` | 100 |
186+
+-------------------------------------------+--------------------------------------+
187+
| :py:const:`MeasurementDelay.DELAY_200MS` | 200 |
188+
+-------------------------------------------+--------------------------------------+
189+
| :py:const:`MeasurementDelay.DELAY_500MS` | 500 |
190+
+-------------------------------------------+--------------------------------------+
191+
| :py:const:`MeasurementDelay.DELAY_1000MS` | 1000 |
192+
+-------------------------------------------+--------------------------------------+
193+
| :py:const:`MeasurementDelay.DELAY_2000MS` | 2000 |
194+
+-------------------------------------------+--------------------------------------+
195+
196+
"""
197+
198+
199+
MeasurementDelay.add_values(
200+
(
201+
("DELAY_25MS", 0, "25", None),
202+
("DELAY_50MS", 1, "50", None),
203+
("DELAY_100MS", 2, "100", None),
204+
("DELAY_200MS", 3, "200", None),
205+
("DELAY_500MS", 4, "500", None),
206+
("DELAY_1000MS", 5, "1000", None),
207+
("DELAY_2000MS", 6, "2000", None),
170208
)
171209
)
172210

@@ -181,6 +219,8 @@ class LTR390: # pylint:disable=too-many-instance-attributes
181219

182220
_gain_bits = RWBits(3, _GAIN, 0)
183221
_resolution_bits = RWBits(3, _MEAS_RATE, 4)
222+
_measurement_delay_bits = RWBits(3, _MEAS_RATE, 0)
223+
_rate_bits = RWBits(3, _MEAS_RATE, 4)
184224
_int_src_bits = RWBits(2, _INT_CFG, 4)
185225
_int_persistance_bits = RWBits(4, _INT_PST, 4)
186226

@@ -308,3 +348,15 @@ def enable_alerts(self, enable, source, persistance):
308348
else:
309349
raise AttributeError("interrupt source must be UV or ALS")
310350
self._int_persistance_bits = persistance
351+
352+
@property
353+
def measurement_delay(self):
354+
"""The delay between measurements. This can be used to set the measurement rate which
355+
affects the sensors power usage."""
356+
return self._measurement_delay_bits
357+
358+
@measurement_delay.setter
359+
def measurement_delay(self, value):
360+
if not MeasurementDelay.is_valid(value):
361+
raise AttributeError("measurement_delay must be a MeasurementDelay")
362+
self._measurement_delay_bits = value
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-FileCopyrightText: 2020 by Bryan Siepert, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
# pylint:disable=unused-import,no-member
5+
6+
import time
7+
import board
8+
import busio
9+
from adafruit_ltr390 import LTR390, MeasurementDelay, Resolution, Gain
10+
11+
i2c = busio.I2C(board.SCL, board.SDA)
12+
13+
ltr = LTR390(i2c)
14+
15+
# ltr.resolution = Resolution.RESOLUTION_16BIT
16+
print("Measurement resolution is", Resolution.string[ltr.resolution])
17+
18+
# ltr.gain = Gain.GAIN_1X
19+
print("Measurement gain is", Gain.string[ltr.gain])
20+
21+
# ltr.measurement_delay = MeasurementDelay.DELAY_100MS
22+
print("Measurement delay is", MeasurementDelay.string[ltr.measurement_delay])
23+
print("")
24+
while True:
25+
print("UV Index:", ltr.uv_index, "\t\tAmbient Light:", ltr.light)
26+
27+
# for shorter measurement delays you may need to make this sleep shorter to see a change
28+
time.sleep(1.0)

0 commit comments

Comments
 (0)