|
39 | 39 | __version__ = "0.0.0+auto.0"
|
40 | 40 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TSL2591.git"
|
41 | 41 |
|
42 |
| - |
43 | 42 | # Internal constants:
|
44 | 43 | _TSL2591_ADDR = const(0x29)
|
45 | 44 | _TSL2591_COMMAND_BIT = const(0xA0)
|
| 45 | +_TSL2591_SPECIAL_BIT = const(0xE0) |
46 | 46 | _TSL2591_ENABLE_POWEROFF = const(0x00)
|
47 | 47 | _TSL2591_ENABLE_POWERON = const(0x01)
|
48 | 48 | _TSL2591_ENABLE_AEN = const(0x02)
|
49 |
| -_TSL2591_ENABLE_AIEN = const(0x10) |
50 |
| -_TSL2591_ENABLE_NPIEN = const(0x80) |
| 49 | + |
51 | 50 | _TSL2591_REGISTER_ENABLE = const(0x00)
|
52 | 51 | _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 | + |
53 | 63 | _TSL2591_REGISTER_DEVICE_ID = const(0x12)
|
54 | 64 | _TSL2591_REGISTER_CHAN0_LOW = const(0x14)
|
55 | 65 | _TSL2591_REGISTER_CHAN1_LOW = const(0x16)
|
|
81 | 91 | """500 millis"""
|
82 | 92 | INTEGRATIONTIME_600MS = 0x05 # 600 millis
|
83 | 93 | """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.""" |
84 | 106 |
|
85 | 107 |
|
86 | 108 | class TSL2591:
|
@@ -167,19 +189,60 @@ def _write_u8(self, address: int, val: int) -> None:
|
167 | 189 | i2c.write(self._BUFFER, end=2)
|
168 | 190 |
|
169 | 191 | def enable(self) -> None:
|
170 |
| - """Put the device in a fully powered enabled mode.""" |
| 192 | + """Put the device in a powered, enabled mode.""" |
171 | 193 | self._write_u8(
|
172 | 194 | _TSL2591_REGISTER_ENABLE,
|
173 | 195 | _TSL2591_ENABLE_POWERON
|
174 |
| - | _TSL2591_ENABLE_AEN |
175 |
| - | _TSL2591_ENABLE_AIEN |
176 |
| - | _TSL2591_ENABLE_NPIEN, |
| 196 | + | _TSL2591_ENABLE_AEN, |
177 | 197 | )
|
178 | 198 |
|
179 | 199 | def disable(self) -> None:
|
180 | 200 | """Disable the device and go into low power mode."""
|
181 | 201 | self._write_u8(_TSL2591_REGISTER_ENABLE, _TSL2591_ENABLE_POWEROFF)
|
182 | 202 |
|
| 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 | + |
183 | 246 | @property
|
184 | 247 | def gain(self) -> int:
|
185 | 248 | """Get and set the gain of the sensor. Can be a value of:
|
@@ -228,6 +291,76 @@ def integration_time(self, val: int) -> None:
|
228 | 291 | # Keep track of integration time for future reading delay times.
|
229 | 292 | self._integration_time = val
|
230 | 293 |
|
| 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 | + |
231 | 364 | @property
|
232 | 365 | def raw_luminosity(self) -> Tuple[int, int]:
|
233 | 366 | """Read the raw luminosity from the sensor (both IR + visible and IR
|
|
0 commit comments