Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added interrupt support #30

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 141 additions & 8 deletions adafruit_tsl2591.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,27 @@
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TSL2591.git"


# Internal constants:
_TSL2591_ADDR = const(0x29)
_TSL2591_COMMAND_BIT = const(0xA0)
_TSL2591_SPECIAL_BIT = const(0xE0)
_TSL2591_ENABLE_POWEROFF = const(0x00)
_TSL2591_ENABLE_POWERON = const(0x01)
_TSL2591_ENABLE_AEN = const(0x02)
_TSL2591_ENABLE_AIEN = const(0x10)
_TSL2591_ENABLE_NPIEN = const(0x80)

_TSL2591_REGISTER_ENABLE = const(0x00)
_TSL2591_REGISTER_CONTROL = const(0x01)

_TSL2591_AILTL = const(0x04)
_TSL2591_AILTH = const(0x05)
_TSL2591_AIHTL = const(0x06)
_TSL2591_AIHTH = const(0x07)
_TSL2591_NPAILTL = const(0x08)
_TSL2591_NPAILTH = const(0x09)
_TSL2591_NPAIHTL = const(0x0A)
_TSL2591_NPAIHTH = const(0x0B)
_TSL2591_PERSIST_FILTER = const(0x0C)

_TSL2591_REGISTER_DEVICE_ID = const(0x12)
_TSL2591_REGISTER_CHAN0_LOW = const(0x14)
_TSL2591_REGISTER_CHAN1_LOW = const(0x16)
Expand Down Expand Up @@ -81,6 +91,18 @@
"""500 millis"""
INTEGRATIONTIME_600MS = 0x05 # 600 millis
"""600 millis"""
CLEAR_INTERRUPT = const(0x06)
"""Clears ALS interrupt"""
CLEAR_ALL_INTERRUPTS = const(0x07)
"""Clears ALS and no persist ALS interrupt"""
CLEAR_PERSIST_INTERRUPT = const(0x0A)
"""Clears no persist ALS interrupt"""
ENABLE_AIEN = const(0x10)
"""ALS Interrupt Enable. When asserted permits ALS interrupts to be generated."""
ENABLE_NPIEN = const(0x80)
"""No Persist Interrupt Enable. When asserted NP Threshold conditions will generate an interrupt."""
ENABLE_NPAIEN = const(0x10 | 0x80)
"""ALS and No Persist Interrupt Enable."""


class TSL2591:
Expand Down Expand Up @@ -167,19 +189,60 @@ def _write_u8(self, address: int, val: int) -> None:
i2c.write(self._BUFFER, end=2)

def enable(self) -> None:
"""Put the device in a fully powered enabled mode."""
"""Put the device in a powered, enabled mode."""
self._write_u8(
_TSL2591_REGISTER_ENABLE,
_TSL2591_ENABLE_POWERON
| _TSL2591_ENABLE_AEN
| _TSL2591_ENABLE_AIEN
| _TSL2591_ENABLE_NPIEN,
_TSL2591_ENABLE_POWERON | _TSL2591_ENABLE_AEN,
)

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

def clear_interrupt(self, operation: int) -> None:
"""Send special function command to control interrupt bits in the status register (0x13).
Can be a value of:
- ``CLEAR_INTERRUPT``
- ``CLEAR_ALL_INTERRUPTS``
- ``CLEAR_PERSIST_INTERRUPT``
"""
assert operation in (
CLEAR_INTERRUPT,
CLEAR_ALL_INTERRUPTS,
CLEAR_PERSIST_INTERRUPT,
)
control = (_TSL2591_SPECIAL_BIT | operation) & 0xFF
with self._device as i2c:
self._BUFFER[0] = control
i2c.write(self._BUFFER, end=1)

