Skip to content

Commit 66bb344

Browse files
authored
Merge pull request #23 from tcfranks/main
Add Missing Type Annotations
2 parents fdebc20 + 31e6fcc commit 66bb344

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

adafruit_vcnl4010.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030

3131
from adafruit_bus_device import i2c_device
3232

33+
try:
34+
import typing # pylint: disable=unused-import
35+
from busio import I2C
36+
except ImportError:
37+
pass
3338

3439
__version__ = "0.0.0+auto.0"
3540
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VCNL4010.git"
@@ -117,7 +122,7 @@ class VCNL4010:
117122
# thread safe!
118123
_BUFFER = bytearray(3)
119124

120-
def __init__(self, i2c, address=_VCNL4010_I2CADDR_DEFAULT):
125+
def __init__(self, i2c: I2C, address: int = _VCNL4010_I2CADDR_DEFAULT) -> None:
121126
self._device = i2c_device.I2CDevice(i2c, address)
122127
# Verify chip ID.
123128
revision = self._read_u8(_VCNL4010_PRODUCTID)
@@ -128,29 +133,29 @@ def __init__(self, i2c, address=_VCNL4010_I2CADDR_DEFAULT):
128133
self.frequency = FREQUENCY_390K625
129134
self._write_u8(_VCNL4010_INTCONTROL, 0x08)
130135

131-
def _read_u8(self, address):
136+
def _read_u8(self, address: int) -> int:
132137
# Read an 8-bit unsigned value from the specified 8-bit address.
133138
with self._device as i2c:
134139
self._BUFFER[0] = address & 0xFF
135140
i2c.write_then_readinto(self._BUFFER, self._BUFFER, out_end=1, in_start=1)
136141
return self._BUFFER[1]
137142

138-
def _read_u16BE(self, address):
143+
def _read_u16BE(self, address: int) -> int:
139144
# Read a 16-bit big-endian unsigned value from the specified 8-bit address.
140145
with self._device as i2c:
141146
self._BUFFER[0] = address & 0xFF
142147
i2c.write_then_readinto(self._BUFFER, self._BUFFER, out_end=1, in_start=1)
143148
return (self._BUFFER[1] << 8) | self._BUFFER[2]
144149

145-
def _write_u8(self, address, val):
150+
def _write_u8(self, address: int, val: int) -> None:
146151
# Write an 8-bit unsigned value to the specified 8-bit address.
147152
with self._device as i2c:
148153
self._BUFFER[0] = address & 0xFF
149154
self._BUFFER[1] = val & 0xFF
150155
i2c.write(self._BUFFER, end=2)
151156

152157
@property
153-
def led_current(self):
158+
def led_current(self) -> int:
154159
"""The current of the LED. The value is in units of 10mA
155160
and can only be set to 0 (0mA/off) to 20 (200mA). See the datasheet
156161
for how LED current impacts proximity measurements. The default is
@@ -159,12 +164,12 @@ def led_current(self):
159164
return self._read_u8(_VCNL4010_IRLED) & 0x3F
160165

161166
@led_current.setter
162-
def led_current(self, val):
167+
def led_current(self, val: int) -> None:
163168
assert 0 <= val <= 20
164169
self._write_u8(_VCNL4010_IRLED, val)
165170

166171
@property
167-
def led_current_mA(self):
172+
def led_current_mA(self) -> int:
168173
"""The current of the LED in milliamps. The value here is
169174
specified in milliamps from 0-200. Note that this value will be
170175
quantized down to a smaller less-accurate value as the chip only
@@ -176,11 +181,11 @@ def led_current_mA(self):
176181
return self.led_current * 10
177182

178183
@led_current_mA.setter
179-
def led_current_mA(self, val):
184+
def led_current_mA(self, val: int) -> None:
180185
self.led_current = val // 10
181186

182187
@property
183-
def samplerate(self):
188+
def samplerate(self) -> int:
184189
"""
185190
The frequency of proximity measurements per second. Must be a value of:
186191
@@ -199,12 +204,12 @@ def samplerate(self):
199204
return self._read_u8(_VCNL4010_PROXRATE)
200205

201206
@samplerate.setter
202-
def samplerate(self, val):
207+
def samplerate(self, val: int) -> None:
203208
assert 0 <= val <= 7
204209
self._write_u8(_VCNL4010_PROXRATE, val)
205210

206211
@property
207-
def frequency(self):
212+
def frequency(self) -> int:
208213
"""
209214
Proximity modulator timimg. This is the frequency of the IR square
210215
wave used for the proximity measurement.
@@ -221,7 +226,7 @@ def frequency(self):
221226
return (self._read_u8(_VCNL4010_MODTIMING) >> 3) & 0x03
222227

223228
@frequency.setter
224-
def frequency(self, val):
229+
def frequency(self, val: int) -> None:
225230
assert 0 <= val <= 3
226231
timing = self._read_u8(_VCNL4010_MODTIMING)
227232
timing &= ~0b00011000
@@ -232,7 +237,7 @@ def frequency(self, val):
232237
# warning for the next few functions (it hates when a loop returns a value).
233238
# pylint: disable=inconsistent-return-statements
234239
@property
235-
def proximity(self):
240+
def proximity(self) -> int:
236241
"""The detected proximity of an object in front of the sensor. This
237242
is a unit-less unsigned 16-bit value (0-65535) INVERSELY proportional
238243
to the distance of an object in front of the sensor (up to a max of
@@ -253,7 +258,7 @@ def proximity(self):
253258
return self._read_u16BE(_VCNL4010_PROXIMITYDATA)
254259

255260
@property
256-
def ambient(self):
261+
def ambient(self) -> int:
257262
"""The detected ambient light in front of the sensor. This is
258263
a unit-less unsigned 16-bit value (0-65535) with higher values for
259264
more detected light. See the :attr:`ambient_lux property` for a value in lux.
@@ -273,7 +278,7 @@ def ambient(self):
273278
# pylint: enable=inconsistent-return-statements
274279

275280
@property
276-
def ambient_lux(self):
281+
def ambient_lux(self) -> float:
277282
"""The detected ambient light in front of the sensor as a value in
278283
lux.
279284
"""

0 commit comments

Comments
 (0)