Skip to content

Add threshold functionality #22

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

Merged
merged 1 commit into from
Feb 6, 2019
Merged
Changes from all 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
76 changes: 76 additions & 0 deletions adafruit_tsl2561.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@

_REGISTER_CONTROL = const(0x00)
_REGISTER_TIMING = const(0x01)
_REGISTER_TH_LOW = const(0x02)
_REGISTER_TH_HIGH = const(0x04)
_REGISTER_INT_CTRL = const(0x06)
_REGISTER_CHAN0_LOW = const(0x0C)
_REGISTER_CHAN1_LOW = const(0x0E)
_REGISTER_ID = const(0x0A)
Expand Down Expand Up @@ -158,6 +161,79 @@ def integration_time(self, value):
with self.i2c_device as i2c:
i2c.write(self.buf, end=2)

@property
def threshold_low(self):
"""The low light interrupt threshold level."""
low, high = self._read_register(_REGISTER_TH_LOW, 2)
return high << 8 | low

@threshold_low.setter
def threshold_low(self, value):
self.buf[0] = _COMMAND_BIT | _WORD_BIT | _REGISTER_TH_LOW
self.buf[1] = value & 0xff
self.buf[2] = (value >> 8) & 0xff
with self.i2c_device as i2c:
i2c.write(self.buf)

@property
def threshold_high(self):
"""The upper light interrupt threshold level."""
low, high = self._read_register(_REGISTER_TH_HIGH, 2)
return high << 8 | low

@threshold_high.setter
def threshold_high(self, value):
self.buf[0] = _COMMAND_BIT | _WORD_BIT | _REGISTER_TH_HIGH
self.buf[1] = value & 0xff
self.buf[2] = (value >> 8) & 0xff
with self.i2c_device as i2c:
i2c.write(self.buf)

@property
def cycles(self):
"""The number of integration cycles for which an out of bounds
value must persist to cause an interrupt."""
value = self._read_register(_REGISTER_INT_CTRL)
return value & 0x0f

@cycles.setter
def cycles(self, value):
current = self._read_register(_REGISTER_INT_CTRL)
self.buf[0] = _COMMAND_BIT | _REGISTER_INT_CTRL
self.buf[1] = current | (value & 0x0f)
with self.i2c_device as i2c:
i2c.write(self.buf, end=2)

@property
def interrupt_mode(self):
"""The interrupt mode selection.

==== =========================
Mode Description
==== =========================
0 Interrupt output disabled
1 Level Interrupt
2 SMBAlert compliant
3 Test Mode
==== =========================

"""
return (self._read_register(_REGISTER_INT_CTRL) >> 4) & 0x03

@interrupt_mode.setter
def interrupt_mode(self, value):
current = self._read_register(_REGISTER_INT_CTRL)
self.buf[0] = _COMMAND_BIT | _REGISTER_INT_CTRL
self.buf[1] = (current & 0x0f) | ((value & 0x03) << 4)
with self.i2c_device as i2c:
i2c.write(self.buf, end=2)

def clear_interrupt(self):
"""Clears any pending interrupt."""
self.buf[0] = 0xc0
with self.i2c_device as i2c:
i2c.write(self.buf, end=1)

def _compute_lux(self):
"""Based on datasheet for FN package."""
ch0, ch1 = self.luminosity
Expand Down