def enable_interrupt(self, interrupts: int) -> None:
"""Enable interrupts on device. ENABLE_NPIEN will turn on No Persist interrupts, these
bypass the persist filter and assert immediately. ENABLE_AIEN will assert after their
threshold values have exceeded the persist filter cycle constraints. The device powers
on with thresholds at 0, meaning enabling interrupts may cause an immediate assertion.
Can be a value of:
- ``ENABLE_NPIEN``
- ``ENABLE_AIEN``
- ``ENABLE_NPAIEN``
"""
assert interrupts in (ENABLE_NPIEN, ENABLE_AIEN, (ENABLE_NPIEN | ENABLE_AIEN))
functions = self._read_u8(_TSL2591_REGISTER_ENABLE)
functions = (functions | interrupts) & 0xFF
self._write_u8(_TSL2591_REGISTER_ENABLE, functions)

def disable_interrupt(self, interrupts: int) -> None:
"""Disables the requested interrupts.
Can be a value of:
- ``ENABLE_NPIEN``
- ``ENABLE_AIEN``
- ``ENABLE_NPAIEN``
"""
assert interrupts in (ENABLE_NPIEN, ENABLE_AIEN, (ENABLE_NPIEN | ENABLE_AIEN))
functions = self._read_u8(_TSL2591_REGISTER_ENABLE)
functions = (functions & ~interrupts) & 0xFF
self._write_u8(_TSL2591_REGISTER_ENABLE, functions)

@property
def gain(self) -> int:
"""Get and set the gain of the sensor. Can be a value of:
Expand Down Expand Up @@ -228,6 +291,76 @@ def integration_time(self, val: int) -> None:
# Keep track of integration time for future reading delay times.
self._integration_time = val

@property
def threshold_low(self) -> int:
"""Get and set the ALS interrupt low threshold bytes. If the detected value exceeds
threshold for the number of persist cycles an interrupt will be triggered.
Can be 16-bit value."""
th_low = self._read_u16LE(_TSL2591_AILTL)
return th_low

@threshold_low.setter
def threshold_low(self, value: int) -> None:
lower = value & 0xFF
upper = (value >> 8) & 0xFF
self._write_u8(_TSL2591_AILTL, lower)
self._write_u8(_TSL2591_AILTH, upper)

@property
def threshold_high(self) -> int:
"""Get and set the ALS interrupt high threshold bytes. If the detected value exceeds
threshold for the number of persist cycles an interrupt will be triggered.
Can be 16-bit value."""
th_high = self._read_u16LE(_TSL2591_AIHTL)
return th_high

@threshold_high.setter
def threshold_high(self, value: int) -> None:
lower = value & 0xFF
upper = (value >> 8) & 0xFF
self._write_u8(_TSL2591_AIHTL, lower)
self._write_u8(_TSL2591_AIHTH, upper)

@property
def nopersist_threshold_low(self) -> int:
"""Get and set the No Persist ALS low threshold bytes. An interrupt will be triggered
immediately once threshold is exceeded. Can be 16-bit value."""
np_th_low = self._read_u16LE(_TSL2591_NPAILTL)
return np_th_low

@nopersist_threshold_low.setter
def nopersist_threshold_low(self, value: int) -> None:
lower = value & 0xFF
upper = (value >> 8) & 0xFF
self._write_u8(_TSL2591_NPAILTL, lower)
self._write_u8(_TSL2591_NPAILTH, upper)

@property
def nopersist_threshold_high(self) -> int:
"""Get and set the No Persist ALS high threshold bytes. An interrupt will be triggered
immediately once theshold is exceeded. Can be 16-bit value."""
np_th_high = self._read_u16LE(_TSL2591_NPAIHTL)
return np_th_high

@nopersist_threshold_high.setter
def nopersist_threshold_high(self, value: int) -> None:
lower = value & 0xFF
upper = (value >> 8) & 0xFF
self._write_u8(_TSL2591_NPAIHTL, lower)
self._write_u8(_TSL2591_NPAIHTH, upper)

@property
def persist(self) -> int:
"""Get and set the interrupt persistence filter - the number of consecutive out-of-range
ALS cycles necessary to generate an interrupt."""
persist = self._read_u8(_TSL2591_PERSIST_FILTER)
return persist & 0x0F

@persist.setter
def persist(self, value: int) -> None:
persist = value & 0x0F
self._write_u8(_TSL2591_PERSIST_FILTER, persist)

@property
def raw_luminosity(self) -> Tuple[int, int]:
"""Read the raw luminosity from the sensor (both IR + visible and IR
Expand Down
Loading