Skip to content

Commit 9125f77

Browse files
authored
Merge pull request #30 from yacobucci/interrupts
Added interrupt support
2 parents 2ea8265 + ea87f52 commit 9125f77

File tree

1 file changed

+166
-8
lines changed

1 file changed

+166
-8
lines changed

adafruit_tsl2591.py

Lines changed: 166 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,27 @@
3939
__version__ = "0.0.0+auto.0"
4040
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TSL2591.git"
4141

42-
4342
# Internal constants:
4443
_TSL2591_ADDR = const(0x29)
4544
_TSL2591_COMMAND_BIT = const(0xA0)
45+
_TSL2591_SPECIAL_BIT = const(0xE0)
4646
_TSL2591_ENABLE_POWEROFF = const(0x00)
4747
_TSL2591_ENABLE_POWERON = const(0x01)
4848
_TSL2591_ENABLE_AEN = const(0x02)
49-
_TSL2591_ENABLE_AIEN = const(0x10)
50-
_TSL2591_ENABLE_NPIEN = const(0x80)
49+
5150
_TSL2591_REGISTER_ENABLE = const(0x00)
5251
_TSL2591_REGISTER_CONTROL = const(0x01)
52+
53+
_TSL2591_AILTL = const(0x04)
54+
_TSL2591_AILTH = const(0x05)
55+
_TSL2591_AIHTL = const(0x06)
56+
_TSL2591_AIHTH = const(0x07)
57+
_TSL2591_NPAILTL = const(0x08)
58+
_TSL2591_NPAILTH = const(0x09)
59+
_TSL2591_NPAIHTL = const(0x0A)
60+
_TSL2591_NPAIHTH = const(0x0B)
61+
_TSL2591_PERSIST_FILTER = const(0x0C)
62+
5363
_TSL2591_REGISTER_DEVICE_ID = const(0x12)
5464
_TSL2591_REGISTER_CHAN0_LOW = const(0x14)
5565
_TSL2591_REGISTER_CHAN1_LOW = const(0x16)
@@ -81,6 +91,18 @@
8191
"""500 millis"""
8292
INTEGRATIONTIME_600MS = 0x05 # 600 millis
8393
"""600 millis"""
94+
CLEAR_INTERRUPT = const(0x06)
95+
"""Clears ALS interrupt"""
96+
CLEAR_ALL_INTERRUPTS = const(0x07)
97+
"""Clears ALS and no persist ALS interrupt"""
98+
CLEAR_PERSIST_INTERRUPT = const(0x0A)
99+
"""Clears no persist ALS interrupt"""
100+
ENABLE_AIEN = const(0x10)
101+
"""ALS Interrupt Enable. When asserted permits ALS interrupts to be generated."""
102+
ENABLE_NPIEN = const(0x80)
103+
"""No Persist Interrupt Enable. When asserted NP Threshold conditions will generate an interrupt."""
104+
ENABLE_NPAIEN = const(0x10 | 0x80)
105+
"""ALS and No Persist Interrupt Enable."""
84106

85107

86108
class TSL2591:
@@ -167,19 +189,62 @@ def _write_u8(self, address: int, val: int) -> None:
167189
i2c.write(self._BUFFER, end=2)
168190

169191
def enable(self) -> None:
170-
"""Put the device in a fully powered enabled mode."""
192+
"""Put the device in a powered, enabled mode."""
171193
self._write_u8(
172194
_TSL2591_REGISTER_ENABLE,
173-
_TSL2591_ENABLE_POWERON
174-
| _TSL2591_ENABLE_AEN
175-
| _TSL2591_ENABLE_AIEN
176-
| _TSL2591_ENABLE_NPIEN,
195+
_TSL2591_ENABLE_POWERON | _TSL2591_ENABLE_AEN,
177196
)
178197

