Skip to content

Update interrupt handling, fix white, and setup PyPi #3

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 4 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
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
7 changes: 2 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ python:
cache:
pip: true

# TODO: if deployment to PyPi is desired, change 'DEPLOY_PYPI' to "true",
# or remove the env block entirely and remove the condition in the
# deploy block.
env:
- DEPLOY_PYPI="false"
- DEPLOY_PYPI="true"

deploy:
- provider: releases
Expand All @@ -31,7 +28,7 @@ deploy:
- provider: pypi
user: adafruit-travis
password:
secure: #-- PASTE ENCRYPTED PASSWORD HERE --#
secure: J/BVp5vHcmjpKFVmYCL0NoUqbk/TLwq1VtpF+EEvpzV+xEAelnwbnv49I27gUsRZH/mfBZi2KO+cTdVXAWCchXAo7QuPyHSnXdHLhmYmO5qRuClAE7Jyn8lYoScPpXhxV9jXg0kpq1CW1xetqxepPOqkT1+i+mL+Cb+2XurXv9kWSE+kzIdGIAAwbxPlgEPnUuNHCAjGBfzEWoCAk3BRT/31sfJ7K87awIcrYeOSxyl4H+uqCXRw5KvaleiW7sSu5Dgl+uwKUXnir/Nwm7eyrfF4N38Yf7sMmqiZxSMFrTsK9aDtr4NFfJvgKfXHKcdqdBXKFplOE7qILG/4WBSCXwO1Gep+sd2aQB6TJCg73Y/vh4skutIgigYKbqkZ7kZ8mFTlFxxRT1pTDf2qbcxUkwwjBGWQO9ORj8q8wwXQAMfMk5bxTgz3Vtqzbk0fruU2TswZu+LrgsLrVOh/gqBb302yiLhI9RzH+06M60JD59M0h1KvLIqJmaYI2oT2oguWpqnNeHLzC+yNx4IRe3IGrK2XF/OVzm9DzIAkgrCguZWq2ksx+RfTtwmRX0wtgUdpy+nCZDGDpWmG/xrC5PaMRTlA8QVK0dETjGs3wXxGxQyM8rjnxJ324+8LfrvT8/m8/AEjXQM/Y9Cv3sHI1hLW9UMPa8fLQFzuuAbNQuuV3Eo=
on:
tags: true
condition: $DEPLOY_PYPI = "true"
Expand Down
2 changes: 0 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ This is easily achieved by downloading

Installing from PyPI
--------------------
.. note:: This library is not available on PyPI yet. Install documentation is included
as a standard element. Stay tuned for PyPI availability!

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
PyPI <https://pypi.org/project/adafruit-circuitpython-vcnl4040/>`_. To install for current user:
Expand Down
77 changes: 58 additions & 19 deletions adafruit_vcnl4040.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
import adafruit_bus_device.i2c_device as i2cdevice
from adafruit_register.i2c_struct import UnaryStruct, ROUnaryStruct
from adafruit_register.i2c_bits import RWBits
from adafruit_register.i2c_bit import RWBit, ROBit
import digitalio
from adafruit_register.i2c_bit import RWBit

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VCNL4040.git"
Expand Down Expand Up @@ -100,6 +99,12 @@ class VCNL4040: # pylint: disable=too-few-public-methods
PS_INT_AWAY = const(0x2)
PS_INT_CLOSE_AWAY = const(0x3)

# Offsets into interrupt status register for different types
ALS_IF_L = const(0x0D)
ALS_IF_H = const(0x0C)
PS_IF_CLOSE = const(0x09)
PS_IF_AWAY = const(0x08)

# ID_LM - Device ID, address
_device_id = UnaryStruct(0x0C, "<H")
"""The device ID."""
Expand Down Expand Up @@ -148,13 +153,22 @@ class VCNL4040: # pylint: disable=too-few-public-methods
# PS_THDH_LM - PS high interrupt threshold setting
proximity_high_threshold = UnaryStruct(0x07, "<H")
"""Proximity sensor interrupt high threshold setting."""

interrupt_state = ROUnaryStruct(0x0B, "<H")

