Skip to content

Commit fba0a81

Browse files
committed
Fix proximity read frequency
Previously, the module used an incorrect register to set the proximity modulator timing of the chip, rather than the number of proximity reads per second. This change updates the getter and setter for frequency to use the correct proximity rate register, and adds a modulation property to continue to provide access to that register (though it probably should be left at the default)
1 parent e07ea77 commit fba0a81

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

adafruit_vcnl4010.py

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

5757
# User-facing constants:
58-
FREQUENCY_3M125 = 3
59-
FREQUENCY_1M5625 = 2
60-
FREQUENCY_781K25 = 1
61-
FREQUENCY_390K625 = 0
58+
# Number of proximity measuremenrs per second
59+
FREQUENCY_1_95 = 0
60+
FREQUENCY_3_90625 = 1
61+
FREQUENCY_7_8125 = 2
62+
FREQUENCY_16_625 = 3
63+
FREQUENCY_31_25 = 4
64+
FREQUENCY_62_5 = 5
65+
FREQUENCY_125 = 6
66+
FREQUENCY_250 = 7
67+
68+
# Proximity modulator timing
69+
MODULATION_3M125 = 3
70+
MODULATION_1M5625 = 2
71+
MODULATION_781K25 = 1
72+
MODULATION_390K625 = 0
6273

6374
# Disable pylint's name warning as it causes too much noise. Suffixes like
6475
# BE (big-endian) or mA (milli-amps) don't confirm to its conventions--by
@@ -113,7 +124,8 @@ 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
116-
self.frequency = FREQUENCY_390K625
127+
self.frequency = FREQUENCY_1_95
128+
self.modulation = MODULATION_390K625
117129
self._write_u8(_VCNL4010_INTCONTROL, 0x08)
118130

119131
def _read_u8(self, address):
@@ -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
@@ -170,20 +182,41 @@ def led_current_mA(self, val):
170182
@property
171183
def frequency(self):
172184
"""
173-
The frequency of proximity measurements. Must be a value of:
185+
The frequency of proximity measurements per second. Must be a value of:
186+
187+
- FREQUENCY_1_95: 1.95 measurements/sec (default)
188+
- FREQUENCY_3_90625: 3.90625 measurements/sec
189+
- FREQUENCY_7_8125: 7.8125 measurements/sec
190+
- FREQUENCY_16_625: 16.625 measurements/sec
191+
- FREQUENCY_31_25: 31.25 measurements/sec
192+
- FREQUENCY_62_5: 62.5 measurements/sec
193+
- FREQUENCY_125: 125 measurements/sec
194+
- FREQUENCY_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+
@frequency.setter
202+
def frequency(self, val):
203+
assert 0 <= val <= 7
204+
self._write_u8(_VCNL4010_PROXRATE, val)
174205

175-
- FREQUENCY_3M125: 3.125 Mhz
176-
- FREQUENCY_1M5625: 1.5625 Mhz
177-
- FREQUENCY_781K25: 781.25 Khz
178-
- FREQUENCY_390K625: 390.625 Khz (default)
206+
@property
207+
def modulation(self):
208+
"""
209+
Proximity modulator timimg. Must be a value of:
179210
180-
See the datasheet for how frequency changes the proximity detection
181-
accuracy.
211+
- MODULATION_3M125: 3.125 Mhz
212+
- MODULATION_1M5625: 1.5625 Mhz
213+
- MODULATION_781K25: 781.25 Khz
214+
- MODULATION_390K625: 390.625 Khz (default)
182215
"""
183216
return (self._read_u8(_VCNL4010_MODTIMING) >> 3) & 0x03
184217

185-
@frequency.setter
186-
def frequency(self, val):
218+
@modulation.setter
219+
def modulation(self, val):
187220
assert 0 <= val <= 3
188221
timing = self._read_u8(_VCNL4010_MODTIMING)
189222
timing &= ~0b00011000

0 commit comments

Comments
 (0)