30
30
31
31
from adafruit_bus_device import i2c_device
32
32
33
+ try :
34
+ import typing # pylint: disable=unused-import
35
+ from busio import I2C
36
+ except ImportError :
37
+ pass
33
38
34
39
__version__ = "0.0.0+auto.0"
35
40
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VCNL4010.git"
@@ -117,7 +122,7 @@ class VCNL4010:
117
122
# thread safe!
118
123
_BUFFER = bytearray (3 )
119
124
120
- def __init__ (self , i2c , address = _VCNL4010_I2CADDR_DEFAULT ):
125
+ def __init__ (self , i2c : I2C , address : int = _VCNL4010_I2CADDR_DEFAULT ) -> None :
121
126
self ._device = i2c_device .I2CDevice (i2c , address )
122
127
# Verify chip ID.
123
128
revision = self ._read_u8 (_VCNL4010_PRODUCTID )
@@ -128,29 +133,29 @@ def __init__(self, i2c, address=_VCNL4010_I2CADDR_DEFAULT):
128
133
self .frequency = FREQUENCY_390K625
129
134
self ._write_u8 (_VCNL4010_INTCONTROL , 0x08 )
130
135
131
- def _read_u8 (self , address ) :
136
+ def _read_u8 (self , address : int ) -> int :
132
137
# Read an 8-bit unsigned value from the specified 8-bit address.
133
138
with self ._device as i2c :
134
139
self ._BUFFER [0 ] = address & 0xFF
135
140
i2c .write_then_readinto (self ._BUFFER , self ._BUFFER , out_end = 1 , in_start = 1 )
136
141
return self ._BUFFER [1 ]
137
142
138
- def _read_u16BE (self , address ) :
143
+ def _read_u16BE (self , address : int ) -> int :
139
144
# Read a 16-bit big-endian unsigned value from the specified 8-bit address.
140
145
with self ._device as i2c :
141
146
self ._BUFFER [0 ] = address & 0xFF
142
147
i2c .write_then_readinto (self ._BUFFER , self ._BUFFER , out_end = 1 , in_start = 1 )
143
148
return (self ._BUFFER [1 ] << 8 ) | self ._BUFFER [2 ]
144
149
145
- def _write_u8 (self , address , val ) :
150
+ def _write_u8 (self , address : int , val : int ) -> None :
146
151
# Write an 8-bit unsigned value to the specified 8-bit address.
147
152
with self ._device as i2c :
148
153
self ._BUFFER [0 ] = address & 0xFF
149
154
self ._BUFFER [1 ] = val & 0xFF
150
155
i2c .write (self ._BUFFER , end = 2 )
151
156
152
157
@property
153
- def led_current (self ):
158
+ def led_current (self ) -> int :
154
159
"""The current of the LED. The value is in units of 10mA
155
160
and can only be set to 0 (0mA/off) to 20 (200mA). See the datasheet
156
161
for how LED current impacts proximity measurements. The default is
@@ -159,12 +164,12 @@ def led_current(self):
159
164
return self ._read_u8 (_VCNL4010_IRLED ) & 0x3F
160
165
161
166
@led_current .setter
162
- def led_current (self , val ) :
167
+ def led_current (self , val : int ) -> None :
163
168
assert 0 <= val <= 20
164
169
self ._write_u8 (_VCNL4010_IRLED , val )
165
170
166
171
@property
167
- def led_current_mA (self ):
172
+ def led_current_mA (self ) -> int :
168
173
"""The current of the LED in milliamps. The value here is
169
174
specified in milliamps from 0-200. Note that this value will be
170
175
quantized down to a smaller less-accurate value as the chip only
@@ -176,11 +181,11 @@ def led_current_mA(self):
176
181
return self .led_current * 10
177
182
178
183
@led_current_mA .setter
179
- def led_current_mA (self , val ) :
184
+ def led_current_mA (self , val : int ) -> None :
180
185
self .led_current = val // 10
181
186
182
187
@property
183
- def samplerate (self ):
188
+ def samplerate (self ) -> int :
184
189
"""
185
190
The frequency of proximity measurements per second. Must be a value of:
186
191
@@ -199,12 +204,12 @@ def samplerate(self):
199
204
return self ._read_u8 (_VCNL4010_PROXRATE )
200
205
201
206
@samplerate .setter
202
- def samplerate (self , val ) :
207
+ def samplerate (self , val : int ) -> None :
203
208
assert 0 <= val <= 7
204
209
self ._write_u8 (_VCNL4010_PROXRATE , val )
205
210
206
211
@property
207
- def frequency (self ):
212
+ def frequency (self ) -> int :
208
213
"""
209
214
Proximity modulator timimg. This is the frequency of the IR square
210
215
wave used for the proximity measurement.
@@ -221,7 +226,7 @@ def frequency(self):
221
226
return (self ._read_u8 (_VCNL4010_MODTIMING ) >> 3 ) & 0x03
222
227
223
228
@frequency .setter
224
- def frequency (self , val ) :
229
+ def frequency (self , val : int ) -> None :
225
230
assert 0 <= val <= 3
226
231
timing = self ._read_u8 (_VCNL4010_MODTIMING )
227
232
timing &= ~ 0b00011000
@@ -232,7 +237,7 @@ def frequency(self, val):
232
237
# warning for the next few functions (it hates when a loop returns a value).
233
238
# pylint: disable=inconsistent-return-statements
234
239
@property
235
- def proximity (self ):
240
+ def proximity (self ) -> int :
236
241
"""The detected proximity of an object in front of the sensor. This
237
242
is a unit-less unsigned 16-bit value (0-65535) INVERSELY proportional
238
243
to the distance of an object in front of the sensor (up to a max of
@@ -253,7 +258,7 @@ def proximity(self):
253
258
return self ._read_u16BE (_VCNL4010_PROXIMITYDATA )
254
259
255
260
@property
256
- def ambient (self ):
261
+ def ambient (self ) -> int :
257
262
"""The detected ambient light in front of the sensor. This is
258
263
a unit-less unsigned 16-bit value (0-65535) with higher values for
259
264
more detected light. See the :attr:`ambient_lux property` for a value in lux.
@@ -273,7 +278,7 @@ def ambient(self):
273
278
# pylint: enable=inconsistent-return-statements
274
279
275
280
@property
276
- def ambient_lux (self ):
281
+ def ambient_lux (self ) -> float :
277
282
"""The detected ambient light in front of the sensor as a value in
278
283
lux.
279
284
"""
0 commit comments