Skip to content

Commit 4b7b958

Browse files
committed
add threshold functionality
1 parent e7c7f0f commit 4b7b958

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

adafruit_tsl2561.py

+76
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363

6464
_REGISTER_CONTROL = const(0x00)
6565
_REGISTER_TIMING = const(0x01)
66+
_REGISTER_TH_LOW = const(0x02)
67+
_REGISTER_TH_HIGH = const(0x04)
68+
_REGISTER_INT_CTRL = const(0x06)
6669
_REGISTER_CHAN0_LOW = const(0x0C)
6770
_REGISTER_CHAN1_LOW = const(0x0E)
6871
_REGISTER_ID = const(0x0A)
@@ -158,6 +161,79 @@ def integration_time(self, value):
158161
with self.i2c_device as i2c:
159162
i2c.write(self.buf, end=2)
160163

164+
@property
165+
def threshold_low(self):
166+
"""The low light interrupt threshold level."""
167+
low, high = self._read_register(_REGISTER_TH_LOW, 2)
168+
return high << 8 | low
169+
170+
@threshold_low.setter
171+
def threshold_low(self, value):
172+
self.buf[0] = _COMMAND_BIT | _WORD_BIT | _REGISTER_TH_LOW
173+
self.buf[1] = value & 0xff
174+
self.buf[2] = (value >> 8) & 0xff
175+
with self.i2c_device as i2c:
176+
i2c.write(self.buf)
177+
178+
@property
179+
def threshold_high(self):
180+
"""The upper light interrupt threshold level."""
181+
low, high = self._read_register(_REGISTER_TH_HIGH, 2)
182+
return high << 8 | low
183+
184+
@threshold_high.setter
185+
def threshold_high(self, value):
186+
self.buf[0] = _COMMAND_BIT | _WORD_BIT | _REGISTER_TH_HIGH
187+
self.buf[1] = value & 0xff
188+
self.buf[2] = (value >> 8) & 0xff
189+
with self.i2c_device as i2c:
190+
i2c.write(self.buf)
191+
192+
@property
193+
def cycles(self):
194+
"""The number of integration cycles for which an out of bounds
195+
value must persist to cause an interrupt."""
196+
value = self._read_register(_REGISTER_INT_CTRL)
197+
return value & 0x0f
198+
199+
@cycles.setter
200+
def cycles(self, value):
201+
current = self._read_register(_REGISTER_INT_CTRL)
202+
self.buf[0] = _COMMAND_BIT | _REGISTER_INT_CTRL
203+
self.buf[1] = current | (value & 0x0f)
204+
with self.i2c_device as i2c:
205+
i2c.write(self.buf, end=2)
206+
207+
@property
208+
def interrupt_mode(self):
209+
"""The interrupt mode selection.
210+
211+
==== =========================
212+
Mode Description
213+
==== =========================
214+
0 Interrupt output disabled
215+
1 Level Interrupt
216+
2 SMBAlert compliant
217+
3 Test Mode
218+
==== =========================
219+
220+
"""
221+
return (self._read_register(_REGISTER_INT_CTRL) >> 4) & 0x03
222+
223+
@interrupt_mode.setter
224+
def interrupt_mode(self, value):
225+
current = self._read_register(_REGISTER_INT_CTRL)
226+
self.buf[0] = _COMMAND_BIT | _REGISTER_INT_CTRL
227+
self.buf[1] = (current & 0x0f) | ((value & 0x03) << 4)
228+
with self.i2c_device as i2c:
229+
i2c.write(self.buf, end=2)
230+
231+
def clear_interrupt(self):
232+
"""Clears any pending interrupt."""
233+
self.buf[0] = 0xc0
234+
with self.i2c_device as i2c:
235+
i2c.write(self.buf, end=1)
236+
161237
def _compute_lux(self):
162238
"""Based on datasheet for FN package."""
163239
ch0, ch1 = self.luminosity

0 commit comments

Comments
 (0)