# INT_FLAG - PS interrupt flag
proximity_high_interrupt = ROBit(0x0B, 9, register_width=2)
"""If interrupt is set to ``PS_INT_CLOSE`` or ``PS_INT_CLOSE_AWAY``, trigger event when
proximity rises above high threshold interrupt."""
proximity_low_interrupt = ROBit(0x0B, 8, register_width=2)
"""If interrupt is set to ``PS_INT_AWAY`` or ``PS_INT_CLOSE_AWAY``, trigger event when
proximity drops below low threshold."""
@property
def proximity_high_interrupt(self):
"""If interrupt is set to ``PS_INT_CLOSE`` or ``PS_INT_CLOSE_AWAY``, trigger event when
proximity rises above high threshold interrupt."""
return self._get_and_clear_cached_interrupt_state(self.PS_IF_CLOSE)

@property
def proximity_low_interrupt(self):
"""If interrupt is set to ``PS_INT_AWAY`` or ``PS_INT_CLOSE_AWAY``, trigger event when
proximity drops below low threshold."""
return self._get_and_clear_cached_interrupt_state(self.PS_IF_AWAY)


led_current = RWBits(3, 0x04, 8, register_width=2)
"""LED current selection setting, in mA. Options are: LED_50MA, LED_75MA, LED_100MA, LED_120MA,
Expand Down Expand Up @@ -220,10 +234,16 @@ class VCNL4040: # pylint: disable=too-few-public-methods
light_high_threshold = UnaryStruct(0x01, "<H")
"""Ambient light interrupt high threshold."""
# INT_FLAG - ALS interrupt flag
light_high_interrupt = ROBit(0x0B, 12, register_width=2)
"""High interrupt event. Triggered when ambient light value exceeds high threshold."""
light_low_interrupt = ROBit(0x0B, 13, register_width=2)
"""Low interrupt event. Triggered when ambient light value drops below low threshold."""

@property
def light_high_interrupt(self):
"""High interrupt event. Triggered when ambient light value exceeds high threshold."""
return self._get_and_clear_cached_interrupt_state(self.ALS_IF_H)

@property
def light_low_interrupt(self):
"""Low interrupt event. Triggered when ambient light value drops below low threshold."""
return self._get_and_clear_cached_interrupt_state(self.ALS_IF_L)

# White_Data_LM - White output data
white = ROUnaryStruct(0x0A, "<H")
Expand All @@ -248,18 +268,37 @@ class VCNL4040: # pylint: disable=too-few-public-methods

# PS_MS - White channel enable/disable, PS mode, PS protection setting, LED current
# White_EN - PS_MS_H, 7th bit - White channel enable/disable
white_enable = RWBit(0x04, 15, register_width=2)
"""White data enable. ``True`` when enabled."""
white_shutdown = RWBit(0x04, 15, register_width=2)
"""White light channel shutdown. When ``True``, white light data is disabled."""


def __init__(self, i2c, address=0x60, interrupt_pin=None):
def __init__(self, i2c, address=0x60):
self.i2c_device = i2cdevice.I2CDevice(i2c, address)
if self._device_id != 0x186:
raise RuntimeError("Failed to find VCNL4040 - check wiring!")

self._interrupt_pin = interrupt_pin
if self._interrupt_pin:
self._interrupt_pin.switch_to_input(pull=digitalio.Pull.UP)
self.cached_interrupt_state = {
self.ALS_IF_L : False,
self.ALS_IF_H : False,
self.PS_IF_CLOSE : False,
self.PS_IF_AWAY : False
}

self.proximity_shutdown = False
self.light_shutdown = False
self.white_enable = True
self.white_shutdown = False

def _update_interrupt_state(self):
interrupts = [self.PS_IF_AWAY, self.PS_IF_CLOSE, self.ALS_IF_H, self.ALS_IF_L]
new_interrupt_state = self.interrupt_state
for interrupt in interrupts:
new_state = (new_interrupt_state & (1 << interrupt) > 0)
if new_state:
self.cached_interrupt_state[interrupt] = new_state

def _get_and_clear_cached_interrupt_state(self, interrupt_offset):
self._update_interrupt_state()
new_interrupt_state = self.cached_interrupt_state[interrupt_offset]
self.cached_interrupt_state[interrupt_offset] = False

return new_interrupt_state
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# digitalio, micropython and busio. List the modules you use. Without it, the
# autodoc module docs will fail to generate with a warning.
autodoc_mock_imports = ["adafruit_register.i2c_struct", "adafruit_register.i2c_bits",
"adafruit_register.i2c_bit"]
"adafruit_register.i2c_bit", "micropython", "adafruit_bus_device", "adafruit_register"]


intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'BusDevice': ('https://circuitpython.readthedocs.io/projects/busdevice/en/latest/', None),'Register': ('https://circuitpython.readthedocs.io/projects/register/en/latest/', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}
Expand Down