179198
def disable(self) -> None:
180199
"""Disable the device and go into low power mode."""
181200
self._write_u8(_TSL2591_REGISTER_ENABLE, _TSL2591_ENABLE_POWEROFF)
182201

202+
def clear_interrupt(self, operation: int) -> None:
203+
"""Send special function command to control interrupt bits in the status register (0x13).
204+
Can be a value of:
205+
- ``CLEAR_INTERRUPT``
206+
- ``CLEAR_ALL_INTERRUPTS``
207+
- ``CLEAR_PERSIST_INTERRUPT``
208+
"""
209+
assert operation in (
210+
CLEAR_INTERRUPT,
211+
CLEAR_ALL_INTERRUPTS,
212+
CLEAR_PERSIST_INTERRUPT,
213+
)
214+
control = (_TSL2591_SPECIAL_BIT | operation) & 0xFF
215+
with self._device as i2c:
216+
self._BUFFER[0] = control
217+
i2c.write(self._BUFFER, end=1)
218+
219+
def enable_interrupt(self, interrupts: int) -> None:
220+
"""Enable interrupts on device. ENABLE_NPIEN will turn on No Persist interrupts, these
221+
bypass the persist filter and assert immediately when values are detected above the high
222+
threshold or below the low threshold. Similarly, ENABLE_AIEN will assert at the respective
223+
ALS thresholds, but only after the values persist longer than the persist filter cycle
224+
duration. The device powers on with thresholds at 0, meaning enabling interrupts may
225+
cause an immediate assertion.
226+
Can be a value of:
227+
- ``ENABLE_NPIEN``
228+
- ``ENABLE_AIEN``
229+
- ``ENABLE_NPAIEN``
230+
"""
231+
assert interrupts in (ENABLE_NPIEN, ENABLE_AIEN, (ENABLE_NPIEN | ENABLE_AIEN))
232+
functions = self._read_u8(_TSL2591_REGISTER_ENABLE)
233+
functions = (functions | interrupts) & 0xFF
234+
self._write_u8(_TSL2591_REGISTER_ENABLE, functions)
235+
236+
def disable_interrupt(self, interrupts: int) -> None:
237+
"""Disables the requested interrupts.
238+
Can be a value of:
239+
- ``ENABLE_NPIEN``
240+
- ``ENABLE_AIEN``
241+
- ``ENABLE_NPAIEN``
242+
"""
243+
assert interrupts in (ENABLE_NPIEN, ENABLE_AIEN, (ENABLE_NPIEN | ENABLE_AIEN))
244+
functions = self._read_u8(_TSL2591_REGISTER_ENABLE)
245+
functions = (functions & ~interrupts) & 0xFF
246+
self._write_u8(_TSL2591_REGISTER_ENABLE, functions)
247+
183248
@property
184249
def gain(self) -> int:
185250
"""Get and set the gain of the sensor. Can be a value of:
@@ -228,6 +293,99 @@ def integration_time(self, val: int) -> None:
228293
# Keep track of integration time for future reading delay times.
229294
self._integration_time = val
230295

296+
@property
297+
def threshold_low(self) -> int:
298+
"""Get and set the ALS interrupt low threshold bytes. If the detected value is below
299+
the low threshold for the number of persist filter cycles an interrupt will be triggered.
300+
Can be 16-bit value."""
301+
th_low = self._read_u16LE(_TSL2591_AILTL)
302+
return th_low
303+
304+
@threshold_low.setter
305+
def threshold_low(self, val: int) -> None:
306+
lower = val & 0xFF
307+
upper = (val >> 8) & 0xFF
308+
self._write_u8(_TSL2591_AILTL, lower)
309+
self._write_u8(_TSL2591_AILTH, upper)
310+
311+
@property
312+
def threshold_high(self) -> int:
313+
"""Get and set the ALS interrupt high threshold bytes. If the detected value is above
314+
the high threshold for the number of persist filter cycles an interrupt will be triggered.
315+
Can be 16-bit value."""
316+
th_high = self._read_u16LE(_TSL2591_AIHTL)
317+
return th_high
318+
319+
@threshold_high.setter
320+
def threshold_high(self, val: int) -> None:
321+
lower = val & 0xFF
322+
upper = (val >> 8) & 0xFF
323+
self._write_u8(_TSL2591_AIHTL, lower)
324+
self._write_u8(_TSL2591_AIHTH, upper)
325+
326+
@property
327+
def nopersist_threshold_low(self) -> int:
328+
"""Get and set the No Persist ALS low threshold bytes. An interrupt will be triggered
329+
immediately once the detected value is below the low threshold. Can be 16-bit value.
330+
"""
331+
np_th_low = self._read_u16LE(_TSL2591_NPAILTL)
332+
return np_th_low
333+
334+
@nopersist_threshold_low.setter
335+
def nopersist_threshold_low(self, val: int) -> None:
336+
lower = val & 0xFF
337+
upper = (val >> 8) & 0xFF
338+
self._write_u8(_TSL2591_NPAILTL, lower)
339+
self._write_u8(_TSL2591_NPAILTH, upper)
340+
341+
@property
342+
def nopersist_threshold_high(self) -> int:
343+
"""Get and set the No Persist ALS high threshold bytes. An interrupt will be triggered
344+
immediately once the detected value is above the high threshold. Can be 16-bit value.
345+
"""
346+
np_th_high = self._read_u16LE(_TSL2591_NPAIHTL)
347+
return np_th_high
348+
349+
@nopersist_threshold_high.setter
350+
def nopersist_threshold_high(self, val: int) -> None:
351+
lower = val & 0xFF
352+
upper = (val >> 8) & 0xFF
353+
self._write_u8(_TSL2591_NPAIHTL, lower)
354+
self._write_u8(_TSL2591_NPAIHTH, upper)
355+
356+
@property
357+
def persist(self) -> int:
358+
"""Get and set the interrupt persist filter - the number of consecutive out-of-range
359+
ALS cycles necessary to generate an interrupt. Valid persist values are 0 - 15 (inclusive),
360+
corresponding to a preset number of cycles. Only the 4 lower bits will be used to write
361+
to the device.
362+
Can be a value of:
363+
- ``0 (0000)`` - Every ALS cycle generates an interrupt.
364+
- ``1 (0001)`` - Any value outside of threshold range.
365+
- ``2 (0010)`` - 2 consecutive values out of range.
366+
- ``3 (0011)`` - 3 consecutive values out of range.
367+
- ``4 (0100)`` - 5 consecutive values out of range.
368+
- ``5 (0101)`` - 10 consecutive values out of range.
369+
- ``6 (0110)`` - 15 consecutive values out of range.
370+
- ``7 (0111)`` - 20 consecutive values out of range.
371+
- ``8 (1000)`` - 25 consecutive values out of range.
372+
- ``9 (1001)`` - 30 consecutive values out of range.
373+
- ``10 (1010)`` - 35 consecutive values out of range.
374+
- ``11 (1011)`` - 40 consecutive values out of range.
375+
- ``12 (1100)`` - 45 consecutive values out of range.
376+
- ``13 (1101)`` - 50 consecutive values out of range.
377+
- ``14 (1110)`` - 55 consecutive values out of range.
378+
- ``15 (1111)`` - 60 consecutive values out of range.
379+
"""
380+
persist = self._read_u8(_TSL2591_PERSIST_FILTER)
381+
return persist & 0x0F
382+
383+
@persist.setter
384+
def persist(self, val: int) -> None:
385+
assert 0 <= val <= 15
386+
persist = val & 0x0F
387+
self._write_u8(_TSL2591_PERSIST_FILTER, persist)
388+
231389
@property
232390
def raw_luminosity(self) -> Tuple[int, int]:
233391
"""Read the raw luminosity from the sensor (both IR + visible and IR

0 commit comments

Comments
 (0)