Skip to content

Commit 55a924e

Browse files
authored
Merge pull request #3 from siddacious/interrupt_update
Update interrupt handling, fix `white`, and setup PyPi
2 parents 0f8dcba + 1785612 commit 55a924e

File tree

4 files changed

+61
-27
lines changed

4 files changed

+61
-27
lines changed

.travis.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ python:
1111
cache:
1212
pip: true
1313

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

2017
deploy:
2118
- provider: releases
@@ -31,7 +28,7 @@ deploy:
3128
- provider: pypi
3229
user: adafruit-travis
3330
password:
34-
secure: #-- PASTE ENCRYPTED PASSWORD HERE --#
31+
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=
3532
on:
3633
tags: true
3734
condition: $DEPLOY_PYPI = "true"

README.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ This is easily achieved by downloading
3030

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

3634
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
3735
PyPI <https://pypi.org/project/adafruit-circuitpython-vcnl4040/>`_. To install for current user:

adafruit_vcnl4040.py

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949
import adafruit_bus_device.i2c_device as i2cdevice
5050
from adafruit_register.i2c_struct import UnaryStruct, ROUnaryStruct
5151
from adafruit_register.i2c_bits import RWBits
52-
from adafruit_register.i2c_bit import RWBit, ROBit
53-
import digitalio
52+
from adafruit_register.i2c_bit import RWBit
5453

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

102+
# Offsets into interrupt status register for different types
103+
ALS_IF_L = const(0x0D)
104+
ALS_IF_H = const(0x0C)
105+
PS_IF_CLOSE = const(0x09)
106+
PS_IF_AWAY = const(0x08)
107+
103108
# ID_LM - Device ID, address
104109
_device_id = UnaryStruct(0x0C, "<H")
105110
"""The device ID."""
@@ -148,13 +153,22 @@ class VCNL4040: # pylint: disable=too-few-public-methods
148153
# PS_THDH_LM - PS high interrupt threshold setting
149154
proximity_high_threshold = UnaryStruct(0x07, "<H")
150155
"""Proximity sensor interrupt high threshold setting."""
156+
157+
interrupt_state = ROUnaryStruct(0x0B, "<H")
158+
151159
# INT_FLAG - PS interrupt flag
152-
proximity_high_interrupt = ROBit(0x0B, 9, register_width=2)
153-
"""If interrupt is set to ``PS_INT_CLOSE`` or ``PS_INT_CLOSE_AWAY``, trigger event when
154-
proximity rises above high threshold interrupt."""
155-
proximity_low_interrupt = ROBit(0x0B, 8, register_width=2)
156-
"""If interrupt is set to ``PS_INT_AWAY`` or ``PS_INT_CLOSE_AWAY``, trigger event when
157-
proximity drops below low threshold."""
160+
@property
161+
def proximity_high_interrupt(self):
162+
"""If interrupt is set to ``PS_INT_CLOSE`` or ``PS_INT_CLOSE_AWAY``, trigger event when
163+
proximity rises above high threshold interrupt."""
164+
return self._get_and_clear_cached_interrupt_state(self.PS_IF_CLOSE)
165+
166+
@property
167+
def proximity_low_interrupt(self):
168+
"""If interrupt is set to ``PS_INT_AWAY`` or ``PS_INT_CLOSE_AWAY``, trigger event when
169+
proximity drops below low threshold."""
170+
return self._get_and_clear_cached_interrupt_state(self.PS_IF_AWAY)
171+
158172

159173
led_current = RWBits(3, 0x04, 8, register_width=2)
160174
"""LED current selection setting, in mA. Options are: LED_50MA, LED_75MA, LED_100MA, LED_120MA,
@@ -220,10 +234,16 @@ class VCNL4040: # pylint: disable=too-few-public-methods
220234
light_high_threshold = UnaryStruct(0x01, "<H")
221235
"""Ambient light interrupt high threshold."""
222236
# INT_FLAG - ALS interrupt flag
223-
light_high_interrupt = ROBit(0x0B, 12, register_width=2)
224-
"""High interrupt event. Triggered when ambient light value exceeds high threshold."""
225-
light_low_interrupt = ROBit(0x0B, 13, register_width=2)
226-
"""Low interrupt event. Triggered when ambient light value drops below low threshold."""
237+
238+
@property
239+
def light_high_interrupt(self):
240+
"""High interrupt event. Triggered when ambient light value exceeds high threshold."""
241+
return self._get_and_clear_cached_interrupt_state(self.ALS_IF_H)
242+
243+
@property
244+
def light_low_interrupt(self):
245+
"""Low interrupt event. Triggered when ambient light value drops below low threshold."""
246+
return self._get_and_clear_cached_interrupt_state(self.ALS_IF_L)
227247

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

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

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

259-
self._interrupt_pin = interrupt_pin
260-
if self._interrupt_pin:
261-
self._interrupt_pin.switch_to_input(pull=digitalio.Pull.UP)
280+
self.cached_interrupt_state = {
281+
self.ALS_IF_L : False,
282+
self.ALS_IF_H : False,
283+
self.PS_IF_CLOSE : False,
284+
self.PS_IF_AWAY : False
285+
}
262286

263287
self.proximity_shutdown = False
264288
self.light_shutdown = False
265-
self.white_enable = True
289+
self.white_shutdown = False
290+
291+
def _update_interrupt_state(self):
292+
interrupts = [self.PS_IF_AWAY, self.PS_IF_CLOSE, self.ALS_IF_H, self.ALS_IF_L]
293+
new_interrupt_state = self.interrupt_state
294+
for interrupt in interrupts:
295+
new_state = (new_interrupt_state & (1 << interrupt) > 0)
296+
if new_state:
297+
self.cached_interrupt_state[interrupt] = new_state
298+
299+
def _get_and_clear_cached_interrupt_state(self, interrupt_offset):
300+
self._update_interrupt_state()
301+
new_interrupt_state = self.cached_interrupt_state[interrupt_offset]
302+
self.cached_interrupt_state[interrupt_offset] = False
303+
304+
return new_interrupt_state

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# digitalio, micropython and busio. List the modules you use. Without it, the
2222
# autodoc module docs will fail to generate with a warning.
2323
autodoc_mock_imports = ["adafruit_register.i2c_struct", "adafruit_register.i2c_bits",
24-
"adafruit_register.i2c_bit"]
24+
"adafruit_register.i2c_bit", "micropython", "adafruit_bus_device", "adafruit_register"]
2525

2626

2727
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)}

0 commit comments

Comments
 (0)