From fba0a81fd7486f1f0364c505b4f9823e44ed22a9 Mon Sep 17 00:00:00 2001 From: George White Date: Sun, 6 Feb 2022 10:24:53 -0500 Subject: [PATCH 1/4] 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) --- adafruit_vcnl4010.py | 65 +++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/adafruit_vcnl4010.py b/adafruit_vcnl4010.py index 0ec8639..86d3e8a 100644 --- a/adafruit_vcnl4010.py +++ b/adafruit_vcnl4010.py @@ -55,10 +55,21 @@ _VCNL4010_AMBIENT_LUX_SCALE = 0.25 # Lux value per 16-bit result value. # User-facing constants: -FREQUENCY_3M125 = 3 -FREQUENCY_1M5625 = 2 -FREQUENCY_781K25 = 1 -FREQUENCY_390K625 = 0 +# Number of proximity measuremenrs per second +FREQUENCY_1_95 = 0 +FREQUENCY_3_90625 = 1 +FREQUENCY_7_8125 = 2 +FREQUENCY_16_625 = 3 +FREQUENCY_31_25 = 4 +FREQUENCY_62_5 = 5 +FREQUENCY_125 = 6 +FREQUENCY_250 = 7 + +# Proximity modulator timing +MODULATION_3M125 = 3 +MODULATION_1M5625 = 2 +MODULATION_781K25 = 1 +MODULATION_390K625 = 0 # Disable pylint's name warning as it causes too much noise. Suffixes like # 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): if (revision & 0xF0) != 0x20: raise RuntimeError("Failed to find VCNL4010, check wiring!") self.led_current = 20 - self.frequency = FREQUENCY_390K625 + self.frequency = FREQUENCY_1_95 + self.modulation = MODULATION_390K625 self._write_u8(_VCNL4010_INTCONTROL, 0x08) def _read_u8(self, address): @@ -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 @@ -170,20 +182,41 @@ def led_current_mA(self, val): @property def frequency(self): """ - The frequency of proximity measurements. Must be a value of: + The frequency of proximity measurements per second. Must be a value of: + + - FREQUENCY_1_95: 1.95 measurements/sec (default) + - FREQUENCY_3_90625: 3.90625 measurements/sec + - FREQUENCY_7_8125: 7.8125 measurements/sec + - FREQUENCY_16_625: 16.625 measurements/sec + - FREQUENCY_31_25: 31.25 measurements/sec + - FREQUENCY_62_5: 62.5 measurements/sec + - FREQUENCY_125: 125 measurements/sec + - FREQUENCY_250: 250 measurements/sec + + See the datasheet for how frequency changes the power consumption and + proximity detection accuracy. + """ + return self._read_u8(_VCNL4010_PROXRATE) + + @frequency.setter + def frequency(self, val): + assert 0 <= val <= 7 + self._write_u8(_VCNL4010_PROXRATE, val) - - FREQUENCY_3M125: 3.125 Mhz - - FREQUENCY_1M5625: 1.5625 Mhz - - FREQUENCY_781K25: 781.25 Khz - - FREQUENCY_390K625: 390.625 Khz (default) + @property + def modulation(self): + """ + Proximity modulator timimg. Must be a value of: - See the datasheet for how frequency changes the proximity detection - accuracy. + - MODULATION_3M125: 3.125 Mhz + - MODULATION_1M5625: 1.5625 Mhz + - MODULATION_781K25: 781.25 Khz + - MODULATION_390K625: 390.625 Khz (default) """ return (self._read_u8(_VCNL4010_MODTIMING) >> 3) & 0x03 - @frequency.setter - def frequency(self, val): + @modulation.setter + def modulation(self, val): assert 0 <= val <= 3 timing = self._read_u8(_VCNL4010_MODTIMING) timing &= ~0b00011000 From dd8a32e40a1f71590eb3c97f3c88e75a19d2aeef Mon Sep 17 00:00:00 2001 From: George White Date: Sun, 6 Feb 2022 10:38:06 -0500 Subject: [PATCH 2/4] Remove space to fix formatting --- adafruit_vcnl4010.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_vcnl4010.py b/adafruit_vcnl4010.py index 86d3e8a..a4ed84e 100644 --- a/adafruit_vcnl4010.py +++ b/adafruit_vcnl4010.py @@ -66,7 +66,7 @@ FREQUENCY_250 = 7 # Proximity modulator timing -MODULATION_3M125 = 3 +MODULATION_3M125 = 3 MODULATION_1M5625 = 2 MODULATION_781K25 = 1 MODULATION_390K625 = 0 From c135baf88da2bd107ba3783e1ea1e6e56d7a45d0 Mon Sep 17 00:00:00 2001 From: George White Date: Sun, 6 Feb 2022 12:18:37 -0500 Subject: [PATCH 3/4] Rename frequency and modulation Restoring the original `frequency` property to refer to the proximity modulator timing (to keep from making a breaking change to the API) and renamed the new property to `samplerate`. --- adafruit_vcnl4010.py | 73 +++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/adafruit_vcnl4010.py b/adafruit_vcnl4010.py index a4ed84e..5a28bc4 100644 --- a/adafruit_vcnl4010.py +++ b/adafruit_vcnl4010.py @@ -56,20 +56,20 @@ # User-facing constants: # Number of proximity measuremenrs per second -FREQUENCY_1_95 = 0 -FREQUENCY_3_90625 = 1 -FREQUENCY_7_8125 = 2 -FREQUENCY_16_625 = 3 -FREQUENCY_31_25 = 4 -FREQUENCY_62_5 = 5 -FREQUENCY_125 = 6 -FREQUENCY_250 = 7 +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 -MODULATION_3M125 = 3 -MODULATION_1M5625 = 2 -MODULATION_781K25 = 1 -MODULATION_390K625 = 0 +FREQUENCY_3M125 = 3 +FREQUENCY_1M5625 = 2 +FREQUENCY_781K25 = 1 +FREQUENCY_390K625 = 0 # Disable pylint's name warning as it causes too much noise. Suffixes like # BE (big-endian) or mA (milli-amps) don't confirm to its conventions--by @@ -124,8 +124,8 @@ 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.frequency = FREQUENCY_1_95 - self.modulation = MODULATION_390K625 + self.samplerate = SAMPLERATE_1_95 + self.frequency = FREQUENCY_390K625 self._write_u8(_VCNL4010_INTCONTROL, 0x08) def _read_u8(self, address): @@ -180,43 +180,48 @@ def led_current_mA(self, val): self.led_current = val // 10 @property - def frequency(self): + def samplerate(self): """ The frequency of proximity measurements per second. Must be a value of: - - FREQUENCY_1_95: 1.95 measurements/sec (default) - - FREQUENCY_3_90625: 3.90625 measurements/sec - - FREQUENCY_7_8125: 7.8125 measurements/sec - - FREQUENCY_16_625: 16.625 measurements/sec - - FREQUENCY_31_25: 31.25 measurements/sec - - FREQUENCY_62_5: 62.5 measurements/sec - - FREQUENCY_125: 125 measurements/sec - - FREQUENCY_250: 250 measurements/sec + - 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) - @frequency.setter - def frequency(self, val): + @samplerate.setter + def samplerate(self, val): assert 0 <= val <= 7 self._write_u8(_VCNL4010_PROXRATE, val) @property - def modulation(self): + def frequency(self): """ - Proximity modulator timimg. Must be a value of: - - - MODULATION_3M125: 3.125 Mhz - - MODULATION_1M5625: 1.5625 Mhz - - MODULATION_781K25: 781.25 Khz - - MODULATION_390K625: 390.625 Khz (default) + 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) + + The datasheet recommended leaving this at the default. """ return (self._read_u8(_VCNL4010_MODTIMING) >> 3) & 0x03 - @modulation.setter - def modulation(self, val): + @frequency.setter + def frequency(self, val): assert 0 <= val <= 3 timing = self._read_u8(_VCNL4010_MODTIMING) timing &= ~0b00011000 From 12fb5c6d3aee2f9fb4483005bf9676a0fd17ee95 Mon Sep 17 00:00:00 2001 From: George White Date: Sun, 6 Feb 2022 12:25:06 -0500 Subject: [PATCH 4/4] Fix a couple of format issues Black flagged some lines with extra spaces --- adafruit_vcnl4010.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_vcnl4010.py b/adafruit_vcnl4010.py index 5a28bc4..7015153 100644 --- a/adafruit_vcnl4010.py +++ b/adafruit_vcnl4010.py @@ -208,14 +208,14 @@ def frequency(self): """ Proximity modulator timimg. This is the frequency of the IR square wave used for the proximity measurement. - - Must be a value of: + + 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) - + The datasheet recommended leaving this at the default. """ return (self._read_u8(_VCNL4010_MODTIMING) >> 3) & 0x03