Skip to content

Fix proximity read frequency #22

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
Feb 6, 2022
Merged
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
48 changes: 43 additions & 5 deletions adafruit_vcnl4010.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@
_VCNL4010_AMBIENT_LUX_SCALE = 0.25 # Lux value per 16-bit result value.

# User-facing constants:
# Number of proximity measuremenrs per second
SAMPLERATE_1_95 = 0
SAMPLERATE_3_90625 = 1
SAMPLERATE_7_8125 = 2
SAMPLERATE_16_625 = 3
SAMPLERATE_31_25 = 4
SAMPLERATE_62_5 = 5
SAMPLERATE_125 = 6
SAMPLERATE_250 = 7

# Proximity modulator timing
FREQUENCY_3M125 = 3
FREQUENCY_1M5625 = 2
FREQUENCY_781K25 = 1
Expand Down Expand Up @@ -113,6 +124,7 @@ def __init__(self, i2c, address=_VCNL4010_I2CADDR_DEFAULT):
if (revision & 0xF0) != 0x20:
raise RuntimeError("Failed to find VCNL4010, check wiring!")
self.led_current = 20
self.samplerate = SAMPLERATE_1_95
self.frequency = FREQUENCY_390K625
self._write_u8(_VCNL4010_INTCONTROL, 0x08)

Expand Down Expand Up @@ -153,8 +165,8 @@ def led_current(self, val):

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

@property
def samplerate(self):
"""
The frequency of proximity measurements per second. Must be a value of:

- SAMPLERATE_1_95: 1.95 measurements/sec (default)
- SAMPLERATE_3_90625: 3.90625 measurements/sec
- SAMPLERATE_7_8125: 7.8125 measurements/sec
- SAMPLERATE_16_625: 16.625 measurements/sec
- SAMPLERATE_31_25: 31.25 measurements/sec
- SAMPLERATE_62_5: 62.5 measurements/sec
- SAMPLERATE_125: 125 measurements/sec
- SAMPLERATE_250: 250 measurements/sec

See the datasheet for how frequency changes the power consumption and
proximity detection accuracy.
"""
return self._read_u8(_VCNL4010_PROXRATE)

@samplerate.setter
def samplerate(self, val):
assert 0 <= val <= 7
self._write_u8(_VCNL4010_PROXRATE, val)

@property
def frequency(self):
"""
The frequency of proximity measurements. Must be a value of:
Proximity modulator timimg. This is the frequency of the IR square
wave used for the proximity measurement.

Must be a value of:

- FREQUENCY_3M125: 3.125 Mhz
- FREQUENCY_1M5625: 1.5625 Mhz
- FREQUENCY_781K25: 781.25 Khz
- FREQUENCY_390K625: 390.625 Khz (default)

See the datasheet for how frequency changes the proximity detection
accuracy.
The datasheet recommended leaving this at the default.
"""
return (self._read_u8(_VCNL4010_MODTIMING) >> 3) & 0x03

Expand Down