Skip to content

Commit f51973b

Browse files
committed
Added interrupt support
Remove NPIEN and AIEN from enable command. Made separate control functions for enabling interrupts. Exposed new variables for setting enabled functions. Added persist, no persist threshold high and low, and ALS threshold high and low properties for setting boundaries. Interrupts are off by default so users have a chance to set thresholds and persist values before they receive an interrupt. Otherwise, with thesholds at 0 interrupts always fire until set or cleared. When both are enabled users can see unexpected interrupts if only only style is expected but both are on. Tested on Raspberry Pi 5.
1 parent 2ea8265 commit f51973b

File tree

1 file changed

+140
-7
lines changed

1 file changed

+140
-7
lines changed

adafruit_tsl2591.py

Lines changed: 140 additions & 7 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,60 @@ 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,
173195
_TSL2591_ENABLE_POWERON
174-
| _TSL2591_ENABLE_AEN
175-
| _TSL2591_ENABLE_AIEN
176-
| _TSL2591_ENABLE_NPIEN,
196+
| _TSL2591_ENABLE_AEN,
177197
)
178198

179199
def disable(self) -> None:
180200
"""Disable the device and go into low power mode."""
181201
self._write_u8(_TSL2591_REGISTER_ENABLE, _TSL2591_ENABLE_POWEROFF)
182202

203+
def clear_interrupt(self, operation: int) -> None:
204+
"""Send special function command to control interrupt bits in the status register (0x13).
205+
Can be a value of:
206+
- ``CLEAR_INTERRUPT``
207+
- ``CLEAR_ALL_INTERRUPTS``
208+
- ``CLEAR_PERSIST_INTERRUPT``
209+
"""
210+
assert operation in (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. ENABLE_AIEN will assert after their
222+
threshold values have exceeded the persist filter cycle constraints. The device powers
223+
on with thresholds at 0, meaning enabling interrupts may cause an immediate assertion.
224+
Can be a value of:
225+
- ``ENABLE_NPIEN``
226+
- ``ENABLE_AIEN``
227+
- ``ENABLE_NPAIEN``
228+
"""
229+
assert interrupts in (ENABLE_NPIEN, ENABLE_AIEN, (ENABLE_NPIEN | ENABLE_AIEN))
230+
functions = self._read_u8(_TSL2591_REGISTER_ENABLE)
231+
functions = (functions | interrupts) & 0xFF
232+
self._write_u8(_TSL2591_REGISTER_ENABLE, functions)
233+
234+
def disable_interrupt(self, interrupts: int) -> None:
235+
"""Disables the requested interrupts.
236+
Can be a value of:
237+
- ``ENABLE_NPIEN``
238+
- ``ENABLE_AIEN``
239+
- ``ENABLE_NPAIEN``
240+
"""
241+
assert interrupts in (ENABLE_NPIEN, ENABLE_AIEN, (ENABLE_NPIEN | ENABLE_AIEN))
242+
functions = self._read_u8(_TSL2591_REGISTER_ENABLE)
243+
functions = (functions & ~interrupts) & 0xFF
244+
self._write_u8(_TSL2591_REGISTER_ENABLE, functions)
245+
183246
@property
184247
def gain(self) -> int:
185248
"""Get and set the gain of the sensor. Can be a value of:
@@ -228,6 +291,76 @@ def integration_time(self, val: int) -> None:
228291
# Keep track of integration time for future reading delay times.
229292
self._integration_time = val
230293

294+
@property
295+
def threshold_low(self) -> int:
296+
"""Get and set the ALS interrupt low threshold bytes. If the detected value exceeds
297+
threshold for the number of persist cycles an interrupt will be triggered.
298+
Can be 16-bit value."""
299+
th_low = self._read_u16LE(_TSL2591_AILTL)
300+
return th_low
301+
302+
@threshold_low.setter
303+
def threshold_low(self, value: int) -> None:
304+
lower = value & 0xFF
305+
upper = (value >> 8) & 0xFF
306+
self._write_u8(_TSL2591_AILTL, lower)
307+
self._write_u8(_TSL2591_AILTH, upper)
308+
309+
@property
310+
def threshold_high(self) -> int:
311+
"""Get and set the ALS interrupt high threshold bytes. If the detected value exceeds
312+
threshold for the number of persist cycles an interrupt will be triggered.
313+
Can be 16-bit value."""
314+
th_high = self._read_u16LE(_TSL2591_AIHTL)
315+
return th_high
316+
317+
@threshold_high.setter
318+
def threshold_high(self, value: int) -> None:
319+
lower = value & 0xFF
320+
upper = (value >> 8) & 0xFF
321+
self._write_u8(_TSL2591_AIHTL, lower)
322+
self._write_u8(_TSL2591_AIHTH, upper)
323+
324+
@property
325+
def nopersist_threshold_low(self) -> int:
326+
"""Get and set the No Persist ALS low threshold bytes. An interrupt will be triggered
327+
immediately once threshold is exceeded. Can be 16-bit value."""
328+
np_th_low = self._read_u16LE(_TSL2591_NPAILTL)
329+
return np_th_low
330+
331+
@nopersist_threshold_low.setter
332+
def nopersist_threshold_low(self, value: int) -> None:
333+
lower = value & 0xFF
334+
upper = (value >> 8) & 0xFF
335+
self._write_u8(_TSL2591_NPAILTL, lower)
336+
self._write_u8(_TSL2591_NPAILTH, upper)
337+
338+
@property
339+
def nopersist_threshold_high(self) -> int:
340+
"""Get and set the No Persist ALS high threshold bytes. An interrupt will be triggered
341+
immediately once theshold is exceeded. Can be 16-bit value."""
342+
np_th_high = self._read_u16LE(_TSL2591_NPAIHTL)
343+
return np_th_high
344+
345+
@nopersist_threshold_high.setter
346+
def nopersist_threshold_high(self, value: int) -> None:
347+
lower = value & 0xFF
348+
upper = (value >> 8) & 0xFF
349+
self._write_u8(_TSL2591_NPAIHTL, lower)
350+
self._write_u8(_TSL2591_NPAIHTH, upper)
351+
352+
@property
353+
def persist(self) -> int:
354+
"""Get and set the interrupt persistence filter - the number of consecutive out-of-range
355+
ALS cycles necessary to generate an interrupt."""
356+
persist = self._read_u8(_TSL2591_PERSIST_FILTER)
357+
return persist & 0x0F
358+
359+
@persist.setter
360+
def persist(self, value: int) -> None:
361+
persist = value & 0x0F
362+
self._write_u8(_TSL2591_PERSIST_FILTER, persist)
363+
231364
@property
232365
def raw_luminosity(self) -> Tuple[int, int]:
233366
"""Read the raw luminosity from the sensor (both IR + visible and IR

0 commit comments

Comments
 (0)