49
49
import adafruit_bus_device .i2c_device as i2cdevice
50
50
from adafruit_register .i2c_struct import UnaryStruct , ROUnaryStruct
51
51
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
54
53
55
54
__version__ = "0.0.0-auto.0"
56
55
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VCNL4040.git"
@@ -100,6 +99,12 @@ class VCNL4040: # pylint: disable=too-few-public-methods
100
99
PS_INT_AWAY = const (0x2 )
101
100
PS_INT_CLOSE_AWAY = const (0x3 )
102
101
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
+
103
108
# ID_LM - Device ID, address
104
109
_device_id = UnaryStruct (0x0C , "<H" )
105
110
"""The device ID."""
@@ -148,13 +153,22 @@ class VCNL4040: # pylint: disable=too-few-public-methods
148
153
# PS_THDH_LM - PS high interrupt threshold setting
149
154
proximity_high_threshold = UnaryStruct (0x07 , "<H" )
150
155
"""Proximity sensor interrupt high threshold setting."""
156
+
157
+ interrupt_state = ROUnaryStruct (0x0B , "<H" )
158
+
151
159
# 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
+
158
172
159
173
led_current = RWBits (3 , 0x04 , 8 , register_width = 2 )
160
174
"""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
220
234
light_high_threshold = UnaryStruct (0x01 , "<H" )
221
235
"""Ambient light interrupt high threshold."""
222
236
# 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 )
227
247
228
248
# White_Data_LM - White output data
229
249
white = ROUnaryStruct (0x0A , "<H" )
@@ -248,18 +268,37 @@ class VCNL4040: # pylint: disable=too-few-public-methods
248
268
249
269
# PS_MS - White channel enable/disable, PS mode, PS protection setting, LED current
250
270
# 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
+
253
274
254
- def __init__ (self , i2c , address = 0x60 , interrupt_pin = None ):
275
+ def __init__ (self , i2c , address = 0x60 ):
255
276
self .i2c_device = i2cdevice .I2CDevice (i2c , address )
256
277
if self ._device_id != 0x186 :
257
278
raise RuntimeError ("Failed to find VCNL4040 - check wiring!" )
258
279
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
+ }
262
286
263
287
self .proximity_shutdown = False
264
288
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
0 commit comments