Skip to content

Commit 786521f

Browse files
authored
Merge pull request #22 from stonehippo/proximity_frequency_read_fix
Fix proximity read frequency
2 parents e07ea77 + 12fb5c6 commit 786521f

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

adafruit_vcnl4010.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@
5555
_VCNL4010_AMBIENT_LUX_SCALE = 0.25 # Lux value per 16-bit result value.
5656

5757
# User-facing constants:
58+
# Number of proximity measuremenrs per second
59+
SAMPLERATE_1_95 = 0
60+
SAMPLERATE_3_90625 = 1
61+
SAMPLERATE_7_8125 = 2
62+
SAMPLERATE_16_625 = 3
63+
SAMPLERATE_31_25 = 4
64+
SAMPLERATE_62_5 = 5
65+
SAMPLERATE_125 = 6
66+
SAMPLERATE_250 = 7
67+
68+
# Proximity modulator timing
5869
FREQUENCY_3M125 = 3
5970
FREQUENCY_1M5625 = 2
6071
FREQUENCY_781K25 = 1
@@ -113,6 +124,7 @@ def __init__(self, i2c, address=_VCNL4010_I2CADDR_DEFAULT):
113124
if (revision & 0xF0) != 0x20:
114125
raise RuntimeError("Failed to find VCNL4010, check wiring!")
115126
self.led_current = 20
127+
self.samplerate = SAMPLERATE_1_95
116128
self.frequency = FREQUENCY_390K625
117129
self._write_u8(_VCNL4010_INTCONTROL, 0x08)
118130

@@ -153,8 +165,8 @@ def led_current(self, val):
153165

154166
@property
155167
def led_current_mA(self):
156-
"""The current of the LED in milli-amps. The value here is
157-
specified in a milliamps from 0-200. Note that this value will be
168+
"""The current of the LED in milliamps. The value here is
169+
specified in milliamps from 0-200. Note that this value will be
158170
quantized down to a smaller less-accurate value as the chip only
159171
supports current changes in 10mA increments, i.e. a value of 123 mA will
160172
actually use 120 mA. See the datasheet for how the LED current impacts
@@ -167,18 +179,44 @@ def led_current_mA(self):
167179
def led_current_mA(self, val):
168180
self.led_current = val // 10
169181

182+
@property
183+
def samplerate(self):
184+
"""
185+
The frequency of proximity measurements per second. Must be a value of:
186+
187+
- SAMPLERATE_1_95: 1.95 measurements/sec (default)
188+
- SAMPLERATE_3_90625: 3.90625 measurements/sec
189+
- SAMPLERATE_7_8125: 7.8125 measurements/sec
190+
- SAMPLERATE_16_625: 16.625 measurements/sec
191+
- SAMPLERATE_31_25: 31.25 measurements/sec
192+
- SAMPLERATE_62_5: 62.5 measurements/sec
193+
- SAMPLERATE_125: 125 measurements/sec
194+
- SAMPLERATE_250: 250 measurements/sec
195+
196+
See the datasheet for how frequency changes the power consumption and
197+
proximity detection accuracy.
198+
"""
199+
return self._read_u8(_VCNL4010_PROXRATE)
200+
201+
@samplerate.setter
202+
def samplerate(self, val):
203+
assert 0 <= val <= 7
204+
self._write_u8(_VCNL4010_PROXRATE, val)
205+
170206
@property
171207
def frequency(self):
172208
"""
173-
The frequency of proximity measurements. Must be a value of:
209+
Proximity modulator timimg. This is the frequency of the IR square
210+
wave used for the proximity measurement.
211+
212+
Must be a value of:
174213
175214
- FREQUENCY_3M125: 3.125 Mhz
176215
- FREQUENCY_1M5625: 1.5625 Mhz
177216
- FREQUENCY_781K25: 781.25 Khz
178217
- FREQUENCY_390K625: 390.625 Khz (default)
179218
180-
See the datasheet for how frequency changes the proximity detection
181-
accuracy.
219+
The datasheet recommended leaving this at the default.
182220
"""
183221
return (self._read_u8(_VCNL4010_MODTIMING) >> 3) & 0x03
184222

0 commit comments

Comments
 (0)